diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index aa1c349b9..bfc150b3e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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; } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 63eaa8997..0d9061b51 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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; }")); }