PATCH: ObjC-specific changes to c-typeck.c

Ziemowit Laski zlaski@apple.com
Wed Aug 25 19:20:00 GMT 2004


On 24 Aug 2004, at 16.49, Joseph S. Myers wrote:

> On Tue, 24 Aug 2004, Ziemowit Laski wrote:
>
>> Actually, you do raise a good point in that I should check the 
>> ObjC-ness
>> of the expression I'm casting as well as that of the type I'm casting 
>> to
>> before performing the silent conversion (although it _should_ be
>> silent).  I'll adjust my patch accordingly.
>
> Remember to include an appropriate testcase if possible....

I have done so, leading to the following result which I shall commit 
shortly
after final sanity checks:

[gcc/ChangeLog]
2004-08-25  Ziemowit Laski  <zlaski@apple.com>

        * c-typeck.c (build_c_cast): In ObjC, always preserve (and 
silently
        accept) a cast from one Objective-C pointer type to another.

[gcc/testsuite/ChangeLog]
2004-08-25  Ziemowit Laski  <zlaski@apple.com>

        * objc.dg/proto-lossage-4.m: New test.

Index: gcc/c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.361
diff -u -3 -p -r1.361 c-typeck.c
--- gcc/c-typeck.c      25 Aug 2004 09:51:21 -0000      1.361
+++ gcc/c-typeck.c      25 Aug 2004 19:08:36 -0000
@@ -2922,8 +2922,10 @@ build_c_cast (tree type, tree expr)
    /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types 
differing
       only in <protocol> qualifications.  But when constructing cast 
expressions,
       the protocols do matter and must be kept around.  */
-  if (!c_dialect_objc () || !objc_is_object_ptr (type))
-    type = TYPE_MAIN_VARIANT (type);
+  if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE 
(expr)))
+    return build1 (NOP_EXPR, type, expr);
+
+  type = TYPE_MAIN_VARIANT (type);

    if (TREE_CODE (type) == ARRAY_TYPE)
      {
Index: gcc/testsuite/objc.dg/proto-lossage-4.m
===================================================================
RCS file: gcc/testsuite/objc.dg/proto-lossage-4.m
diff -N gcc/testsuite/objc.dg/proto-lossage-4.m
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/objc.dg/proto-lossage-4.m     25 Aug 2004 19:08:57 
-0000
@@ -0,0 +1,51 @@
+/* Test for situations in which protocol conformance information
+   may be lost while casting.  */
+/* Author: Ziemowit Laski <zlaski@apple.com>.  */
+/* { dg-do compile } */
+
+/* One-line substitute for objc/objc.h */
+typedef struct objc_object { struct objc_class *class_pointer; } *id;
+
+@protocol Proto
+- (int)someValue;
+@end
+
+@interface Obj
+- (int)anotherValue;
+@end
+
+int foo(void) {
+  int receiver = 2;
+  Obj *objrcvr;
+  Obj <Proto> *objrcvr2;
+
+  receiver += [receiver someValue]; /* { dg-warning "invalid receiver 
type .int( )?." } */
+/* { dg-warning "Messages without a matching method signature" "" { 
target *-*-* } 22 } */
+/* { dg-warning "will be assumed to return .id. and accept" "" { 
target *-*-* } 22 } */
+/* { dg-warning ".\.\.\.. as arguments" "" { target *-*-* } 22 } */
+/* { dg-warning "assignment makes integer from pointer without a cast" 
} */
+
+  receiver += [receiver anotherValue]; /* { dg-warning "invalid 
receiver type .int( )?." } */
+/* { dg-warning "assignment makes integer from pointer without a cast" 
"" { target *-*-* } 28 } */
+
+  receiver += [(Obj *)receiver someValue]; /* { dg-warning ".Obj. may 
not respond to .\\-someValue." } */
+/* { dg-warning "assignment makes integer from pointer without a cast" 
"" { target *-*-* } 31 } */
+
+  receiver += [(Obj *)receiver anotherValue];
+  receiver += [(Obj <Proto> *)receiver someValue];
+  receiver += [(Obj <Proto> *)receiver anotherValue];
+  receiver += [objrcvr someValue]; /* { dg-warning ".Obj. may not 
respond to .\\-someValue." } */
+/* { dg-warning "assignment makes integer from pointer without a cast" 
"" { target *-*-* } 37 } */
+
+  receiver += [objrcvr anotherValue];
+  receiver += [(Obj <Proto> *)objrcvr someValue];
+  receiver += [(Obj <Proto> *)objrcvr anotherValue];
+  receiver += [objrcvr2 someValue];
+  receiver += [objrcvr2 anotherValue];
+  receiver += [(Obj *)objrcvr2 someValue]; /* { dg-warning ".Obj. may 
not respond to .\\-someValue." } */
+/* { dg-warning "assignment makes integer from pointer without a cast" 
"" { target *-*-* } 45 } */
+
+  receiver += [(Obj *)objrcvr2 anotherValue];
+
+  return receiver;
+}
--------------------------------------------------------------
Ziemowit Laski                 1 Infinite Loop, MS 301-2K
Mac OS X Compiler Group        Cupertino, CA USA  95014-2083
Apple Computer, Inc.           +1.408.974.6229  Fax .5477



More information about the Gcc-patches mailing list