Fixed #7740 (Tokenizer::setVarId: Function declaration does not start with 'return')

This commit is contained in:
Harald Scheidl 2016-10-03 10:02:18 +02:00 committed by Daniel Marjamäki
parent 143e7bf8b1
commit ba6cda9c86
2 changed files with 20 additions and 2 deletions

View File

@ -2766,10 +2766,12 @@ void Tokenizer::setVarIdPass1()
continue;
}
// function declaration inside executable scope?
// 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;
// search begin of function declaration
for (start = tok; Token::Match(start, "%name%|*|&|,|("); start = start->previous()) {
if (start->str() == "(") {
if (par)
@ -2783,8 +2785,15 @@ void Tokenizer::setVarIdPass1()
if (start->varId() > 0U)
break;
}
// search end of function declaration
for (end = tok->next(); Token::Match(end, "%name%|*|&|,"); end = end->next()) {}
if (Token::Match(start, "[;{}] %type% %name%|*") && par && Token::simpleMatch(end, ") ;"))
// there are tokens which can't appear at the begin of a function declaration such as "return"
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)
// function declaration => don't set varid
continue;
}

View File

@ -758,6 +758,15 @@ private:
"3: void bar ( int * p ) ;\n"
"4: }\n";
ASSERT_EQUALS(expected2, tokenize(code2));
// #7740
const char code3[] = "Float f(float scale) {\n"
" return Float(val * scale);\n"
"}\n";
const char expected3[] = "1: Float f ( float scale@1 ) {\n"
"2: return Float ( val * scale@1 ) ;\n"
"3: }\n";
ASSERT_EQUALS(expected3, tokenize(code3));
}
void varid36() { // ticket #2980 (segmentation fault)