Fixed #2713 (False positive (Redundant assignment))

This commit is contained in:
Daniel Marjamäki 2011-04-16 12:07:56 +02:00
parent cc41da1dc4
commit 739b6a93e2
4 changed files with 45 additions and 5 deletions

View File

@ -6213,6 +6213,22 @@ void Tokenizer::simplifyIfNotNull()
{
Token *deleteFrom = NULL;
// Remove 'x = (x != 0)'
if (Token::simpleMatch(tok, "= ("))
{
if (Token::Match(tok->tokAt(-2), "[;{}] %var%"))
{
const unsigned int varid = tok->previous()->varId();
if (Token::Match(tok, "= ( %varid% != 0 ) ;", varid) ||
Token::Match(tok, "= ( 0 != %varid% ) ;", varid))
{
tok = tok->tokAt(-2);
Token::eraseTokens(tok, tok->tokAt(9));
}
}
continue;
}
if (Token::Match(tok, "(|&&|%oror%"))
{
tok = tok->next();

View File

@ -253,7 +253,8 @@ public:
/**
* simplify if-not NULL
* - "if(0!=x);" => "if(x);"
* Example: "if(0!=x);" => "if(x);"
* Special case: 'x = (0 != x);' is removed.
*/
void simplifyIfNotNull();

View File

@ -1582,6 +1582,18 @@ private:
" Fred fred; fred = fred;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" x = (x == 0);"
" func(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" x = (x != 0);"
" func(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void testScanf1()

View File

@ -6727,11 +6727,22 @@ private:
ASSERT_EQUALS("", errout.str());
}
void simplifyIfNotNull() // ticket # 2601 segmentation fault
void simplifyIfNotNull()
{
const char code[] = "|| #if #define <=";
tok(code, false);
ASSERT_EQUALS("", errout.str());
{
// ticket # 2601 segmentation fault
const char code[] = "|| #if #define <=";
tok(code, false);
ASSERT_EQUALS("", errout.str());
}
{
const char code[] = "void f(int x) {\n"
" x = (x != 0);\n"
"}";
ASSERT_EQUALS("void f ( int x ) { }", tok(code, false));
}
}
};