Skip to content

Commit ba6cda9

Browse files
githubharalddanmar
authored andcommitted
Fixed cppcheck-opensource#7740 (Tokenizer::setVarId: Function declaration does not start with 'return')
1 parent 143e7bf commit ba6cda9

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

lib/tokenize.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,10 +2766,12 @@ void Tokenizer::setVarIdPass1()
27662766
continue;
27672767
}
27682768

2769-
// function declaration inside executable scope?
2769+
// function declaration inside executable scope? Function declaration is of form: type name "(" args ")"
27702770
if (scopeStack.top().isExecutable && Token::Match(tok, "%name% [,)]")) {
27712771
bool par = false;
27722772
const Token *start, *end;
2773+
2774+
// search begin of function declaration
27732775
for (start = tok; Token::Match(start, "%name%|*|&|,|("); start = start->previous()) {
27742776
if (start->str() == "(") {
27752777
if (par)
@@ -2783,8 +2785,15 @@ void Tokenizer::setVarIdPass1()
27832785
if (start->varId() > 0U)
27842786
break;
27852787
}
2788+
2789+
// search end of function declaration
27862790
for (end = tok->next(); Token::Match(end, "%name%|*|&|,"); end = end->next()) {}
2787-
if (Token::Match(start, "[;{}] %type% %name%|*") && par && Token::simpleMatch(end, ") ;"))
2791+
2792+
// there are tokens which can't appear at the begin of a function declaration such as "return"
2793+
const bool isNotstartKeyword = start->next() && notstart.find(start->next()->str()) != notstart.end();
2794+
2795+
// now check if it is a function declaration
2796+
if (Token::Match(start, "[;{}] %type% %name%|*") && par && Token::simpleMatch(end, ") ;") && !isNotstartKeyword)
27882797
// function declaration => don't set varid
27892798
continue;
27902799
}

test/testvarid.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,15 @@ class TestVarID : public TestFixture {
758758
"3: void bar ( int * p ) ;\n"
759759
"4: }\n";
760760
ASSERT_EQUALS(expected2, tokenize(code2));
761+
762+
// #7740
763+
const char code3[] = "Float f(float scale) {\n"
764+
" return Float(val * scale);\n"
765+
"}\n";
766+
const char expected3[] = "1: Float f ( float scale@1 ) {\n"
767+
"2: return Float ( val * scale@1 ) ;\n"
768+
"3: }\n";
769+
ASSERT_EQUALS(expected3, tokenize(code3));
761770
}
762771

763772
void varid36() { // ticket #2980 (segmentation fault)

0 commit comments

Comments
 (0)