diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7a7122020..632a268f3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1968,9 +1968,6 @@ bool Tokenizer::tokenize(std::istream &code, // specify array size.. arraySize(); - // simplify labels.. - labels(); - simplifyDoWhileAddBraces(); if (!simplifyIfAddBraces()) @@ -2031,6 +2028,9 @@ bool Tokenizer::tokenize(std::istream &code, } } + // simplify labels.. + labels(); + // ";a+=b;" => ";a=a+b;" simplifyCompoundAssignment(); @@ -2456,9 +2456,9 @@ void Tokenizer::labels() if (tok->str() == "{") ++indentlevel; else if (tok->str() == "}") { - if (indentlevel <= 1) - break; --indentlevel; + if (!indentlevel) + break; } if (tok->str() == "(") @@ -2468,6 +2468,17 @@ void Tokenizer::labels() break; --indentroundbraces; } + if (!indentroundbraces && tok->str() == "case") + { + while (0 != (tok = tok->next())) { + if (Token::Match(tok->previous(), "%any% :")) + break; + } + if (!(tok->next()) || tok->next()->str() != ";"){ + tok->insertToken(";"); + tok = tok->next(); + } + } // simplify label.. except for unhandled macro if (!indentroundbraces && Token::Match(tok, "[;{}] %var% :") && !Token::Match(tok->next(), "public|protected|private") @@ -4143,13 +4154,6 @@ bool Tokenizer::simplifyTokenList() simplifyIfAssign(); // could be affected by simplifyIfNot - for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok, "case %any% : %var%")) - tok->tokAt(2)->insertToken(";"); - if (Token::Match(tok, "default : %var%")) - tok->next()->insertToken(";"); - } - // In case variable declarations have been updated... setVarId(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 889321ecc..2508ace4e 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4666,8 +4666,11 @@ private: } void switchCase() { - ASSERT_EQUALS("void foo ( int i ) { switch ( i ) { case -1 : break ; } }", + ASSERT_EQUALS("void foo ( int i ) { switch ( i ) { case -1 : ; break ; } }", tokenizeAndStringify("void foo (int i) { switch(i) { case -1: break; } }")); + //ticket #3227 + ASSERT_EQUALS("void foo ( ) { switch ( n ) { label : ; case 1 : ; label1 : ; label2 : ; break ; } }", + tokenizeAndStringify("void foo(){ switch (n){ label: case 1: label1: label2: break; }}")); } void simplifyPointerToStandardType() {