From 58e3f19ed82ae658170c83661bf8b650ebe26245 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 3 Apr 2020 03:04:10 -0500 Subject: [PATCH] Fix issue 9662: AST broken, ternary operator missing operand(s) on valid C++ code (#2589) * Fix issue 9662: AST broken, ternary operator missing operand(s) on valid C++ code * Add test for issue 9537 --- lib/tokenlist.cpp | 8 +++++++- test/testtokenize.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 3340b7df2..6438729c5 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1520,9 +1520,15 @@ static Token * createAstAtToken(Token *tok, bool cpp) } if (cpp && tok->str() == "{" && iscpp11init(tok)) { + Token * const tok1 = tok; AST_state state(cpp); compileExpression(tok, state); - return tok; + const Token * const endToken = tok; + if (endToken == tok1 || !endToken) + return tok1; + + createAstAtTokenInner(tok1->next(), endToken, cpp); + return endToken->previous(); } return tok; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 0993e40dd..3abe63260 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7887,6 +7887,9 @@ private: testAst("a = [&]() -> std::pair { return 0; };\n" "b = [=]() { for (i = 0; i != 10; ++i); };")); + // #9662 + ASSERT_EQUALS("b{[{ stdunique_ptr::0nullptrnullptr:?{", testAst("auto b{[] { std::unique_ptr{0 ? nullptr : nullptr}; }};")); + } void astcase() { @@ -8237,6 +8240,14 @@ private: "};\n" "void e(\n" " int, a f = [] {});\n")) + + // #9537 + ASSERT_NO_THROW(tokenizeAndStringify("struct a {\n" + " template a(b) {}\n" + "};\n" + "a c{[] {\n" + " if (0) {}\n" + "}};\n")) } void checkIfCppCast() { ASSERT_NO_THROW(tokenizeAndStringify("struct a {\n"