diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index b54006143..00a8fdd72 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -465,8 +465,9 @@ struct AST_state { bool cpp; int assign; bool inCase; // true from case to : + bool stopAtColon; // help to properly parse ternary operators const Token *functionCallEndPar; - explicit AST_state(bool cpp) : depth(0), inArrayAssignment(0), cpp(cpp), assign(0), inCase(false), functionCallEndPar(nullptr) {} + explicit AST_state(bool cpp) : depth(0), inArrayAssignment(0), cpp(cpp), assign(0), inCase(false),stopAtColon(false), functionCallEndPar(nullptr) {} }; static Token * skipDecl(Token *tok) @@ -762,8 +763,13 @@ static void compileTerm(Token *&tok, AST_state& state) if (Token::Match(tok, "return|case") || (state.cpp && tok->str() == "throw")) { if (tok->str() == "case") state.inCase = true; + const bool tokIsReturn = tok->str() == "return"; + const bool stopAtColon = state.stopAtColon; + state.stopAtColon=true; compileUnaryOp(tok, state, compileExpression); - state.op.pop(); + state.stopAtColon=stopAtColon; + if (tokIsReturn) + state.op.pop(); if (state.inCase && Token::simpleMatch(tok, ": ;")) { state.inCase = false; tok = tok->next(); @@ -1209,6 +1215,8 @@ static void compileAssignTernary(Token *&tok, AST_state& state) // http://en.cppreference.com/w/cpp/language/operator_precedence says about ternary operator: // "The expression in the middle of the conditional operator (between ? and :) is parsed as if parenthesized: its precedence relative to ?: is ignored." // Hence, we rely on Tokenizer::prepareTernaryOpForAST() to add such parentheses where necessary. + const bool stopAtColon = state.stopAtColon; + state.stopAtColon = false; if (tok->strAt(1) == ":") { state.op.push(nullptr); } @@ -1216,12 +1224,15 @@ static void compileAssignTernary(Token *&tok, AST_state& state) state.assign = 0; compileBinOp(tok, state, compileAssignTernary); state.assign = assign; + state.stopAtColon = stopAtColon; } else if (tok->str() == ":") { if (state.depth == 1U && state.inCase) { state.inCase = false; tok = tok->next(); break; } + if (state.stopAtColon) + break; if (state.assign > 0) break; compileBinOp(tok, state, compileAssignTernary); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d01a6e16e..e2e2c8197 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7484,6 +7484,10 @@ private: ASSERT_EQUALS("fabc,de,:?=", testAst("f = (a ? b, c : (d, e));")); ASSERT_EQUALS("ab35,4:?foo(:?return", testAst("return (a ? b ? (3,5) : 4 : foo());")); ASSERT_EQUALS("check(result_type00,{invalid:?return", testAst("return check() ? result_type {0, 0} : invalid;")); + ASSERT_EQUALS("x01:?return", testAst("return x ? 0 : 1;")); + ASSERT_EQUALS("x00throw:?return", testAst("return x ? 0 : throw 0;")); // #9768 + ASSERT_EQUALS("val0<1throwval:?return", testAst("return val < 0 ? throw 1 : val;")); // #8526 + ASSERT_EQUALS("ix0<00throw:?=", testAst("int i = x < 0 ? 0 : throw 0;")); ASSERT_EQUALS("a\"\"=", testAst("a=\"\"")); ASSERT_EQUALS("a\'\'=", testAst("a=\'\'")); @@ -7927,6 +7931,7 @@ private: ASSERT_EQUALS("0case", testAst("case 0:")); ASSERT_EQUALS("12+case", testAst("case 1+2:")); ASSERT_EQUALS("xyz:?case", testAst("case (x?y:z):")); + ASSERT_EQUALS("switchx( 1case y++ 2case", testAst("switch(x){case 1:{++y;break;case 2:break;}}")); } void astrefqualifier() {