Skip to content

Extend googletest for ACTION macros - #9028

Open
fliiiix wants to merge 1 commit into
cppcheck-opensource:mainfrom
fliiiix:bugfix/extend-googletest-action-p-macro
Open

fliiiix wants to merge 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

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
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
Author

Updated missing things and added some tests cases.

@fliiiix

fliiiix commented Nov 16, 2026

Copy link
Copy Markdown
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
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.

AI review

The fixes asked for in the earlier review (C2/C3/C4) are in: the ACTION macros now expand to a function header, ACTION_P2…ACTION_P10 exist, and there are tests.

New problem: the implicit action arguments are declared as int (int arg0, int arg1). In real gmock actions these arguments are often pointers or strings, so the PR causes false positives that did not exist before:

  • AssignmentIntegerToAddress for char *s = arg1;
  • invalidPrintfArgType_s for printf("%s", arg0)

It can also send ValueFlow down the wrong path, because arg0 is assumed to be an integer. Making the argument types template parameters fixes this, for example template<class arg0_type, class arg1_type> auto name(const arg0_type& arg0, const arg1_type& arg1). That also matches gmock, which passes them as const argK_type&. I tested this: the false positives go away, and the real bugs in ACTION bodies are still found.

A smaller point: ACTION declares arg0 and arg1, but ACTION_P* declares only arg0, which is inconsistent.

Earlier comments

# Comment By Where Status
1 ACTION bodies no longer analysed (expand to nothing) AI review general ✅ Fixed
2 ACTION_Pk is not a real macro; ACTION_P2..P10 missing AI review general ✅ Fixed
3 No tests in test/cfg/googletest.cpp AI review general ✅ Fixed
4 Expand ACTION to a function header so the body is analysed AI review cfg/googletest.cfg:62 ✅ Fixed
5 Add ACTION_P2..ACTION_P10 and tests AI review cfg/googletest.cfg:64 ✅ Fixed

Testing

I built cppcheck from pr/ and found no build problems. To get the base behaviour I ran the PR binary with --library=../base/cfg/googletest.cfg.

  • Motivating cases: ACTION, ACTION_P and ACTION_P2 at top level and inside namespaces no longer give syntaxError. Bugs in ACTION bodies are found (nullPointer, uninitvar).
  • Regression check: ACTION_TEMPLATE still gives syntaxError, but it did before the PR as well, so this is not a regression.
  • New false positives, caused by arg0/arg1 being declared as int:
// c.cpp
#include <cstring>
#include <cstdio>
ACTION(B4) { char *s = arg1; s[0] = 'a'; }
ACTION_P(B5, v) { const char *s = arg0; return strlen(s) + v; }
ACTION(B6) { printf("%s", arg0); }
$ ./cppcheck -q --enable=all --inconclusive --library=googletest c.cpp
c.cpp:3:22: portability: Assigning an integer to a pointer is not portable. [AssignmentIntegerToAddress]
c.cpp:4:33: portability: Assigning an integer to a pointer is not portable. [AssignmentIntegerToAddress]
c.cpp:5:14: warning: %s in format string (no. 1) requires 'char *' but the argument type is 'signed int'. [invalidPrintfArgType_s]

The line numbers above are for this 5-line file. In my original test file they were 6, 7 and 8 because of two more ACTION lines.

  • Suggested fix: I edited a copy of the cfg so that ACTION and ACTION_P use template types (template<class arg0_type, class arg1_type> auto name(const arg0_type& arg0, const arg1_type& arg1), and the same idea for ACTION_P). With it, all three warnings above go away. My other scratch files still report nullPointer, uninitvar and unassignedVariable in ACTION bodies.
  • Not run: I did not run the full test/cfg runtests script. The author reports that it passes locally.

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

Comment thread cfg/googletest.cfg Resolved
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_P3(B, p0, p1, p3){}}

// Regression
ACTION(Foo) { int *p = 0; return *p; }
```
@fliiiix
fliiiix force-pushed the bugfix/extend-googletest-action-p-macro branch from 87df375 to 2040587 Compare November 17, 2026 11:57
@fliiiix

fliiiix commented Nov 17, 2026

Copy link
Copy Markdown
Author

Made all ACTION* macros a template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants