Repository navigation
Do not record a usage for a function-like macro name without ( - #759
JayToltTech wants to merge 4 commits into
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. |
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.