This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC][objc] Get rid of aliasing warning for @selector()

For a long time, the objective-c frontend has been issuing (bogus, as
far as I can tell) strict-aliasing warnings for @selector() expressions,
including the ones implicitly used when sending messages, when using the
GNU runtime and typed selectors (the default).

The relevant selector references are built in
build_typed_selector_reference() in gcc/objc/objc-act.c by constructing
a pointer to an element of the UOBJC_SELECTOR_TABLE_decl array (which is
constructed by the frontend).

The type of the selector needs to be 'const struct objc_selector *'
(aka. 'SEL', known as 'objc_selector_type' in the frontend), which is
declared in the obj-c runtime headers. These may or may not be included,
so the type might be incomplete. Thus, this type can't be used to
construct the selector table array. Instead the frontend creates its own
complete struct type ('objc_selector_template' in the frontend) and uses
it for the array.

The two structs are, in practice, identical (modulo a const), but
they're different types, so build_c_cast() warns when the selector
reference is cast to the right type. The attached patch fixes this by
adding a 'void *' cast before casting to 'const struct objc_selector *'.
This confuses build_c_cast() enough to make it not warn. libobjc builds,
'make check-objc' doesn't show any new failures, and on a ~8000 line
sample, the generated code is identical.

Is this an acceptable way of dealing with this problem? If not, what
would be?

- Alexander Malmberg

2004-06-23  Alexander Malmberg  <>

        * objc/objc-act.c (build_typed_selector_reference): Cast to
'void *'
        before casting to objc_selector_type.

2004-06-23  Alexander Malmberg  <>

        * objc.dg/selector-2.m: New.
/* Make sure that we don't give bogus warnings for @selector.  */
/* { dg-do compile } */

void foo(void)
  @selector(quux); /* shouldn't warn */

Index: objc-act.c
RCS file: /cvsroot/gcc/gcc/gcc/objc/objc-act.c,v
retrieving revision 1.225
diff -u -r1.225 objc-act.c
--- objc-act.c	22 Jun 2004 03:07:05 -0000	1.225
+++ objc-act.c	23 Jun 2004 00:53:15 -0000
@@ -2261,6 +2249,9 @@
 			 build_array_ref (UOBJC_SELECTOR_TABLE_decl,
 					  build_int_2 (index, 0)),
+  /* Cast to 'void *' first to avoid a bogus warning about strict-aliasing
+     from build_c_cast.  */
+  expr = build_c_cast (ptr_type_node, expr);
   return build_c_cast (objc_selector_type, expr);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]