Improve tokenizing of "do{ ... } while(0)".

The simplification will be the same for every combination, except for the 'continue' and the 'break'. In that case, keep the block braces.
This commit is contained in:
Edoardo Prezioso 2011-10-19 18:20:29 +02:00
parent c9ee19c654
commit 882b0c15a0
2 changed files with 15 additions and 8 deletions

View File

@ -8662,19 +8662,24 @@ void Tokenizer::simplifyWhile0()
if (while0 && Token::simpleMatch(tok->previous(), "}")) {
// find "do"
Token *tok2 = tok->previous()->link();
tok2 = tok2 ? tok2->previous() : 0;
if (tok2 && tok2->str() == "do" && !Token::findmatch(tok2, "continue|break", tok)) {
// delete "do {"
tok2->deleteThis();
tok2 = tok2->previous();
if (tok2 && tok2->str() == "do") {
bool flowmatch = Token::findmatch(tok2, "continue|break", tok) != NULL;
// delete "do ({)"
tok2->deleteThis();
if (!flowmatch)
tok2->deleteThis();
// delete "} while ( 0 )"
// delete "(}) while ( 0 ) (;)"
tok = tok->previous();
tok->deleteNext(); // while
tok->deleteNext(); // (
tok->deleteNext(); // 0
tok->deleteNext(); // )
tok->deleteThis(); // }
if (tok->next() && tok->next()->str() == ";")
tok->deleteNext(); // ;
if (!flowmatch)
tok->deleteThis(); // }
continue;
}

View File

@ -6450,8 +6450,10 @@ private:
void while0() {
ASSERT_EQUALS("; x = 1 ;", tok("; do { x = 1 ; } while (0);"));
ASSERT_EQUALS("; do { continue ; } while ( false ) ;", tok("; do { continue ; } while (0);"));
ASSERT_EQUALS("; do { break ; } while ( false ) ;", tok("; do { break; } while (0);"));
ASSERT_EQUALS("; return 0 ;", tok("; do { return 0; } while (0);"));
ASSERT_EQUALS("void foo ( ) { goto label ; }", tok("void foo() { do { goto label; } while (0); }"));
ASSERT_EQUALS("; { continue ; }", tok("; do { continue ; } while (0);"));
ASSERT_EQUALS("; { break ; }", tok("; do { break; } while (0);"));
ASSERT_EQUALS(";", tok("; while (false) { a; }"));
}