[cfe-commits] r66468 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.def lib/Sema/SemaExprObjC.cpp test/SemaObjC/bad-receiver-1.m test/SemaObjC/message.m test/SemaObjC/super.m

Chris Lattner sabre at nondot.org
Mon Mar 9 14:19:25 PDT 2009


Author: lattner
Date: Mon Mar  9 16:19:16 2009
New Revision: 66468

URL: http://llvm.org/viewvc/llvm-project?rev=66468&view=rev
Log:
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.


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
    cfe/trunk/lib/Sema/SemaExprObjC.cpp
    cfe/trunk/test/SemaObjC/bad-receiver-1.m
    cfe/trunk/test/SemaObjC/message.m
    cfe/trunk/test/SemaObjC/super.m

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def Mon Mar  9 16:19:16 2009
@@ -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,

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Mon Mar  9 16:19:16 2009
@@ -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;
   }
   

Modified: cfe/trunk/test/SemaObjC/bad-receiver-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/bad-receiver-1.m?rev=66468&r1=66467&r2=66468&view=diff

==============================================================================
--- cfe/trunk/test/SemaObjC/bad-receiver-1.m (original)
+++ cfe/trunk/test/SemaObjC/bad-receiver-1.m Mon Mar  9 16:19:16 2009
@@ -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}}
 }

Modified: cfe/trunk/test/SemaObjC/message.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/message.m?rev=66468&r1=66467&r2=66468&view=diff

==============================================================================
--- cfe/trunk/test/SemaObjC/message.m (original)
+++ cfe/trunk/test/SemaObjC/message.m Mon Mar  9 16:19:16 2009
@@ -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'}}
+}
+
+
+

Modified: cfe/trunk/test/SemaObjC/super.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/super.m?rev=66468&r1=66467&r2=66468&view=diff

==============================================================================
--- cfe/trunk/test/SemaObjC/super.m (original)
+++ cfe/trunk/test/SemaObjC/super.m Mon Mar  9 16:19:16 2009
@@ -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'}}





More information about the cfe-commits mailing list