From 197bcf17ba17e1b7f21053314702aa0208e06bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 28 Dec 2009 08:37:34 +0100 Subject: [PATCH] Tokenizer: Simplify 'while (0)' better --- lib/tokenize.cpp | 41 +++++++++++++++++++++++++++++++++++++ lib/tokenize.h | 5 +++++ test/testsimplifytokens.cpp | 10 ++++++++- test/testtokenize.cpp | 11 ++-------- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 71a83a3f5..f0d0e8833 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2214,6 +2214,7 @@ bool Tokenizer::simplifyTokenList() simplifyIfNotNull(); simplifyComparisonOrder(); simplifyNestedStrcat(); + simplifyWhile0(); for (Token *tok = _tokens; tok; tok = tok->next()) { @@ -4957,3 +4958,43 @@ void Tokenizer::getErrorMessages() { 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; + } + } + } +} diff --git a/lib/tokenize.h b/lib/tokenize.h index 03f438956..f39354a92 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -320,6 +320,11 @@ private: */ void simplifyConst(); + /** + * simplify "while (0)" + */ + void simplifyWhile0(); + /** * Remove exception specifications. This function calls itself recursively. * @param tok First token in scope to cleanup diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index be9d188f7..dc68a72df 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -149,6 +149,9 @@ private: TEST_CASE(pointeralias); TEST_CASE(reduceConstness); + + // simplify "while (0)" + TEST_CASE(while0); } std::string tok(const char code[], bool simplify = true) @@ -991,7 +994,7 @@ private: " sizeof 1;\n" " while (0);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { sizeof ( 1 ) ; while ( false ) { ; } }", tok(code)); + ASSERT_EQUALS("void f ( ) { sizeof ( 1 ) ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -2498,6 +2501,11 @@ private: { ASSERT_EQUALS("char * p ;", tok("char * const p;")); } + + void while0() + { + ASSERT_EQUALS("; x = 1 ; ;", tok("; do { x = 1 ; } while (0);")); + } }; REGISTER_TEST(TestSimplifyTokens) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index a571285cb..f01c83ca7 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -65,7 +65,6 @@ private: TEST_CASE(ifAddBraces5); TEST_CASE(ifAddBraces6); TEST_CASE(ifAddBraces7); - TEST_CASE(ifAddBraces8); TEST_CASE(ifAddBraces9); TEST_CASE(whileAddBraces); @@ -575,12 +574,6 @@ private: "}", tokenizeAndStringify(code, true)); } - void ifAddBraces8() - { - const char code[] = "do { ; } while(0);"; - ASSERT_EQUALS("do { ; } while ( false ) ;", tokenizeAndStringify(code, true)); - } - void ifAddBraces9() { // ticket #990 @@ -610,9 +603,9 @@ private: { { 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)); } {