r174780 - [analyzer] IvarInvalidation: refactor, pull out the diagnostic printing

Anna Zaks ganna at apple.com
Fri Feb 8 15:55:45 PST 2013


Author: zaks
Date: Fri Feb  8 17:55:45 2013
New Revision: 174780

URL: http://llvm.org/viewvc/llvm-project?rev=174780&view=rev
Log:
[analyzer] IvarInvalidation: refactor, pull out the diagnostic printing

Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/IvarInvalidationChecker.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/IvarInvalidationChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/IvarInvalidationChecker.cpp?rev=174780&r1=174779&r2=174780&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/IvarInvalidationChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/IvarInvalidationChecker.cpp Fri Feb  8 17:55:45 2013
@@ -191,7 +191,19 @@ class IvarInvalidationChecker :
   /// Print ivar name or the property if the given ivar backs a property.
   static void printIvar(llvm::raw_svector_ostream &os,
                         const ObjCIvarDecl *IvarDecl,
-                        IvarToPropMapTy &IvarToPopertyMap);
+                        const IvarToPropMapTy &IvarToPopertyMap);
+
+  static void reportNoInvalidationMethod(const ObjCIvarDecl *FirstIvarDecl,
+                                      const IvarToPropMapTy &IvarToPopertyMap,
+                                      const ObjCInterfaceDecl *InterfaceD,
+                                      BugReporter &BR,
+                                      bool MissingDeclaration);
+  static void reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
+                                      const IvarToPropMapTy &IvarToPopertyMap,
+                                      const ObjCMethodDecl *MethodD,
+                                      AnalysisManager& Mgr,
+                                      BugReporter &BR);
+
 
 public:
   void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
@@ -335,10 +347,10 @@ const ObjCIvarDecl *IvarInvalidationChec
 }
 
 void IvarInvalidationChecker::printIvar(llvm::raw_svector_ostream &os,
-                                        const ObjCIvarDecl *IvarDecl,
-                                        IvarToPropMapTy &IvarToPopertyMap) {
+                                      const ObjCIvarDecl *IvarDecl,
+                                      const IvarToPropMapTy &IvarToPopertyMap) {
   if (IvarDecl->getSynthesize()) {
-    const ObjCPropertyDecl *PD = IvarToPopertyMap[IvarDecl];
+    const ObjCPropertyDecl *PD = IvarToPopertyMap.lookup(IvarDecl);
     assert(PD &&"Do we synthesize ivars for something other than properties?");
     os << "Property "<< PD->getName() << " ";
   } else {
@@ -381,9 +393,8 @@ void IvarInvalidationChecker::checkASTDe
 
     const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars,
                                                      &FirstIvarDecl);
-    if (!ID) {
+    if (!ID)
       continue;
-    }
 
     // Store the mappings.
     PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
@@ -449,19 +460,8 @@ void IvarInvalidationChecker::checkASTDe
 
   // Report an error in case none of the invalidation methods are declared.
   if (!Info.needsInvalidation()) {
-    SmallString<128> sbuf;
-    llvm::raw_svector_ostream os(sbuf);
-    assert(FirstIvarDecl);
-    printIvar(os, FirstIvarDecl, IvarToPopertyMap);
-    os << "needs to be invalidated; ";
-    os << "no invalidation method is declared for " << InterfaceD->getName();
-
-    PathDiagnosticLocation IvarDecLocation =
-      PathDiagnosticLocation::createBegin(FirstIvarDecl, BR.getSourceManager());
-
-    BR.EmitBasicReport(FirstIvarDecl, "Incomplete invalidation",
-                       categories::CoreFoundationObjectiveC, os.str(),
-                       IvarDecLocation);
+    reportNoInvalidationMethod(FirstIvarDecl, IvarToPopertyMap, InterfaceD, BR,
+                               /*MissingDeclaration*/ true);
     return;
   }
 
@@ -493,40 +493,60 @@ void IvarInvalidationChecker::checkASTDe
         continue;
 
       // Warn on the ivars that were not invalidated by the method.
-      for (IvarSet::const_iterator I = IvarsI.begin(),
-                                   E = IvarsI.end(); I != E; ++I) {
-        SmallString<128> sbuf;
-        llvm::raw_svector_ostream os(sbuf);
-        printIvar(os, I->first, IvarToPopertyMap);
-        os << "needs to be invalidated or set to nil";
-        PathDiagnosticLocation MethodDecLocation =
-                               PathDiagnosticLocation::createEnd(D->getBody(),
-                               BR.getSourceManager(),
-                               Mgr.getAnalysisDeclContext(D));
-        BR.EmitBasicReport(D, "Incomplete invalidation",
-                           categories::CoreFoundationObjectiveC, os.str(),
-                           MethodDecLocation);
-      }
+      for (IvarSet::const_iterator
+          I = IvarsI.begin(), E = IvarsI.end(); I != E; ++I)
+        reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, D, Mgr, BR);
     }
   }
 
   // Report an error in case none of the invalidation methods are implemented.
-  if (!AtImplementationContainsAtLeastOneInvalidationMethod) {
-    SmallString<128> sbuf;
-    llvm::raw_svector_ostream os(sbuf);
-    assert(FirstIvarDecl);
-    printIvar(os, FirstIvarDecl, IvarToPopertyMap);
-    os << "needs to be invalidated; ";
-    os << "no invalidation method is defined in the @implementation for "
-       << InterfaceD->getName();
-
-    PathDiagnosticLocation IvarDecLocation =
-        PathDiagnosticLocation::createBegin(FirstIvarDecl,
-                                            BR.getSourceManager());
-    BR.EmitBasicReport(FirstIvarDecl, "Incomplete invalidation",
-                       categories::CoreFoundationObjectiveC, os.str(),
-                       IvarDecLocation);
-  }
+  if (!AtImplementationContainsAtLeastOneInvalidationMethod)
+    reportNoInvalidationMethod(FirstIvarDecl, IvarToPopertyMap, InterfaceD, BR,
+                               /*MissingDeclaration*/ false);
+}
+
+void IvarInvalidationChecker::
+reportNoInvalidationMethod(const ObjCIvarDecl *FirstIvarDecl,
+                           const IvarToPropMapTy &IvarToPopertyMap,
+                           const ObjCInterfaceDecl *InterfaceD,
+                           BugReporter &BR,
+                           bool MissingDeclaration) {
+  SmallString<128> sbuf;
+  llvm::raw_svector_ostream os(sbuf);
+  assert(FirstIvarDecl);
+  printIvar(os, FirstIvarDecl, IvarToPopertyMap);
+  os << "needs to be invalidated; ";
+  if (MissingDeclaration)
+    os << "no invalidation method is declared for ";
+  else
+    os << "no invalidation method is defined in the @implementation for ";
+  os << InterfaceD->getName();
+
+  PathDiagnosticLocation IvarDecLocation =
+    PathDiagnosticLocation::createBegin(FirstIvarDecl, BR.getSourceManager());
+
+  BR.EmitBasicReport(FirstIvarDecl, "Incomplete invalidation",
+                     categories::CoreFoundationObjectiveC, os.str(),
+                     IvarDecLocation);
+}
+
+void IvarInvalidationChecker::
+reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
+                                    const IvarToPropMapTy &IvarToPopertyMap,
+                                    const ObjCMethodDecl *MethodD,
+                                    AnalysisManager& Mgr,
+                                    BugReporter &BR) {
+  SmallString<128> sbuf;
+  llvm::raw_svector_ostream os(sbuf);
+  printIvar(os, IvarD, IvarToPopertyMap);
+  os << "needs to be invalidated or set to nil";
+  PathDiagnosticLocation MethodDecLocation =
+                         PathDiagnosticLocation::createEnd(MethodD->getBody(),
+                         BR.getSourceManager(),
+                         Mgr.getAnalysisDeclContext(MethodD));
+  BR.EmitBasicReport(MethodD, "Incomplete invalidation",
+                     categories::CoreFoundationObjectiveC, os.str(),
+                     MethodDecLocation);
 }
 
 void IvarInvalidationChecker::MethodCrawler::markInvalidated(





More information about the cfe-commits mailing list