SymbolDatabase: New scope type eLambda

This commit is contained in:
PKEuS 2014-08-31 10:21:10 +02:00
parent bfa788a329
commit 862b18e0e2
3 changed files with 32 additions and 3 deletions

View File

@ -756,7 +756,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
scopeList.push_back(Scope(this, tok, scope, Scope::eWhile, tok1));
else if (tok->str() == "catch") {
scopeList.push_back(Scope(this, tok, scope, Scope::eCatch, tok1));
} else if (tok->str() == "switch")
} else // if (tok->str() == "switch")
scopeList.push_back(Scope(this, tok, scope, Scope::eSwitch, tok1));
scope->nestedList.push_back(&scopeList.back());
@ -767,7 +767,11 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
scope->checkVariable(tok->tokAt(2), Throw); // check for variable declaration and add it to new scope if found
tok = tok1;
} else if (tok->str() == "{" && !tok->previous()->varId()) {
if (!Token::Match(tok->previous(), "=|,")) {
if (tok->strAt(-1) == ")" && tok->linkAt(-1)->strAt(-1) == "]") {
scopeList.push_back(Scope(this, tok->linkAt(-1)->linkAt(-1), scope, Scope::eLambda, tok));
scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back();
} else if (!Token::Match(tok->previous(), "=|,")) {
scopeList.push_back(Scope(this, tok, scope, Scope::eUnconditional, tok));
scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back();
@ -1798,6 +1802,7 @@ static std::ostream & operator << (std::ostream & s, Scope::ScopeType type)
type == Scope::eTry ? "Try" :
type == Scope::eCatch ? "Catch" :
type == Scope::eUnconditional ? "Unconditional" :
type == Scope::eLambda ? "Lambda" :
"Unknown");
return s;
}

View File

@ -656,7 +656,7 @@ public:
const Scope *scope;
};
enum ScopeType { eGlobal, eClass, eStruct, eUnion, eNamespace, eFunction, eIf, eElse, eFor, eWhile, eDo, eSwitch, eUnconditional, eTry, eCatch };
enum ScopeType { eGlobal, eClass, eStruct, eUnion, eNamespace, eFunction, eIf, eElse, eFor, eWhile, eDo, eSwitch, eUnconditional, eTry, eCatch, eLambda };
Scope(const SymbolDatabase *check_, const Token *classDef_, const Scope *nestedIn_);
Scope(const SymbolDatabase *check_, const Token *classDef_, const Scope *nestedIn_, ScopeType type_, const Token *start_);

View File

@ -250,6 +250,8 @@ private:
TEST_CASE(varTypesOther); // (un)known
TEST_CASE(functionPrototype); // ticket #5867
TEST_CASE(lambda); // ticket #5867
}
void array() const {
@ -2469,6 +2471,28 @@ private:
"}\n", true);
ASSERT_EQUALS("", errout.str());
}
void lambda() {
GET_SYMBOL_DB("void func() {\n"
" float y = 0.0f;\n"
" auto lambda = [&]()\n"
" {\n"
" float x = 1.0f;\n"
" y += 10.0f - x;\n"
" }\n"
" lambda();\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);
}
}
};
REGISTER_TEST(TestSymbolDatabase)