From ae1b9cb14e6f3f9e981cd7785b41567028f5b004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 4 Nov 2020 07:17:17 +0100 Subject: [PATCH] SymbolDatabase: Improved function matching in C code --- lib/symboldatabase.cpp | 6 +++++- test/testsymboldatabase.cpp | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index e13454961..b12f3a9b3 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -4807,7 +4807,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const fallback1++; // Try to evaluate the apparently more complex expression - else { + else if (check->isCPP()) { const Token *vartok = arguments[j]; while (vartok->isUnaryOp("&") || vartok->isUnaryOp("*")) vartok = vartok->astOperand1(); @@ -4825,6 +4825,10 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const break; } } + + else + // C code: if number of arguments match then do not match types + fallback1++; } const size_t hasToBe = func->isVariadic() ? (func->argCount() - 1) : args; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index e743df8b3..2c4bf4c1a 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -378,6 +378,7 @@ private: TEST_CASE(findFunction29); TEST_CASE(findFunction30); TEST_CASE(findFunction31); + TEST_CASE(findFunction32); // C: relax type matching TEST_CASE(findFunctionContainer); TEST_CASE(findFunctionExternC); TEST_CASE(findFunctionGlobalScope); // ::foo @@ -6179,6 +6180,17 @@ private: ASSERT_EQUALS(1, foo->function()->tokenDef->linenr()); } + void findFunction32() { + GET_SYMBOL_DB_C("void foo(char *p);\n" + "void bar() { foo(\"123\"); }"); + (void)db; + const Token *foo = Token::findsimplematch(tokenizer.tokens(), "foo ( \"123\" ) ;"); + ASSERT(foo); + ASSERT(foo->function()); + ASSERT(foo->function()->tokenDef); + ASSERT_EQUALS(1, foo->function()->tokenDef->linenr()); + } + void findFunctionContainer() { { GET_SYMBOL_DB("void dostuff(std::vector v);\n"