Fixed #3058 (False positive: Uninitialized variable: data)

This commit is contained in:
Daniel Marjamäki 2011-08-31 19:45:20 +02:00
parent 477d1e92c9
commit a96028b43b
2 changed files with 47 additions and 10 deletions

View File

@ -7075,14 +7075,37 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
// Stop if there is a pointer alias and a shadow variable is
// declared in an inner scope (#3058)
if (valueIsPointer && tok3->varId() > 0 &&
tok3->previous() && tok3->previous()->isName() &&
tok3->previous() && (tok3->previous()->isName() || tok3->previous()->str() == "*") &&
valueToken->str() == "&" &&
valueToken->next() &&
valueToken->next()->isName() &&
tok3->str() == valueToken->next()->str() &&
tok3->varId() > valueToken->next()->varId())
{
break;
// more checking if this is a variable declaration
bool decl = true;
for (const Token *tok4 = tok3->previous(); tok4; tok4 = tok4->previous())
{
if (Token::Match(tok4, "[;{}]"))
break;
else if (tok4->isName())
{
if (tok4->varId() > 0)
{
decl = false;
break;
}
}
else if (!Token::Match(tok4, "[&*]"))
{
decl = false;
break;
}
}
if (decl)
break;
}
// Stop if label is found

View File

@ -2162,15 +2162,29 @@ private:
void simplifyKnownVariables43()
{
const char code[] = "void f() {\n"
" int a, *p; p = &a;\n"
" { int a = *p; }\n"
"}";
const char expected[] = "void f ( ) {\n"
"int a ; int * p ; p = & a ;\n"
"{ int a ; a = * p ; }\n"
{
const char code[] = "void f() {\n"
" int a, *p; p = &a;\n"
" { int a = *p; }\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
const char expected[] = "void f ( ) {\n"
"int a ; int * p ; p = & a ;\n"
"{ int a ; a = * p ; }\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
{
const char code[] = "void f() {\n"
" int *a, **p; p = &a;\n"
" { int *a = *p; }\n"
"}";
const char expected[] = "void f ( ) {\n"
"int * a ; int * * p ; p = & a ;\n"
"{ int * a ; a = * p ; }\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
}
void simplifyKnownVariablesBailOutAssign1()