]> granicus.if.org Git - clang/commitdiff
Compare matching selectors in current and
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 4 Aug 2009 01:07:16 +0000 (01:07 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 4 Aug 2009 01:07:16 +0000 (01:07 +0000)
super class(s) and warn on any parameter
type mismatch if potentially unsafe.

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/Sema.h
lib/Sema/SemaDeclObjC.cpp
test/SemaObjC/warn-superclass-method-mismatch.m [new file with mode: 0644]

index 5a7fbf4ff5712f6e0689fe286434db3f27ad8a76..cdb79b4dc4ac73aac4e07e322ce410ec9bd72c8b 100644 (file)
@@ -2060,6 +2060,7 @@ def error_protected_ivar_access : Error<"instance variable %0 is protected">,
 def warn_maynot_respond : Warning<"%0  may not respond to %1">;
 def warn_attribute_method_def : Warning<
   "method attribute can only be specified on method declarations">;
-
-
+def ext_typecheck_base_super : ExtWarn<
+  "method parameter type %0 does not match "
+  "super class method parameter type %1">;
 }
index 2a77f21a2d4726ffb1cfd9032a9f1e76587c27ad..6cad4b2f1442dbef92152914170e935db5bb54b0 100644 (file)
@@ -2872,6 +2872,10 @@ public:
                                 const IdentifierInfo *Name);
   void ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl);
   
+  void CompareMethodParamsInBaseAndSuper(Decl *IDecl, 
+                                         ObjCMethodDecl *MethodDecl,
+                                         bool IsInstance);
+  
   void MergeProtocolPropertiesIntoClass(Decl *CDecl,
                                         DeclPtrTy MergeProtocols);
   
index 26f590578c82d77c9d8566dc89492b3c7eb81c07..0ee0ad730f38baa617595fcf47d73f138b651170 100644 (file)
@@ -1464,6 +1464,37 @@ void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
     AddInstanceMethodToGlobalPool(SetterMethod);     
 }
 
+void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
+                                             ObjCMethodDecl *Method,
+                                             bool IsInstance)  {
+  if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
+    while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
+      if (ObjCMethodDecl *SuperMethodDecl = 
+          SD->lookupMethod(Method->getSelector(), IsInstance)) {
+        ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
+        E = Method->param_end();
+        ObjCMethodDecl::param_iterator PrevI = 
+        SuperMethodDecl->param_begin();
+        for (; ParamI != E; ++ParamI, ++PrevI) {
+          assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
+          QualType T1 = Context.getCanonicalType((*ParamI)->getType());
+          QualType T2 = Context.getCanonicalType((*PrevI)->getType());
+          if (T1 != T2) {
+            AssignConvertType ConvTy = CheckAssignmentConstraints(T1, T2);
+            if (ConvTy == Incompatible || ConvTy == IncompatiblePointer) {
+              Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) 
+                << T1 << T2;
+              Diag(SuperMethodDecl->getLocation(), 
+                   diag::note_previous_declaration);
+              return;
+            }
+          }
+        }
+      }
+      ID = SD;
+    }
+}
+
 // Note: For class/category implemenations, allMethods/allProperties is
 // always null.
 void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
@@ -1509,6 +1540,7 @@ void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
         InsMap[Method->getSelector()] = Method;
         /// The following allows us to typecheck messages to "id".
         AddInstanceMethodToGlobalPool(Method);
+        CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
       }
     }
     else {
@@ -1526,6 +1558,7 @@ void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
         ClsMap[Method->getSelector()] = Method;
         /// The following allows us to typecheck messages to "Class".
         AddFactoryMethodToGlobalPool(Method);
+        CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
       }
     }
   }
diff --git a/test/SemaObjC/warn-superclass-method-mismatch.m b/test/SemaObjC/warn-superclass-method-mismatch.m
new file mode 100644 (file)
index 0000000..eb75ead
--- /dev/null
@@ -0,0 +1,46 @@
+// RUN: clang-cc  -fsyntax-only -verify %s
+
+@interface Root
+-(void) method_r: (char)ch : (float*)f1 : (int*) x; // expected-note {{previous declaration is here}}
+@end
+
+@class Sub;
+
+@interface Base : Root
+-(void) method: (int*) x; // expected-note {{previous declaration is here}}
+-(void) method1: (Base*) x; // expected-note {{previous declaration is here}}
+-(void) method2: (Sub*) x;
++ method3: (int)x1 : (Base *)x2 : (float)x3; // expected-note {{previous declaration is here}}
++ mathod4: (id)x1;
+@end
+
+struct A {
+  int x,y,z;
+};
+
+@interface Sub : Base
+-(void) method: (struct A*) a; // expected-warning {{method parameter type 'struct A *' does not match super class method parameter type 'int *'}}
+-(void) method1: (Sub*) x;     // expected-warning {{method parameter type 'Sub *' does not match super class method parameter type 'Base *'}}
+-(void) method2: (Base*) x;    // no need to warn. At call point we warn if need be.
++ method3: (int)x1 : (Sub *)x2 : (float)x3;    // expected-warning {{method parameter type 'Sub *' does not match super class method parameter type 'Base *'}}
++ mathod4: (Base*)x1;
+-(void) method_r: (char)ch : (float*)f1 : (Sub*) x; // expected-warning {{method parameter type 'Sub *' does not match super class method parameter type 'int *'}}
+@end
+
+void f(Base *base, Sub *sub) {
+  int x;
+  [base method:&x];  // warn. if base is actually 'Sub' it will use -[Sub method] with wrong arguments
+  
+  Base *b;
+  [base method1:b]; // if base is actuall 'Sub'  it will use [Sub method1] with wrong argument.
+
+  [base method2:b];  // expected-warning {{}}
+
+  Sub *s;
+  [base method2:s]; // if base is actually 'Sub' OK. Either way OK.
+  
+}
+
+
+
+