[Bug c++/86210] [6/7/8/9 Regression] Missing -Wnonnull warning for function defined in the same TU

2018-06-20 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86210

--- Comment #6 from Jakub Jelinek  ---
Regression fixed for 8.2+ so far by the above changes, for the enhancement see
above comment.

[Bug c++/86210] [6/7/8/9 Regression] Missing -Wnonnull warning for function defined in the same TU

2018-06-20 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86210

--- Comment #5 from Jakub Jelinek  ---
Author: jakub
Date: Wed Jun 20 16:08:14 2018
New Revision: 261812

URL: https://gcc.gnu.org/viewcvs?rev=261812&root=gcc&view=rev
Log:
PR c++/86210
* c-common.c (check_nonnull_arg): Use fold_for_warn.  Adjust obsolete
comment.

* g++.dg/warn/Wnonnull4.C: New test.

Added:
branches/gcc-8-branch/gcc/testsuite/g++.dg/warn/Wnonnull4.C
Modified:
branches/gcc-8-branch/gcc/c-family/ChangeLog
branches/gcc-8-branch/gcc/c-family/c-common.c
branches/gcc-8-branch/gcc/testsuite/ChangeLog

[Bug c++/86210] [6/7/8/9 Regression] Missing -Wnonnull warning for function defined in the same TU

2018-06-20 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86210

--- Comment #4 from Jakub Jelinek  ---
Author: jakub
Date: Wed Jun 20 16:07:21 2018
New Revision: 261811

URL: https://gcc.gnu.org/viewcvs?rev=261811&root=gcc&view=rev
Log:
PR c++/86210
* c-common.c (check_nonnull_arg): Use fold_for_warn.  Adjust obsolete
comment.

* g++.dg/warn/Wnonnull4.C: New test.

Added:
trunk/gcc/testsuite/g++.dg/warn/Wnonnull4.C
Modified:
trunk/gcc/c-family/ChangeLog
trunk/gcc/c-family/c-common.c
trunk/gcc/testsuite/ChangeLog

[Bug c++/86210] [6/7/8/9 Regression] Missing -Wnonnull warning for function defined in the same TU

2018-06-20 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86210

--- Comment #3 from Jakub Jelinek  ---
WIP patch to warn also during inlining, with the intent to handle e.g.
  int *p = 0;
  declared_and_defined(p);
for both C/C++.  Unfortunately if it is inlined during early inlining, we still
don't warn, because no forward propagation etc. is done before early inlining.
With -fno-early-inlining -O2 -Wnonnull we don't warn either, because the call
is optimized away, as it is determined const and doesn't use return value. 
With a side-effect in there it warns without early inlining.

Is this still worth doing?

--- gcc/tree-ssa-ccp.c.jj   2018-06-13 10:05:30.357110986 +0200
+++ gcc/tree-ssa-ccp.c  2018-06-20 17:14:00.374389421 +0200
@@ -3391,6 +3391,41 @@ make_pass_fold_builtins (gcc::context *c
   return new pass_fold_builtins (ctxt);
 }

+/* Emit -Wnonnull warnings for call STMT.  */
+
+void
+warn_nonnull_call (gcall *stmt)
+{
+  bitmap nonnullargs = get_nonnull_args (gimple_call_fntype (stmt));
+  if (!nonnullargs)
+return;
+
+  for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
+{
+  tree arg = gimple_call_arg (stmt, i);
+  if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
+   continue;
+  if (!integer_zerop (arg))
+   continue;
+  if (!bitmap_empty_p (nonnullargs) && !bitmap_bit_p (nonnullargs, i))
+   continue;
+
+  location_t loc = gimple_location (stmt);
+  if (warning_at (loc, OPT_Wnonnull,
+ "%Gargument %u null where non-null expected",
+ stmt, i + 1))
+   {
+ tree fndecl = gimple_call_fndecl (stmt);
+ if (fndecl && DECL_IS_BUILTIN (fndecl))
+   inform (loc, "in a call to built-in function %qD", fndecl);
+ else if (fndecl)
+   inform (DECL_SOURCE_LOCATION (fndecl),
+   "in a call to function %qD declared here", fndecl);
+   }
+}
+  BITMAP_FREE (nonnullargs);
+}
+
 /* A simple pass that emits some warnings post IPA.  */

 namespace {
@@ -3437,41 +3474,7 @@ pass_post_ipa_warn::execute (function *f
continue;

  if (warn_nonnull)
-   {
- bitmap nonnullargs
-   = get_nonnull_args (gimple_call_fntype (stmt));
- if (nonnullargs)
-   {
- for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
-   {
- tree arg = gimple_call_arg (stmt, i);
- if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
-   continue;
- if (!integer_zerop (arg))
-   continue;
- if (!bitmap_empty_p (nonnullargs)
- && !bitmap_bit_p (nonnullargs, i))
-   continue;
-
- location_t loc = gimple_location (stmt);
- if (warning_at (loc, OPT_Wnonnull,
- "%Gargument %u null where non-null "
- "expected", as_a (stmt), i + 1))
-   {
- tree fndecl = gimple_call_fndecl (stmt);
- if (fndecl && DECL_IS_BUILTIN (fndecl))
-   inform (loc, "in a call to built-in function %qD",
-   fndecl);
- else if (fndecl)
-   inform (DECL_SOURCE_LOCATION (fndecl),
-   "in a call to function %qD declared here",
-   fndecl);
-
-   }
-   }
- BITMAP_FREE (nonnullargs);
-   }
-   }
+   warn_nonnull_call (as_a (stmt));
}
 }
   return 0;
--- gcc/tree-ssa-ccp.h.jj   2018-01-03 10:19:54.257533814 +0100
+++ gcc/tree-ssa-ccp.h  2018-06-20 17:14:38.915447584 +0200
@@ -26,4 +26,6 @@ void bit_value_binop (enum tree_code, si
 void bit_value_unop (enum tree_code, signop, int, widest_int *, widest_int *,
 signop, int, const widest_int &, const widest_int &);

+void warn_nonnull_call (gcall *);
+
 #endif
--- gcc/tree-inline.c.jj2018-06-20 08:15:41.224868655 +0200
+++ gcc/tree-inline.c   2018-06-20 17:34:39.676261250 +0200
@@ -60,6 +60,7 @@ along with GCC; see the file COPYING3.
 #include "stringpool.h"
 #include "attribs.h"
 #include "sreal.h"
+#include "tree-ssa-ccp.h"

 /* I'm not real happy about this, but we need to handle gimple and
non-gimple trees.  */
@@ -4409,6 +4410,9 @@ expand_call_inline (basic_block bb, gimp
 }
   id->src_node = cg_edge->callee;

+  if (warn_nonnull && !gimple_no_warning_p (call_stmt))
+warn_nonnull_call (call_stmt);
+
   /* If callee is thunk, all we need is to adjust the THIS pointer
  and redirect to function being thunked.  */
   if (id->src_node->thunk.thunk_p)

[Bug c++/86210] [6/7/8/9 Regression] Missing -Wnonnull warning for function defined in the same TU

2018-06-20 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86210

Jakub Jelinek  changed:

   What|Removed |Added

 Status|UNCONFIRMED |ASSIGNED
   Last reconfirmed||2018-06-20
   Assignee|unassigned at gcc dot gnu.org  |jakub at gcc dot gnu.org
 Ever confirmed|0   |1

--- Comment #2 from Jakub Jelinek  ---
Created attachment 44302
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44302&action=edit
gcc9-pr86210.patch

Untested patch to fix the C++ FE regression (presumably started with delayed
folding).
The enhancement to warn during inlining can be done incrementally (but that one
certainly shouldn't be backported).

[Bug c++/86210] [6/7/8/9 Regression] Missing -Wnonnull warning for function defined in the same TU

2018-06-20 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86210

Jakub Jelinek  changed:

   What|Removed |Added

 CC||jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek  ---
For C we've started warning for the declared_not_defined function with r243661,
because the warning is done during expansion, if you inline the call, then no
warning is emitted.  Perhaps we could also warn during inlining if we inline
and pass integer_zerop to a nonnull argument of the inline function.  It
wouldn't warn if something isn't simplified into the constant yet (especially a
problem during early inlining), but perhaps better than nothing.

[Bug c++/86210] [6/7/8/9 Regression] Missing -Wnonnull warning for function defined in the same TU

2018-06-19 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86210

Richard Biener  changed:

   What|Removed |Added

   Target Milestone|--- |6.5