Fixed #4627 (Tokenizer::setVarId: function declaration in function body)

This commit is contained in:
Daniel Marjamäki 2015-11-19 16:27:16 +01:00
parent ab171fc027
commit 40851a12ef
2 changed files with 38 additions and 2 deletions

View File

@ -2858,6 +2858,29 @@ void Tokenizer::setVarId()
continue; continue;
} }
// function declaration inside executable scope?
if (scopeStack.top().isExecutable && Token::Match(tok, "%name% [,)]")) {
bool par = false;
const Token *start, *end;
for (start = tok; Token::Match(start, "%name%|*|&|,|("); start = start->previous()) {
if (start->str() == "(") {
if (par)
break;
par = true;
}
if (Token::Match(start, "[(,]")) {
if (!Token::Match(start, "[(,] %type% %name%|*|&"))
break;
}
if (start->varId() > 0U)
break;
}
for (end = tok->next(); Token::Match(end, "%name%|*|&|,"); end = end->next()) {}
if (Token::Match(start, "[;{}] %type% %name%|*") && par && Token::simpleMatch(end, ") ;"))
// function declaration => don't set varid
continue;
}
const std::map<std::string, unsigned int>::const_iterator it = variableId.find(tok->str()); const std::map<std::string, unsigned int>::const_iterator it = variableId.find(tok->str());
if (it != variableId.end()) { if (it != variableId.end()) {
tok->varId(it->second); tok->varId(it->second);

View File

@ -64,7 +64,7 @@ private:
TEST_CASE(varid32); // ticket #2835 (segmentation fault) TEST_CASE(varid32); // ticket #2835 (segmentation fault)
TEST_CASE(varid33); // ticket #2875 (segmentation fault) TEST_CASE(varid33); // ticket #2875 (segmentation fault)
TEST_CASE(varid34); // ticket #2825 TEST_CASE(varid34); // ticket #2825
TEST_CASE(varid35); // ticket #2937 TEST_CASE(varid35); // function declaration inside function body
TEST_CASE(varid36); // ticket #2980 (segmentation fault) TEST_CASE(varid36); // ticket #2980 (segmentation fault)
TEST_CASE(varid37); // ticket #3092 (varid for 'Bar bar(*this);') TEST_CASE(varid37); // ticket #3092 (varid for 'Bar bar(*this);')
TEST_CASE(varid38); // ticket #3272 (varid for 'FOO class C;') TEST_CASE(varid38); // ticket #3272 (varid for 'FOO class C;')
@ -760,7 +760,8 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void varid35() { // ticket #2937 void varid35() { // function declaration inside function body
// #2937
const char code[] ="int foo() {\n" const char code[] ="int foo() {\n"
" int f(x);\n" " int f(x);\n"
" return f;\n" " return f;\n"
@ -771,6 +772,18 @@ private:
"3: return f@1 ;\n" "3: return f@1 ;\n"
"4: }\n"; "4: }\n";
ASSERT_EQUALS(expected, tokenize(code)); ASSERT_EQUALS(expected, tokenize(code));
// #4627
const char code2[] = "void f() {\n"
" int *p;\n"
" void bar(int *p);\n"
"}";
const char expected2[] = "\n\n##file 0\n"
"1: void f ( ) {\n"
"2: int * p@1 ;\n"
"3: void bar ( int * p ) ;\n"
"4: }\n";
ASSERT_EQUALS(expected2, tokenize(code2));
} }
void varid36() { // ticket #2980 (segmentation fault) void varid36() { // ticket #2980 (segmentation fault)