Fixed #592 (Tokenizer: improve the tokenization of do .. while)

http://sourceforge.net/apps/trac/cppcheck/ticket/592
This commit is contained in:
Slava Semushin 2009-08-22 17:42:19 +07:00
parent 94c49bc34e
commit acc38a8bbf
3 changed files with 62 additions and 0 deletions

View File

@ -1561,6 +1561,7 @@ void Tokenizer::simplifyTokenList()
}
}
simplifyDoWhileAddBraces();
simplifyIfAddBraces();
simplifyFunctionParameters();
@ -1884,6 +1885,54 @@ bool Tokenizer::simplifyIfAddBraces()
return ret;
}
bool Tokenizer::simplifyDoWhileAddBraces()
{
bool ret = false;
for (Token *tok = _tokens; tok; tok = (tok ? tok->next() : NULL))
{
if (! Token::Match(tok, "do !!{"))
{
continue;
}
Token *tok1 = tok; // token with "do"
Token *tok2 = NULL; // token with "while"
Token *tok3 = tok;
// skip loop body
while (tok3)
{
if (tok3->str() == "while")
{
tok2 = tok3;
break;
}
tok3 = tok3->next();
}
if (tok2)
{
// insert "{" after "do"
tok1->insertToken("{");
// insert "}" before "while"
tok2->previous()->insertToken("}");
// allow link() works
tok1 = tok1->next();
tok2 = tok2->previous();
tok1->link(tok2);
tok2->link(tok1);
ret = true;
}
}
return ret;
}
bool Tokenizer::simplifyConditionOperator()
{
bool ret = false;

View File

@ -185,6 +185,12 @@ private:
*/
bool simplifyIfAddBraces();
/** Add braces to an do-while block
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyDoWhileAddBraces();
/** Simplify casts
* @return true if something is modified
* false if nothing is done.

View File

@ -62,6 +62,7 @@ private:
TEST_CASE(ifAddBraces8);
TEST_CASE(whileAddBraces);
TEST_CASE(doWhileAddBraces);
TEST_CASE(numeric_true_condition);
TEST_CASE(pointers_condition);
@ -521,7 +522,13 @@ private:
}
}
void doWhileAddBraces()
{
const char code[] = "do ; while (0);";
const char result[] = "do { ; } while ( false ) ;";
ASSERT_EQUALS(result, tokenizeAndStringify(code, true));
}
void simplifyKnownVariables1()
{