Skip to content

Commit 67fe99f

Browse files
IOBYTEdanmar
authored andcommitted
Fix symbol database argsMatch function and its usage so qualified types are handled properly. (cppcheck-opensource#1470)
* Fix symbol database argsMatch function and its usage so qualified types are handled properly. * Remove assert.
1 parent 36e663e commit 67fe99f

2 files changed

Lines changed: 28 additions & 29 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
593593
else if (declEnd && declEnd->str() == ";") {
594594
bool newFunc = true; // Is this function already in the database?
595595
for (std::multimap<std::string, const Function *>::const_iterator i = scope->functionMap.find(tok->str()); i != scope->functionMap.end() && i->first == tok->str(); ++i) {
596-
if (Function::argsMatch(scope, i->second->argDef->next(), argStart->next(), emptyString, 0)) {
596+
if (Function::argsMatch(scope, i->second->argDef, argStart, emptyString, 0)) {
597597
newFunc = false;
598598
break;
599599
}
@@ -1856,24 +1856,26 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se
18561856
if (!isCPP) // C does not support overloads
18571857
return true;
18581858

1859-
// skip "struct"
1860-
if (first->str() == "struct" || first->str() == "enum")
1861-
first = first->next();
1862-
if (second->str() == "struct" || second->str() == "enum")
1863-
second = second->next();
1864-
1865-
// skip const on type passed by value
1866-
if (Token::Match(first, "const %type% %name%|,|)"))
1867-
first = first->next();
1868-
if (Token::Match(second, "const %type% %name%|,|)"))
1869-
second = second->next();
1870-
18711859
unsigned int arg_path_length = path_length;
18721860

18731861
while (first->str() == second->str() &&
18741862
first->isLong() == second->isLong() &&
18751863
first->isUnsigned() == second->isUnsigned()) {
18761864

1865+
// skip "struct"
1866+
if (first->strAt(1) == "struct" || first->strAt(1) == "enum")
1867+
first = first->next();
1868+
if (second->strAt(1) == "struct" || second->strAt(1) == "enum")
1869+
second = second->next();
1870+
1871+
// skip const on type passed by value
1872+
if (Token::Match(first->next(), "const %type% %name%|,|)") &&
1873+
!Token::Match(first->next(), "const %type% %name%| ["))
1874+
first = first->next();
1875+
if (Token::Match(second->next(), "const %type% %name%|,|)") &&
1876+
!Token::Match(second->next(), "const %type% %name%| ["))
1877+
second = second->next();
1878+
18771879
// at end of argument list
18781880
if (first->str() == ")") {
18791881
return true;
@@ -2028,20 +2030,6 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se
20282030
// reset path length
20292031
if (first->str() == "," || second->str() == ",")
20302032
arg_path_length = path_length;
2031-
2032-
// skip "struct"
2033-
if (first->str() == "struct" || first->str() == "enum")
2034-
first = first->next();
2035-
if (second->str() == "struct" || second->str() == "enum")
2036-
second = second->next();
2037-
2038-
// skip const on type passed by value
2039-
if (Token::Match(first, "const %type% %name%|,|)") &&
2040-
!Token::Match(first, "const %type% %name%| ["))
2041-
first = first->next();
2042-
if (Token::Match(second, "const %type% %name%|,|)") &&
2043-
!Token::Match(second, "const %type% %name%| ["))
2044-
second = second->next();
20452033
}
20462034

20472035
return false;
@@ -2063,7 +2051,7 @@ Function* SymbolDatabase::addGlobalFunction(Scope*& scope, const Token*& tok, co
20632051
const Function *f = i->second;
20642052
if (f->hasBody())
20652053
continue;
2066-
if (Function::argsMatch(scope, f->argDef->next(), argStart->next(), emptyString, 0)) {
2054+
if (Function::argsMatch(scope, f->argDef, argStart, emptyString, 0)) {
20672055
function = const_cast<Function *>(i->second);
20682056
break;
20692057
}
@@ -4734,7 +4722,7 @@ Function * SymbolDatabase::findFunctionInScope(const Token *func, const Scope *n
47344722
for (std::multimap<std::string, const Function *>::const_iterator it = ns->functionMap.find(func->str());
47354723
it != ns->functionMap.end() && it->first == func->str(); ++it) {
47364724

4737-
if (Function::argsMatch(ns, it->second->argDef->next(), func->tokAt(2), path, path_length) &&
4725+
if (Function::argsMatch(ns, it->second->argDef, func->next(), path, path_length) &&
47384726
it->second->isDestructor() == destructor) {
47394727
function = it->second;
47404728
break;

test/testsymboldatabase.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,6 +3103,17 @@ class TestSymbolDatabase: public TestFixture {
31033103
"}");
31043104
ASSERT(db != nullptr);
31053105
ASSERT(db && db->scopeList.size() == 5);
3106+
if (db && db->scopeList.size() == 5) {
3107+
const Scope *scope = db->findScopeByName("A");
3108+
ASSERT(scope != nullptr);
3109+
if (scope) {
3110+
const Function *function = findFunctionByName("Foo", scope);
3111+
ASSERT(function != nullptr);
3112+
if (function) {
3113+
ASSERT(function->hasBody());
3114+
}
3115+
}
3116+
}
31063117
}
31073118

31083119
void symboldatabase63() {

0 commit comments

Comments
 (0)