https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47931

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |msebor at gcc dot 
gnu.org
      Known to fail|                            |4.8.4, 4.9.3, 5.1.0, 6.0

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
I have a simple patch for the C++ side of the problem (below).  Let me add the
C bits along with the cleanup discussed in the context of the fix for bug 70194
(https://gcc.gnu.org/ml/gcc-patches/2016-03/msg01041.html) and post it for
review in stage 1 of GCC 7.

Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c     (revision 234306)
+++ gcc/cp/typeck.c     (working copy)
@@ -3985,13 +3985,20 @@ warn_for_null_address (location_t locati
       || TREE_NO_WARNING (op))
     return;

+  STRIP_NOPS (op);
   tree cop = fold_non_dependent_expr (op);
-
-  if (TREE_CODE (cop) == ADDR_EXPR
-      && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
-      && !TREE_NO_WARNING (cop))
-    warning_at (location, OPT_Waddress, "the address of %qD will never "
-               "be NULL", TREE_OPERAND (cop, 0));
+  if (TREE_CODE (cop) == ADDR_EXPR && !TREE_NO_WARNING (cop))
+    {
+      cop = TREE_OPERAND (cop, 0);
+      if (TREE_CODE (cop) == ARRAY_REF)
+       cop = TREE_OPERAND (cop, 0);
+      if (TREE_CODE (cop) == STRING_CST)
+       warning_at (location, OPT_Waddress,
+                   "the address of a string literal will never be NULL");
+      else if (decl_with_nonnull_addr_p (cop))
+       warning_at (location, OPT_Waddress,
+                   "the address of %qD will never be NULL", cop);
+    }

   if (CONVERT_EXPR_P (op)
       && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)

Reply via email to