]> granicus.if.org Git - clang/commitdiff
Contains the following (related to problems found while investigting <rdar://problem...
authorSteve Naroff <snaroff@apple.com>
Sun, 22 Feb 2009 19:35:57 +0000 (19:35 +0000)
committerSteve Naroff <snaroff@apple.com>
Sun, 22 Feb 2009 19:35:57 +0000 (19:35 +0000)
- Implement instance/class overloading in ObjCContainerDecl (removing a FIXME). This involved hacking NamedDecl::declarationReplaces(), which took awhile to figure out (didn't realize replace was the default).
- Changed Sema::ActOnInstanceMessage() to remove redundant warnings when dealing with protocols. For now, I've omitted the "protocol" term in the diagnostic. It simplifies the code flow and wan't always 100% accurate (e.g. "Foo<Prot>" looks in the class interface, not just the protocol).
- Changed several test cases to jive with the above changes.

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

include/clang/Basic/DiagnosticSemaKinds.def
lib/AST/Decl.cpp
lib/AST/DeclObjC.cpp
lib/Sema/SemaExprObjC.cpp
test/SemaObjC/property-11.m
test/SemaObjC/protocol-id-test-1.m
test/SemaObjC/protocol-id-test-2.m

index fb9f36399baf1c088730eb028ea5f58c64e47f07..2549df6b93b403ee873704ccb1b1abcab68c2c26 100644 (file)
@@ -916,8 +916,6 @@ DIAG(error_no_super_class, ERROR,
      "no super class declared in @interface for %0")
 DIAG(err_invalid_receiver_to_message, ERROR,
      "invalid receiver to message expression")
-DIAG(warn_method_not_found_in_protocol, WARNING,
-     "method %objcinstance0 not found in protocol (return type defaults to 'id')")
 DIAG(error_bad_receiver_type, ERROR,
      "bad receiver type %0")
 DIAG(error_objc_throw_expects_object, ERROR,
index 387f79a013f38f8d892ef1dfe519e9ad8284a8ec..f7608529d4838d3a9c273d1ed0b22632af85c472 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclCXX.h"
+#include "clang/AST/DeclObjC.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Stmt.h"
 #include "clang/AST/Expr.h"
@@ -194,6 +195,10 @@ bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
     // For function declarations, we keep track of redeclarations.
     return FD->getPreviousDeclaration() == OldD;
 
+  // For method declarations, we keep track of redeclarations.
+  if (isa<ObjCMethodDecl>(this))
+    return false;
+    
   // For non-function declarations, if the declarations are of the
   // same kind then this must be a redeclaration, or semantic analysis
   // would not have given us the new declaration.
index 461bb9697fe93fd2e5ddc37a7a4d170b8bd82396..a9f7922ed04921b19f2073d54d9f7457a495b195 100644 (file)
@@ -14,6 +14,7 @@
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Stmt.h"
+#include "llvm/ADT/STLExtras.h"
 using namespace clang;
 
 //===----------------------------------------------------------------------===//
@@ -42,21 +43,42 @@ void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
 //===----------------------------------------------------------------------===//
 
 // Get the local instance method declared in this interface.
-// FIXME: handle overloading, instance & class methods can have the same name.
 ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
-  lookup_const_result MethodResult = lookup(Sel);
-  if (MethodResult.first)
-    return const_cast<ObjCMethodDecl*>(
-                        dyn_cast<ObjCMethodDecl>(*MethodResult.first));
+  // Since instance & class methods can have the same name, the loop below
+  // ensures we get the correct method.
+  //
+  // @interface Whatever
+  // - (int) class_method;
+  // + (float) class_method;
+  // @end
+  //
+  lookup_const_iterator Meth, MethEnd;
+  for (llvm::tie(Meth, MethEnd) = lookup(Sel);
+       Meth != MethEnd; ++Meth) {
+    ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
+    if (MD && MD->isInstanceMethod())
+      return MD;
+  }
   return 0;
 }
 
 // Get the local class method declared in this interface.
 ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
-  lookup_const_result MethodResult = lookup(Sel);
-  if (MethodResult.first)
-    return const_cast<ObjCMethodDecl*>(
-                           dyn_cast<ObjCMethodDecl>(*MethodResult.first));
+  // Since instance & class methods can have the same name, the loop below
+  // ensures we get the correct method.
+  //
+  // @interface Whatever
+  // - (int) class_method;
+  // + (float) class_method;
+  // @end
+  //
+  lookup_const_iterator Meth, MethEnd;
+  for (llvm::tie(Meth, MethEnd) = lookup(Sel);
+       Meth != MethEnd; ++Meth) {
+    ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
+    if (MD && MD->isClassMethod())
+      return MD;
+  }
   return 0;
 }
 
index e5fefd3e3a638cf9b5d4b59b631534003644681e..d8f3fd4cf74dfc088a5d999bf0513f0eaf1ef407 100644 (file)
@@ -397,9 +397,6 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
       if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
         break;
     }
-    if (!Method)
-      Diag(lbrac, diag::warn_method_not_found_in_protocol)
-        << Sel << RExpr->getSourceRange();
   // Check for GCC extension "Class<foo>".
   } else if (ObjCQualifiedClassType *QIT = 
                dyn_cast<ObjCQualifiedClassType>(ReceiverCType)) {
@@ -409,9 +406,6 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
       if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
         break;
     }
-    if (!Method)
-      Diag(lbrac, diag::warn_method_not_found_in_protocol)
-        << Sel << RExpr->getSourceRange();
   } else if (const ObjCInterfaceType *OCIReceiver = 
                 ReceiverCType->getAsPointerToObjCInterfaceType()) {
     // We allow sending a message to a pointer to an interface (an object).
@@ -422,19 +416,29 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
     // The idea is to add class info to InstanceMethodPool.
     Method = ClassDecl->lookupInstanceMethod(Sel);
     
+    bool haveQualifiers = false;
     if (!Method) {
       // Search protocol qualifiers.
       for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
            E = OCIReceiver->qual_end(); QI != E; ++QI) {
+        haveQualifiers = true;
         if ((Method = (*QI)->lookupInstanceMethod(Sel)))
           break;
       }
     }
-    
-    if (!Method && !OCIReceiver->qual_empty())
-      Diag(lbrac, diag::warn_method_not_found_in_protocol)
-        << Sel << SourceRange(lbrac, rbrac);
-    
+    if (!Method) {
+      // If we have an implementation in scope, check "private" methods.
+      if (ClassDecl)
+        if (ObjCImplementationDecl *ImpDecl = 
+              ObjCImplementations[ClassDecl->getIdentifier()])
+          Method = ImpDecl->getInstanceMethod(Sel);
+          // If we still haven't found a method, look in the global pool. This
+          // behavior isn't very desirable, however we need it for GCC
+          // compatibility. FIXME: should we deviate??
+          if (!Method && !haveQualifiers)
+            Method = LookupInstanceMethodInGlobalPool(
+                                 Sel, SourceRange(lbrac,rbrac));
+    }
     if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
       return true;
   } else {
@@ -443,19 +447,6 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
     return true;
   }
   
-  if (!Method) {
-    // If we have an implementation in scope, check "private" methods.
-    if (ClassDecl)
-      if (ObjCImplementationDecl *ImpDecl = 
-            ObjCImplementations[ClassDecl->getIdentifier()])
-        Method = ImpDecl->getInstanceMethod(Sel);
-        // If we still haven't found a method, look in the global pool. This
-        // behavior isn't very desirable, however we need it for GCC
-        // compatibility.
-        if (!Method)
-          Method = LookupInstanceMethodInGlobalPool(
-                               Sel, SourceRange(lbrac,rbrac));
-  }
   if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
                                 lbrac, rbrac, returnType))
     return true;
index 803832800256d7ef0e828f73dd8b6642aae12b13..d28608aecfb1e5ce1a4fbcfff77e5368ff7a9e30 100644 (file)
   NSSound *x;
   id o;
 
-  o = [x foo]; 
+  // GCC does *not* warn about the following. Since foo/setFoo: are not in the
+  // class or category interface for NSSound, the compiler shouldn't find them.
+  // For now, we will support GCC's behavior (sigh).
+  o = [x foo];
   o = x.foo;
   [x setFoo:o];
   x.foo = o;
index 765500e10d8130056b50d69e35a08da9e4e75ebf..4e636d7e1c77893c2e9029bd825f403789ed1e55 100644 (file)
@@ -12,5 +12,5 @@
 @end
 
 @implementation INTF
-- (void)IMeth {INTF<P> *pi;  [pi Meth]; } // expected-warning {{method '-Meth' not found in protocol (return type defaults to 'id')}}
+- (void)IMeth {INTF<P> *pi;  [pi Meth]; } // expected-warning {{method '-Meth' not found (return type defaults to 'id')}}
 @end
index 525d2cca9e9d148d7829b81a126978a0c00af480..3b4adcfb5b6f176da29d7e8949c08659326dd8d4 100644 (file)
@@ -5,10 +5,8 @@
 
 @interface INTF<P>
 - (void)IMeth;
- - (void) Meth;
 @end
 
 @implementation INTF
-- (void)IMeth { [(id<P>)self Meth]; }  // expected-warning {{method '-Meth' not found in protocol (return type defaults to 'id')}}
-- (void) Meth {}
+- (void)IMeth { [(id<P>)self Meth]; }  // expected-warning {{method '-Meth' not found (return type defaults to 'id')}}
 @end