Tokenizer: Simplify 'while (0)' better

This commit is contained in:
Daniel Marjamäki 2009-12-28 08:37:34 +01:00
parent b1e963fe2c
commit 197bcf17ba
4 changed files with 57 additions and 10 deletions

View File

@ -2214,6 +2214,7 @@ bool Tokenizer::simplifyTokenList()
simplifyIfNotNull(); simplifyIfNotNull();
simplifyComparisonOrder(); simplifyComparisonOrder();
simplifyNestedStrcat(); simplifyNestedStrcat();
simplifyWhile0();
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
@ -4957,3 +4958,43 @@ void Tokenizer::getErrorMessages()
{ {
syntaxError(0, ' '); syntaxError(0, ' ');
} }
void Tokenizer::simplifyWhile0()
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (!Token::simpleMatch(tok, "while ( 0 )"))
continue;
// remove "while (0) { .. }"
if (Token::simpleMatch(tok->tokAt(4), "{"))
{
Token::eraseTokens(tok, tok->tokAt(4)->link());
tok->deleteThis(); // delete "while"
tok->deleteThis(); // delete "}"
}
if (Token::simpleMatch(tok->previous(), "}"))
{
// find "do"
Token *tok2 = tok->previous()->link();
tok2 = tok2 ? tok2->previous() : 0;
if (tok2 && tok2->str() == "do")
{
// delete "do {"
tok2->deleteThis();
tok2->deleteThis();
// delete "} while ( 0 )"
tok = tok->previous();
tok->deleteNext(); // while
tok->deleteNext(); // (
tok->deleteNext(); // 0
tok->deleteNext(); // )
tok->deleteThis(); // }
continue;
}
}
}
}

View File

@ -320,6 +320,11 @@ private:
*/ */
void simplifyConst(); void simplifyConst();
/**
* simplify "while (0)"
*/
void simplifyWhile0();
/** /**
* Remove exception specifications. This function calls itself recursively. * Remove exception specifications. This function calls itself recursively.
* @param tok First token in scope to cleanup * @param tok First token in scope to cleanup

View File

@ -149,6 +149,9 @@ private:
TEST_CASE(pointeralias); TEST_CASE(pointeralias);
TEST_CASE(reduceConstness); TEST_CASE(reduceConstness);
// simplify "while (0)"
TEST_CASE(while0);
} }
std::string tok(const char code[], bool simplify = true) std::string tok(const char code[], bool simplify = true)
@ -991,7 +994,7 @@ private:
" sizeof 1;\n" " sizeof 1;\n"
" while (0);\n" " while (0);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { sizeof ( 1 ) ; while ( false ) { ; } }", tok(code)); ASSERT_EQUALS("void f ( ) { sizeof ( 1 ) ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -2498,6 +2501,11 @@ private:
{ {
ASSERT_EQUALS("char * p ;", tok("char * const p;")); ASSERT_EQUALS("char * p ;", tok("char * const p;"));
} }
void while0()
{
ASSERT_EQUALS("; x = 1 ; ;", tok("; do { x = 1 ; } while (0);"));
}
}; };
REGISTER_TEST(TestSimplifyTokens) REGISTER_TEST(TestSimplifyTokens)

View File

@ -65,7 +65,6 @@ private:
TEST_CASE(ifAddBraces5); TEST_CASE(ifAddBraces5);
TEST_CASE(ifAddBraces6); TEST_CASE(ifAddBraces6);
TEST_CASE(ifAddBraces7); TEST_CASE(ifAddBraces7);
TEST_CASE(ifAddBraces8);
TEST_CASE(ifAddBraces9); TEST_CASE(ifAddBraces9);
TEST_CASE(whileAddBraces); TEST_CASE(whileAddBraces);
@ -575,12 +574,6 @@ private:
"}", tokenizeAndStringify(code, true)); "}", tokenizeAndStringify(code, true));
} }
void ifAddBraces8()
{
const char code[] = "do { ; } while(0);";
ASSERT_EQUALS("do { ; } while ( false ) ;", tokenizeAndStringify(code, true));
}
void ifAddBraces9() void ifAddBraces9()
{ {
// ticket #990 // ticket #990
@ -610,9 +603,9 @@ private:
{ {
{ {
const char code[] = "do ; while (0);"; const char code[] = "do ; while (0);";
const char result[] = "do { ; } while ( false ) ;"; const char result[] = "do { ; } while ( 0 ) ;";
ASSERT_EQUALS(result, tokenizeAndStringify(code, true)); ASSERT_EQUALS(result, tokenizeAndStringify(code, false));
} }
{ {