diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ad6e14486..df40355f0 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2694,36 +2694,48 @@ void Tokenizer::arraySize() void Tokenizer::simplifyLabelsCaseDefault() { + bool executablescope = false; + unsigned int indentlevel = 0; for (Token *tok = _tokens; tok; tok = tok->next()) { + // Simplify labels in the executable scope.. if (Token::Match(tok, ") const| {")) { - // Simplify labels in the executable scope.. - unsigned int indentlevel = 0; - while (NULL != (tok = tok->next())) { - if (tok->str() == "{") { - if (tok->previous() && tok->previous()->str() == "=") - tok = tok->link(); - else - ++indentlevel; - } else if (tok->str() == "}") { - --indentlevel; - if (!indentlevel) - break; - } else if (tok->str() == "(" || tok->str() == "[") - tok = tok->link(); + tok = tok->next(); + if (tok->str() == "const") + tok = tok->next(); + executablescope = true; + } - if (Token::Match(tok, "[;{}] case")) { - while (NULL != (tok = tok->next())) { - if (tok->str() == ":") - break; - } - if (Token::Match(tok, ": !!;")) { - tok->insertToken(";"); - } - } else if (Token::Match(tok, "[;{}] %var% : !!;")) { - tok = tok->tokAt(2); - tok->insertToken(";"); - } + if (!executablescope) + continue; + + if (tok->str() == "{") { + if (tok->previous()->str() == "=") + tok = tok->link(); + else + ++indentlevel; + } else if (tok->str() == "}") { + --indentlevel; + if (!indentlevel) { + executablescope = false; + continue; } + } else if (tok->str() == "(" || tok->str() == "[") + tok = tok->link(); + + if (Token::Match(tok, "[;{}] case")) { + while (NULL != (tok = tok->next())) { + if (tok->str() == ":") + break; + } + if (!tok) + break; + else if (tok->str() == ":" && + (!tok->next() || tok->next()->str() != ";")) { + tok->insertToken(";"); + } + } else if (Token::Match(tok, "[;{}] %var% : !!;")) { + tok = tok->tokAt(2); + tok->insertToken(";"); } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 418169821..5abf37c69 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5239,6 +5239,8 @@ private: //with unhandled MACRO() code ASSERT_EQUALS(" void f(){ MACRO( ab: b=0;, foo)}", labels_("void f() { MACRO(ab: b=0;, foo)}")); ASSERT_EQUALS(" void f(){ MACRO( bar, ab:{&(* b. x)=0;})}", labels_("void f() { MACRO(bar, ab: {&(*b.x)=0;})}")); + //don't crash with garbage code + ASSERT_EQUALS(" switch(){ case}", labels_("switch(){case}")); } // Check simplifyInitVar