Fixed #9000 (SymbolDatabase: lambda scope)

This commit is contained in:
Daniel Marjamäki 2019-03-30 10:32:36 +01:00
parent 74babafb21
commit b0c58f2b10
2 changed files with 33 additions and 19 deletions

View File

@ -19,6 +19,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "symboldatabase.h" #include "symboldatabase.h"
#include "astutils.h"
#include "errorlogger.h" #include "errorlogger.h"
#include "platform.h" #include "platform.h"
#include "settings.h" #include "settings.h"
@ -640,21 +641,16 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
else if (scope->type == Scope::eCatch) else if (scope->type == Scope::eCatch)
scope->checkVariable(tok->tokAt(2), Throw, mSettings); // check for variable declaration and add it to new scope if found scope->checkVariable(tok->tokAt(2), Throw, mSettings); // check for variable declaration and add it to new scope if found
tok = scopeStartTok; tok = scopeStartTok;
} else if (tok->str() == "{") { } else if (Token::Match(tok, "%var% {")) {
if (tok->previous()->varId()) tok = tok->linkAt(1);
tok = tok->link(); } else if (const Token *lambdaEndToken = findLambdaEndToken(tok)) {
else { const Token *lambdaStartToken = lambdaEndToken->link();
const Token* tok2 = tok->previous(); scopeList.emplace_back(this, tok, scope, Scope::eLambda, lambdaStartToken);
while (!Token::Match(tok2, ";|}|{|)"))
tok2 = tok2->previous();
if (tok2->next() != tok && tok2->strAt(1) != ".")
tok2 = nullptr; // No lambda
if (tok2 && tok2->str() == ")" && tok2->link()->strAt(-1) == "]") {
scopeList.emplace_back(this, tok2->link()->linkAt(-1), scope, Scope::eLambda, tok);
scope->nestedList.push_back(&scopeList.back()); scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back(); scope = &scopeList.back();
} else if (!Token::Match(tok->previous(), "=|,|(|return") && !(tok->strAt(-1) == ")" && Token::Match(tok->linkAt(-1)->previous(), "=|,|(|return"))) { tok = lambdaStartToken;
} else if (tok->str() == "{") {
if (!Token::Match(tok->previous(), "=|,|(|return") && !(tok->strAt(-1) == ")" && Token::Match(tok->linkAt(-1)->previous(), "=|,|(|return"))) {
scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok); scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok);
scope->nestedList.push_back(&scopeList.back()); scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back(); scope = &scopeList.back();
@ -665,7 +661,6 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
} }
} }
} }
}
void SymbolDatabase::createSymbolDatabaseClassInfo() void SymbolDatabase::createSymbolDatabaseClassInfo()
{ {

View File

@ -359,6 +359,7 @@ private:
TEST_CASE(lambda); // #5867 TEST_CASE(lambda); // #5867
TEST_CASE(lambda2); // #7473 TEST_CASE(lambda2); // #7473
TEST_CASE(lambda3);
TEST_CASE(circularDependencies); // #6298 TEST_CASE(circularDependencies); // #6298
@ -5835,6 +5836,24 @@ private:
ASSERT_EQUALS(Scope::eLambda, scope->type); ASSERT_EQUALS(Scope::eLambda, scope->type);
} }
} }
void lambda3() {
GET_SYMBOL_DB("void func() {\n"
" auto f = []() mutable {}\n"
"}");
ASSERT(db && db->scopeList.size() == 3);
if (db && db->scopeList.size() == 3) {
std::list<Scope>::const_iterator scope = db->scopeList.begin();
ASSERT_EQUALS(Scope::eGlobal, scope->type);
++scope;
ASSERT_EQUALS(Scope::eFunction, scope->type);
++scope;
ASSERT_EQUALS(Scope::eLambda, scope->type);
}
}
// #6298 "stack overflow in Scope::findFunctionInBase (endless recursion)" // #6298 "stack overflow in Scope::findFunctionInBase (endless recursion)"
void circularDependencies() { void circularDependencies() {
check("template<template<class> class E,class D> class C : E<D> {\n" check("template<template<class> class E,class D> class C : E<D> {\n"