From 882b0c15a0e697e8c533ec81d83c486eee430cf0 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Wed, 19 Oct 2011 18:20:29 +0200 Subject: [PATCH] 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. --- lib/tokenize.cpp | 17 +++++++++++------ test/testsimplifytokens.cpp | 6 ++++-- 2 files changed, 15 insertions(+), 8 deletions(-) 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; }")); }