Fixed #3058 (False positive: Uninitialized variable: data)
This commit is contained in:
parent
477d1e92c9
commit
a96028b43b
|
@ -7075,14 +7075,37 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
|
||||||
// Stop if there is a pointer alias and a shadow variable is
|
// Stop if there is a pointer alias and a shadow variable is
|
||||||
// declared in an inner scope (#3058)
|
// declared in an inner scope (#3058)
|
||||||
if (valueIsPointer && tok3->varId() > 0 &&
|
if (valueIsPointer && tok3->varId() > 0 &&
|
||||||
tok3->previous() && tok3->previous()->isName() &&
|
tok3->previous() && (tok3->previous()->isName() || tok3->previous()->str() == "*") &&
|
||||||
valueToken->str() == "&" &&
|
valueToken->str() == "&" &&
|
||||||
valueToken->next() &&
|
valueToken->next() &&
|
||||||
valueToken->next()->isName() &&
|
valueToken->next()->isName() &&
|
||||||
tok3->str() == valueToken->next()->str() &&
|
tok3->str() == valueToken->next()->str() &&
|
||||||
tok3->varId() > valueToken->next()->varId())
|
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
|
// Stop if label is found
|
||||||
|
|
|
@ -2162,15 +2162,29 @@ private:
|
||||||
|
|
||||||
void simplifyKnownVariables43()
|
void simplifyKnownVariables43()
|
||||||
{
|
{
|
||||||
const char code[] = "void f() {\n"
|
{
|
||||||
" int a, *p; p = &a;\n"
|
const char code[] = "void f() {\n"
|
||||||
" { int a = *p; }\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));
|
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()
|
void simplifyKnownVariablesBailOutAssign1()
|
||||||
|
|
Loading…
Reference in New Issue