Repository navigation
Report the usages of a macro that is undefined or redefined - #764
JayToltTech wants to merge 2 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.
preprocess() built the macro usage list from the macros still defined when preprocessing ended, so the usages of a macro were lost after #undef, and after a redefinition the usages of the first definition were lost. An #ifdef before the #undef was reported against the later definition. The usages are now reported when the definition goes away.
e377a41 to
a5ca9c6
Compare
|
Independent review by Claude Fable 5.2 (Anthropic), run at the PR author's request. Line numbers refer to VerdictThe change is correct and complete; I found no bug in the code. What was checked:
Removal paths. The macro map is only erased or overwritten in two places,
No duplicates. A reported macro is either erased or overwritten by Order.
Issues1. The list is now partially filled when #define A 1
A
#undef A
#error stop
The same happens for a failed 2. clang-tidy: one new diagnostic with v23, probably not in CI. clang-tidy 23.1.5 reports Behaviour to be aware of (not bugs)Outputs are Uses seen before a definition move to the next definition, not the last. #ifdef A
#endif
#define A 1
#define A 2
#ifdef A
#endif
Uses while the name is undefined still attach to a later definition. #define A 1
#ifdef A
#endif
#undef A
#ifdef A
#endif
#if A
#endif
#define A 2
If the name is never defined again, those entries are dropped, as before. So attribution of Header guard followed by Identical redefinition yields two definitions.
Builtins and __COUNTER__ __LINE__
#undef __COUNTER__
#define __LINE__ 7
__LINE__ PRE
#undef PREnewly gives Optional suggestions
|
The usages of a macro that is undefined or redefined were added to the caller's list right away, so an error later in the file left that list partly filled, where before it stayed empty. Keep them in a local list and hand it over only when preprocessing succeeds.
preprocess()builds the macro usage list from the macros that are still in its map when preprocessing ends. So after#undef Aevery usage ofAis gone from the list, and ifAis redefined the usages of the first definition are gone too.That gives an empty usage list, where I'd expect
A 1:9 use 2:1.Came up in cppcheck-opensource/cppcheck#9018, where cppcheck uses the usage list to apply
cppcheck-suppress-macro. A suppression on a macro that's#undef'd after its use doesn't match anything.This reports a macro's usages at the point its definition goes away, on
#undefand on redefinition, and at the end for whatever is left as before. The#ifdefusages for that name are handed over at the same time so they don't get counted again for a later definition. Since the rebase onto master the usages keep theirargfrom #761 through the#undef.The second commit holds those usages back until
preprocess()finishes. Before it, an error after an#undefleft the caller's list partly filled, where master leaves it empty. Now it's empty again on any error.Some of what changes isn't strictly the bug, from the review:
-Dmacros that get#undef'd or redefined are now reported, with the placeholder location1:1and no known value. Macros that survive to the end were already reported that way.#undefafter a header guard now reports the guard's#ifndefas a usage, same as without the#undef.<macro-usage>entries. X-macro code gets one per use.The
#ifdefattribution is better but not fixed. An#ifdef AwhileAis undefined still goes toA's next definition, and gets dropped if there isn't one. An#ifdef Abefore the first definition now goes to that first definition, where master put it on the last.Tests are
macroUsageUndef,macroUsageRedefine,macroUsageUndefIfdefandmacroUsageUndefError, and all four fail without the change. testrunner and run-tests.py pass on macOS arm64, andrunformatmakes no changes.I also dropped this branch into
externals/simplecppon the cppcheck-opensource/cppcheck#9018 branch. The#undefcase there is now suppressed with no unmatched suppression, and its TODO test passes. Otherwise the 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.Separate from #759, they don't touch the same lines.