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
35 changes: 35 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4712,6 +4712,41 @@ void Tokenizer::setVarIdPass1()
bool initlist = false;
bool inlineFunction = false;
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::simpleMatch(tok, ") (") && Token::simpleMatch(tok->link(), "( *")) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to set the parameter ids when the function pointer id is set. I fear I feel this code is a too quick and easy. Using parsedecl would be better.

I.e. this is valid usage of a function pointer:

int(*fp)(int x);
int data = y * (*fp)(12*y);

if y is undeclared it doesn't have varid.

Undeclared variables/types sometimes means setVarId can't determine if a statement is a declaration or not. But parsedecl is somewhat intelligent it should see that neither fp nor y is declared on line 2.

const Token *typeTok = tok->link()->previous();
while (Token::Match(typeTok, "*|&")) {
typeTok = tok->previous();
}

if (!Token::Match(typeTok, "%type%"))
continue;

// Add globally unique varids to function pointer arguments
variableMap.enterScope();

tok = tok->next();
const Token *endTok = tok->link();
tok = tok->next();

for (Token *argTok = tok; argTok != endTok; argTok = argTok->next()) {
if (Token::simpleMatch(argTok, "("))
argTok = argTok->link();

if (!Token::Match(argTok, "%name%|*|&|&& %name% ,|)"))
continue;

argTok = argTok->next();

if (argTok->isStandardType())
continue;

variableMap.addVariable(argTok->str(), false);
argTok->varId(variableMap.getVarId());
}

variableMap.leaveScope();
continue;
}
if (tok->isOp())
continue;
if (cpp && Token::simpleMatch(tok, "template <")) {
Expand Down
8 changes: 7 additions & 1 deletion test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3540,9 +3540,15 @@ class TestVarID : public TestFixture {
"}\n";
ASSERT_EQUALS("1: void f ( ) {\n"
"2: int * p@1 ;\n"
"3: void ( * a@2 [ 1 ] ) ( int * p ) = { 0 } ;\n"
"3: void ( * a@2 [ 1 ] ) ( int * p@3 ) = { 0 } ;\n"
"4: }\n",
tokenize(code4));

const char code5[] = "int *p;\n"
"void (*a[1])(int* p) = { 0 } ;\n";
ASSERT_EQUALS("1: int * p@1 ;\n"
"2: void ( * a@2 [ 1 ] ) ( int * p@3 ) = { 0 } ;\n"
, tokenize(code5));
}

void varid_alignas() {
Expand Down
Loading