aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFariborz Jahanian <fjahanian@apple.com>2009-08-04 01:07:16 +0000
committerFariborz Jahanian <fjahanian@apple.com>2009-08-04 01:07:16 +0000
commitdbdec8bcb5686cd34763db49488f77e88c43eb64 (patch)
tree3a851e2cd1bd7315b04ab517f689c3ebe9a606f0
parent8eec7c00e6e8e7243776d89c3897a48d354aecbf (diff)
Compare matching selectors in current and
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
-rw-r--r--include/clang/Basic/DiagnosticSemaKinds.td5
-rw-r--r--lib/Sema/Sema.h4
-rw-r--r--lib/Sema/SemaDeclObjC.cpp33
-rw-r--r--test/SemaObjC/warn-superclass-method-mismatch.m46
4 files changed, 86 insertions, 2 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 5a7fbf4ff5..cdb79b4dc4 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -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">;
}
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 2a77f21a2d..6cad4b2f14 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -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);
diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp
index 26f590578c..0ee0ad730f 100644
--- a/lib/Sema/SemaDeclObjC.cpp
+++ b/lib/Sema/SemaDeclObjC.cpp
@@ -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
index 0000000000..eb75eade0a
--- /dev/null
+++ b/test/SemaObjC/warn-superclass-method-mismatch.m
@@ -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.
+
+}
+
+
+
+