Author: dergachev Date: Mon Jun 25 16:43:45 2018 New Revision: 335555 URL: http://llvm.org/viewvc/llvm-project?rev=335555&view=rev Log: [analyzer] Fix invalidation on C++ const methods with arrow syntax.
Conservative evaluation of a C++ method call would invalidate the object, as long as the method is not const or the object has mutable fields. When checking for mutable fields, we need to scan the type of the object on which the method is called, which may be more specific than the type of the object on which the method is defined, hence we look up the type from the this-argument expression. If arrow syntax or implicit-this syntax is used, this-argument expression has pointer type, not record type, and lookup accidentally failed for that reason. Obtain object type correctly. Differential Revision: https://reviews.llvm.org/D48460 Modified: cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp cfe/trunk/test/Analysis/const-method-call.cpp Modified: cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp?rev=335555&r1=335554&r2=335555&view=diff ============================================================================== --- cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp (original) +++ cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp Mon Jun 25 16:43:45 2018 @@ -534,9 +534,14 @@ void CXXInstanceCall::getExtraInvalidate // Get the record decl for the class of 'This'. D->getParent() may return a // base class decl, rather than the class of the instance which needs to be // checked for mutable fields. + // TODO: We might as well look at the dynamic type of the object. const Expr *Ex = getCXXThisExpr()->ignoreParenBaseCasts(); - const CXXRecordDecl *ParentRecord = Ex->getType()->getAsCXXRecordDecl(); - if (!ParentRecord || ParentRecord->hasMutableFields()) + QualType T = Ex->getType(); + if (T->isPointerType()) // Arrow or implicit-this syntax? + T = T->getPointeeType(); + const CXXRecordDecl *ParentRecord = T->getAsCXXRecordDecl(); + assert(ParentRecord); + if (ParentRecord->hasMutableFields()) return; // Preserve CXXThis. const MemRegion *ThisRegion = ThisVal.getAsRegion(); Modified: cfe/trunk/test/Analysis/const-method-call.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/const-method-call.cpp?rev=335555&r1=335554&r2=335555&view=diff ============================================================================== --- cfe/trunk/test/Analysis/const-method-call.cpp (original) +++ cfe/trunk/test/Analysis/const-method-call.cpp Mon Jun 25 16:43:45 2018 @@ -6,6 +6,14 @@ struct A { int x; void foo() const; void bar(); + + void testImplicitThisSyntax() { + x = 3; + foo(); + clang_analyzer_eval(x == 3); // expected-warning{{TRUE}} + bar(); + clang_analyzer_eval(x == 3); // expected-warning{{UNKNOWN}} + } }; struct B { @@ -108,6 +116,22 @@ void checkThatContainedConstMethodDoesNo clang_analyzer_eval(t.in.x == 2); // expected-warning{{TRUE}} } +void checkPointerTypedThisExpression(A *a) { + a->x = 3; + a->foo(); + clang_analyzer_eval(a->x == 3); // expected-warning{{TRUE}} + a->bar(); + clang_analyzer_eval(a->x == 3); // expected-warning{{UNKNOWN}} +} + +void checkReferenceTypedThisExpression(A &a) { + a.x = 3; + a.foo(); + clang_analyzer_eval(a.x == 3); // expected-warning{{TRUE}} + a.bar(); + clang_analyzer_eval(a.x == 3); // expected-warning{{UNKNOWN}} +} + // --- Versions of the above tests where the const method is inherited --- // struct B1 { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits