diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4f60be6ca..3dc5b8335 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4823,8 +4823,12 @@ bool Tokenizer::simplifyConstTernaryOp() const int offset = (tok->previous()->str() == ")") ? 2 : 1; - if (tok->strAt(-2*offset) == "<" && !TemplateSimplifier::templateParameters(tok->tokAt(-2*offset))) - continue; + bool inTemplateParameter = false; + if (tok->strAt(-2*offset) == "<") { + if (!TemplateSimplifier::templateParameters(tok->tokAt(-2*offset))) + continue; + inTemplateParameter = true; + } // Find the token ":" then go to the next token Token *semicolon = skipTernaryOp(tok); @@ -4869,6 +4873,8 @@ bool Tokenizer::simplifyConstTernaryOp() else if (Token::Match(endTok, ")|}|]|;|,|:|>")) { if (endTok->str() == ":" && ternaryOplevel) --ternaryOplevel; + else if (endTok->str() == ">" && !inTemplateParameter) + ; else { Token::eraseTokens(semicolon->tokAt(-2), endTok); ret = true; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 1936428b9..56ab03f50 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -3599,17 +3599,24 @@ private: } void simplify_constants6() { // Ticket #5625 - const char code[] = "template < class T > struct foo ;\n" - "void bar ( ) {\n" - "foo < 1 ? 0 ? 1 : 6 : 2 > x ;\n" - "foo < 1 ? 0 : 2 > y ;\n" - "}"; - const char exp [] = "template < class T > struct foo ;\n" - "void bar ( ) {\n" - "foo < 6 > x ;\n" - "foo < 0 > y ;\n" - "}"; - ASSERT_EQUALS(exp, tokenizeAndStringify(code, true)); + { + const char code[] = "template < class T > struct foo ;\n" + "void bar ( ) {\n" + "foo < 1 ? 0 ? 1 : 6 : 2 > x ;\n" + "foo < 1 ? 0 : 2 > y ;\n" + "}"; + const char exp [] = "template < class T > struct foo ;\n" + "void bar ( ) {\n" + "foo < 6 > x ;\n" + "foo < 0 > y ;\n" + "}"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code, true)); + } + { + const char code[] = "bool b = true ? false : 1 > 2 ;"; + const char exp [] = "bool b ; b = false ;"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code, true)); + } } void simplify_null() {