]> granicus.if.org Git - clang/commitdiff
(Next runtime only) check to see if class implements forwardInvocation method
authorFariborz Jahanian <fjahanian@apple.com>
Fri, 22 May 2009 17:12:32 +0000 (17:12 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Fri, 22 May 2009 17:12:32 +0000 (17:12 +0000)
and objects of this class are derived from 'NSProxy'.
Under such conditions, which means that every method possible is
implemented in the class, we should not issue "Method definition not found"
warnings.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72267 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclObjC.h
lib/AST/DeclObjC.cpp
lib/Sema/SemaDeclObjC.cpp
test/SemaObjC/no-warn-unimpl-method.m [new file with mode: 0644]

index e8c554b67bcdb68741afadaed9aa11c15dd0ff4d..6e89a7ae7bc9f3b6354a01881b6e0039a5ff3269 100644 (file)
@@ -486,6 +486,7 @@ public:
   // found, we search referenced protocols and class categories.
   ObjCMethodDecl *lookupInstanceMethod(ASTContext &Context, Selector Sel);
   ObjCMethodDecl *lookupClassMethod(ASTContext &Context, Selector Sel);
+  ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
 
   // Location information, modeled after the Stmt API. 
   SourceLocation getLocStart() const { return getLocation(); } // '@'interface
index bcd2e08f6d6b48176e98d70fb8bf4d432f7ea4f1..f4bb8957307499e5d03cfec31c8a26e877c03d31 100644 (file)
@@ -161,6 +161,20 @@ ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
   return NULL;
 }
 
+/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
+/// class whose name is passed as argument. If it is not one of the super classes
+/// the it returns NULL.
+ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
+                                        const IdentifierInfo*ICName) {
+  ObjCInterfaceDecl* ClassDecl = this;
+  while (ClassDecl != NULL) {
+    if (ClassDecl->getIdentifier() == ICName)
+      return ClassDecl;
+    ClassDecl = ClassDecl->getSuperClass();
+  }
+  return NULL;
+}
+
 /// lookupInstanceMethod - This method returns an instance method by looking in
 /// the class, its categories, and its super classes (using a linear search).
 ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(ASTContext &Context,
index 35faa4a5eafc44c23c608b122a89db65e3ff9da4..8f580341bdc699a4dc2891b134421007a8452352 100644 (file)
@@ -866,29 +866,46 @@ void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
                                    const llvm::DenseSet<Selector> &ClsMap,
                                    ObjCInterfaceDecl *IDecl) {
   ObjCInterfaceDecl *Super = IDecl->getSuperClass();
-
+  ObjCInterfaceDecl *NSIDecl = 0;
+  if (getLangOptions().NeXTRuntime) {
+    // check to see if class implements forwardInvocation method and objects 
+    // of this class are derived from 'NSProxy' so that to forward requests 
+    // from one object to another.
+    // Under such conditions, which means that every method possible is 
+    // implemented in the class, we should not issue "Method definition not 
+    // found" warnings.
+    // FIXME: Use a general GetUnarySelector method for this.
+    IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
+    Selector fISelector = Context.Selectors.getSelector(1, &II);
+    if (InsMap.count(fISelector))
+      // Is IDecl derived from 'NSProxy'? If so, no instance methods
+      // need be implemented in the implementation.
+      NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
+  }
+  
   // If a method lookup fails locally we still need to look and see if
   // the method was implemented by a base class or an inherited
   // protocol. This lookup is slow, but occurs rarely in correct code
   // and otherwise would terminate in a warning.
 
   // check unimplemented instance methods.
-  for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context), 
-       E = PDecl->instmeth_end(Context); I != E; ++I) {
-    ObjCMethodDecl *method = *I;
-    if (method->getImplementationControl() != ObjCMethodDecl::Optional && 
-        !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
-        (!Super || 
-         !Super->lookupInstanceMethod(Context, method->getSelector()))) {
-        // Ugly, but necessary. Method declared in protcol might have
-        // have been synthesized due to a property declared in the class which
-        // uses the protocol.
-        ObjCMethodDecl *MethodInClass = 
-          IDecl->lookupInstanceMethod(Context, method->getSelector());
-        if (!MethodInClass || !MethodInClass->isSynthesized())
-          WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
-      }
-  }
+  if (!NSIDecl)
+    for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context), 
+         E = PDecl->instmeth_end(Context); I != E; ++I) {
+      ObjCMethodDecl *method = *I;
+      if (method->getImplementationControl() != ObjCMethodDecl::Optional && 
+          !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
+          (!Super || 
+           !Super->lookupInstanceMethod(Context, method->getSelector()))) {
+            // Ugly, but necessary. Method declared in protcol might have
+            // have been synthesized due to a property declared in the class which
+            // uses the protocol.
+            ObjCMethodDecl *MethodInClass = 
+            IDecl->lookupInstanceMethod(Context, method->getSelector());
+            if (!MethodInClass || !MethodInClass->isSynthesized())
+              WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
+          }
+    }
   // check unimplemented class methods
   for (ObjCProtocolDecl::classmeth_iterator 
          I = PDecl->classmeth_begin(Context),
diff --git a/test/SemaObjC/no-warn-unimpl-method.m b/test/SemaObjC/no-warn-unimpl-method.m
new file mode 100644 (file)
index 0000000..756c47b
--- /dev/null
@@ -0,0 +1,42 @@
+// RUN: clang-cc -triple x86_64-apple-darwin10 -fsyntax-only -verify %s
+// This program tests that if class implements the forwardInvocation method, then
+// every method possible is implemented in the class and should not issue
+// warning of the "Method definition not found" kind. */
+
+@interface NSObject
+@end
+
+@interface NSInvocation
+@end
+
+@interface NSProxy
+@end
+
+@protocol MyProtocol
+        -(void) doSomething;
+@end
+
+@interface DestinationClass : NSObject<MyProtocol>
+        -(void) doSomething;
+@end
+
+@implementation DestinationClass
+        -(void) doSomething
+        {
+        }
+@end
+
+@interface MyProxy : NSProxy<MyProtocol>
+{
+        DestinationClass        *mTarget;
+}
+        - (id) init;
+        - (void)forwardInvocation:(NSInvocation *)anInvocation;
+@end
+
+@implementation MyProxy
+        - (void)forwardInvocation:(NSInvocation *)anInvocation
+        {
+        }
+       - (id) init {}        
+@end