diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e8b6020bc..046651cff 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2353,6 +2353,29 @@ void Tokenizer::arraySize() } } +static Token *skipTernaryOp(Token *); + +static Token *skipTernaryOp(Token *tok) +{ + if (!tok || tok->str() != "?") + return tok; + unsigned int colonlevel = 1; + while (NULL != (tok = tok->next())) { + if (tok->str() == "?") { + ++colonlevel; + } else if (tok->str() == ":") { + --colonlevel; + if (colonlevel == 0) { + tok = tok->next(); + break; + } + } + if (Token::Match(tok->next(), "[{};]")) + break; + } + return tok; +} + /** simplify labels and case|default in the code: add a ";" if not already in.*/ bool Tokenizer::simplifyLabelsCaseDefault() @@ -2389,6 +2412,8 @@ bool Tokenizer::simplifyLabelsCaseDefault() while (NULL != (tok = tok->next())) { if (tok->str() == "(" || tok->str() == "[") { tok = tok->link(); + } else if (tok->str() == "?") { + tok = skipTernaryOp(tok); } if (Token::Match(tok->next(),"[:{};]")) break; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 937727eaf..6c7a5b344 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -732,6 +732,12 @@ private: tokenizeAndStringify("void f() {switch (n) { case 0:; break;}}"); ASSERT_EQUALS("", errout.str()); + tokenizeAndStringify("void f() {switch (n) { case 0?1:2 : z(); break;}}"); + ASSERT_EQUALS("", errout.str()); + + tokenizeAndStringify("void f() {switch (n) { case 0?(1?3:4):2 : z(); break;}}"); + ASSERT_EQUALS("", errout.str()); + //'b' can be or a macro or an undefined enum tokenizeAndStringify("void f() {switch (n) { case b: z(); break;}}"); ASSERT_EQUALS("", errout.str()); @@ -764,6 +770,15 @@ private: tokenizeAndStringify("void f() {switch (n) { case {}: z(); break;}}"); ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); + tokenizeAndStringify("void f() {switch (n) { case 0?{1}:{2} : z(); break;}}"); + ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); + + tokenizeAndStringify("void f() {switch (n) { case 0?1;:{2} : z(); break;}}"); + ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); + + tokenizeAndStringify("void f() {switch (n) { case 0?(1?{3:4}):2 : z(); break;}}"); + ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); + //ticket #4234 tokenizeAndStringify("( ) { switch break ; { switch ( x ) { case } y break ; : } }"); ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str());