]> granicus.if.org Git - clang/commitdiff
Fix PR3766, a really nasty silent miscompilation case where we emitted
authorChris Lattner <sabre@nondot.org>
Mon, 9 Mar 2009 21:19:16 +0000 (21:19 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 9 Mar 2009 21:19:16 +0000 (21:19 +0000)
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
lib/Sema/SemaExprObjC.cpp
test/SemaObjC/bad-receiver-1.m
test/SemaObjC/message.m
test/SemaObjC/super.m

index 56fe9d2a3896001f744cd5857ea381e8f4f7ae74..17eb0270f166da08c22ae9cb33188f1070279ab4 100644 (file)
@@ -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,
index 512a72f9b4730ac5a119e0465f66f0dfa1f8e139..98fbd9633113e4ea09ef3a7231e2605521a2b46c 100644 (file)
@@ -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;
   }
   
index c9d2a2a65107dc0fe9f9916b964ad3f887ffabc8..a7377544b8056e1974198467f33581c28a12a567 100644 (file)
@@ -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}}
 }
index f961e3587049d919b6cdd60432a401d4b4767c37..a232f5c512d9874dd1929a2c2bbc01c16c91c6f4 100644 (file)
@@ -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'}}
+}
+
+
+
index 7a5cf9bd0b347f2f535cb700ade4d0db9067f269..6b4c37f49c394def1ef70f4e2b893c4582223b09 100644 (file)
@@ -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'}}