[cfe-commits] r65532 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaExprObjC.cpp test/SemaObjC/category-method-lookup-2.m

Steve Naroff snaroff at apple.com
Thu Feb 26 07:55:07 PST 2009


Author: snaroff
Date: Thu Feb 26 09:55:06 2009
New Revision: 65532

URL: http://llvm.org/viewvc/llvm-project?rev=65532&view=rev
Log:
Fix http://llvm.org/bugs/show_bug.cgi?id=3544.

The code for looking up local/private method in Sema::ActOnInstanceMessage() was not handling categories properly. Sema::ActOnClassMessage() didn't have this bug.
Created a helper with the correct logic and changed both methods to use it.


Added:
    cfe/trunk/test/SemaObjC/category-method-lookup-2.m
Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExprObjC.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=65532&r1=65531&r2=65532&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu Feb 26 09:55:06 2009
@@ -1683,6 +1683,11 @@
     AttributeList *AttrList, tok::ObjCKeywordKind MethodImplKind,
     bool isVariadic = false);
 
+  // Helper method for ActOnClassMethod/ActOnInstanceMethod.
+  // Will search "local" class/category implementations for a method decl.
+  // Returns 0 if no method is found.
+  ObjCMethodDecl *LookupPrivateMethod(Selector Sel, ObjCInterfaceDecl *CDecl);
+  
   // ActOnClassMessage - used for both unary and keyword messages.
   // ArgExprs is optional - if it is present, the number of expressions
   // is obtained from NumArgs.

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=65532&r1=65531&r2=65532&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Thu Feb 26 09:55:06 2009
@@ -202,6 +202,27 @@
   return anyIncompatibleArgs;
 }
 
+// Helper method for ActOnClassMethod/ActOnInstanceMethod.
+// Will search "local" class/category implementations for a method decl.
+// Returns 0 if no method is found.
+ObjCMethodDecl *Sema::LookupPrivateMethod(Selector Sel,
+                                          ObjCInterfaceDecl *ClassDecl) {
+  ObjCMethodDecl *Method = 0;
+  
+  if (ObjCImplementationDecl *ImpDecl = 
+      ObjCImplementations[ClassDecl->getIdentifier()])
+    Method = ImpDecl->getClassMethod(Sel);
+    
+  // Look through local category implementations associated with the class.
+  if (!Method) {
+    for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
+      if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
+        Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
+    }
+  }
+  return Method;
+}
+
 // ActOnClassMessage - used for both unary and keyword messages.
 // ArgExprs is optional - if it is present, the number of expressions
 // is obtained from Sel.getNumArgs().
@@ -282,19 +303,9 @@
   Method = ClassDecl->lookupClassMethod(Sel);
   
   // If we have an implementation in scope, check "private" methods.
-  if (!Method) {
-    if (ObjCImplementationDecl *ImpDecl = 
-        ObjCImplementations[ClassDecl->getIdentifier()])
-      Method = ImpDecl->getClassMethod(Sel);
-      
-    // Look through local category implementations associated with the class.
-    if (!Method) {
-      for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
-        if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
-          Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
-      }
-    }
-  }
+  if (!Method)
+    Method = LookupPrivateMethod(Sel, ClassDecl);
+
   // Before we give up, check if the selector is an instance method.
   if (!Method)
     Method = ClassDecl->lookupInstanceMethod(Sel);
@@ -379,12 +390,8 @@
         // First check the public methods in the class interface.
         Method = ClassDecl->lookupClassMethod(Sel);
         
-        if (!Method) {
-          // If we have an implementation in scope, check "private" methods.
-          if (ObjCImplementationDecl *ImpDecl = 
-                ObjCImplementations[ClassDecl->getIdentifier()])
-            Method = ImpDecl->getClassMethod(Sel);
-        }
+        if (!Method)
+          Method = LookupPrivateMethod(Sel, ClassDecl);
       }
       if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
         return true;

Added: cfe/trunk/test/SemaObjC/category-method-lookup-2.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/category-method-lookup-2.m?rev=65532&view=auto

==============================================================================
--- cfe/trunk/test/SemaObjC/category-method-lookup-2.m (added)
+++ cfe/trunk/test/SemaObjC/category-method-lookup-2.m Thu Feb 26 09:55:06 2009
@@ -0,0 +1,22 @@
+// RUN: clang -fsyntax-only -verify %s
+
+typedef struct objc_class *Class;
+ at interface NSObject
+- (Class)class;
+ at end
+ at interface Bar : NSObject
+ at end
+ at interface Bar (Cat)
+ at end
+
+// NOTE: No class implementation for Bar precedes this category definition.
+ at implementation Bar (Cat)
+
+// private method.
++ classMethod { return self; }
+
+- instanceMethod {
+  [[self class] classMethod];
+}
+
+ at end
\ No newline at end of file





More information about the cfe-commits mailing list