From 6bde2445a64c4413caf2969183320f3c34e731b3 Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Sun, 23 Sep 2018 10:27:38 +0200 Subject: [PATCH] Ticket #8632: Parenthesize ternary operator operands containing < to avoid wrongly thinking a template instantiation is met. (#1389) --- lib/tokenize.cpp | 2 ++ test/testsimplifytemplate.cpp | 2 +- test/testtokenize.cpp | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0bc8624a6..79ca6d3f3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -10162,6 +10162,8 @@ void Tokenizer::prepareTernaryOpForAST() break; else if (tok2->str() == ",") parenthesesNeeded = true; + else if (tok2->str() == "<") + parenthesesNeeded = true; else if (tok2->str() == "?") { depth++; parenthesesNeeded = true; diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index db3a6d1ac..d6a40cfa1 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -1138,7 +1138,7 @@ private: "template class C3 {};\n" "template C3::C3(const C3 &v) { C1 c1; }\n" "C3 c3;"; - const char exp[] = "template < class T > void f ( ) { x = y ? C1 < int > :: allocate ( 1 ) : 0 ; } " + const char exp[] = "template < class T > void f ( ) { x = y ? ( C1 < int > :: allocate ( 1 ) ) : 0 ; } " "C3 c3 ; " "class C3 { } ; " "C3 :: C3 ( const C3 & v ) { C1 c1 ; } " diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ebce0c6c8..794cd4b50 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -8110,6 +8110,8 @@ private: ASSERT_EQUALS("a ? ( 1 ? ( a , b ) : 3 ) : d ;", tokenizeAndStringify("a ? 1 ? a, b : 3 : d;")); ASSERT_EQUALS("a ? ( std :: map < int , int > ( ) ) : 0 ;", tokenizeAndStringify("typedef std::map mymap; a ? mymap() : 0;")); + + ASSERT_EQUALS("a ? ( b < c ) : d > e", tokenizeAndStringify("a ? b < c : d > e")); } std::string testAst(const char code[],bool verbose=false) {