Skip to content

Fix #15246 (Remove the unexpanded macro name filter from setLocationMacros) - #9056

Merged
danmar merged 1 commit into
cppcheck-opensource:mainfrom
JayToltTech:cleanup-15208-filter
Nov 22, 2026
Merged

danmar merged 1 commit into
cppcheck-opensource:mainfrom
JayToltTech:cleanup-15208-filter

Conversation

@JayToltTech

@JayToltTech JayToltTech commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

https://trac.cppcheck.net/ticket/15246

Rebased onto main now that #9048 is merged.

#9018 added a filter to setLocationMacros that skips a macro usage at the position of a bare function-like macro name, because simplecpp recorded a usage there although nothing was expanded. simplecpp 1.10.2 doesn't record it anymore (cppcheck-opensource/simplecpp#759), so the filter has nothing left to do.

The test I added in #9018 for that case turned out not to cover the filter. It has no other macro on the line, so the usage never reached mLocationMacros either way. The new test puts DIV(1,0) on the same line as a bare ALIAS with cppcheck-suppress-macro zerodiv on ALIAS. Confirmed it fails with the filter removed and simplecpp's old usage order put back, and passes on 1.10.2.

TestSuppressions, TestPreprocessor and TestCppcheck pass on macOS arm64 with -Werror, and runformat with uncrustify 0.80.1 makes no changes.

@dmcppcheck

Copy link
Copy Markdown
Contributor

There should probably be a trac ticket for this PR. Please create it, or ask us to create it. Then the PR title should be updated.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

I found no new problems in the PR's own commit (0d83da1). That commit removes the unexpanded-name filter from setLocationMacros and adds a test for it.

simplecpp 1.10.2 only records a usage in two cases: when a macro is really expanded, or when the name appears in a directive (#ifdef, #ifndef, defined, or a function-like name without ( in #if). Directive lines produce no tokens, so those usages never match an entry in mLocationMacros. The filter therefore has nothing left to catch.

I tried several ways of putting a bare or indirectly used function-like macro name on the same line as DIV(1,0). In none of them was the zerodiv wrongly suppressed. Usages that really are expanded on the line are still suppressed, as before.

The other commits (the simplecpp 1.10.x update from #9048) were not the focus of this review.

Testing

  • Build: make -j8 cppcheck testrunner in pr/ succeeded.

  • Unit tests: ./testrunner TestSuppressions TestPreprocessor TestCppcheck ran 271 tests with 0 failures and 6 todos.

  • Python tests: python3 -m pytest other_test.py in test/cli gave 210 passed and 10 failed. All 10 failures come from the environment, not from the PR:

    • the --rule/--rule-file tests fail because this build has no rules support;
    • the test_redundant_file_reads tests fail with FileNotFoundError, probably because strace is missing here.

    None of the tests that the PR changes failed.

  • Manual tests, run with cppcheck --inline-suppr --enable=information. In each case ALIAS has cppcheck-suppress-macro zerodiv on its definition and DIV does not:

    • These should still report zerodiv, and they do. The ALIAS suppression is reported as unmatched, and the dump has no usage of ALIAS:
      • int ALIAS = DIV(1,0);
      • #define G ALIAS then int G = DIV(1,0);
      • int ID(ALIAS) = DIV(1,0);
      • sizeof(int(*)[ID(ALIAS)]) on the same line as DIV(1,0)
      • int ALIAS; int x = DIV(1,0);
    • These should be suppressed, and they are: DIV(1,0) + CALL(ALIAS) with #define CALL(m) m(1,1), and the self-referential macros #define x x and #define F(a) F(a) when they are expanded on the line of a DIV(1,0).
  • I did not build base/ for a comparison, because it also has the older simplecpp and the results would not isolate this commit.


Automated review of 0d83da1 by Claude. The pull request was built and tested locally.

@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit 0d83da1 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9056/
packages: 100
Differing warnings: 23 (+18 -5)
AI review: 17 improvement, 6 neutral (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9056/2026-11-21_13-59-32-0d83da19e8b1-review.txt)
Timing: +2.1% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9056/2026-11-21_13-59-32-0d83da19e8b1_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@JayToltTech JayToltTech changed the title Remove the unexpanded macro name filter from setLocationMacros Fix #15246 (cppcheck-suppress-macro: remove the unexpanded macro name filter from setLocationMacros now that simplecpp records only real macro usages) Nov 21, 2026
@JayToltTech JayToltTech changed the title Fix #15246 (cppcheck-suppress-macro: remove the unexpanded macro name filter from setLocationMacros now that simplecpp records only real macro usages) Fix #15246 (Remove the unexpanded macro name filter from setLocationMacros) Nov 21, 2026
@JayToltTech

Copy link
Copy Markdown
Contributor Author

Created https://trac.cppcheck.net/ticket/15246 and updated the PR title.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

I found no new problems in this PR's own commit, which removes the unexpanded-name filter from setLocationMacros and adds a TestSuppressions case. To isolate that change, I built a copy of the PR with only the old filter put back in lib/cppcheck.cpp. On 15 inputs, it and the PR's cppcheck gave identical results. The inputs put a bare or indirectly used function-like macro name on the same line as DIV(1,0). So with simplecpp 1.10.2 the filter no longer changes the results. The trac ticket that C1 asked for has been created, and the title has been updated.

Comments

# Comment By Where Status
1 PR should have a trac ticket and an updated title @dmcppcheck general ✅ Fixed

Testing

Builds: make -j8 cppcheck testrunner in pr/ succeeded, and make -j8 cppcheck in base/ succeeded. I also built a copy of pr/ (scratch/prf) where lib/cppcheck.cpp is replaced by the base version, which still has the filter. All other code is the same as the PR, so any difference between the two would come from removing the filter.

Unit tests: ./testrunner TestSuppressions TestPreprocessor TestCppcheck ran 271 tests with 0 failures and 6 todos.

Manual tests: each file starts with

#define DIV(A,B) A/B
// cppcheck-suppress-macro zerodiv
#define ALIAS(A,B) A/B

Each was run with cppcheck -q --inline-suppr --enable=information --template='{line}:{column} {id}', using the PR's cppcheck and the cppcheck with the filter put back. The bodies tested:

  • int ALIAS = DIV(1,0);
  • ALIAS LP 1,0), where LP is defined as (
  • #define F ALIAS followed by F(1,0)
  • ID(ALIAS)(1,0) and ID(ALIAS) + DIV(1,0)
  • ALIAS(1,0) + DIV(1,0)
  • G(ALIAS), where G(x) is x(1,0)
  • STR(ALIAS(1,0)), where STR stringifies its argument
  • CAT(ALI,AS)(1,0), where CAT pastes its arguments together
  • #define ALIAS2 ALIAS
  • #if defined(ALIAS) before the line with DIV
  • #ifdef ALIAS in the middle of a statement
  • DIV(ALIAS,0)
  • #line directives that give an #ifdef ALIAS line the same line number as the DIV line
  • a self-referencing function-like macro, S(A,B) S + A/B, next to a variable named S

Both builds gave the same output for every file. The zerodiv was suppressed only where ALIAS (or the suppressed macro) is really expanded on the line. That is the existing line-based behaviour and was not changed by this PR.


Automated review of 0613c15 by Claude. The pull request was built and tested locally.

@dmcppcheck

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved.

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.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

I found no new problems. With simplecpp 1.10.2, a bare function-like macro name no longer gets a macro usage, so the removed filter in setLocationMacros had nothing left to catch. The simplified loop behaves the same as before. C2 and C5 already compared the PR with a build that still has the filter and saw no difference on a wide range of inputs. The containers and algorithms still used in lib/cppcheck.cpp (std::set, std::transform, std::inserter) are used elsewhere in the file, so removing this code leaves no includes unused.

Comments

# Comment By Where Status
1 PR should have a trac ticket and an updated title @dmcppcheck general ✅ Fixed

Testing

  • Build: make -j8 cppcheck testrunner in pr/ succeeded.
  • Unit tests: ./testrunner TestSuppressions TestPreprocessor TestCppcheck ran 271 tests with 0 failures and 6 todos.
  • Manual test: I ran cppcheck -q --inline-suppr --enable=information on a file where ALIAS has cppcheck-suppress-macro zerodiv and this line contains both a bare ALIAS and a real ALIAS(1,0) expansion:
    int ALIAS = DIV(1,0); int y = ALIAS(1,0);
    • Both zerodivs on that line were suppressed. That is the existing line-based behaviour: ALIAS is really expanded on the line. The PR does not change it.
    • The PR's own new test, with only the bare name on the line, reports zerodiv as it should.
  • Not repeated: C2 and C5 already covered the wider set of bare and indirect macro-name inputs and the comparison with the filter put back, so I did not run them again.

Automated review of b277a55 by Claude. The pull request was built and tested locally.

@JayToltTech

Copy link
Copy Markdown
Contributor Author

Re the manual test where both zerodivs on int ALIAS = DIV(1,0); int y = ALIAS(1,0); were suppressed: agreed it's the existing line-based matching and not from this PR. It is a bug though, since the DIV(1,0) zerodiv has nothing to do with ALIAS. I filed it as https://trac.cppcheck.net/ticket/15247, with a smaller repro.

@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit b277a55 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9056/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: +0.2% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9056/2026-11-21_23-25-33-b277a55d67e6_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@danmar
danmar merged commit 7ce8186 into cppcheck-opensource:main Nov 22, 2026
74 checks passed
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.

3 participants