Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#12420: SymbolDatabase: function in blockscope #5960

Merged
merged 7 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,33 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
} else {
tok = tok->link();
}
} else if (Token::Match(tok, "%name% (")) {
if (tok->next() && Token::simpleMatch(tok->next()->link(), ") ;")) {
olabetskyi marked this conversation as resolved.
Show resolved Hide resolved
const Token *funcStart = nullptr;
danmar marked this conversation as resolved.
Show resolved Hide resolved
const Token *argStart = nullptr;
const Token *declEnd = nullptr;
if (isFunction(tok, scope, &funcStart, &argStart, &declEnd)) {
if (declEnd && declEnd->str() == ";") {
bool newFunc = true; // Is this function already in the database?
auto range = scope->functionMap.equal_range(tok->str());
for (std::multimap<std::string, const Function*>::const_iterator it = range.first; it != range.second; ++it) {
if (it->second->argsMatch(scope, it->second->argDef, argStart, emptyString, 0)) {
newFunc = false;
break;
}
}
// save function prototype in database
if (newFunc) {
Function function(tok, scope, funcStart, argStart);
if (function.isExtern()) {
scope->addFunction(std::move(function));
tok = declEnd;
}
}
continue;
}
}
}
}
// syntax error?
if (!scope)
Expand Down Expand Up @@ -5892,6 +5919,10 @@ const Function* SymbolDatabase::findFunction(const Token* const tok) const
// find the scope this function is in
const Scope *currScope = tok->scope();
while (currScope && currScope->isExecutable()) {
if (const Function* f = currScope->findFunction(tok)) {
if (f->isExtern())
return f;
}
if (currScope->functionOf)
currScope = currScope->functionOf;
else
Expand Down
14 changes: 14 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(memberFunctionOfUnknownClassMacro2);
TEST_CASE(memberFunctionOfUnknownClassMacro3);
TEST_CASE(functionLinkage);
TEST_CASE(externalFunctionsInsideAFunction); // #12420

TEST_CASE(classWithFriend);

Expand Down Expand Up @@ -2298,6 +2299,19 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(f && f->function() && !f->function()->isExtern() && f->function()->retDef->str() == "void");
}

void externalFunctionsInsideAFunction() {
GET_SYMBOL_DB("void foo( void )\n"
"{\n"
" extern void bar( void );\n"
" bar();\n"
"}\n");

ASSERT(db && errout.str().empty());

const Token *f = Token::findsimplematch(tokenizer.tokens(), "bar");
ASSERT(f && f->function() && f->function()->isExtern() && f->function()->retDef->str() == "void");
olabetskyi marked this conversation as resolved.
Show resolved Hide resolved
}
olabetskyi marked this conversation as resolved.
Show resolved Hide resolved

void classWithFriend() {
GET_SYMBOL_DB("class Foo {}; class Bar1 { friend class Foo; }; class Bar2 { friend Foo; };");
// 3 scopes: Global, 3 classes
Expand Down
Loading