re PR c++/52299 (GCC warns on compile time division by zero erroneously)
authorPaolo Carlini <paolo.carlini@oracle.com>
Mon, 12 Mar 2012 19:29:38 +0000 (19:29 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Mon, 12 Mar 2012 19:29:38 +0000 (19:29 +0000)
/cp
2012-03-12  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/52299
* pt.c (tsubst_copy_and_build, case COND_EXPR): Avoid bogus
division by zero warnings.

/testsuite
2012-03-12  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/52299
* g++.dg/warn/Wdiv-by-zero-bogus.C: New.

From-SVN: r185264

gcc/cp/ChangeLog
gcc/cp/pt.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wdiv-by-zero-bogus.C [new file with mode: 0644]

index 24e629fe8cd86873285394bfb9a5bd6cb90ed46a..1a3ac83ac77ab7d0c5b3833cf3a5cdb33de311d7 100644 (file)
@@ -1,3 +1,9 @@
+2012-03-12  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/52299
+       * pt.c (tsubst_copy_and_build, case COND_EXPR): Avoid bogus
+       division by zero warnings.
+
 2012-03-08  Paolo Carlini  <paolo.carlini@oracle.com>
 
        * typeck.c (build_array_ref, cp_build_addr_expr_1, convert_ptrmem,
index 4980c19ae06993fe5868b4cbc6cf95c951a055ac..54d540d0f52be37dacb4ab8bb052b36a51f8093a 100644 (file)
@@ -13943,11 +13943,35 @@ tsubst_copy_and_build (tree t,
       }
 
     case COND_EXPR:
-      return build_x_conditional_expr
-       (RECUR (TREE_OPERAND (t, 0)),
-        RECUR (TREE_OPERAND (t, 1)),
-        RECUR (TREE_OPERAND (t, 2)),
-         complain);
+      {
+       tree cond = RECUR (TREE_OPERAND (t, 0));
+       tree exp1, exp2;
+
+       if (TREE_CODE (cond) == INTEGER_CST)
+         {
+           if (integer_zerop (cond))
+             {
+               ++c_inhibit_evaluation_warnings;
+               exp1 = RECUR (TREE_OPERAND (t, 1));
+               --c_inhibit_evaluation_warnings;
+               exp2 = RECUR (TREE_OPERAND (t, 2));
+             }
+           else
+             {
+               exp1 = RECUR (TREE_OPERAND (t, 1));
+               ++c_inhibit_evaluation_warnings;
+               exp2 = RECUR (TREE_OPERAND (t, 2));
+               --c_inhibit_evaluation_warnings;
+             }
+         }
+       else
+         {
+           exp1 = RECUR (TREE_OPERAND (t, 1));
+           exp2 = RECUR (TREE_OPERAND (t, 2));
+         }
+
+       return build_x_conditional_expr (cond, exp1, exp2, complain);
+      }
 
     case PSEUDO_DTOR_EXPR:
       return finish_pseudo_destructor_expr
index 130a23fad71d63ca9f657b7ea45555c8b489b087..90b1197435978707195ebec215b894a812e495b5 100644 (file)
@@ -1,3 +1,8 @@
+2012-03-12  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/52299
+       * g++.dg/warn/Wdiv-by-zero-bogus.C: New.
+
 2012-03-12  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
 
        PR tree-optimization/46728
@@ -18,7 +23,7 @@
        * gcc.target/avr/torture/addr-space-0.h: New test.
        * gcc.target/avr/torture/addr-space-1.h: New test.
        * gcc.target/avr/torture/addr-space-x.h: New test.
-       
+
 2012-03-12  Andrew Pinski  <apinski@cavium.com>
 
        * gcc.dg/tree-ssa/phi-opt-7.c: New testcase.
diff --git a/gcc/testsuite/g++.dg/warn/Wdiv-by-zero-bogus.C b/gcc/testsuite/g++.dg/warn/Wdiv-by-zero-bogus.C
new file mode 100644 (file)
index 0000000..2157df3
--- /dev/null
@@ -0,0 +1,30 @@
+// PR c++/52299
+
+template<unsigned x>
+struct test0 {
+  static const unsigned a_
+  = x ? 10 / x : 10;
+};
+
+template<unsigned x>
+struct test1 {
+  static const unsigned a_
+  = !x ? 10 : 10 / x;
+};
+
+template<bool x>
+struct test2 {
+  static const unsigned a_
+  = x ? 10 / x : 10;
+};
+
+template<bool x>
+struct test3 {
+  static const unsigned a_
+  = !x ? 10 : 10 / x;
+};
+
+unsigned i0 = test0<0>::a_;
+unsigned i1 = test1<0>::a_;
+unsigned i2 = test2<false>::a_;
+unsigned i3 = test3<false>::a_;