]> granicus.if.org Git - clang/commitdiff
[analyzer] Address Jordan's code review for r164790.
authorAnna Zaks <ganna@apple.com>
Thu, 27 Sep 2012 21:57:17 +0000 (21:57 +0000)
committerAnna Zaks <ganna@apple.com>
Thu, 27 Sep 2012 21:57:17 +0000 (21:57 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@164803 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/DirectIvarAssignment.cpp
test/Analysis/objc-properties.m

index db3a88535032fa69ef1b445e0ef0c0d81cb2b3a4..86468295483870d58669450eb0396cbd8211319b 100644 (file)
@@ -64,21 +64,23 @@ public:
 };
 
 static const ObjCIvarDecl *findPropertyBackingIvar(const ObjCPropertyDecl *PD,
-                                                   ObjCInterfaceDecl *InterD,
-                                            ASTContext &Ctx) {
+                                               const ObjCInterfaceDecl *InterD,
+                                               ASTContext &Ctx) {
   // Check for synthesized ivars.
   ObjCIvarDecl *ID = PD->getPropertyIvarDecl();
   if (ID)
     return ID;
 
+  ObjCInterfaceDecl *NonConstInterD = const_cast<ObjCInterfaceDecl*>(InterD);
+
   // Check for existing "_PropName".
-  ID = InterD->lookupInstanceVariable(PD->getDefaultSynthIvarName(Ctx));
+  ID = NonConstInterD->lookupInstanceVariable(PD->getDefaultSynthIvarName(Ctx));
   if (ID)
     return ID;
 
   // Check for existing "PropName".
   IdentifierInfo *PropIdent = PD->getIdentifier();
-  ID = InterD->lookupInstanceVariable(PropIdent);
+  ID = NonConstInterD->lookupInstanceVariable(PropIdent);
 
   return ID;
 }
@@ -97,9 +99,8 @@ void DirectIvarAssignment::checkASTDecl(const ObjCImplementationDecl *D,
     ObjCPropertyDecl *PD = *I;
 
     // Find the corresponding IVar.
-    const ObjCIvarDecl *ID = findPropertyBackingIvar(PD,
-                               const_cast<ObjCInterfaceDecl*>(InterD),
-                               Mgr.getASTContext());
+    const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterD,
+                                                     Mgr.getASTContext());
 
     if (!ID)
       continue;
@@ -117,15 +118,16 @@ void DirectIvarAssignment::checkASTDecl(const ObjCImplementationDecl *D,
     ObjCMethodDecl *M = *I;
     AnalysisDeclContext *DCtx = Mgr.getAnalysisDeclContext(M);
 
+    // Skip the init, dealloc functions and any functions that might be doing
+    // initialization based on their name.
     if (M->getMethodFamily() == OMF_init ||
         M->getMethodFamily() == OMF_dealloc ||
-        M->getSelector().getAsString().find("init") != StringRef::npos ||
-        M->getSelector().getAsString().find("Init") != StringRef::npos)
+        M->getSelector().getNameForSlot(0).find("init") != StringRef::npos ||
+        M->getSelector().getNameForSlot(0).find("Init") != StringRef::npos)
       continue;
 
     const Stmt *Body = M->getBody();
-    if (!Body)
-      continue;
+    assert(Body);
 
     MethodCrawler MC(IvarToPropMap, M->getCanonicalDecl(), InterD, BR, DCtx);
     MC.VisitStmt(Body);
@@ -137,7 +139,8 @@ void DirectIvarAssignment::MethodCrawler::VisitBinaryOperator(
   if (!BO->isAssignmentOp())
     return;
 
-  const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(BO->getLHS());
+  const ObjCIvarRefExpr *IvarRef =
+          dyn_cast<ObjCIvarRefExpr>(BO->getLHS()->IgnoreParenCasts());
 
   if (!IvarRef)
     return;
@@ -158,16 +161,13 @@ void DirectIvarAssignment::MethodCrawler::VisitBinaryOperator(
       if (GetterMethod && GetterMethod->getCanonicalDecl() == MD)
         return;
 
-
-      PathDiagnosticLocation IvarRefLocation =
-          PathDiagnosticLocation::createBegin(IvarRef,
-              BR.getSourceManager(), DCtx);
-
       BR.EmitBasicReport(MD,
           "Property access",
           categories::CoreFoundationObjectiveC,
           "Direct assignment to an instance variable backing a property; "
-          "use the setter instead", IvarRefLocation);
+          "use the setter instead", PathDiagnosticLocation(IvarRef,
+                                                          BR.getSourceManager(),
+                                                          DCtx));
     }
   }
 }
index a25791ea88613ed1472af5920047532eba13acd1..2311ef2437cd244dacff413b44a8fa6a198a53a3 100644 (file)
@@ -13,7 +13,7 @@
 
   @property (assign, nonatomic) MyClass* Y; // automatically synthesized, implemented
 
-  @property (assign, nonatomic) MyClass* Z; // non synthesized, implemented
+  @property (assign, nonatomic) MyClass* Z; // non synthesized ivar, implemented setter
   @property (readonly) id nonSynth;  // non synthesized, explicitly implemented to return ivar with expected name
   
   - (id) initWithPtr:(MyClass*) value;
@@ -47,7 +47,7 @@
   }
 
   - (void) someMethod: (MyClass*)In {
-    __A = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
+    (__A) = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
     _X = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
     _Y = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
     _Z = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}