[patch] fix another issue in -Wunused warning on C++ code (PR c++/39803)

Le-Chun Wu lcwu@google.com
Sat Apr 18 00:28:00 GMT 2009


Hi Jason and Mark,

This patch fixes another issue in -Wunused warning on C++ code.
(Please see PR c++/39803.) Basically the code generated by C++
font-end to initialize non-POD arrays contains a top-level
INDIRECT_REF that's not used and therefore removed later. However, the
original change I submitted two days ago didn't recognize that the
INDIRECT_REF was generated by the compiler (instead of coming from the
user code) and mistakenly emits a bogus warning on the array
declaration.

The fix is simply setting TREE_NO_WARNING on the compiler-generated
INDIRECT_REF in build_vec_init code. Is this patch OK for the
mainline? (I am not sure if there are other places in C++ FE that
could generate useless INDIRECT_REF not in the user code that I should
take care of right now. Do you know of any?)

The patch was bootstrapped and tested on x86_64-linux-gnu.

Thanks,

Le-chun

2009-04-17  Le-Chun Wu  <lcwu@google.com>

        PR c++/39803
        * gcc/cp/init.c (build_vec_init): Set TREE_NO_WARNING on the
        compiler-generated INDIRECT_REF expression.
        * gcc/testsuite/g++.dg/warn/Wunused-14.C: New test.


Index: gcc/testsuite/g++.dg/warn/Wunused-14.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wunused-14.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/Wunused-14.C	(revision 0)
@@ -0,0 +1,14 @@
+// Test that -Wunused should not emit a warning on the initialization of
+// non-POD arrays. See PR c++/39803.
+// { dg-do compile }
+// { dg-options "-Wunused" }
+
+#include <utility>
+
+using std::pair;
+
+int foo() {
+  pair<int, const char*> components[3]; // { dg-bogus "value computed
is not used" }
+  components[0].first = 0;
+  return 0;
+}
Index: gcc/cp/init.c
===================================================================
--- gcc/cp/init.c	(revision 146298)
+++ gcc/cp/init.c	(working copy)
@@ -2920,6 +2920,7 @@ build_vec_init (tree base, tree maxindex
       atype = build_pointer_type (atype);
       stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
       stmt_expr = cp_build_indirect_ref (stmt_expr, NULL, complain);
+      TREE_NO_WARNING (stmt_expr) = 1;
     }

   current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;



More information about the Gcc-patches mailing list