From 4023a487ffed99743b24303c14a30c5726250e15 Mon Sep 17 00:00:00 2001 From: Ken-Patrick Lehrmann Date: Sun, 14 Jun 2020 14:46:16 +0200 Subject: [PATCH] 9768: Fix ast with throw in the middle of return ``` int f(bool x) { return x ? 0 : throw 0; } ``` The `throw` part was not included in the ast, leading to an invalid ternary operator. --- lib/tokenlist.cpp | 4 +++- test/testtokenize.cpp | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index b54006143..a8868cd5c 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -762,8 +762,10 @@ 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"; compileUnaryOp(tok, state, compileExpression); - state.op.pop(); + if (tokIsReturn) + state.op.pop(); if (state.inCase && Token::simpleMatch(tok, ": ;")) { state.inCase = false; tok = tok->next(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d01a6e16e..4cb4e1e15 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7484,6 +7484,8 @@ 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("a\"\"=", testAst("a=\"\"")); ASSERT_EQUALS("a\'\'=", testAst("a=\'\'")); @@ -7927,6 +7929,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() {