]> granicus.if.org Git - clang/commitdiff
[analyzer] Do the self-init check only on NSObject subclasses. Patch by Jean-Daniel...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 25 Jan 2011 23:54:44 +0000 (23:54 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 25 Jan 2011 23:54:44 +0000 (23:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124249 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
test/Analysis/self-init.m

index c887ac86ef50043452bf127e60b8ded1f72595de..b62d2309fce75f4220954d7f7830ce10aec60d77 100644 (file)
@@ -270,11 +270,23 @@ static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
   const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
   if (!MD)
     return false;
-  if (!MD->getClassInterface()->getSuperClass())
-    return false;
   if (!isInitializationMethod(MD))
     return false;
 
+  // self = [super init] applies only to NSObject subclasses.
+  // For instance, NSProxy doesn't implement -init.
+  ASTContext& Ctx = MD->getASTContext();
+  IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
+  ObjCInterfaceDecl* ID = MD->getClassInterface()->getSuperClass();
+  for ( ; ID ; ID = ID->getSuperClass()) {
+    IdentifierInfo *II = ID->getIdentifier();
+
+    if (II == NSObjectII)
+      break;
+  }
+  if (!ID)
+    return false;
+
   return true;
 }
 
index 8b2b82fabae1149fb1186df78be69b29c1f310ae..9c30d26be0bbd4cd430b2c0a44deec42ada970b7 100644 (file)
@@ -17,6 +17,9 @@
 -(id)init;
 -(id)release;
 @end
+@interface NSProxy <NSObject> {}
+@end
+
 //#import "Foundation/NSObject.h"
 typedef unsigned NSUInteger;
 typedef int NSInteger;
@@ -48,6 +51,10 @@ extern void *somePtr;
 -(void)doSomething;
 @end
 
+@interface MyProxyObj : NSProxy {}
+-(id)init;
+@end
+
 @implementation MyObj
 
 -(id)init {
@@ -137,3 +144,9 @@ extern void *somePtr;
 -(void)doSomething {}
 
 @end
+
+@implementation MyProxyObj
+
+- (id)init { return self; }
+
+@end