Skip to content

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

Open
JayToltTech wants to merge 2 commits into
cppcheck-opensource:masterfrom
JayToltTech:macro-usage-undef
Open

JayToltTech wants to merge 2 commits into
cppcheck-opensource:masterfrom
JayToltTech:macro-usage-undef

Conversation

@JayToltTech

@JayToltTech JayToltTech commented Nov 16, 2026 •

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.

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. Since the rebase onto master the usages keep their arg from #761 through the #undef.

The second commit holds those usages back until preprocess() finishes. Before it, an error after an #undef left 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:

  • Builtins and -D macros that get #undef'd or redefined are now reported, with the placeholder location 1:1 and no known value. Macros that survive to the end were already reported that way.
  • An identical redefinition gives two entries, one per definition.
  • #undef after a header guard now reports the guard's #ifndef as a usage, same as without the #undef.
  • The list is longer overall, so cppcheck dump files get more <macro-usage> entries. X-macro code gets one per use.

The #ifdef attribution is better but not fixed. An #ifdef A while A is undefined still goes to A's next definition, and gets dropped if there isn't one. An #ifdef A before the first definition now goes to that first definition, where master put it on the last.

Tests are macroUsageUndef, macroUsageRedefine, macroUsageUndefIfdef and macroUsageUndefError, and all four fail without the change. testrunner and run-tests.py pass on macOS arm64, and runformat makes no changes.

I also dropped this branch into externals/simplecpp on the cppcheck-opensource/cppcheck#9018 branch. The #undef case 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::garbage and TestPreprocessor::standard, which come from simplecpp being newer than cppcheck's copy.

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

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.

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.
@JayToltTech

Copy link
Copy Markdown
Author

Independent review by Claude Fable 5.2 (Anthropic), run at the PR author's request.

Line numbers refer to simplecpp.cpp on this branch after the rebase onto current master.

Verdict

The change is correct and complete; I found no bug in the code.

What was checked:

  • testrunner passes.
  • A before/after comparison (master vs. this branch) over 15 edge-case inputs showed only the intended differences.
  • Uncrustify 0.80.1 (the pinned version) produces no diff on simplecpp.cpp or test.cpp.
  • The Makefile's -std=c++11 warning flags give no warnings.

Removal paths. The macro map is only erased or overwritten in two places, #define (4418-4424) and #undef (4713-4718), and both now call addMacroUsage(). Nothing else removes a definition:

  • dui.undefined only stops insertion.
  • -D and the builtins use insert, which never replaces, and run before any usage exists.
  • Includes and #pragma once do not touch the map.
  • A #define or #undef in a skipped block is ignored.

No duplicates. A reported macro is either erased or overwritten by operator=, which replaces usageList with the new macro's empty one. The maybeUsedMacros entry is erased when it is handed over.

Order. MacroMap is an unordered_map, so the list order was already unspecified. cppcheck does not depend on it: setLocationMacros (lib/cppcheck.cpp) builds sets, the dump writer just iterates, and misra.py does membership or pairwise checks. The test helper toString() sorts, so the new tests are stable across standard libraries.

arg. It survives both #undef and redefinition.

Issues

1. The list is now partially filled when preprocess() returns early (minor). The early returns clear output but not macroUsage.

#define A 1
A
#undef A
#error stop
  • Before: empty list.
  • After: A 1:9 use 2:1.
  • Without the #undef, the list is empty in both.

The same happens for a failed #define parse (return at 4447) and every other error return after line 4354. ifCond is already left partially filled on those paths, and cppcheck is not harmed, so this is defensible. It may still be worth either a macroUsage->clear() or a sentence in the PR description.

2. clang-tidy: one new diagnostic with v23, probably not in CI. clang-tidy 23.1.5 reports misc-const-correctness at 4192:130: parameter macroUsage "can be declared const". The same diagnostic fires on the existing preprocess() signature on master, and CI pins clang-tidy 22, so it is very likely a v23-only check on parameters. This was not confirmed with v22. Nothing else is new: 35 diagnostics before and after apart from this one.

Behaviour to be aware of (not bugs)

Outputs are name defLine:col use line:col.

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
  • Before: A 4:9 use 1:8, A 4:9 use 5:8.
  • After: A 3:9 use 1:8, A 4:9 use 5:8.

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
  • Before: all three uses on 9:9.
  • After: A 1:9 use 2:8, A 9:9 use 5:8, A 9:9 use 7:5.

If the name is never defined again, those entries are dropped, as before. So attribution of #ifdef/defined() usages is improved, not fully fixed.

Header guard followed by #undef is now reported. #ifndef G / #define G / #endif / #undef G gave nothing before and gives G 2:9 use 1:9 after. This matches what the no-#undef case already did.

Identical redefinition yields two definitions. #define A 1 / A / #define A 1 / A gave A 3:9 use 4:1 before; after it also gives A 1:9 use 2:1.

#undef of an undefined name is a no-op, and a later #ifdef of it is not reported, same as before.

Builtins and -D macros that are undefined or redefined are now reported with the placeholder location 1:1 and macroValueKnown=false. With -DPRE=1:

__COUNTER__ __LINE__
#undef __COUNTER__
#define __LINE__ 7
__LINE__ PRE
#undef PRE

newly gives __COUNTER__ 1:1 use 1:1, __LINE__ 1:1 use 1:13 and PRE 1:1 use 4:10, all with unknown value. Macros that survive to the end were already reported this way.

Optional suggestions

  • Tests. The three new tests fail on master and cover the core. Cases not covered:
    • arg across a redefinition (only #undef is tested);
    • #ifdef before the first definition followed by a redefinition;
    • a -D macro that is #undef'd (the unknown-value case);
    • #undef of an undefined name;
    • a macro defined and undefined inside an included header;
    • an error after #undef, if issue 1 is kept as is.
  • Keep an if (macroUsage) guard around the final loop at 4771, so nothing iterates when no list was requested.
  • The doc comment at 4191 is one long line and could be wrapped.
  • The PR description could mention that the list, and so cppcheck's dump <macro-usage>, grows: X-macro-heavy code gets an entry per use. misra 2.5 benefits, since a macro that is used and then undefined now counts as used.

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.
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