Fixed #2638 (Tokenizer::setVarId : varid is wrongly given when unknown macro is used)

This commit is contained in:
Daniel Marjamäki 2011-03-13 17:52:45 +01:00
parent 3e1df1b463
commit 07fe361964
4 changed files with 46 additions and 0 deletions

View File

@ -2032,6 +2032,9 @@ bool CheckBufferOverrun::ArrayInfo::declare(const Token *tok, const Tokenizer &t
atok = atok->tokAt(2);
}
if (Token::Match(atok, "] = !!{"))
return false;
return (!_num.empty() && Token::Match(atok, "] ;|="));
}

View File

@ -3504,6 +3504,19 @@ void Tokenizer::setVarId()
}
}
// Don't set variable id for 'AAA a[0] = 0;' declaration (#2638)
if (tok2->previous()->varId() && tok2->str() == "[")
{
const Token *tok3 = tok2;
while (tok3 && tok3->str() == "[")
{
tok3 = tok3->link();
tok3 = tok3 ? tok3->next() : NULL;
}
if (Token::Match(tok3, "= !!{"))
continue;
}
// Variable declaration found => Set variable ids
if (Token::Match(tok2, "[,();[=]") && !varname.empty())
{

View File

@ -211,6 +211,8 @@ private:
TEST_CASE(scope); // handling different scopes
TEST_CASE(getErrorMessages);
TEST_CASE(unknownMacroNoDecl); // #2638 - not variable declaration: 'AAA a[0] = 0;'
}
@ -2941,6 +2943,16 @@ private:
CheckBufferOverrun c;
c.getErrorMessages(this, 0);
}
void unknownMacroNoDecl()
{
check("void f() {\n"
" int a[10];\n"
" AAA a[0] = 0;\n" // <- not a valid array declaration
" a[1] = 1;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestBufferOverrun)

View File

@ -181,6 +181,7 @@ private:
TEST_CASE(varid_in_class2);
TEST_CASE(varid_operator);
TEST_CASE(varid_throw);
TEST_CASE(varid_unknown_macro); // #2638 - unknown macro is not type
TEST_CASE(varidclass1);
TEST_CASE(varidclass2);
@ -3133,6 +3134,23 @@ private:
ASSERT_EQUALS(expected, actual);
}
void varid_unknown_macro()
{
// #2638 - unknown macro
const char code[] = "void f() {\n"
" int a[10];\n"
" AAA\n"
" a[0] = 0;\n"
"}";
const char expected[] = "\n\n##file 0\n"
"1: void f ( ) {\n"
"2: int a@1 [ 10 ] ;\n"
"3: AAA\n"
"4: a@1 [ 0 ] = 0 ;\n"
"5: }\n";
ASSERT_EQUALS(expected, tokenizeDebugListing(code));
}
void varidclass1()
{
const std::string actual = tokenizeDebugListing(