Patchwork [ObjC*] don't warn 'set not used' when an object is a receiver.

login
register
mail settings
Submitter IainS
Date Oct. 6, 2010, 6:07 p.m.
Message ID <2D419F6A-9F07-4533-B7FF-A51464F0E118@sandoe-acoustics.co.uk>
Download mbox | patch
Permalink /patch/66955/
State New
Headers show

Comments

IainS - Oct. 6, 2010, 6:07 p.m.
This sets "used" and "read" on object refs when they are the receiver  
in a message expression
viz:
[a someMethod];

which clears the 'set but not used' warning for this case.

OK for trunk?
Iain

gcc/objc:

	* objc-act.c (objc_build_message_expr):  Set TREE_USED and  
DECL_READ_P when
	the receiver is an instance reference.

testsuite:

	* objc.dg/set-not-used-1.m: New
	* obj-c++.dg/set-not-used-1.mm: New.
Jakub Jelinek - Oct. 6, 2010, 6:14 p.m.
On Wed, Oct 06, 2010 at 07:07:43PM +0100, IainS wrote:
> Index: gcc/objc/objc-act.c
> ===================================================================
> --- gcc/objc/objc-act.c	(revision 165023)
> +++ gcc/objc/objc-act.c	(working copy)
> @@ -6409,7 +6409,7 @@ objc_build_message_expr (tree mess)
>        method_params = args;
>      }
>  #endif
> -
> +  
>  #ifdef OBJCPLUS
>    if (processing_template_decl)
>      /* Must wait until template instantiation time.  */
> @@ -6455,6 +6455,13 @@ objc_finish_message_expr (tree receiver, tree sel_
>    tree selector, retval, class_tree;
>    int self, super, have_cast;
>  
> +  /* If we have an instance, then it has been used/read.  */
> +  if (TREE_CODE (receiver) == VAR_DECL)
> +    {
> +      TREE_USED (receiver) = 1;
> +      DECL_READ_P (receiver) = 1;
> +    }
> +

I think you should just do
  mark_exp_read (receiver);
instead.

> --- gcc/testsuite/objc.dg/set-not-used-1.m	(revision 0)
> +++ gcc/testsuite/objc.dg/set-not-used-1.m	(revision 0)
> @@ -0,0 +1,36 @@
> +
> +/* { dg-do compile } */
> +/* { dg-options "-Wunused-but-set-variable" } */
> +
> +#import "../objc-obj-c++-shared/Object1.h"
> +#include <objc/objc-api.h>
> +
> +@interface obj : Object
> +{
> +  int value;
> +}
> +- (int) value;
> +- (void) setValue: (int)number;
> +@end
> +
> +@implementation obj : Object
> +
> +- (int) value { return value; }
> +- (void) setValue: (int)number { value = number; }
> +
> +@end
> +
> +int main (void)
> +{
> +  obj *a;		/* { dg-bogus "set but not used" } */
> +  obj *b;		/* { dg-bogus "set but not used" } */
> +  obj *c;		/* { dg-warning "set but not used" } */
> +
> +  a = [obj new];
> +  b = [obj new];
> +  c = [obj new];
> +
> +  [b setValue: [a value]];
> +
> +  return [a value];
> +}
> Index: gcc/testsuite/obj-c++.dg/set-not-used-1.mm
> ===================================================================
> --- gcc/testsuite/obj-c++.dg/set-not-used-1.mm	(revision 0)
> +++ gcc/testsuite/obj-c++.dg/set-not-used-1.mm	(revision 0)
> @@ -0,0 +1,36 @@
> +
> +/* { dg-do compile } */
> +/* { dg-options "-Wunused-but-set-variable" } */
> +
> +#import "../objc-obj-c++-shared/Object1.h"
> +#include <objc/objc-api.h>
> +
> +@interface obj : Object
> +{
> +  int value;
> +}
> +- (int) value;
> +- (void) setValue: (int)number;
> +@end
> +
> +@implementation obj : Object
> +
> +- (int) value { return value; }
> +- (void) setValue: (int)number { value = number; }
> +
> +@end
> +
> +int main (void)
> +{
> +  obj *a;		/* { dg-bogus "set but not used" } */
> +  obj *b;		/* { dg-bogus "set but not used" } */
> +  obj *c;		/* { dg-warning "set but not used" } */
> +
> +  a = [obj new];
> +  b = [obj new];
> +  c = [obj new];
> +
> +  [b setValue: [a value]];
> +
> +  return [a value];
> +}

	Jakub

Patch

Index: gcc/objc/objc-act.c
===================================================================
--- gcc/objc/objc-act.c	(revision 165023)
+++ gcc/objc/objc-act.c	(working copy)
@@ -6409,7 +6409,7 @@  objc_build_message_expr (tree mess)
       method_params = args;
     }
 #endif
-
+  
 #ifdef OBJCPLUS
   if (processing_template_decl)
     /* Must wait until template instantiation time.  */
@@ -6455,6 +6455,13 @@  objc_finish_message_expr (tree receiver, tree sel_
   tree selector, retval, class_tree;
   int self, super, have_cast;
 
+  /* If we have an instance, then it has been used/read.  */
+  if (TREE_CODE (receiver) == VAR_DECL)
+    {
+      TREE_USED (receiver) = 1;
+      DECL_READ_P (receiver) = 1;
+    }
+
   /* Extract the receiver of the message, as well as its type
      (where the latter may take the form of a cast or be inferred
      from the implementation context).  */
Index: gcc/testsuite/objc.dg/set-not-used-1.m
===================================================================
--- gcc/testsuite/objc.dg/set-not-used-1.m	(revision 0)
+++ gcc/testsuite/objc.dg/set-not-used-1.m	(revision 0)
@@ -0,0 +1,36 @@ 
+
+/* { dg-do compile } */
+/* { dg-options "-Wunused-but-set-variable" } */
+
+#import "../objc-obj-c++-shared/Object1.h"
+#include <objc/objc-api.h>
+
+@interface obj : Object
+{
+  int value;
+}
+- (int) value;
+- (void) setValue: (int)number;
+@end
+
+@implementation obj : Object
+
+- (int) value { return value; }
+- (void) setValue: (int)number { value = number; }
+
+@end
+
+int main (void)
+{
+  obj *a;		/* { dg-bogus "set but not used" } */
+  obj *b;		/* { dg-bogus "set but not used" } */
+  obj *c;		/* { dg-warning "set but not used" } */
+
+  a = [obj new];
+  b = [obj new];
+  c = [obj new];
+
+  [b setValue: [a value]];
+
+  return [a value];
+}
Index: gcc/testsuite/obj-c++.dg/set-not-used-1.mm
===================================================================
--- gcc/testsuite/obj-c++.dg/set-not-used-1.mm	(revision 0)
+++ gcc/testsuite/obj-c++.dg/set-not-used-1.mm	(revision 0)
@@ -0,0 +1,36 @@ 
+
+/* { dg-do compile } */
+/* { dg-options "-Wunused-but-set-variable" } */
+
+#import "../objc-obj-c++-shared/Object1.h"
+#include <objc/objc-api.h>
+
+@interface obj : Object
+{
+  int value;
+}
+- (int) value;
+- (void) setValue: (int)number;
+@end
+
+@implementation obj : Object
+
+- (int) value { return value; }
+- (void) setValue: (int)number { value = number; }
+
+@end
+
+int main (void)
+{
+  obj *a;		/* { dg-bogus "set but not used" } */
+  obj *b;		/* { dg-bogus "set but not used" } */
+  obj *c;		/* { dg-warning "set but not used" } */
+
+  a = [obj new];
+  b = [obj new];
+  c = [obj new];
+
+  [b setValue: [a value]];
+
+  return [a value];
+}