Fix ticket #83 (cppcheck hangs) and add a test case for it

This commit is contained in:
Reijo Tomperi 2009-02-07 19:15:10 +00:00
parent e71c6aaa9f
commit 798d86216a
3 changed files with 30 additions and 2 deletions

View File

@ -1261,7 +1261,7 @@ bool Tokenizer::simplifyConditions()
Token::Match(tok->next(), "%num%") && Token::Match(tok->next(), "%num%") &&
(tok2->str() == ")" || tok2->str() == "&&" || tok2->str() == "||")) (tok2->str() == ")" || tok2->str() == "&&" || tok2->str() == "||"))
{ {
tok->next()->str(); tok->next()->str((tok->next()->str() != "0") ? "true" : "false");
ret = true; ret = true;
} }

View File

@ -84,6 +84,7 @@ private:
TEST_CASE(sizeof2); TEST_CASE(sizeof2);
TEST_CASE(sizeof3); TEST_CASE(sizeof3);
TEST_CASE(sizeof4); TEST_CASE(sizeof4);
TEST_CASE(simplify_numeric_condition);
} }
@ -939,6 +940,33 @@ private:
ostr << " " << tok->str(); ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" for ( int i = 0 ; i < 100 ; i + + ) { }"), ostr.str()); ASSERT_EQUALS(std::string(" for ( int i = 0 ; i < 100 ; i + + ) { }"), ostr.str());
} }
void simplify_numeric_condition()
{
const char code[] =
"void f()\n"
"{\n"
"int x = 0;\n"
"if( !x || 0 )\n"
"{\n"
"}\n"
"}";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void f ( ) { int x ; x = 0 ; if ( ! x ) { } }"), ostr.str());
}
}; };
REGISTER_TEST(TestTokenizer) REGISTER_TEST(TestTokenizer)