Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,19 @@ const Token *Tokenizer::processFunc(const Token *tok2, bool inOperator) const
while (Token::Match(tok2, "%name% ::"))
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 ) ;.

tok2 = tok2->next();

if (!tok2)
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.

tok2 = tok2->link();
while (Token::simpleMatch(tok2->next(), "["))
tok2 = tok2->linkAt(1);
}

if (tok2->str() == "(" &&
tok2->link()->next() &&
tok2->link()->strAt(1) == "(") {
Expand Down
15 changes: 15 additions & 0 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedefFunction9);
TEST_CASE(simplifyTypedefFunction10); // #5191
TEST_CASE(simplifyTypedefFunction11);
TEST_CASE(simplifyTypedefFunction12);

TEST_CASE(simplifyTypedefStruct); // #12081 - volatile struct

Expand Down Expand Up @@ -4512,6 +4513,20 @@ class TestSimplifyTypedef : public TestFixture {
ignore_errout(); // we are not interested in the output
}

void simplifyTypedefFunction12() { // #15311
const char code1[] = "typedef int (FN)(void*);\n"
"void f(FN * const []);\n";
ASSERT_EQUALS("void f ( int ( * const [ ] ) ( void * ) ) ;", simplifyTypedefC(code1));

const char code2[] = "typedef int (FN)(void*);\n"
"void f(FN * [2][3]);\n";
ASSERT_EQUALS("void f ( int ( * [ 2 ] [ 3 ] ) ( void * ) ) ;", simplifyTypedefC(code2));

const char code3[] = "typedef int (FN)(void*);\n"
"void f(FN * const, int);\n";
ASSERT_EQUALS("void f ( int ( * const ) ( void * ) , int ) ;", simplifyTypedefC(code3));
}

void simplifyTypedefStruct() {
const char code1[] = "typedef struct S { int x; } xyz;\n"
"xyz var;\n";
Expand Down
Loading