Repository navigation
Fix #15311 internalError with unnamed array parameter of function typedef type - #9117
wjakobsson wants to merge 1 commit into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix works for the reported case and its variations. It also stops the internalError for an unnamed FN * const parameter (void f(FN * const);, sizeof(FN * const), void f(FN * const, FN * const)), and I saw no regressions. One new problem: the new const check stops only before , and ). When const is followed by > or =, the closing ) of the function pointer type is still put in the wrong place, for example std::vector<FN * const> v; or void f(FN * const = nullptr);. The *|& loop just above already stops before [)>,]. A related case that is still not handled is an unnamed array after a second pointer level (FN * const * []): the [] ends up outside the function pointer type. That was the same before the PR.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | const followed by > or = is still mishandled (std::vector<FN * const>, FN * const = nullptr) |
AI review | lib/tokenize.cpp:470 |
🆕 New |
| 2 | Unnamed array after * const * is still not handled |
AI review | lib/tokenize.cpp:477 |
🆕 New |
@wjakobsson, please fix, resolve or reply to the open and new comments: #1, #2.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestSimplifyTypedef TestTokenizer TestSimplifyTemplate reports 0 failures.
Each scratch file starts with typedef int (FN)(void*);, and I compared cppcheck -q --debug-normal between base and PR.
Fixed by the PR (base gave internalError or a broken simplification): void f(FN * const []);, FN * [2][3], FN * const [], int, FN ** const [], FN *[], FN * [] = nullptr, void f(FN * const [2], FN * []);, void f(FN * const);, sizeof(FN * const), void f(FN * const, FN * const);.
Same result in base and PR: named arrays such as FN * const x[] and FN * const a[3];, plus FN &, FN * const & and FN * volatile [].
Still wrong with the PR:
std::vector<FN * const> v;
-> std :: vector < int ( * const > ) ( void * ) v ;
void f(FN * const = nullptr);
-> void f ( int ( * const = ) ( void * ) nullptr ) ;
void f(FN * const * []);
-> void f ( int ( * const * ) ( void * ) [ ] ) ;
In a local copy I changed line 470 to !Token::Match(tok2->next(), "[,)>=]"). That gave std :: vector < int ( * const ) ( void * ) > v ; and void f ( int ( * const ) ( void * ) = nullptr ) ;. I did not run the testrunner on that copy.
Automated review of edd19e6 by Claude. The pull request was built and tested locally.
| tok2 = tok2->tokAt(2); | ||
|
|
||
| if (Token::simpleMatch(tok2, "const")) | ||
| if (Token::simpleMatch(tok2, "const") && !Token::Match(tok2->next(), "[,)]")) |
There was a problem hiding this comment.
AI review
The check stops only before , and ). When const is followed by > (a template argument) or = (a default argument), the closing ) still ends up in the wrong place. The *|& loop above already stops before [)>,].
typedef int (FN)(void*);
std::vector<FN * const> v; // -> std :: vector < int ( * const > ) ( void * ) v ;
void f(FN * const = nullptr); // -> void f ( int ( * const = ) ( void * ) nullptr ) ;
I tried "[,)>=]" here in a local copy. It gives std :: vector < int ( * const ) ( void * ) > v ; and void f ( int ( * const ) ( void * ) = nullptr ) ;.
| return nullptr; | ||
|
|
||
| // unnamed array parameter: void f(FN * const []) | ||
| if (Token::simpleMatch(tok2, "[")) { |
There was a problem hiding this comment.
AI review
The [ is skipped only directly after the first */const group. With another pointer level after const, the array is still put outside the type:
typedef int (FN)(void*);
void f(FN * const * []); // -> void f ( int ( * const * ) ( void * ) [ ] ) ;
This was the same before the PR, but it is the same kind of unnamed array parameter.
No description provided.