Repository navigation
Fix #15208 (cppcheck-suppress-macro has no effect on a macro whose body is a single macro call) - #9018
Fix #15208 (cppcheck-suppress-macro has no effect on a macro whose body is a single macro call)#9018JayToltTech wants to merge 4 commits into
Conversation
…dy is a single macro call) A token only records the innermost macro it was expanded from, so a line like ALIAS(x) where ALIAS(...) is defined as CAST(...) carries only CAST tokens and a suppression on ALIAS never matches. Add the macro names from the preprocessor's macro usage list for each use line as well.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix works for the reported case (ALIAS(...) → DIV(...)), for deeper chains and for __VA_ARGS__ wrappers. I found one new problem: simplecpp's macro usage list does not only record macros that were expanded. simplecpp::Macro::expand() adds the use location to the list before it checks whether a function-like macro is followed by (. If the name of a function-like macro appears on a line without parentheses, for example as a variable name, the PR now applies that macro's cppcheck-suppress-macro to the whole line and hides real errors there (a new false negative, reproduced below). Lines that use an empty macro are now suppressed in the same way, because the macro contributes no tokens. Otherwise I saw no problems.
Testing
Built cppcheck and testrunner in pr/ and cppcheck in base/ without errors. ./testrunner TestSuppressions: 71 tests, 0 failures. python3 -m pytest inline-suppress_test.py other_test.py -k "suppress or macro": 35 passed, 7 skipped, 1 xfailed.
Behaviour the PR fixes (with --inline-suppr): with ALIAS(A,B) DIV(A,B), a two-level ALIAS2 → ALIAS → DIV chain, a V(...) DIV(__VA_ARGS__) wrapper and a call split over two lines, base reports zerodiv and an unmatched suppression. The PR suppresses all of them. Direct uses of DIV are still reported, as is 1/0 inside #ifdef ALIAS / #if defined(ALIAS) blocks.
New false negative: function-like macro name used without (
// cppcheck-suppress-macro zerodiv
#define ALIAS(A,B) A/B
void f() {
int ALIAS = 1/0;
}cppcheck -q --inline-suppr t5.c
- base:
t5.c:4:18: error: Division by zero. [zerodiv] - PR: no output
cppcheck -E shows that ALIAS is not expanded on that line (int ALIAS = 1 / 0 ;). Even so, simplecpp records a usage for it (usageList.emplace_back(loc) at the start of Macro::expand), so the PR adds ALIAS to the macros of line 4.
Empty macros: int a = DIV(1,0) EMPTY;, with the suppression on #define EMPTY, is reported by base and suppressed by the PR.
Automated review of 5a5c660 by Claude. The pull request was built and tested locally.
| } | ||
| // a token only knows the innermost macro it was expanded from | ||
| for (const simplecpp::MacroUsage& usage : macroUsage) | ||
| mLocationMacros[Location(files[usage.useLocation.fileIndex], usage.useLocation.line)].emplace(usage.macroName); |
There was a problem hiding this comment.
AI review
The macro usage list holds more than the expansions this change needs. simplecpp's Macro::expand() calls usageList.emplace_back(loc) before it checks whether a function-like macro is followed by (. So the name of a function-like macro on a line where it is not expanded still counts as a use. Example:
// cppcheck-suppress-macro zerodiv
#define ALIAS(A,B) A/B
void f() {
int ALIAS = 1/0;
}Base reports zerodiv on line 4. With this PR the error is suppressed, although ALIAS is not expanded there (-E gives int ALIAS = 1 / 0 ;). Empty macros on a line now suppress that line in the same way. It would be safer to add a usage only when the macro really produced tokens on that line, for example only for outer macros of tokens that came from a macro expansion, or to drop function-like macros that are not followed by (. A test for this case would help.
There was a problem hiding this comment.
Good catch, confirmed both on the previous commit. int ALIAS = 1/0; and EMPTY int a = 1/0; both came back with no zerodiv.
06cdcba filters it on the cppcheck side. A usage only counts if its line has tokens that came from a macro expansion, and it's skipped if the macro name is still sitting on that line as a plain identifier. Both of your cases are in testsuppressions.cpp now.
The unexpanded case is really simplecpp recording a usage before the ( check, so I've opened cppcheck-opensource/simplecpp#759 for that. Once it's in, the identifier check here can go, but the line check has to stay for empty macros.
Still not covered: an empty macro on a line that also expands some other macro still counts. Fixing that properly needs simplecpp to carry the full macro chain on each output token, which it doesn't expose today, so I've left it.
testrunner passes on macOS arm64. runformat with uncrustify 0.80.1 makes no changes.
simplecpp records a usage for a function-like macro name even when it is
not followed by '(' and so is not expanded, and for empty macros. Both
made cppcheck-suppress-macro hide warnings on lines where the macro
contributed no code. Skip a usage when its line has no expanded tokens
or when the macro name is still present there as a plain identifier.
Selfcheck reported useStlAlgorithm on the raw loop added in setLocationMacros().
|
Test results for commit 82ffca5 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9018/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found two new false negatives. In both, the fix does nothing and the warning is still reported. I found nothing new that wrongly hides a warning.
#undefturns the fix off. simplecpp reports macro usages only for the macros still in its macro map at the end of preprocessing (simplecpp.cpp~4035). After#undef ALIAS, its usages are dropped, so thecppcheck-suppress-macroonALIASmatches nothing again and is reported as unmatched. A macro that is redefined loses the usages of its first definition the same way.- The identifier check is too broad. It works per line, not per usage. If the macro is expanded on a line and its name also appears there as a plain identifier (e.g.
return ALIAS(1,0) + s.ALIAS;), the real expansion is skipped and the warning is not suppressed.
Neither case breaks existing behaviour: base reports the same warnings. Neither case has a test, and nothing documents them.
The earlier problem with unexpanded names and empty macros (C3) is fixed for the cases it named.
Earlier comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Macro usage list also records unexpanded function-like macro names and empty macros, hiding real errors | AI review | lib/cppcheck.cpp:118 |
✅ Fixed |
Testing
Built cppcheck and testrunner in pr/ and cppcheck in base/ with no errors.
./testrunner TestSuppressions: 0 failures.test/cli:python3 -m pytest inline-suppress_test.py: 26 passed, 7 skipped, 1 xfailed.
Cases that work (with --inline-suppr):
- The main case,
ALIAS→DIV(t4), is suppressed with the PR and reported by base. - The same macros defined in an included header (t7) are suppressed too.
- These lines are still reported, as in base:
int NAME = 1/0;with#define NAME ALIASint ID(ALIAS) = 1/0;int ALIAS = ONE/0;int x = ONE/0 + s.ALIAS;
#undef after the use (t3.c):
#define DIV(A,B) A/B
// cppcheck-suppress-macro zerodiv
#define ALIAS(A,B) DIV(A,B)
void f() {
int a = ALIAS(1,0);
}
#undef ALIAScppcheck -q --inline-suppr t3.c gives the same result for base and PR:
t3.c:5:13: error: Division by zero. [zerodiv]
With --enable=information the PR also prints t3.c:3:1: information: Unmatched suppression: zerodiv [unmatchedSuppression].
Macro name used as an identifier on the same line as an expansion (t8.c):
#define DIV(A,B) A/B
// cppcheck-suppress-macro zerodiv
#define ALIAS(A,B) DIV(A,B)
struct S { int ALIAS; };
int f(struct S s) {
return ALIAS(1,0) + s.ALIAS;
}The PR reports t8.c:6:12: error: Division by zero. [zerodiv]. Without + s.ALIAS the warning is suppressed.
Automated review of 82ffca5 by Claude. The pull request was built and tested locally.
| unexpanded.emplace(loc, tok->str()); | ||
| } | ||
| // a token only knows the innermost macro it was expanded from; usages that produced no tokens are skipped | ||
| for (const simplecpp::MacroUsage& usage : macroUsage) { |
There was a problem hiding this comment.
AI review
The macro usage list from simplecpp only has usages of macros that are still defined at the end of preprocessing. simplecpp::preprocess builds it by iterating over the final macros map. If the outer macro is #undef'd (or redefined) after it is used, its usages are gone and the suppression still doesn't match:
#define DIV(A,B) A/B
// cppcheck-suppress-macro zerodiv
#define ALIAS(A,B) DIV(A,B)
void f() {
int a = ALIAS(1,0);
}
#undef ALIASThe PR still reports zerodiv on line 5 plus Unmatched suppression: zerodiv. Fixing this probably needs a change in simplecpp. Otherwise, a TODO test or a note would help.
There was a problem hiding this comment.
Confirmed, #undef ALIAS after the use still gives zerodiv plus the unmatched suppression. That one is simplecpp, the usage list only has the macros still defined when preprocessing ends.
Opened cppcheck-opensource/simplecpp#764 for it. It reports the usages when the definition goes away, so #undef and redefinition both keep them. With that dropped into externals/simplecpp locally your example is suppressed.
6c9ac81 has it as a TODO test here until simplecpp gets bumped.
| for (const simplecpp::MacroUsage& usage : macroUsage) { | ||
| const Location loc(files[usage.useLocation.fileIndex], usage.useLocation.line); | ||
| const auto it = mLocationMacros.find(loc); | ||
| if (it != mLocationMacros.end() && unexpanded.count({loc, usage.macroName}) == 0) |
There was a problem hiding this comment.
AI review
unexpanded is keyed by line, not by usage. Any plain identifier with the macro's name on the line drops every usage of that macro on the line, including real expansions. For example, return ALIAS(1,0) + s.ALIAS; (with struct S { int ALIAS; };) is still reported, but without + s.ALIAS it is suppressed. Comparing the column of the usage with the column of the identifier token would make the check match only the unexpanded occurrence.
There was a problem hiding this comment.
Yep, return ALIAS(1,0) + s.ALIAS; wasn't suppressed.
6c9ac81 keys unexpanded by line and column, so only the usage sitting at the identifier's column is skipped. Your example is in testsuppressions.cpp now. s.ALIAS + 1/0 on a line with no expansion is still reported.
testrunner passes on macOS arm64. I haven't run the test/cli pytest tests locally.
A plain identifier with the macro's name dropped every usage of that macro on the line, so `return ALIAS(1,0) + s.ALIAS;` was not suppressed. Only the usage at the identifier's own column is skipped now. Also adds a TODO test for a macro that is undefined after its use. simplecpp only reports the usages of macros that are still defined when preprocessing ends, so that needs a change there.
https://trac.cppcheck.net/ticket/15208
A
cppcheck-suppress-macroon a macro whose body is one call to another macro never matches. Zephyr'sLOG_ERR(...)->Z_LOG(...)has this shape, which is where I ran into it.Root cause: macro suppressions are matched per line against the macro names on that line's tokens, and a token only records the innermost macro it was expanded from. With
#define ALIAS(...) CAST(__VA_ARGS__)every token on theALIAS(d)line saysCAST, so nothing carriesALIAS. Wrapping the body indo { } while (0)'works' only because those tokens belong to the outer macro.The preprocessor already has the outer expansions in its macro usage list with their use locations, so
setLocationMacros()now adds those names for the use line too. No change to simplecpp.Added a test in
TestSuppressionsthat fails without the change.testrunnerpasses locally (macOS arm64). I haven't run thetest/cliPython tests.runformatwith uncrustify 0.80.1 makes no changes.Added myself to AUTHORS as this is my first PR here.