RedDocMD created this revision. Herald added subscribers: steakhal, ASDenysPetrov, martong, dkrupp, donat.nagy, Szelethus, a.sidorin, szepet, baloghadamsoftware. RedDocMD requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
In the function isValidBaseClass() of clang/lib/StaticAnalyzer/Core/MemRegion.cpp, added a case to return true when BaseClass and Super refer to the same CXXRecordDecl. This case arises when a pointer-to-member field is declared with a static cast from a pointer-to-member of a sub-class. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D95307 Files: clang/lib/StaticAnalyzer/Core/MemRegion.cpp clang/test/Analysis/pointer-to-member.cpp Index: clang/test/Analysis/pointer-to-member.cpp =================================================================== --- clang/test/Analysis/pointer-to-member.cpp +++ clang/test/Analysis/pointer-to-member.cpp @@ -231,6 +231,23 @@ } } // end of testPointerToMemberDiamond namespace +namespace testPointerToMemberViaStaticCast { +// From bug #48739 +struct Base { + int field; +}; + +struct Derived : public Base {}; + +void static_cast_test() { + int Derived::* derived_field = &Derived::field; + Base base; + base.field = 5; + int Base::* base_field = static_cast<int Base::*>(derived_field); + clang_analyzer_eval(base.*base_field == 5); // expected-warning {{TRUE}} +} +} + namespace testAnonymousMember { struct A { int a; Index: clang/lib/StaticAnalyzer/Core/MemRegion.cpp =================================================================== --- clang/lib/StaticAnalyzer/Core/MemRegion.cpp +++ clang/lib/StaticAnalyzer/Core/MemRegion.cpp @@ -1172,6 +1172,9 @@ if (!Class) return true; + if (Class == BaseClass) + return true; + if (IsVirtual) return Class->isVirtuallyDerivedFrom(BaseClass);
Index: clang/test/Analysis/pointer-to-member.cpp =================================================================== --- clang/test/Analysis/pointer-to-member.cpp +++ clang/test/Analysis/pointer-to-member.cpp @@ -231,6 +231,23 @@ } } // end of testPointerToMemberDiamond namespace +namespace testPointerToMemberViaStaticCast { +// From bug #48739 +struct Base { + int field; +}; + +struct Derived : public Base {}; + +void static_cast_test() { + int Derived::* derived_field = &Derived::field; + Base base; + base.field = 5; + int Base::* base_field = static_cast<int Base::*>(derived_field); + clang_analyzer_eval(base.*base_field == 5); // expected-warning {{TRUE}} +} +} + namespace testAnonymousMember { struct A { int a; Index: clang/lib/StaticAnalyzer/Core/MemRegion.cpp =================================================================== --- clang/lib/StaticAnalyzer/Core/MemRegion.cpp +++ clang/lib/StaticAnalyzer/Core/MemRegion.cpp @@ -1172,6 +1172,9 @@ if (!Class) return true; + if (Class == BaseClass) + return true; + if (IsVirtual) return Class->isVirtuallyDerivedFrom(BaseClass);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits