PR c++/65168 - -Waddress in unevaluated context.
authorJason Merrill <jason@redhat.com>
Thu, 21 Jul 2016 06:05:39 +0000 (02:05 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Thu, 21 Jul 2016 06:05:39 +0000 (02:05 -0400)
gcc/c-family/
* c-common.c (c_common_truthvalue_conversion): Check
c_inhibit_evaluation_warnings for warning about address of
reference.
gcc/cp/
* typeck.c (cp_truthvalue_conversion): Compare pointers to nullptr.
Don't set c_inhibit_evaluation_warnings.

From-SVN: r238560

gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/g++.dg/warn/Waddress-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Walways-true-1.C
gcc/testsuite/g++.dg/warn/Walways-true-2.C

index 3842929..e244e8a 100644 (file)
@@ -1,3 +1,10 @@
+2016-07-21  Jason Merrill  <jason@redhat.com>
+
+       PR c++/65168
+       * c-common.c (c_common_truthvalue_conversion): Check
+       c_inhibit_evaluation_warnings for warning about address of
+       reference.
+
 2016-07-20  David Malcolm  <dmalcolm@redhat.com>
 
        * c-common.h (lookup_name_fuzzy): Convert return type from tree to
index 936ddfb..9900e93 100644 (file)
@@ -4551,6 +4551,7 @@ c_common_truthvalue_conversion (location_t location, tree expr)
        tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
 
        if (POINTER_TYPE_P (totype)
+           && !c_inhibit_evaluation_warnings
            && TREE_CODE (fromtype) == REFERENCE_TYPE)
          {
            tree inner = expr;
index 8ff7f75..be223f2 100644 (file)
@@ -1,5 +1,9 @@
 2016-07-21  Jason Merrill  <jason@redhat.com>
 
+       PR c++/65168
+       * typeck.c (cp_truthvalue_conversion): Compare pointers to nullptr.
+       Don't set c_inhibit_evaluation_warnings.
+
        PR c++/71121
        * cp-gimplify.c (cp_fully_fold): First call maybe_constant_value.
 
index f9e45ee..d4bfb11 100644 (file)
@@ -5459,21 +5459,10 @@ tree
 cp_truthvalue_conversion (tree expr)
 {
   tree type = TREE_TYPE (expr);
-  if (TYPE_PTRDATAMEM_P (type)
+  if (TYPE_PTR_OR_PTRMEM_P (type)
       /* Avoid ICE on invalid use of non-static member function.  */
       || TREE_CODE (expr) == FUNCTION_DECL)
-    return build_binary_op (EXPR_LOCATION (expr),
-                           NE_EXPR, expr, nullptr_node, 1);
-  else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
-    {
-      /* With -Wzero-as-null-pointer-constant do not warn for an
-        'if (p)' or a 'while (!p)', where p is a pointer.  */
-      tree ret;
-      ++c_inhibit_evaluation_warnings;
-      ret = c_common_truthvalue_conversion (input_location, expr);
-      --c_inhibit_evaluation_warnings;
-      return ret;
-    }
+    return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, 1);
   else
     return c_common_truthvalue_conversion (input_location, expr);
 }
diff --git a/gcc/testsuite/g++.dg/warn/Waddress-3.C b/gcc/testsuite/g++.dg/warn/Waddress-3.C
new file mode 100644 (file)
index 0000000..13d7cd2
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/65168
+// { dg-do compile { target c++11 } }
+// { dg-options -Waddress }
+// We shouldn't warn in unevaluated context about the address of a reference
+// always being true.
+
+template <class T, class U>
+auto f(U&& u) -> decltype(T(u)) { }
+
+int main()
+{
+  bool ar[4];
+  f<bool>(ar);
+}
index ae6f9dc..48b9f72 100644 (file)
@@ -12,19 +12,19 @@ void
 bar (int a)
 {
  lab:
-  if (foo)     // { dg-warning "always evaluate as" "correct warning" }
+  if (foo)     // { dg-warning "always evaluate as|never be NULL" "correct warning" }
     foo (0);
   if (foo (1))
     ;
-  if (&i)      // { dg-warning "always evaluate as" "correct warning" }
+  if (&i)      // { dg-warning "always evaluate as|never be NULL" "correct warning" }
     foo (2);
   if (i)
     foo (3);
-  if (&a)      // { dg-warning "always evaluate as" "correct warning" }
+  if (&a)      // { dg-warning "always evaluate as|never be NULL" "correct warning" }
     foo (4);
   if (a)
     foo (5);
-  if (&&lab)   // { dg-warning "always evaluate as" "correct warning" }
+  if (&&lab)   // { dg-warning "always evaluate as|never be NULL" "correct warning" }
     foo (6);
   if (foo == 0)        // { dg-warning "never be NULL" "correct warning" }
     foo (7);
index f157347..e4b5713 100644 (file)
@@ -23,11 +23,11 @@ bar (int a)
     foo (2);
   if (i)
     foo (3);
-  if (&a)      // { dg-warning "always evaluate as" "correct warning" }
+  if (&a)      // { dg-warning "always evaluate as|never be NULL" "correct warning" }
     foo (4);
   if (a)
     foo (5);
-  if (&&lab)   // { dg-warning "always evaluate as" "correct warning" }
+  if (&&lab)   // { dg-warning "always evaluate as|never be NULL" "correct warning" }
     foo (6);
   if (foo == 0)
     foo (7);