From 9257e8247579859e5b88fe1a37c7ec0cf44d4e22 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Wed, 19 Oct 2011 18:49:02 +0200 Subject: [PATCH] Improve tokenizer: don't confuse between a label and the case/default statements. --- lib/tokenize.cpp | 7 ++++--- test/testsimplifytokens.cpp | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index bfc150b3e..4e5f82282 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7409,7 +7409,7 @@ void Tokenizer::simplifyGoto() else if (Token::Match(tok, "goto %var% ;")) gotos.push_back(tok); - else if (indentlevel == 1 && Token::Match(tok->previous(), "[};] %var% : ;")) { + else if (indentlevel == 1 && Token::Match(tok->previous(), "[{};] %var% : ;") && tok->str() != "default") { // Is this label at the end.. bool end = false; unsigned int level = 0; @@ -7435,7 +7435,7 @@ void Tokenizer::simplifyGoto() ++level; } - if (Token::Match(tok2, "%var% : ;") || tok2->str() == "goto") { + if ((Token::Match(tok2->previous(), "[{};] %var% : ;") && tok2->str() != "default") || tok2->str() == "goto") { break; } } @@ -8688,7 +8688,8 @@ void Tokenizer::simplifyWhile0() // remove "while (0) { .. }" if (Token::simpleMatch(tok->next()->link(), ") {")) { const Token *end = tok->next()->link()->next()->link(); - if (!Token::findmatch(tok, "%var% : ;", end)) { + const Token *labelmatch = Token::findmatch(tok, "[{};] %var% : ;", end); + if (!labelmatch || labelmatch->next()->str() == "default") { Token::eraseTokens(tok, end ? end->next() : 0); tok->deleteThis(); // delete "while" } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 0d9061b51..df94353a2 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -6455,6 +6455,7 @@ private: ASSERT_EQUALS("; { continue ; }", tok("; do { continue ; } while (0);")); ASSERT_EQUALS("; { break ; }", tok("; do { break; } while (0);")); ASSERT_EQUALS(";", tok("; while (false) { a; }")); + ASSERT_EQUALS(";", tok("; while (false) { switch (n) { case 0: return; default: break; } n*=1; }")); } void while0for() {