Repository navigation
Fix #773 Do not record a usage for a function-like macro name without ( - #759
Conversation
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.
|
sorry but CI failed and now there are conflicts.. could you please take a look? I have just released 1.10.0 but a 1.10.1 with this fix would be possible.. |
expand() added the use location to the usage list before checking whether a function-like macro is followed by (, so `int F = 1;` with F defined as a function-like macro was reported as a usage of F although nothing is expanded. Record the usage after that check. macroUsageArg4 expected such a usage for `INT64_C;`; it now expects none.
05a5ed3 to
7e57597
Compare
|
Independent review by Claude Fable 5.2 (Anthropic), run at the PR author's request. Line numbers refer to VerdictThe change is correct for its stated aim, with one gap: How it was checked: the full What holds:
IssuesA.
|
| Input | Before | After |
|---|---|---|
#define F(x) 1#if F#endif |
F used at 2:5 |
no usage |
#if F#endif#define F(x) 1 |
F used at 1:5 |
F used at 1:5 |
#define F(x) 1#if 0#elif F || 1#endif |
F used at 3:7 |
no usage |
The same #if F is a usage when F is defined later, but not when it is defined earlier. #ifdef F and defined(F) still count as usages in both cases.
Two ways to resolve it:
- add such names to
maybeUsedMacrosat line 4621, so#if Fstays a usage; or - decide that
#if Fis not a usage, and correct the comment at line 4620.
Either way it needs a test.
B. The preprocessed output changes for one input (lines 2398-2402)
The early return now also precedes expandBuiltin, so this is not purely a usage-list change:
#define __COUNTER__(x) x
__COUNTER__;This used to output 0 ; and now outputs __COUNTER__ ;. The new result is arguably the right one and the input is contrived, but the commit message describes only the usage list changing.
Optional suggestions
- Redundant condition at line 2412. After the early return,
functionLike() && nameTokInst->nextalready impliesnameTokInst->next->op == '(', so that test can go. - Test coverage.
tokenMacro16andmacroUsageArg4cover only a bare name at the top level. These cases also changed and are cheap to add:G(F): no usage ofF.#define H(a) a F a(5)withH(F): exactly one usage ofF(was two).F\n(1): still one usage.#define F() 1withF; F();: one usage.- the
#if Fcases from issue A.
- Downstream effect. cppcheck uses the list for
cppcheck-suppress-macroand writes it to the dump file as<macro-usage>, so dump consumers will see fewer entries. That may be worth a line in the PR description.
In an #if or #elif condition the usage of a defined macro is left to expand(), which no longer records one for a function-like macro name that is not followed by (. So `#if F` was a usage when F was defined after it, but not when F was defined before it. Record such names like undefined ones.
|
Rebased onto master, conflicts are resolved. The CI failure was the windows-11-arm job hitting the CMake non-target-directive error, which #763 already fixed. The one real conflict was with #761. There's also a second commit. The review above found that The workflows are waiting on approval to run. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. The usage is now recorded after the "no arguments" check, which is correct. The second commit fixes the #if F inconsistency raised in C3: when the macro is defined before the #if, #if F and #elif F || 1 are recorded as usages again, the same as when it is defined after. I compared base and PR on bare function-like names at top level, in macro arguments, in #if and in nested expansions. The only differences were phantom usages that disappeared, and in each of those cases gcc does not expand the name either. __COUNTER__ values and the preprocessed output did not change. The #define __COUNTER__(x) x case is the exception, and the description already covers it.
From C3, two optional suggestions are still unaddressed: the redundant nameTokInst->next->op == '(' test, and the extra tests for the argument and nested cases such as G(F), H(F) and F\n(1).
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | CI failed and there are merge conflicts | @danmar | general | ✅ Fixed |
| 2 | #if/#elif F no longer records a usage when F(x) is defined before it | @JayToltTech | general | ✅ Fixed |
| 3 | Moving the early return before expandBuiltin changes the output for #define COUNTER(x) x | @JayToltTech | general | 💬 Answered: Author thinks the new output is right and documented it in the description |
| 4 | Redundant nameTokInst->next->op == '(' test after the early return | @JayToltTech | general | |
| 5 | Missing tests for G(F), H(F), F\n(1) and F; F(); cases | @JayToltTech | general | |
| 6 | Downstream effect on cppcheck dump entries should be mentioned | @JayToltTech | general | ✅ Fixed |
@JayToltTech, please fix, resolve or reply to the open and new comments: #4, #5.
Testing
Built /work/pr with make -j8 testrunner simplecpp CXXOPTS=-Werror with no warnings, and built base/ too.
./testrunner: passes, exit code 0.python3 run-tests.py: 0 failed.python3 -m pytest integration_test.py: 128 passed../selfcheck.sh: exit code 0.
The command line tool doesn't print the usage list, so I wrote a small driver (scratch/drv.cpp) that calls simplecpp::preprocess with a MacroUsage list. I built it against both base and PR and compared the output and the usages:
| Input | base | PR |
|---|---|---|
#define F(x) x / #define G(a) a / G(F); |
F used at 3:1 | no F usage (output F ; in both) |
#define F(x) x / #define G(a) a(1) / G(F); |
F recorded twice | F recorded once (output 1 ;) |
#define F(x) #x / #define G(a) F(a) / G(F); |
F recorded twice | F recorded once |
#define F(x) x / #define E / F E (1); |
F used | no F usage; output F ( 1 ) ; in both, and gcc -E doesn't expand it either |
__COUNTER__ F __COUNTER__ F(__COUNTER__) |
0 F 1 2 |
0 F 1 2; only the bare F usage is gone |
#if defined F && F (F function-like) |
usages at 2:13 and 2:18 | same |
#if F / (1) on the next line |
output ( 1 ), F used at 2:5 |
same |
#if F /*c*/ (1), with comments kept and removed |
evaluation error | same error |
#define G F / #if G |
G used | G used |
#if F at end of file |
"Wrong number of parameters" | same |
I found no wrong output, crash or regression.
Automated review of 5beedaf by Claude. The pull request was built and tested locally.
The early return above it already leaves expand() for a function-like macro whose next token is not (, so the test is always true there.
tokenMacro18 covers the cases the change affects beyond a bare name at the top level: G(F) and a replacement list that names F without ( record no usage of F, while F followed by ( on the next line, and F() after a bare F, each record one.
|
#4 and #5 from the review are addressed in 5435607. #4: dropped the #5: testrunner, run-tests.py and selfcheck.sh pass, and runformat with uncrustify 0.80.1 makes no changes. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. The usage is now recorded after the "no arguments" early return, so a bare function-like macro name is no longer reported as a usage, whether it is at the top level, in a macro argument, after ## or after an empty macro. In #if/#elif such a name is still recorded through maybeUsedMacros. I compared base and PR on these cases: the preprocessed output and __COUNTER__ values did not change, and in each case only the usages that the PR is about went away. Every point from the earlier review (C3) has now been fixed or answered: the redundant ( test is gone, tokenMacro18 has been added and the description has been updated.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | CI failed and there are merge conflicts | @danmar | general | ✅ Fixed |
| 2 | #if/#elif F no longer records a usage when F(x) is defined before it | @JayToltTech | general | ✅ Fixed |
| 3 | Early return before expandBuiltin changes output for #define COUNTER(x) x | @JayToltTech | general | 💬 Answered: Author thinks the new output is right and documented it in the description |
| 4 | Redundant nameTokInst->next->op == '(' test after the early return | @JayToltTech | general | ✅ Fixed |
| 5 | Missing tests for G(F), H(F), F\n(1), F; F(); cases | @JayToltTech | general | ✅ Fixed |
| 6 | Downstream effect on cppcheck dump entries should be mentioned | @JayToltTech | general | ✅ Fixed |
Testing
Build and test suites
make -j8 testrunner simplecpp CXXOPTS=-Werrorinpr/built with no warnings. I also builtbase/../testrunner: 0 failed.python3 run-tests.py: 176 tests, 0 failed, 21 skipped.python3 -m pytest integration_test.py: 128 passed../selfcheck.sh: exit code 0.
Comparing base and PR
The command line tool does not print the usage list, so I wrote a driver, scratch/drv.cpp. It calls simplecpp::preprocess with a MacroUsage list and prints the output and the usages. I built it against both base and PR. For the cases that are not in #if, I also checked the output with gcc -E.
| Input | base | PR |
|---|---|---|
#define F(x) x / #define G(a) a / G(F)(1); G(F) (1); |
F recorded 4 times | F recorded 2 times (only the real calls); output 1 ; 1 ; in both |
#define C(a,b) a##b / C(F,)(1); C(F,); |
F once | F once; output the same |
#define F(x) x / #define E / F E(1); |
F used | no F usage; output F ( 1 ) ; in both, and gcc does not expand it either |
#define H F / H; H(2); |
F once, at 4:2 | same |
#define G() F / G()(3); G(); |
F once | same |
G(INT64_C)(5); |
INT64_C twice, args [] and [5] |
INT64_C once, arg [5] |
#define F(x) 1 / #if F / (void)0; on the next line |
F used at 2:5 | same |
#define F(x) 1 / #define G F / #if G |
G used | same |
#if G(F) |
G and F used | only G used (consistent with the case outside #if) |
#ifdef X / #elif F |
F used | same |
#if 1 / #elif F (not evaluated) |
no usage | same |
#if F followed later by a bare F |
"Wrong number of parameters" | same |
None of these inputs gave wrong output, a crash or a hang.
Automated review of 5435607 by Claude. The pull request was built and tested locally.
The filter skipped a macro usage whose location held the bare name of a function-like macro, because simplecpp recorded a usage there although nothing was expanded. simplecpp 1.10.2 no longer records it (cppcheck-opensource/simplecpp#759), so the filter has nothing left to do. The existing test for that case passes with or without the filter, as no other macro is expanded on its line. The new test puts DIV(1,0) on the same line as a bare ALIAS, and fails if simplecpp records the bare name and the filter is gone. The comment on the usage loop now says what it skips: usages on lines without macro-expanded tokens.
The filter skipped a macro usage whose location held the bare name of a function-like macro, because simplecpp recorded a usage there although nothing was expanded. simplecpp 1.10.2 no longer records it (cppcheck-opensource/simplecpp#759), so the filter has nothing left to do. The existing test for that case passes with or without the filter, as no other macro is expanded on its line. The new test puts DIV(1,0) on the same line as a bare ALIAS, and fails if simplecpp records the bare name and the filter is gone. The comment on the usage loop now says what it skips: usages on lines without macro-expanded tokens.
Macro::expand()adds the use location tousageListbefore it checks whether a function-like macro is followed by(. So with#define F(x) x, theFinint F = 1;shows up in the macro usage list even though nothing is expanded there.Came up in cppcheck-opensource/cppcheck#9018, where cppcheck uses the usage list to apply
cppcheck-suppress-macroand that phantom usage suppressed a realzerodiv. That PR filters it on the cppcheck side for now.This moves the
usageList.emplace_backbelow the no-arguments check.__COUNTER__still sees its usage before it reads the count, and builtins aren't function-like so they're unaffected.One output change goes with it. The early return now comes before
expandBuiltin, so#define __COUNTER__(x) xfollowed by a bare__COUNTER__;gives__COUNTER__ ;where it used to give0 ;. I think the new output is the right one, but it isn't only the usage list that changes.The second commit fixes the gap the review found in
#if. A defined name there is left forexpand()to record, which it no longer does for a bare function-like name, so#if Fwas a usage whenFwas defined after it but not before. Those names are now recorded like undefined ones.Outside
#if, cppcheck dump files will have one less<macro-usage>entry per bare function-like name.Tests are
tokenMacro16,tokenMacro17andtokenMacro18, which covers a bare name in a macro argument and in a replacement list, andmacroUsageArg4from #761 now expects no usage forINT64_C;. testrunner and run-tests.py pass on macOS arm64 after the rebase onto master, andrunformatwith uncrustify 0.80.1 makes no changes.I also dropped this branch into
externals/simplecppon the cppcheck-opensource/cppcheck#9018 branch. Its testrunner fails the same two tests it fails with plain simplecpp master,TestPreprocessor::garbageandTestPreprocessor::standard, which come from simplecpp being newer than cppcheck's copy. Nothing new fails.