Fix syntaxError on lambda inside decltype (#4650)

Use case where it gave an issue:

using customComparator = decltype([] (const X& lhs, const X& rhs) { return lhs.CompareTo(rhs); });
std::map<X, int, costomComparator> m;

Co-authored-by: Gerbo Engels <gerbo.engels@ortec-finance.com>
This commit is contained in:
gerboengels 2022-12-18 16:46:04 +01:00 committed by GitHub
parent 7799f820a3
commit 63e30d1b8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 4 deletions

View File

@ -387,9 +387,13 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
tok = tok->tokAt(3);
while (tok && tok->str() != ";")
while (tok && tok->str() != ";") {
if (Token::simpleMatch(tok, "decltype ("))
tok = tok->linkAt(1);
else
tok = tok->next();
}
}
// unnamed struct and union
else if (tok->isKeyword() && Token::Match(tok, "struct|union {") &&

View File

@ -122,10 +122,14 @@ public:
else if (classDef_ && classDef_->str() == "using") {
typeStart = classDef->tokAt(3);
typeEnd = typeStart;
while (typeEnd->next() && typeEnd->next()->str() != ";")
while (typeEnd->next() && typeEnd->next()->str() != ";") {
if (Token::simpleMatch(typeEnd, "decltype ("))
typeEnd = typeEnd->linkAt(1);
else
typeEnd = typeEnd->next();
}
}
}
const std::string& name() const;

View File

@ -366,6 +366,7 @@ private:
TEST_CASE(symboldatabase100); // #10174
TEST_CASE(symboldatabase101);
TEST_CASE(symboldatabase102);
TEST_CASE(symboldatabase103);
TEST_CASE(createSymbolDatabaseFindAllScopes1);
TEST_CASE(createSymbolDatabaseFindAllScopes2);
@ -5060,6 +5061,15 @@ private:
ASSERT(db->scopeList.back().className == "g");
}
void symboldatabase103() {
GET_SYMBOL_DB("void f() {\n"
"using lambda = decltype([]() { return true; });\n"
"lambda{}();\n"
"}\n");
ASSERT(db != nullptr);
ASSERT_EQUALS("", errout.str());
}
void createSymbolDatabaseFindAllScopes1() {
GET_SYMBOL_DB("void f() { union {int x; char *p;} a={0}; }");
ASSERT(db->scopeList.size() == 3);