Fixed #2618 (Tokenizer::simplifyKnownVariables: Don't use known string value if address is wanted)

This commit is contained in:
Daniel Marjamäki 2011-03-01 20:20:48 +01:00
parent db04d7b71e
commit 8e9a1c33ad
2 changed files with 32 additions and 10 deletions

View File

@ -6636,6 +6636,12 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
Token::Match(tok3, ("!|==|!=|<|<=|>|>= " + structname + " %varid% ==|!=|<|<=|>|>=|)|;").c_str(), varid) ||
Token::Match(tok3->previous(), "strlen|free ( %varid% )", varid))
{
if (value[0] == '\"' && tok3->strAt(-1) != "strlen")
{
// bail out if value is a string unless if it's just given
// as parameter to strlen
break;
}
if (!structname.empty())
{
tok3->deleteNext();

View File

@ -2052,6 +2052,7 @@ private:
}
void simplifyKnownVariables42()
{
{
const char code[] = "void f() {\n"
" char str1[10], str2[10];\n"
@ -2066,6 +2067,21 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
{
const char code[] = "void f() {"
" char *s = malloc(10);"
" strcpy(s, \"\");"
" free(s);"
"}";
const char expected[] = "void f ( ) {"
" char * s ; s = malloc ( 10 ) ;"
" strcpy ( s , \"\" ) ;"
" free ( s ) ; "
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
}
void simplifyKnownVariablesBailOutAssign()
{
const char code[] = "int foo() {\n"