From 1b98be458dbecd50871aaa8e7af8d5947d137665 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 3 Jun 2023 11:08:06 +0200 Subject: [PATCH] Fix #11739 Assert failure in valueflow.cpp / setSymbolic() (#5104) --- lib/symboldatabase.cpp | 8 +++++++- test/testvarid.cpp | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index c131d4734..7991a0010 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1584,7 +1584,13 @@ void SymbolDatabase::createSymbolDatabaseExprIds() inConstExpr = tok->next()->link(); } } - for (const Scope * scope : functionScopes) { + + auto exprScopes = functionScopes; // functions + global lambdas + std::copy_if(scopeList.front().nestedList.begin(), scopeList.front().nestedList.end(), std::back_inserter(exprScopes), [](const Scope* scope) { + return scope && scope->type == Scope::eLambda; + }); + + for (const Scope * scope : exprScopes) { nonneg int thisId = 0; std::unordered_map> exprs; diff --git a/test/testvarid.cpp b/test/testvarid.cpp index a08cf582b..586386df3 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -232,6 +232,7 @@ private: TEST_CASE(decltype2); TEST_CASE(exprid1); + TEST_CASE(exprid2); TEST_CASE(structuredBindings); } @@ -3760,6 +3761,25 @@ private: ASSERT_EQUALS(expected, actual); } + void exprid2() { + const std::string actual = tokenizeExpr( // #11739 + "struct S { std::unique_ptr u; };\n" + "auto f = [](const S& s) -> std::unique_ptr {\n" + " if (auto p = s.u.get())\n" + " return std::make_unique(*p);\n" + " return nullptr;\n" + "};\n"); + + const char expected[] = "1: struct S { std :: unique_ptr < int > u ; } ;\n" + "2: auto f ; f = [ ] ( const S & s ) . std :: unique_ptr < int > {\n" + "3: if (@5 auto p@4 =@6 s@3 .@7 u@5 .@8 get (@9 ) ) {\n" + "4: return std ::@10 make_unique < int > (@11 *@12 p@4 ) ; }\n" + "5: return nullptr ;\n" + "6: } ;\n"; + + ASSERT_EQUALS(expected, actual); + } + void structuredBindings() { const char code[] = "int foo() { auto [x,y] = xy(); return x+y; }"; ASSERT_EQUALS("1: int foo ( ) { auto [ x@1 , y@2 ] = xy ( ) ; return x@1 + y@2 ; }\n",