Skip to content

Commit

Permalink
Fix #12537 FP unusedVariable with local function declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 21, 2024
1 parent e2081e8 commit 4b5dd18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4826,7 +4826,8 @@ void Tokenizer::setVarIdPass1()
// function declaration inside executable scope? Function declaration is of form: type name "(" args ")"
if (scopeStack.top().isExecutable && Token::Match(tok, "%name% [,)[]")) {
bool par = false;
const Token *start, *end;
const Token* start;
Token* end;

// search begin of function declaration
for (start = tok; Token::Match(start, "%name%|*|&|,|("); start = start->previous()) {
Expand All @@ -4850,9 +4851,11 @@ void Tokenizer::setVarIdPass1()
const bool isNotstartKeyword = start->next() && notstart.find(start->next()->str()) != notstart.end();

// now check if it is a function declaration
if (Token::Match(start, "[;{}] %type% %name%|*") && par && Token::simpleMatch(end, ") ;") && !isNotstartKeyword)
if (Token::Match(start, "[;{}] %type% %name%|*") && par && Token::simpleMatch(end, ") ;") && !isNotstartKeyword) {
// function declaration => don't set varid
tok = end;
continue;
}
}

if ((!scopeStack.top().isEnum || !(Token::Match(tok->previous(), "{|,") && Token::Match(tok->next(), ",|=|}"))) &&
Expand Down
14 changes: 14 additions & 0 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,20 @@ class TestVarID : public TestFixture {
"6: }\n";
ASSERT_EQUALS(expected, actual);
}

{
const std::string actual = tokenize("void f(int n) {\n" // #12537
" int n1;\n"
" void g(int is, int n1);\n"
" n1 = n - 1;\n"
"}\n", "test.cpp");
const char expected[] = "1: void f ( int n@1 ) {\n"
"2: int n1@2 ;\n"
"3: void g ( int is , int n1 ) ;\n"
"4: n1@2 = n@1 - 1 ;\n"
"5: }\n";
ASSERT_EQUALS(expected, actual);
}
}

void varid_sizeof() {
Expand Down

0 comments on commit 4b5dd18

Please sign in to comment.