Repository navigation
Fix #15153 FP constParameterPointer with unused attribute - #8976
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no problems that need fixing, and the change looks correct.
The change to getVariableTokenAfterAttributes reaches further than the parameter case the PR is about. It also fixes local variables and struct members whose declaration lists several names, because the attribute used to land on the next name:
int x __attribute__((unused)), y;used to markyand reportunusedVariableforx. Nowxis marked and that false positive is gone.struct S { int a __attribute__((unused)), b; };used to markband hide its warning. NowS::bis correctly reported asunusedStructMember.
Neither of these effects is covered by a test. A tokenizer or unused-variable test for them would protect the fix from regressions.
One more effect: isMaybeUnused() is now true for local variables with __attribute__((unused)), so constVariable and unreadVariable are skipped for them in the same way as for [[maybe_unused]]. That is consistent with the existing behaviour.
Testing
-
I built
cppcheckandtestrunnerfrompr/andcppcheckfrombase/; both built without errors. -
./testrunner TestOther TestTokenizer TestUnusedVar TestSymbolDatabase TestUnusedFunctions TestUnusedPrivateFunction TestGarbage TestVarIDran 1841 tests with 0 failures. -
I ran both binaries with
cppcheck -q --enable=all --inconclusiveon a scratch file with these cases:- the attribute before and after the parameter name
__unused__, andunusedcombined withaligned(4)used, which still warns as expected- an unnamed attributed parameter
- a function pointer parameter
- an array parameter
- a parameter that is actually dereferenced (now suppressed, the same as
[[maybe_unused]]today) - local declarations with several names
- struct members
sizeof(int __attribute__((unused)))
Differences from base:
- All
constParameterPointer/constParameterreports for__attribute__((unused))parameters are gone, which is the intended fix. a.c:11:9: style: Unused variable: x [unusedVariable]forint x __attribute__((unused)), y;is no longer reported (correct).struct member 'S::b' is never used. [unusedStructMember]forstruct S { int a __attribute__((unused)), b; };is now reported (correct; base had markedb).
-
With
--dump, for the unnamed parameter invoid f(int __attribute__((unused)), int *q)the attribute now lands on the type tokenint. Base put it on the next parameterq, which was wrong. I found no check that is affected by this. -
I found no crashes or hangs.
Automated review of 3c56ef2 by Claude. The pull request was built and tested locally.
|
Test results for commit 3c56ef2 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8976/ 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. |
|
Can this be merged?
|
|
spontanously to me the AI review about the comma operator makes sense. Don't you agree with it? |
Unused parameters annotated with the unused attribute were reported as they could be declared as pointer to const, while [[maybe_unused]] parameters were not.
The unused attribute in declarations with several names now marks the correct variable or struct member.
3c56ef2 to
ee22687
Compare
Sure, fixed. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. Parameters marked with __attribute__((unused)) no longer get constParameterPointer. The attribute now lands on the right name in declarations with several names: local variables, struct members and enumerators. The earlier review comment asking for tests of those declarations is fixed, because the PR now has tests for both cases. I compared this PR with base on many cases and found no regressions, crashes or new false positives.
Earlier comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | No test covers the attribute in declarations with several names (locals and struct members) | AI review | lib/tokenize.cpp:9722 |
✅ Fixed |
Testing
-
I built
cppcheckandtestrunnerfrompr/andcppcheckfrombase/; both built without errors. -
./testrunner TestOther TestUnusedVar TestTokenizer TestSymbolDatabase TestGarbage TestVarID: 1763 tests, 0 failed. -
I ran both binaries with
cppcheck -q --enable=all --inconclusiveon scratch files with these cases:- attributed parameters before and after the name
- an unnamed attributed parameter
- an array parameter
- a parameter that is dereferenced
cleanup(...)on a parameter- a function pointer parameter whose own parameter has the attribute
- a cast
(int __attribute__((unused)))v - an enum
enum E { A __attribute__((unused)), B }; - globals, locals and struct members with several names, mixing
unusedandused - the attribute only on the prototype and not on the definition
Differences from base:
- The
constParameterPointerfalse positive for the attributed parameter is gone (k6). S::m1andS::m2are no longer falsely reported;S::m3is now correctly reported asunusedStructMember.- For locals,
aandb(markedunusedandused) are no longer reported; the plaincnow is.
All of these are correct.
-
When the attribute is only on the prototype, the definition still gets
constParameterPointer. This was already so in base, and GCC also applies the attribute only to the parameters of the definition. A localint *a __attribute__((unused)) = 0;still getsconstVariablePointer, the same as[[maybe_unused]]does today. -
I found no crashes or hangs.
Automated review of ee22687 by Claude. The pull request was built and tested locally.
Unused parameters annotated with the unused attribute were reported as they could be declared as pointer to const, while [[maybe_unused]] parameters were not.