Skip to content

Report the usages of a macro that is undefined or redefined - #764

Open
JayToltTech wants to merge 1 commit into
cppcheck-opensource:masterfrom
JayToltTech:macro-usage-undef
Open

JayToltTech wants to merge 1 commit into
cppcheck-opensource:masterfrom
JayToltTech:macro-usage-undef

Conversation

@JayToltTech

Copy link
Copy Markdown

preprocess() builds the macro usage list from the macros that are still in its map when preprocessing ends. So after #undef A every usage of A is gone from the list, and if A is redefined the usages of the first definition are gone too.

#define A 1
A
#undef A

That gives an empty usage list, where I'd expect A 1:9 use 2:1.

There's a second effect with #ifdef. Those usages are kept by name, so an #ifdef A that came before #undef A / #define A gets reported against the second definition.

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 #undef and on redefinition, and at the end for whatever is left as before. The #ifdef usages for that name are handed over at the same time so they don't get counted again for a later definition.

Tests are macroUsageUndef, macroUsageRedefine and macroUsageUndefIfdef, and all three fail without the change. testrunner and run-tests.py pass on macOS arm64. I also dropped the change into cppcheck's externals/simplecpp locally and the #undef case in #9018 is suppressed with it. runformat makes no changes.

Separate from #759, they don't touch the same lines.

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.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 23:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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