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
3 changes: 2 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5058,7 +5058,8 @@ void Tokenizer::setVarIdPass1()

if (tok3->isLiteral() ||
(tok3->isName() && (variableMap.hasVariable(tok3->str()) ||
(tok3->strAt(-1) == "(" && Token::simpleMatch(tok3->next(), "(") && !Token::simpleMatch(tok3->linkAt(1)->next(), "(")))) ||
(tok3->strAt(-1) == "(" && Token::simpleMatch(tok3->next(), "(") && !Token::simpleMatch(tok3->linkAt(1)->next(), "(") &&
!(Token::Match(tok3->next(), "( *|&|&& %name%| ) [") && !variableMap.hasVariable(tok3->strAt(3)))))) ||

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 pattern ( *|&|&& %name%| ) [ only matches a single pointer or reference token directly before the name. Other pointer-to-array declarators are still read as variable initialisations, so the function name still gets a varid:

A h3(T (*const p)[16]);   // A h3@var2 ( T ( * const p@var3 ) [ 16 ] ) ;
A h4(T (**p)[16]);        // A h4@var4 ( T ( * * p@var5 ) [ 16 ] ) ;

One way to fix this is to skip a run of *|&|&&|const|volatile tokens before the name, for example by walking from tok3->tokAt(2) up to the ) and then checking for ) [.

tok3->isOp() ||
tok3->str() == "(" ||
notstart.find(tok3->str()) != notstart.end()) {
Expand Down
10 changes: 8 additions & 2 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1632,12 +1632,18 @@ class TestVarID : public TestFixture {
"A f(3);\n"
"A f2(true);\n"
"A g();\n"
"A e(int c);\n", dinit(TokenizeOptions, $.cpp = false));
"A e(int c);\n"
"A h(T (*p)[16]);\n"
"int x(g(1)[0]);\n"
"A u(T (*)[16]);\n", dinit(TokenizeOptions, $.cpp = false));

const char expected[] = "1: A f@1 ( 3 ) ;\n"
"2: A f2@2 ( true ) ;\n"
"3: A g ( ) ;\n"
"4: A e ( int c@3 ) ;\n";
"4: A e ( int c@3 ) ;\n"
"5: A h ( T ( * p@4 ) [ 16 ] ) ;\n"
"6: int x@5 ( g ( 1 ) [ 0 ] ) ;\n"
"7: A u ( T ( * ) [ 16 ] ) ;\n";

ASSERT_EQUALS(expected, actual);
}
Expand Down
Loading