Fix #289 (if() is not properly tokenized)

http://apps.sourceforge.net/trac/cppcheck/ticket/289
This commit is contained in:
Reijo Tomperi 2009-05-09 23:12:14 +03:00
parent e7cff4bc43
commit f2a5527e60
3 changed files with 23 additions and 1 deletions

View File

@ -1619,6 +1619,11 @@ bool Tokenizer::simplifyIfAddBraces()
continue;
}
// If there is no code after he if(), abort
if (!tok->next())
return false;
// insert open brace..
tok->insertToken("{");
tok = tok->next();

View File

@ -365,7 +365,7 @@ private:
{
// remove parantheses..
ASSERT_EQUALS("= p ; ", tok("= (p);"));
ASSERT_EQUALS("if ( a < p ) { ", tok("if(a<(p))"));
ASSERT_EQUALS("if ( a < p ) { } ", tok("if(a<(p)){}"));
// keep parantheses..
ASSERT_EQUALS("= a ; ", tok("= (char)a;"));

View File

@ -78,6 +78,7 @@ private:
TEST_CASE(ifAddBraces3);
TEST_CASE(ifAddBraces4);
TEST_CASE(ifAddBraces5);
TEST_CASE(ifAddBraces6);
TEST_CASE(numeric_true_condition);
@ -436,6 +437,22 @@ private:
ASSERT_EQUALS(std::string(" void f ( ) { for ( int i = 0 ; i < 2 ; i ++ ) { if ( true ) { return ; } } return ; }"), ostr.str());
}
void ifAddBraces6()
{
const char code[] = "if()";
// tokenize..
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS(false, tokenizer.simplifyIfAddBraces());
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" if ( )"), ostr.str());
}
void simplifyKnownVariables1()
{