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
diff --git a/include/clang/Basic/DiagnosticSemaKinds.def b/include/clang/Basic/DiagnosticSemaKinds.def
index 56fe9d2..17eb027 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.def
+++ b/include/clang/Basic/DiagnosticSemaKinds.def
@@ -969,6 +969,8 @@
      "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 512a72f..98fbd96 100644
--- a/lib/Sema/SemaExprObjC.cpp
+++ b/lib/Sema/SemaExprObjC.cpp
@@ -489,7 +489,7 @@
 
   // 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 @@
     }
     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 c9d2a2a..a737754 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 @@
 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 f961e35..a232f5c 100644
--- a/test/SemaObjC/message.m
+++ b/test/SemaObjC/message.m
@@ -75,3 +75,16 @@
     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 7a5cf9b..6b4c37f 100644
--- a/test/SemaObjC/super.m
+++ b/test/SemaObjC/super.m
@@ -32,7 +32,8 @@
   [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'}}