From fe9da23de1b885d5aeda0c15b7b2712b13ffe4af Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 7 Apr 2021 21:54:52 -0500 Subject: [PATCH 1/2] Fix issue 10202: FP: constParameter --- lib/symboldatabase.cpp | 2 ++ test/testother.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 41498ae5817..0a434d530d4 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -6812,6 +6812,8 @@ ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const Va } if (call->pointer > 0 && ((call->constness | func->constness) != func->constness)) return ValueType::MatchResult::NOMATCH; + if (func->reference != Reference::None && func->constness > call->constness) + return ValueType::MatchResult::FALLBACK1; if (call->type != func->type) { if (call->type == ValueType::Type::VOID || func->type == ValueType::Type::VOID) return ValueType::MatchResult::FALLBACK1; diff --git a/test/testother.cpp b/test/testother.cpp index a3767643350..1847773a58f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2325,6 +2325,16 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("", errout.str()); + // #10202 + check("struct A {};\n" + "const int* g(const A&);\n" + "int* g(A&);\n" + "void f(A& x) {\n" + " int* y = g(x);\n" + " *y = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("void e();\n" "void g(void);\n" "void h(void);\n" From e0e5e061669141a60940405bb3e4d533fc959399 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 7 Apr 2021 23:20:22 -0500 Subject: [PATCH 2/2] Add tests to symboldb --- lib/symboldatabase.cpp | 17 +++++++++----- test/testother.cpp | 10 -------- test/testsymboldatabase.cpp | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 0a434d530d4..e6215d21f03 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -6812,8 +6812,6 @@ ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const Va } if (call->pointer > 0 && ((call->constness | func->constness) != func->constness)) return ValueType::MatchResult::NOMATCH; - if (func->reference != Reference::None && func->constness > call->constness) - return ValueType::MatchResult::FALLBACK1; if (call->type != func->type) { if (call->type == ValueType::Type::VOID || func->type == ValueType::Type::VOID) return ValueType::MatchResult::FALLBACK1; @@ -6832,20 +6830,27 @@ ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const Va return ValueType::MatchResult::UNKNOWN; // TODO } - if (call->typeScope != nullptr || func->typeScope != nullptr) - return call->typeScope == func->typeScope ? ValueType::MatchResult::SAME : ValueType::MatchResult::NOMATCH; + if (call->typeScope != nullptr || func->typeScope != nullptr) { + if (call->typeScope != func->typeScope) + return ValueType::MatchResult::NOMATCH; + } if (call->container != nullptr || func->container != nullptr) { if (call->container != func->container) return ValueType::MatchResult::NOMATCH; } - else if (func->type < ValueType::Type::VOID || func->type == ValueType::Type::UNKNOWN_INT) - return ValueType::MatchResult::UNKNOWN; + if (func->typeScope != nullptr && func->container != nullptr) { + if (func->type < ValueType::Type::VOID || func->type == ValueType::Type::UNKNOWN_INT) + return ValueType::MatchResult::UNKNOWN; + } if (call->isIntegral() && func->isIntegral() && call->sign != ValueType::Sign::UNKNOWN_SIGN && func->sign != ValueType::Sign::UNKNOWN_SIGN && call->sign != func->sign) return ValueType::MatchResult::FALLBACK1; + if (func->reference != Reference::None && func->constness > call->constness) + return ValueType::MatchResult::FALLBACK1; + return ValueType::MatchResult::SAME; } diff --git a/test/testother.cpp b/test/testother.cpp index 1847773a58f..a3767643350 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2325,16 +2325,6 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("", errout.str()); - // #10202 - check("struct A {};\n" - "const int* g(const A&);\n" - "int* g(A&);\n" - "void f(A& x) {\n" - " int* y = g(x);\n" - " *y = 0;\n" - "}\n"); - ASSERT_EQUALS("", errout.str()); - check("void e();\n" "void g(void);\n" "void h(void);\n" diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index fd3286e97c1..3d6062d7dd6 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -405,6 +405,8 @@ class TestSymbolDatabase: public TestFixture { TEST_CASE(findFunction38); // #10125 TEST_CASE(findFunction39); // #10127 TEST_CASE(findFunction40); // #10135 + TEST_CASE(findFunction41); // #10202 + TEST_CASE(findFunction42); TEST_CASE(findFunctionContainer); TEST_CASE(findFunctionExternC); TEST_CASE(findFunctionGlobalScope); // ::foo @@ -6378,6 +6380,51 @@ class TestSymbolDatabase: public TestFixture { ASSERT_EQUALS(3, functok->function()->tokenDef->linenr()); } + void findFunction41() { // #10202 + { + GET_SYMBOL_DB("struct A {};\n" + "const int* g(const A&);\n" + "int* g(A&);\n" + "void f(A& x) {\n" + " int* y = g(x);\n" + " *y = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + const Token *functok = Token::findsimplematch(tokenizer.tokens(), "g ( x )"); + ASSERT(functok); + ASSERT(functok->function()); + ASSERT(functok->function()->name() == "g"); + ASSERT_EQUALS(3, functok->function()->tokenDef->linenr()); + } + { + GET_SYMBOL_DB("struct A {};\n" + "const int* g(const A&);\n" + "int* g(A&);\n" + "void f(const A& x) {\n" + " int* y = g(x);\n" + " *y = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + const Token *functok = Token::findsimplematch(tokenizer.tokens(), "g ( x )"); + ASSERT(functok); + ASSERT(functok->function()); + ASSERT(functok->function()->name() == "g"); + ASSERT_EQUALS(2, functok->function()->tokenDef->linenr()); + } + } + + void findFunction42() { + GET_SYMBOL_DB("void a(const std::string &, const std::string &);\n" + "void a(long, long);\n" + "void b() { a(true, false); }\n"); + ASSERT_EQUALS("", errout.str()); + const Token *functok = Token::findsimplematch(tokenizer.tokens(), "a ( true , false )"); + ASSERT(functok); + ASSERT(functok->function()); + ASSERT(functok->function()->name() == "a"); + ASSERT_EQUALS(2, functok->function()->tokenDef->linenr()); + } + void findFunctionContainer() { { GET_SYMBOL_DB("void dostuff(std::vector v);\n"