From 0c73f37f0a48a1512bc0477a71f0d6cffcb78fc0 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 9 Mar 2009 21:19:16 +0000 Subject: [PATCH] Fix PR3766, a really nasty silent miscompilation case where we emitted a warning and then threw away the AST. While I'm in there, tighten up the code to actually reject completely bogus cases (sending a message to a struct). We still allow sending a message to an int, which doesn't make sense but GCC allows it and is easy to support. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66468 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.def | 2 ++ lib/Sema/SemaExprObjC.cpp | 13 +++++++++++-- test/SemaObjC/bad-receiver-1.m | 6 ++++-- test/SemaObjC/message.m | 13 +++++++++++++ test/SemaObjC/super.m | 3 ++- 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.def b/include/clang/Basic/DiagnosticSemaKinds.def index 56fe9d2a38..17eb0270f1 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.def +++ b/include/clang/Basic/DiagnosticSemaKinds.def @@ -969,6 +969,8 @@ DIAG(err_invalid_receiver_to_message, ERROR, "invalid receiver to message expression") DIAG(warn_bad_receiver_type, WARNING, "bad receiver type %0") +DIAG(err_bad_receiver_type, ERROR, + "bad receiver type %0") DIAG(error_objc_throw_expects_object, ERROR, "invalid %0 argument (expected an ObjC object type)") DIAG(error_rethrow_used_outside_catch, ERROR, diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index 512a72f9b4..98fbd96331 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -489,7 +489,7 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, // Handle messages to id. if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) || - ReceiverCType->getAsBlockPointerType()) { + ReceiverCType->isBlockPointerType()) { ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool( Sel, SourceRange(lbrac,rbrac)); if (!Method) @@ -582,9 +582,18 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, } if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) return true; - } else { + } else if (!Context.getObjCIdType().isNull() && + (ReceiverCType->isPointerType() || + (ReceiverCType->isIntegerType() && + ReceiverCType->isScalarType()))) { + // Implicitly convert integers and pointers to 'id' but emit a warning. Diag(lbrac, diag::warn_bad_receiver_type) << RExpr->getType() << RExpr->getSourceRange(); + ImpCastExprToType(RExpr, Context.getObjCIdType()); + } else { + // Reject other random receiver types (e.g. structs). + Diag(lbrac, diag::err_bad_receiver_type) + << RExpr->getType() << RExpr->getSourceRange(); return true; } diff --git a/test/SemaObjC/bad-receiver-1.m b/test/SemaObjC/bad-receiver-1.m index c9d2a2a651..a7377544b8 100644 --- a/test/SemaObjC/bad-receiver-1.m +++ b/test/SemaObjC/bad-receiver-1.m @@ -5,7 +5,8 @@ @end void __raiseExc1() { - [objc_lookUpClass("NSString") retain]; // expected-warning {{ "bad receiver type 'int'" }} + [objc_lookUpClass("NSString") retain]; // expected-warning {{ "bad receiver type 'int'" }} \ + expected-warning {{method '-retain' not found}} } typedef const struct __CFString * CFStringRef; @@ -13,5 +14,6 @@ typedef const struct __CFString * CFStringRef; void func() { CFStringRef obj; - [obj self]; // expected-warning {{bad receiver type 'CFStringRef' (aka 'struct __CFString const *')}} + [obj self]; // expected-warning {{bad receiver type 'CFStringRef' (aka 'struct __CFString const *')}} \\ + expected-warning {{method '-self' not found}} } diff --git a/test/SemaObjC/message.m b/test/SemaObjC/message.m index f961e35870..a232f5c512 100644 --- a/test/SemaObjC/message.m +++ b/test/SemaObjC/message.m @@ -75,3 +75,16 @@ int f2() { return 0; } + +// PR3766 +struct S { int X; } S; + +int test5(int X) { + int a = [X somemsg]; // expected-warning {{bad receiver type 'int'}} \ + expected-warning {{method '-somemsg' not found}} \ + expected-warning {{incompatible pointer to integer conversion initializing 'id', expected 'int'}} + int b = [S somemsg]; // expected-error {{bad receiver type 'struct S'}} +} + + + diff --git a/test/SemaObjC/super.m b/test/SemaObjC/super.m index 7a5cf9bd0b..6b4c37f49c 100644 --- a/test/SemaObjC/super.m +++ b/test/SemaObjC/super.m @@ -32,7 +32,8 @@ void f(id super) { [super m]; } void f0(int super) { - [super m]; // expected-warning{{bad receiver type 'int'}} + [super m]; // expected-warning{{bad receiver type 'int'}} \ + expected-warning {{method '-m' not found (return type defaults to 'id')}} } void f1(int puper) { [super m]; // expected-error{{use of undeclared identifier 'super'}} -- 2.40.0