Fixed #1633 (tokenizer: simplifyKnownVariable: improved handling of arithmetic)

This commit is contained in:
Daniel Marjamäki 2010-06-19 14:00:45 +02:00
parent a402b6bb98
commit 82b63dd736
4 changed files with 24 additions and 4 deletions

View File

@ -2234,7 +2234,7 @@ void CheckOther::nullConstantDereference()
else if (Token::simpleMatch(tok, "* 0")) else if (Token::simpleMatch(tok, "* 0"))
{ {
if (Token::Match(tok->previous(), "[;{}=+-/(,]") || if (Token::Match(tok->previous(), "[<>;{}=+-*/(,]") ||
Token::Match(tok->previous(), "return|<<")) Token::Match(tok->previous(), "return|<<"))
{ {
nullPointerError(tok); nullPointerError(tok);

View File

@ -5521,10 +5521,11 @@ bool Tokenizer::simplifyKnownVariables()
} }
// Variable is used in calculation.. // Variable is used in calculation..
if (Token::Match(tok3, "[=+-*/[] %varid% [=?+-*/;]]", varid) || if (Token::Match(tok3, "[=+-*/[] %varid% [=?+-*/;])]", varid) ||
Token::Match(tok3, "[(=+-*/[] %varid% <<|>>", varid) || Token::Match(tok3, "[(=+-*/[] %varid% <<|>>", varid) ||
Token::Match(tok3, "<< %varid% [+-*/;])]", varid) || Token::Match(tok3, "<< %varid% [+-*/;])]", varid) ||
Token::Match(tok3, ">> %varid% [+-*/])]", varid)) Token::Match(tok3, ">> %varid% [+-*/])]", varid) ||
Token::Match(tok3->previous(), "[=+-*/[] ( %varid%", varid))
{ {
tok3 = tok3->next(); tok3 = tok3->next();
tok3->str(value); tok3->str(value);

View File

@ -1050,7 +1050,7 @@ private:
" int *p = 0;\n" " int *p = 0;\n"
" if (3 > *p);\n" " if (3 > *p);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str());
checkNullPointer("void f()\n" checkNullPointer("void f()\n"
"{\n" "{\n"

View File

@ -109,6 +109,7 @@ private:
TEST_CASE(simplifyKnownVariables24); TEST_CASE(simplifyKnownVariables24);
TEST_CASE(simplifyKnownVariables25); TEST_CASE(simplifyKnownVariables25);
TEST_CASE(simplifyKnownVariables26); TEST_CASE(simplifyKnownVariables26);
TEST_CASE(simplifyKnownVariables27);
TEST_CASE(match1); TEST_CASE(match1);
@ -1316,6 +1317,24 @@ private:
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
void simplifyKnownVariables27()
{
// This testcase is related to ticket #1633
const char code[] = "void foo()\n"
"{\n"
" int i1 = 1;\n"
" int i2 = 2;\n"
" int i3 = (i1 + i2) * 3;\n"
"}\n";
ASSERT_EQUALS(
"void foo ( ) "
"{"
" int i1 ; i1 = 1 ;"
" int i2 ; i2 = 2 ;"
" int i3 ; i3 = ( 1 + 2 ) * 3 ; "
"}",
simplifyKnownVariables(code));
}
void match1() void match1()
{ {