Skip to content

Fix #15242 (Extend googletest for ACTION macros) - #9028

Merged
danmar merged 1 commit into
cppcheck-opensource:mainfrom
fliiiix:bugfix/extend-googletest-action-p-macro
Nov 19, 2026
Merged

danmar merged 1 commit into
cppcheck-opensource:mainfrom
fliiiix:bugfix/extend-googletest-action-p-macro

Conversation

@fliiiix

@fliiiix fliiiix commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

They seem to fail when they are in a namespace
with a statement before the macro.

Motivating examples:

namespace{int x;ACTION_P(B, p){}}

namespace{int x;ACTION(B){}}

namespace{int x;ACTION_Pk(Foo, p1, p2, p3){}}

@fliiiix

fliiiix commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

I had a quick look at some of the failed jobs and they seem not related to my changes is this correct? Or is it expected from me to fixe something to get this change merged?

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

Requesting changes: the PR stops the syntax error but also stops cppcheck from checking ACTION bodies.

  • Bodies are no longer analysed (new false negatives). ACTION and ACTION_P now expand to nothing, so the body is left as a bare { ... } block at namespace scope. Cppcheck doesn't analyse code there. At top level the base config checks these bodies and finds bugs in them; with this PR it reports nothing. The macros should expand to a function header instead, as TEST and MATCHER already do in this file. I tried that and both the syntax error and the missing warnings went away.
  • ACTION_Pk is not a real gmock macro. gmock uses ACTION_P2 … ACTION_P10, which this PR doesn't define. ACTION_P2(Foo, p1, p2){} inside a namespace still gives syntaxError. The k in the PR description is a placeholder, not a macro name.
  • No tests. test/cfg/googletest.cpp gets no cases for the new defines.

Testing

Built cppcheck from pr/ with make -j8 cppcheck. I didn't build base/: to get the old behaviour I ran the PR's binary with --library=../base/cfg/googletest.cfg. The PR only changes the cfg, so that gives the same results as a base build.

The motivating cases are fixed. namespace{int x;ACTION_P(B, p){}} and namespace{int x;ACTION(B){}} give syntaxError with the base cfg and no error with the PR.

Real ACTION_P2/ACTION_P3 still fail

// c.cpp
namespace{int x;ACTION_P2(Foo, p1, p2){}}
$ cppcheck -q --library=googletest c.cpp
c.cpp:1:17: error: syntax error [syntaxError]

Regression: bugs inside ACTION bodies are no longer found

// e.cpp
ACTION(Foo) { int *p = 0; return *p; }
ACTION_P(Bar, n) { char buf[10]; buf[10] = n; return 0; }
void f() { int *p = 0; *p = 1; }

With the base cfg (--library=base/cfg/googletest.cfg):

e.cpp:2:37: error: Array 'buf[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]
e.cpp:1:35: error: Null pointer dereference: p [nullPointer]
e.cpp:3:25: error: Null pointer dereference: p [nullPointer]

With the PR (--library=googletest), only the warning in f() is left:

e.cpp:3:25: error: Null pointer dereference: p [nullPointer]

--debug shows why: ACTION(Foo) { ... } turns into a bare block at file scope, 1: { int * p@var1 ; p@var1 = 0 ; return * p@var1 ; }, which is never analysed.

The suggested fix works. I tried these defines in a modified copy of the cfg:

  • ACTION(name) → auto name##Action(int arg0, int arg1)
  • ACTION_P(name,p0) → template<class p0##_type> auto name##ActionP(p0##_type p0, int arg0)

With them, a.cpp and b.cpp have no syntax error and e.cpp reports all three errors again.


Automated review of ac6bae4 by Claude. The pull request was built and tested locally.

Comment thread cfg/googletest.cfg Resolved
Comment thread cfg/googletest.cfg Resolved
@fliiiix
fliiiix force-pushed the bugfix/extend-googletest-action-p-macro branch 2 times, most recently from c21b1b6 to e3cb45e Compare November 16, 2026 12:01
@fliiiix

fliiiix commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Updated missing things and added some tests cases.

@fliiiix

fliiiix commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for running the CI 👍 i will take an other look into it tomorrow seems the new test doesn't do what i expected in ci

@fliiiix
fliiiix force-pushed the bugfix/extend-googletest-action-p-macro branch from e3cb45e to 87df375 Compare November 17, 2026 07:54
@fliiiix

fliiiix commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Tests pass now locally:

100% tests passed, 0 tests failed out of 116

Total Test time (real) =  21.31 sec
[100%] Built target check

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.