Skip to content

Commit e21be4e

Browse files
authored
Fix #14272 (symboldatabase: findFunction match wrong function when there is extra const) (danmar#7964)
1 parent bb78bd8 commit e21be4e

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

lib/symboldatabase.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,7 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars()
15561556
}
15571557
}
15581558

1559+
// cppcheck-suppress functionConst - has side effects
15591560
void SymbolDatabase::createSymbolDatabaseEscapeFunctions()
15601561
{
15611562
for (const Scope& scope : scopeList) {
@@ -8574,7 +8575,8 @@ ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const Va
85748575
return ValueType::MatchResult::NOMATCH; // TODO
85758576
}
85768577
if (call->pointer > 0) {
8577-
if ((call->constness | func->constness) != func->constness)
8578+
const unsigned int mask = (1U << call->pointer) - 1;
8579+
if (((call->constness | func->constness) & mask) != (func->constness & mask))
85788580
return ValueType::MatchResult::NOMATCH;
85798581
if ((call->volatileness | func->volatileness) != func->volatileness)
85808582
return ValueType::MatchResult::NOMATCH;

lib/valueflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5507,7 +5507,7 @@ static void valueFlowForLoopSimplifyAfter(Token* fortok, nonneg int varid, const
55075507
}
55085508
}
55095509

5510-
static void valueFlowForLoop(TokenList &tokenlist, const SymbolDatabase& symboldatabase, ErrorLogger &errorLogger, const Settings &settings)
5510+
static void valueFlowForLoop(const TokenList &tokenlist, const SymbolDatabase& symboldatabase, ErrorLogger &errorLogger, const Settings &settings)
55115511
{
55125512
for (const Scope &scope : symboldatabase.scopeList) {
55135513
if (scope.type != ScopeType::eFor)

test/testsymboldatabase.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ class TestSymbolDatabase : public TestFixture {
535535
TEST_CASE(findFunction59);
536536
TEST_CASE(findFunction60);
537537
TEST_CASE(findFunction61);
538+
TEST_CASE(findFunction62); // #14272 - pointer passed to function is const
538539
TEST_CASE(findFunctionRef1);
539540
TEST_CASE(findFunctionRef2); // #13328
540541
TEST_CASE(findFunctionContainer);
@@ -8694,6 +8695,22 @@ class TestSymbolDatabase : public TestFixture {
86948695
ASSERT(fun && !fun->function());
86958696
}
86968697

8698+
void findFunction62() { // #14272
8699+
GET_SYMBOL_DB("class Token {\n"
8700+
" std::string stringifyList(const Token* end, bool attributes = true) const;\n"
8701+
" std::string stringifyList(bool varid = false) const;\n"
8702+
"};\n"
8703+
"\n"
8704+
"void foo(const Token * const tokIf) {\n"
8705+
" tokIf->stringifyList(tokIf);\n"
8706+
"}\n");
8707+
const Token* functionCall = Token::findsimplematch(tokenizer.tokens(), "stringifyList ( tokIf )");
8708+
ASSERT(functionCall);
8709+
ASSERT(functionCall->function());
8710+
ASSERT(functionCall->function()->token);
8711+
ASSERT_EQUALS(2, functionCall->function()->token->linenr());
8712+
}
8713+
86978714
void findFunctionRef1() {
86988715
GET_SYMBOL_DB("struct X {\n"
86998716
" const std::vector<int> getInts() const & { return mInts; }\n"

0 commit comments

Comments
 (0)