Skip to content

Commit 13f5b56

Browse files
authored
Fix 10555: FP knownConditionTrueFalse with non-const function in base class (#3559)
1 parent 49d3e07 commit 13f5b56

4 files changed

Lines changed: 49 additions & 8 deletions

File tree

lib/astutils.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -751,18 +751,20 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth)
751751
return true;
752752
++depth;
753753
// calling nonstatic method?
754-
if (Token::Match(expr->previous(), "!!:: %name% (") && expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct()) {
754+
if (Token::Match(expr->previous(), "!!:: %name% (") && expr->function() && expr->function()->nestedIn &&
755+
expr->function()->nestedIn->isClassOrStruct()) {
755756
// is it a method of this?
756757
const Scope* fScope = expr->scope();
757758
while (!fScope->functionOf && fScope->nestedIn)
758759
fScope = fScope->nestedIn;
759-
const Scope* nestedIn = fScope->functionOf;
760-
if (nestedIn && nestedIn->function)
761-
nestedIn = nestedIn->function->token->scope();
762-
while (nestedIn && nestedIn != expr->function()->nestedIn) {
763-
nestedIn = nestedIn->nestedIn;
764-
}
765-
return nestedIn == expr->function()->nestedIn;
760+
761+
const Scope* classScope = fScope->functionOf;
762+
if (classScope && classScope->function)
763+
classScope = classScope->function->token->scope();
764+
765+
if (classScope && classScope->isClassOrStruct())
766+
return contains(classScope->findAssociatedScopes(), expr->function()->nestedIn);
767+
return false;
766768
} else if (onVar && Token::Match(expr, "%var%") && expr->variable()) {
767769
const Variable* var = expr->variable();
768770
return (var->isPrivate() || var->isPublic() || var->isProtected());

lib/symboldatabase.cpp

100644100755
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4892,6 +4892,24 @@ const Scope *Scope::findRecordInBase(const std::string & name) const
48924892
return nullptr;
48934893
}
48944894

4895+
std::vector<const Scope*> Scope::findAssociatedScopes() const
4896+
{
4897+
std::vector<const Scope*> result = {this};
4898+
if (isClassOrStruct() && definedType && !definedType->derivedFrom.empty()) {
4899+
const std::vector<Type::BaseInfo>& derivedFrom = definedType->derivedFrom;
4900+
for (const Type::BaseInfo& i : derivedFrom) {
4901+
const Type* base = i.type;
4902+
if (base && base->classScope) {
4903+
if (contains(result, base->classScope))
4904+
continue;
4905+
std::vector<const Scope*> baseScopes = base->classScope->findAssociatedScopes();
4906+
result.insert(result.end(), baseScopes.begin(), baseScopes.end());
4907+
}
4908+
}
4909+
}
4910+
return result;
4911+
}
4912+
48954913
//---------------------------------------------------------------------------
48964914

48974915
static void checkVariableCallMatch(const Variable* callarg, const Variable* funcarg, size_t& same, size_t& fallback1, size_t& fallback2)

lib/symboldatabase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,8 @@ class CPPCHECKLIB Scope {
11921192

11931193
const Scope *findRecordInBase(const std::string &name) const;
11941194

1195+
std::vector<const Scope*> findAssociatedScopes() const;
1196+
11951197
private:
11961198
/**
11971199
* @brief helper function for getVariableList()

test/testcondition.cpp

100644100755
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,6 +3894,25 @@ class TestCondition : public TestFixture {
38943894
" return delta;\n"
38953895
"}\n");
38963896
ASSERT_EQUALS("", errout.str());
3897+
3898+
// #10555
3899+
check("struct C {\n"
3900+
" int GetI() const { return i; }\n"
3901+
" int i{};\n"
3902+
"};\n"
3903+
"struct B {\n"
3904+
" C *m_PC{};\n"
3905+
" Modify();\n"
3906+
"};\n"
3907+
"struct D : B {\n"
3908+
" void test(); \n"
3909+
"};\n"
3910+
"void D::test() {\n"
3911+
" const int I = m_PC->GetI();\n"
3912+
" Modify();\n"
3913+
" if (m_PC->GetI() != I) {}\n"
3914+
"}\n");
3915+
ASSERT_EQUALS("", errout.str());
38973916
}
38983917

38993918
void alwaysTrueInfer() {

0 commit comments

Comments
 (0)