Skip to content

Fix #15311 internalError with unnamed array parameter of function typedef type - #9117

Open
wjakobsson wants to merge 1 commit into
cppcheck-opensource:mainfrom
wjakobsson:fix-15311
Open

wjakobsson wants to merge 1 commit into
cppcheck-opensource:mainfrom
wjakobsson:fix-15311

Conversation

@wjakobsson

Copy link
Copy Markdown
Collaborator

No description provided.

@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

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.

Comment thread lib/tokenize.cpp
tok2 = tok2->tokAt(2);

if (Token::simpleMatch(tok2, "const"))
if (Token::simpleMatch(tok2, "const") && !Token::Match(tok2->next(), "[,)]"))

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

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

Comment thread lib/tokenize.cpp
return nullptr;

// unnamed array parameter: void f(FN * const [])
if (Token::simpleMatch(tok2, "[")) {

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

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.

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