Fixed #2031 (Tokenizer: simplify known value (string has known value after strcpy))

This commit is contained in:
Daniel Marjamäki 2011-02-26 21:11:56 +01:00
parent 96c85104ab
commit c1c9b96bb6
2 changed files with 32 additions and 0 deletions

View File

@ -6356,6 +6356,22 @@ bool Tokenizer::simplifyKnownVariables()
ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, valueToken, indentlevel); ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, valueToken, indentlevel);
} }
else if (Token::Match(tok2, "strcpy ( %var% , %str% ) ;"))
{
const unsigned int varid(tok2->tokAt(2)->varId());
if (varid == 0)
continue;
const std::string structname("");
const Token * const valueToken = tok2->tokAt(4);
std::string value(valueToken->str());
const unsigned int valueVarId(0);
const bool valueIsPointer(false);
Token *tok3 = tok2;
for (int i = 0; i < 6; ++i)
tok3 = tok3->next();
ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, valueToken, indentlevel);
}
} }
if (tok2) if (tok2)

View File

@ -129,6 +129,7 @@ private:
TEST_CASE(simplifyKnownVariables39); TEST_CASE(simplifyKnownVariables39);
TEST_CASE(simplifyKnownVariables40); TEST_CASE(simplifyKnownVariables40);
TEST_CASE(simplifyKnownVariables41); // p=&x; if (p) .. TEST_CASE(simplifyKnownVariables41); // p=&x; if (p) ..
TEST_CASE(simplifyKnownVariables42); // ticket #2031 - known string value after strcpy
TEST_CASE(simplifyKnownVariablesBailOutAssign); TEST_CASE(simplifyKnownVariablesBailOutAssign);
TEST_CASE(simplifyKnownVariablesBailOutFor1); TEST_CASE(simplifyKnownVariablesBailOutFor1);
TEST_CASE(simplifyKnownVariablesBailOutFor2); TEST_CASE(simplifyKnownVariablesBailOutFor2);
@ -2040,6 +2041,21 @@ private:
ASSERT_EQUALS("void f ( ) {\nint x ; x = 0 ;\nconst int * p ; p = & x ;\nif ( & x ) { return 0 ; }\n}", tokenizeAndStringify(code, true)); ASSERT_EQUALS("void f ( ) {\nint x ; x = 0 ;\nconst int * p ; p = & x ;\nif ( & x ) { return 0 ; }\n}", tokenizeAndStringify(code, true));
} }
void simplifyKnownVariables42()
{
const char code[] = "void f() {\n"
" char str1[10], str2[10];\n"
" strcpy(str1, \"abc\");\n"
" strcpy(str2, str1);\n"
"}";
const char expected[] = "void f ( ) {\n"
"char str1 [ 10 ] ; char str2 [ 10 ] ;\n"
"strcpy ( str1 , \"abc\" ) ;\n"
"strcpy ( str2 , \"abc\" ) ;\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariablesBailOutAssign() void simplifyKnownVariablesBailOutAssign()
{ {
const char code[] = "int foo() {\n" const char code[] = "int foo() {\n"