From cf965b72b5dc8e4443466432c36bea128b0e54ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 1 Oct 2019 19:09:34 +0200 Subject: [PATCH] SymbolDatabase: look for functions in anonymous namespaces in the findFunction --- lib/symboldatabase.cpp | 23 +++++++++++++++++------ test/testsymboldatabase.cpp | 9 +++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 2e275a346..01713b413 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -4233,13 +4233,24 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const // find all the possible functions that could match const std::size_t args = arguments.size(); - for (std::multimap::const_iterator it = functionMap.find(tok->str()); it != functionMap.cend() && it->first == tok->str(); ++it) { - const Function *func = it->second; - if (args == func->argCount() || - (func->isVariadic() && args >= (func->argCount() - 1)) || - (args < func->argCount() && args >= func->minArgCount())) { - matches.push_back(func); + + auto addMatchingFunctions = [&](const Scope *scope) { + for (std::multimap::const_iterator it = scope->functionMap.find(tok->str()); it != scope->functionMap.cend() && it->first == tok->str(); ++it) { + const Function *func = it->second; + if (args == func->argCount() || + (func->isVariadic() && args >= (func->argCount() - 1)) || + (args < func->argCount() && args >= func->minArgCount())) { + matches.push_back(func); + } } + }; + + addMatchingFunctions(this); + + // check in anonumous namespaces + for (const Scope *nestedScope : nestedList) { + if (nestedScope->type == eNamespace && nestedScope->className.empty()) + addMatchingFunctions(nestedScope); } // check in base classes diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index b015c1688..080c6e5e4 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -344,6 +344,7 @@ private: TEST_CASE(findFunction24); // smart pointer TEST_CASE(findFunction25); // std::vector> TEST_CASE(findFunction26); // #8668 - pointer parameter in function call, const pointer function argument + TEST_CASE(findFunction27); TEST_CASE(findFunctionContainer); TEST_CASE(valueTypeMatchParameter); // ValueType::matchParameter @@ -5568,6 +5569,14 @@ private: ASSERT(dostuff1->function() && dostuff1->function()->token && dostuff1->function()->token->linenr() == 1); } + void findFunction27() { + GET_SYMBOL_DB("namespace { void a(int); }\n" + "void f() { a(9); }"); + const Token *a = Token::findmatch(tokenizer.tokens(), "a ( 9 )"); + ASSERT(a); + ASSERT(a->function()); + } + void findFunctionContainer() { { GET_SYMBOL_DB("void dostuff(std::vector v);\n"