[cfe-commits] r78029 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/Sema.h lib/Sema/SemaDeclObjC.cpp test/SemaObjC/warn-superclass-method-mismatch.m

Fariborz Jahanian fjahanian at apple.com
Mon Aug 3 18:07:16 PDT 2009


Author: fjahanian
Date: Mon Aug  3 20:07:16 2009
New Revision: 78029

URL: http://llvm.org/viewvc/llvm-project?rev=78029&view=rev
Log:
Compare matching selectors in current and
super class(s) and warn on any parameter
type mismatch if potentially unsafe. 

Added:
    cfe/trunk/test/SemaObjC/warn-superclass-method-mismatch.m
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDeclObjC.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=78029&r1=78028&r2=78029&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Aug  3 20:07:16 2009
@@ -2060,6 +2060,7 @@
 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">;
 }

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

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Mon Aug  3 20:07:16 2009
@@ -2872,6 +2872,10 @@
                                 const IdentifierInfo *Name);
   void ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl);
   
+  void CompareMethodParamsInBaseAndSuper(Decl *IDecl, 
+                                         ObjCMethodDecl *MethodDecl,
+                                         bool IsInstance);
+  
   void MergeProtocolPropertiesIntoClass(Decl *CDecl,
                                         DeclPtrTy MergeProtocols);
   

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Mon Aug  3 20:07:16 2009
@@ -1464,6 +1464,37 @@
     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 @@
         InsMap[Method->getSelector()] = Method;
         /// The following allows us to typecheck messages to "id".
         AddInstanceMethodToGlobalPool(Method);
+        CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
       }
     }
     else {
@@ -1526,6 +1558,7 @@
         ClsMap[Method->getSelector()] = Method;
         /// The following allows us to typecheck messages to "Class".
         AddFactoryMethodToGlobalPool(Method);
+        CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
       }
     }
   }

Added: cfe/trunk/test/SemaObjC/warn-superclass-method-mismatch.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/warn-superclass-method-mismatch.m?rev=78029&view=auto

==============================================================================
--- cfe/trunk/test/SemaObjC/warn-superclass-method-mismatch.m (added)
+++ cfe/trunk/test/SemaObjC/warn-superclass-method-mismatch.m Mon Aug  3 20:07:16 2009
@@ -0,0 +1,46 @@
+// RUN: clang-cc  -fsyntax-only -verify %s
+
+ at interface Root
+-(void) method_r: (char)ch : (float*)f1 : (int*) x; // expected-note {{previous declaration is here}}
+ at end
+
+ at class Sub;
+
+ at 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;
+ at end
+
+struct A {
+  int x,y,z;
+};
+
+ at 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 *'}}
+ at 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.
+  
+}
+
+
+
+





More information about the cfe-commits mailing list