Skip to content

Commit fe1f601

Browse files
IOBYTEdanmar
authored andcommitted
Fix #9389 ("debug: Executable scope 'x' with unknown function." with … (cppcheck-opensource#2261)
* Fix #9389 ("debug: Executable scope 'x' with unknown function." with "using namespace") * use static rather than anonymous namespace for new functions
1 parent 1c23366 commit fe1f601

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,13 +2001,53 @@ Function::Function(const Tokenizer *mTokenizer,
20012001
}
20022002
}
20032003

2004+
static std::string qualifiedName(const Scope *scope)
2005+
{
2006+
std::string name = scope->className;
2007+
while (scope->nestedIn) {
2008+
if (!scope->nestedIn->className.empty())
2009+
name = (scope->nestedIn->className + " :: ") + name;
2010+
scope = scope->nestedIn;
2011+
}
2012+
return name;
2013+
}
2014+
2015+
static bool usingNamespace(const Scope *scope, const Token *first, const Token *second, int &offset)
2016+
{
2017+
offset = 0;
2018+
std::string name = first->str();
2019+
2020+
while (Token::Match(first, "%type% :: %type%")) {
2021+
if (offset)
2022+
name += (" :: " + first->str());
2023+
offset += 2;
2024+
first = first->tokAt(2);
2025+
if (first->str() == second->str()) {
2026+
break;
2027+
}
2028+
}
2029+
2030+
if (offset) {
2031+
while (scope) {
2032+
for (const auto & info : scope->usingList) {
2033+
if (name == qualifiedName(info.scope))
2034+
return true;
2035+
}
2036+
scope = scope->nestedIn;
2037+
}
2038+
}
2039+
2040+
return false;
2041+
}
2042+
20042043
bool Function::argsMatch(const Scope *scope, const Token *first, const Token *second, const std::string &path, nonneg int path_length)
20052044
{
20062045
const bool isCPP = scope->check->isCPP();
20072046
if (!isCPP) // C does not support overloads
20082047
return true;
20092048

20102049
int arg_path_length = path_length;
2050+
int offset = 0;
20112051

20122052
while (first->str() == second->str() &&
20132053
first->isLong() == second->isLong() &&
@@ -2120,6 +2160,10 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se
21202160
}
21212161
}
21222162

2163+
// using namespace
2164+
else if (usingNamespace(scope, first->next(), second->next(), offset))
2165+
first = first->tokAt(offset);
2166+
21232167
// variable with class path
21242168
else if (arg_path_length && Token::Match(first->next(), "%name%") && first->strAt(1) != "const") {
21252169
std::string param = path;

‎test/testsymboldatabase.cpp‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ class TestSymbolDatabase: public TestFixture {
302302
TEST_CASE(symboldatabase77); // #8663
303303
TEST_CASE(symboldatabase78); // #9147
304304
TEST_CASE(symboldatabase79); // #9392
305+
TEST_CASE(symboldatabase80); // #9389
305306

306307
TEST_CASE(createSymbolDatabaseFindAllScopes1);
307308

@@ -4287,6 +4288,67 @@ class TestSymbolDatabase: public TestFixture {
42874288
ASSERT(db->scopeList.back().functionList.front().isDefault() == true);
42884289
}
42894290

4291+
void symboldatabase80() { // #9389
4292+
{
4293+
GET_SYMBOL_DB("namespace ns {\n"
4294+
"class A {};\n"
4295+
"}\n"
4296+
"class AA {\n"
4297+
"private:\n"
4298+
" void f(const ns::A&);\n"
4299+
"};\n"
4300+
"using namespace ns;\n"
4301+
"void AA::f(const A&) { }");
4302+
ASSERT(db->scopeList.size() == 5);
4303+
ASSERT(db->functionScopes.size() == 1);
4304+
const Scope *scope = db->findScopeByName("AA");
4305+
ASSERT(scope);
4306+
ASSERT(scope->functionList.size() == 1);
4307+
ASSERT(scope->functionList.front().name() == "f");
4308+
ASSERT(scope->functionList.front().hasBody() == true);
4309+
}
4310+
{
4311+
GET_SYMBOL_DB("namespace ns {\n"
4312+
"namespace ns1 {\n"
4313+
"class A {};\n"
4314+
"}\n"
4315+
"}\n"
4316+
"class AA {\n"
4317+
"private:\n"
4318+
" void f(const ns::ns1::A&);\n"
4319+
"};\n"
4320+
"using namespace ns::ns1;\n"
4321+
"void AA::f(const A&) { }");
4322+
ASSERT(db->scopeList.size() == 6);
4323+
ASSERT(db->functionScopes.size() == 1);
4324+
const Scope *scope = db->findScopeByName("AA");
4325+
ASSERT(scope);
4326+
ASSERT(scope->functionList.size() == 1);
4327+
ASSERT(scope->functionList.front().name() == "f");
4328+
ASSERT(scope->functionList.front().hasBody() == true);
4329+
}
4330+
{
4331+
GET_SYMBOL_DB("namespace ns {\n"
4332+
"namespace ns1 {\n"
4333+
"class A {};\n"
4334+
"}\n"
4335+
"}\n"
4336+
"class AA {\n"
4337+
"private:\n"
4338+
" void f(const ns::ns1::A&);\n"
4339+
"};\n"
4340+
"using namespace ns;\n"
4341+
"void AA::f(const ns1::A&) { }");
4342+
ASSERT(db->scopeList.size() == 6);
4343+
ASSERT(db->functionScopes.size() == 1);
4344+
const Scope *scope = db->findScopeByName("AA");
4345+
ASSERT(scope);
4346+
ASSERT(scope->functionList.size() == 1);
4347+
ASSERT(scope->functionList.front().name() == "f");
4348+
ASSERT(scope->functionList.front().hasBody() == true);
4349+
}
4350+
}
4351+
42904352
void createSymbolDatabaseFindAllScopes1() {
42914353
GET_SYMBOL_DB("void f() { union {int x; char *p;} a={0}; }");
42924354
ASSERT(db->scopeList.size() == 3);

0 commit comments

Comments
 (0)