]> granicus.if.org Git - clang/commitdiff
Implements gcc's -Wstrict-selector-match.
authorFariborz Jahanian <fjahanian@apple.com>
Mon, 9 Aug 2010 23:27:58 +0000 (23:27 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Mon, 9 Aug 2010 23:27:58 +0000 (23:27 +0000)
(radar 8127244).

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

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/Sema.h
lib/Sema/SemaDeclObjC.cpp
lib/Sema/SemaExprObjC.cpp
test/SemaObjC/warn-strict-selector-match.m [new file with mode: 0644]

index 378a8bfd1295f998fbaafefc8a54989ac8d6190d..6849349a91bcba040f60a59d68d1f2ee1245266d 100644 (file)
@@ -121,7 +121,7 @@ def : DiagGroup<"strict-overflow">;
 
 def InvalidOffsetof : DiagGroup<"invalid-offsetof">;
 def : DiagGroup<"strict-prototypes">;
-def : DiagGroup<"strict-selector-match">;
+def StrictSelector : DiagGroup<"strict-selector-match">;
 def SwitchEnum     : DiagGroup<"switch-enum">;
 def Switch         : DiagGroup<"switch", [SwitchEnum]>;
 def Trigraphs      : DiagGroup<"trigraphs">;
index 3a85f4b8078460cef5344d07acf26113512b69c1..d18f849d85f4627985f61a6320be25ef663b657e 100644 (file)
@@ -330,6 +330,8 @@ def warn_implements_nscopying : Warning<
 "NSCopying protocol is not appropriate with -fobjc-gc[-only]">;
 
 def warn_multiple_method_decl : Warning<"multiple methods named %0 found">;
+def warn_strict_multiple_method_decl : Warning<
+  "multiple methods named %0 found">, InGroup<StrictSelector>, DefaultIgnore;
 def warn_accessor_property_type_mismatch : Warning<
   "type of property %0 does not match type of accessor %1">;
 def note_declared_at : Note<"declared here">;
index d22714adeddd28c5183a50bc302e981c5cf6d4a9..1e5da3d339982a98a6ba5a6fe575794536687123 100644 (file)
@@ -1677,7 +1677,8 @@ public:
   /// true, or false, accordingly.
   bool MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
                                   const ObjCMethodDecl *PrevMethod,
-                                  bool matchBasedOnSizeAndAlignment = false);
+                                  bool matchBasedOnSizeAndAlignment = false,
+                                  bool matchBasedOnStrictEqulity = false);
 
   /// MatchAllMethodDeclarations - Check methods declaraed in interface or
   /// or protocol against those declared in their implementations.
@@ -1698,6 +1699,7 @@ private:
   /// LookupMethodInGlobalPool - Returns the instance or factory method and
   /// optionally warns if there are multiple signatures.
   ObjCMethodDecl *LookupMethodInGlobalPool(Selector Sel, SourceRange R,
+                                           bool receiverIdOrClass,
                                            bool warn, bool instance);
 
 public:
@@ -1717,15 +1719,19 @@ public:
   /// LookupInstanceMethodInGlobalPool - Returns the method and warns if
   /// there are multiple signatures.
   ObjCMethodDecl *LookupInstanceMethodInGlobalPool(Selector Sel, SourceRange R,
+                                                   bool receiverIdOrClass=false,
                                                    bool warn=true) {
-    return LookupMethodInGlobalPool(Sel, R, warn, /*instance*/true);
+    return LookupMethodInGlobalPool(Sel, R, receiverIdOrClass, 
+                                    warn, /*instance*/true);
   }
 
   /// LookupFactoryMethodInGlobalPool - Returns the method and warns if
   /// there are multiple signatures.
   ObjCMethodDecl *LookupFactoryMethodInGlobalPool(Selector Sel, SourceRange R,
+                                                  bool receiverIdOrClass=false,
                                                   bool warn=true) {
-    return LookupMethodInGlobalPool(Sel, R, warn, /*instance*/false);
+    return LookupMethodInGlobalPool(Sel, R, receiverIdOrClass,
+                                    warn, /*instance*/false);
   }
 
   /// LookupImplementedMethodInGlobalPool - Returns the method which has an
index 283376bfde9104c86619717636e1120989caa0f4..160bc19b3f219ee3f5e5a8c794c479a189ebd250 100644 (file)
@@ -1100,13 +1100,14 @@ Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
 /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
 bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
                                       const ObjCMethodDecl *PrevMethod,
-                                      bool matchBasedOnSizeAndAlignment) {
+                                      bool matchBasedOnSizeAndAlignment,
+                                      bool matchBasedOnStrictEqulity) {
   QualType T1 = Context.getCanonicalType(Method->getResultType());
   QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
 
   if (T1 != T2) {
     // The result types are different.
-    if (!matchBasedOnSizeAndAlignment)
+    if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
       return false;
     // Incomplete types don't have a size and alignment.
     if (T1->isIncompleteType() || T2->isIncompleteType())
@@ -1126,7 +1127,7 @@ bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
     T2 = Context.getCanonicalType((*PrevI)->getType());
     if (T1 != T2) {
       // The result types are different.
-      if (!matchBasedOnSizeAndAlignment)
+      if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
         return false;
       // Incomplete types don't have a size and alignment.
       if (T1->isIncompleteType() || T2->isIncompleteType())
@@ -1188,8 +1189,8 @@ void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
   Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
 }
 
-// FIXME: Finish implementing -Wno-strict-selector-match.
 ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
+                                               bool receiverIdOrClass,
                                                bool warn, bool instance) {
   GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
   if (Pos == MethodPool.end()) {
@@ -1201,15 +1202,30 @@ ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
 
   ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
 
+  bool strictSelectorMatch = receiverIdOrClass && warn &&
+    (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl) != 
+      Diagnostic::Ignored);
   if (warn && MethList.Method && MethList.Next) {
     bool issueWarning = false;
-    for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
-      // This checks if the methods differ by size & alignment.
-      if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
-        issueWarning = true;
-    }
+    if (strictSelectorMatch)
+      for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
+        // This checks if the methods differ in type mismatch.
+        if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
+          issueWarning = true;
+      }
+
+    if (!issueWarning)
+      for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
+        // This checks if the methods differ by size & alignment.
+        if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
+          issueWarning = true;
+      }
+
     if (issueWarning) {
-      Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
+      if (strictSelectorMatch)
+        Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
+      else
+        Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
       Diag(MethList.Method->getLocStart(), diag::note_using)
         << MethList.Method->getSourceRange();
       for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
index bf933c3a430bf5e767fc841bacabf82db733c853..df970411e7f52d0f87179ac6f7236f316edb7118 100644 (file)
@@ -153,7 +153,7 @@ Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
                                                    SourceLocation LParenLoc,
                                                    SourceLocation RParenLoc) {
   ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
-                             SourceRange(LParenLoc, RParenLoc), false);
+                             SourceRange(LParenLoc, RParenLoc), false, false);
   if (!Method)
     Method = LookupFactoryMethodInGlobalPool(Sel,
                                           SourceRange(LParenLoc, RParenLoc));
@@ -863,13 +863,17 @@ Sema::OwningExprResult Sema::BuildInstanceMessage(ExprArg ReceiverE,
 
   if (!Method) {
     // Handle messages to id.
-    if (ReceiverType->isObjCIdType() || ReceiverType->isBlockPointerType() ||
+    bool receiverIsId = (ReceiverType->isObjCIdType() || 
+                          ReceiverType->isObjCQualifiedIdType());
+    if (receiverIsId || ReceiverType->isBlockPointerType() ||
         (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
       Method = LookupInstanceMethodInGlobalPool(Sel, 
-                                              SourceRange(LBracLoc, RBracLoc));
+                                                SourceRange(LBracLoc, RBracLoc),
+                                                receiverIsId);
       if (!Method)
         Method = LookupFactoryMethodInGlobalPool(Sel, 
-                                               SourceRange(LBracLoc, RBracLoc));
+                                                 SourceRange(LBracLoc, RBracLoc),
+                                                 receiverIsId);
     } else if (ReceiverType->isObjCClassType() ||
                ReceiverType->isObjCQualifiedClassType()) {
       // Handle messages to Class.
@@ -891,12 +895,14 @@ Sema::OwningExprResult Sema::BuildInstanceMessage(ExprArg ReceiverE,
         // If not messaging 'self', look for any factory method named 'Sel'.
         if (!Receiver || !isSelfExpr(Receiver)) {
           Method = LookupFactoryMethodInGlobalPool(Sel, 
-                                               SourceRange(LBracLoc, RBracLoc));
+                                               SourceRange(LBracLoc, RBracLoc),
+                                                   true);
           if (!Method) {
             // If no class (factory) method was found, check if an _instance_
             // method of the same name exists in the root class only.
             Method = LookupInstanceMethodInGlobalPool(Sel,
-                                               SourceRange(LBracLoc, RBracLoc));
+                                               SourceRange(LBracLoc, RBracLoc),
+                                                      true);
             if (Method)
                 if (const ObjCInterfaceDecl *ID =
                   dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
@@ -951,7 +957,7 @@ Sema::OwningExprResult Sema::BuildInstanceMessage(ExprArg ReceiverE,
             // compatibility. FIXME: should we deviate??
             if (OCIType->qual_empty()) {
               Method = LookupInstanceMethodInGlobalPool(Sel,
-                                                 SourceRange(LBracLoc, RBracLoc));
+                                                 SourceRange(LBracLoc, RBracLoc)); 
               if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
                 Diag(Loc, diag::warn_maynot_respond)
                   << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
diff --git a/test/SemaObjC/warn-strict-selector-match.m b/test/SemaObjC/warn-strict-selector-match.m
new file mode 100644 (file)
index 0000000..8ac0ca4
--- /dev/null
@@ -0,0 +1,75 @@
+// RUN: %clang_cc1  -Wstrict-selector-match -fsyntax-only -verify %s
+
+@interface Foo
+-(int) method; // expected-note {{using}}
+@end
+
+@interface Bar
+-(float) method;       // expected-note {{also found}}
+@end
+
+int main() { [(id)0 method]; } // expected-warning {{multiple methods named 'method' found [-Wstrict-selector-match]}}
+
+@interface Object @end
+
+@interface Class1
+- (void)setWindow:(Object *)wdw;       // expected-note {{using}}
+@end
+
+@interface Class2
+- (void)setWindow:(Class1 *)window;    // expected-note {{also found}}
+@end
+
+id foo(void) {
+  Object *obj = 0;
+  id obj2 = obj;
+  [obj setWindow:0];   // expected-warning {{Object' may not respond to 'setWindow:'}}
+  [obj2 setWindow:0]; // expected-warning {{multiple methods named 'setWindow:' found [-Wstrict-selector-match]}}
+  return obj;
+}
+
+@protocol MyObject
+- (id)initWithData:(Object *)data;     // expected-note {{using}} \
+                                       // expected-note {{passing argument to parameter 'data' here}}
+@end
+
+@protocol SomeOther
+- (id)initWithData:(int)data;  // expected-note {{also found}}
+@end
+
+@protocol MyCoding
+- (id)initWithData:(id<MyObject, MyCoding>)data;       // expected-note {{also found}}
+@end
+
+@interface NTGridDataObject: Object <MyCoding>
+{
+    Object<MyCoding> *_data;
+}
++ (NTGridDataObject*)dataObject:(id<MyObject, MyCoding>)data;
+@end
+
+@implementation NTGridDataObject
+- (id)initWithData:(id<MyObject, MyCoding>)data {
+  return data;
+}
++ (NTGridDataObject*)dataObject:(id<MyObject, MyCoding>)data
+{
+    NTGridDataObject *result = [(id)0 initWithData:data]; // expected-warning {{multiple methods named 'initWithData:' found [-Wstrict-selector-match]}} \
+    expected-warning {{sending 'id<MyObject,MyCoding>' to parameter of incompatible type 'Object *'}}
+    return result;
+}
+@end
+
+@interface Base
+- (unsigned)port;
+@end
+
+@interface Derived: Base
+- (Object *)port;
++ (Protocol *)port;
+@end
+
+void foo1(void) {
+  [(Class)0 port]; // OK - gcc issues warning but there is only one Class method so no ambiguity to warn
+}
+