r344017 - [analyzer] Support Reinitializes attribute in MisusedMovedObject check

Gabor Horvath via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 9 00:28:58 PDT 2018


Author: xazax
Date: Tue Oct  9 00:28:57 2018
New Revision: 344017

URL: http://llvm.org/viewvc/llvm-project?rev=344017&view=rev
Log:
[analyzer] Support Reinitializes attribute in MisusedMovedObject check

Differential Revision: https://reviews.llvm.org/D52983

Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp
    cfe/trunk/test/Analysis/MisusedMovedObject.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp?rev=344017&r1=344016&r2=344017&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp Tue Oct  9 00:28:57 2018
@@ -314,17 +314,18 @@ bool MisusedMovedObjectChecker::isMoveSa
       return true;
   }
   // Function call `empty` can be skipped.
-  if (MethodDec && MethodDec->getDeclName().isIdentifier() &&
+  return (MethodDec && MethodDec->getDeclName().isIdentifier() &&
       (MethodDec->getName().lower() == "empty" ||
-       MethodDec->getName().lower() == "isempty"))
-    return true;
-
-  return false;
+       MethodDec->getName().lower() == "isempty"));
 }
 
 bool MisusedMovedObjectChecker::isStateResetMethod(
     const CXXMethodDecl *MethodDec) const {
-  if (MethodDec && MethodDec->getDeclName().isIdentifier()) {
+  if (!MethodDec)
+      return false;
+  if (MethodDec->hasAttr<ReinitializesAttr>())
+      return true;
+  if (MethodDec->getDeclName().isIdentifier()) {
     std::string MethodName = MethodDec->getName().lower();
     if (MethodName == "reset" || MethodName == "clear" ||
         MethodName == "destroy")
@@ -429,8 +430,7 @@ void MisusedMovedObjectChecker::checkPre
 
   // We want to investigate the whole object, not only sub-object of a parent
   // class in which the encountered method defined.
-  while (const CXXBaseObjectRegion *BR =
-             dyn_cast<CXXBaseObjectRegion>(ThisRegion))
+  while (const auto *BR = dyn_cast<CXXBaseObjectRegion>(ThisRegion))
     ThisRegion = BR->getSuperRegion();
 
   if (isMoveSafeMethod(MethodDecl))
@@ -487,13 +487,9 @@ ProgramStateRef MisusedMovedObjectChecke
     ThisRegion = IC->getCXXThisVal().getAsRegion();
   }
 
-  for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(),
-                                             E = ExplicitRegions.end();
-       I != E; ++I) {
-    const auto *Region = *I;
-    if (ThisRegion != Region) {
+  for (const auto *Region : ExplicitRegions) {
+    if (ThisRegion != Region)
       State = removeFromState(State, Region);
-    }
   }
 
   return State;

Modified: cfe/trunk/test/Analysis/MisusedMovedObject.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/MisusedMovedObject.cpp?rev=344017&r1=344016&r2=344017&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/MisusedMovedObject.cpp (original)
+++ cfe/trunk/test/Analysis/MisusedMovedObject.cpp Tue Oct  9 00:28:57 2018
@@ -638,7 +638,10 @@ void ifStmtSequencesDeclAndConditionTest
   }
 }
 
-class C : public A {};
+struct C : public A {
+  [[clang::reinitializes]] void reinit();
+};
+
 void subRegionMoveTest() {
   {
     A a;
@@ -672,6 +675,13 @@ void resetSuperClass() {
   C c2 = c; // no-warning
 }
 
+void resetSuperClass2() {
+  C c;
+  C c1 = std::move(c);
+  c.reinit();
+  C c2 = c; // no-warning
+}
+
 void reportSuperClass() {
   C c;
   C c1 = std::move(c); // expected-note {{'c' became 'moved-from' here}}




More information about the cfe-commits mailing list