It would be useful to have a function attribute which specifies that the function never returns NULL. Currently the GCC 4 snapshots with -O2 -Wall generate ~15 spurious warnings in the Subversion source code which could be eliminated if a couple of functions could be marked as never returning NULL.
Confirmed, PR 19476 is case which depends on this.
I don't object that this feature is indeed needed, but I would still like to see a reduced testcase from Subversion which shows a bogus warning that could be fixed with this attribute. BTW, Diego, once ASSERT_EXPR becomes a generic node, can't just the C++ FE uses it to assert that the return value for a CALL_EXPR to an allocation function is non-zero?
Created attachment 8331 [details] Pre-processed SVN code.
Subject: Re: RFE: add attribute to specify that a function never returns NULL giovannibajo at libero dot it wrote: > ------- Additional Comments From giovannibajo at libero dot it 2005-03-04 17:53 ------- > I don't object that this feature is indeed needed, but I would still like to > see a reduced testcase from Subversion which shows a bogus warning that could > be fixed with this attribute. > I have attached the pre-processed test case from Joe. > BTW, Diego, once ASSERT_EXPR becomes a generic node, can't just the C++ FE uses > it to assert that the return value for a CALL_EXPR to an allocation function is > non-zero? > The attribute would be enough. The FE need only set an ECF_ flag to the CALL_EXPR and the optimizers will pick it up from there. Diego.
Adding some of the early analysis that went on in private mail. > So is there an __attribute__ that we can use to mark that function to > always return non-NULL, so the compiler knows that path is not possible? > No. There is __attribute__ ((noreturn)), but the compiler has no way of knowing that svn_fs_fs__id_parse is guaranteed to return non-NULL. This is what the compiler sees in the IL: root_id_41 = svn_fs_fs__id_parse (node_id_str_39, D.10885_40, pool_5); if (root_id_41 == 0B) goto <L13>; else goto <L17>; <L13>:; svn_error__locate (...); D.10887_45 = dgettext (...); svn_err__temp_46 = svn_error_create (160004, 0B, D.10887_45); # root_id_257 = PHI <root_id_28(10), root_id_41(8)>; <L17>:; [ ... ] <L19>:; *root_id_p_30 = root_id_257; At L19, it sees two possible values for root_id (root_id_28 and root_id_41). root_id_28 is the uninitialized value. We don't have a "can't return NULL" attribute. Though it would be useful. In this case, you're better off initializing *root_id_p at the start of the function. Diego.
Here is a reduced testcase for the SVN code: void *g(); static void *count_and_verify_instructions(int *ninst,const unsigned char *p) { if (p == ((void *)0)) return g(); *ninst = 0; return 0; } void f(int); void *decode_window(const unsigned char *data) { int ninst; void *svn_err__temp = count_and_verify_instructions(&ninst, data); if (svn_err__temp) return svn_err__temp; f (ninst); return 0; } ----- cut --------- g returns only non null values. This reduced testcase comes from PR 21320 which comes from OLH.
This feature would also be useful for java, as we can eliminate certain inlined null pointer checks when we know that a method can't return null. See http://gcc.gnu.org/ml/java/2005-11/msg00124.html
I have a patch already.
No longer working on this, I am too busy working on the gfortran front-end.
Author: glisse Date: Wed Oct 9 13:03:13 2013 New Revision: 203316 URL: http://gcc.gnu.org/viewcvs?rev=203316&root=gcc&view=rev Log: 2013-10-09 Marc Glisse <marc.glisse@inria.fr> PR tree-optimization/20318 gcc/c-family/ * c-common.c (handle_returns_nonnull_attribute): New function. (c_common_attribute_table): Add returns_nonnull. gcc/ * doc/extend.texi (returns_nonnull): New function attribute. * fold-const.c (tree_expr_nonzero_warnv_p): Look for returns_nonnull attribute. * tree-vrp.c (gimple_stmt_nonzero_warnv_p): Likewise. (stmt_interesting_for_vrp): Accept all GIMPLE_CALL. gcc/testsuite/ * c-c++-common/pr20318.c: New file. * gcc.dg/tree-ssa/pr20318.c: New file. Added: trunk/gcc/testsuite/c-c++-common/pr20318.c (with props) trunk/gcc/testsuite/gcc.dg/tree-ssa/pr20318.c (with props) Modified: trunk/gcc/ChangeLog trunk/gcc/c-family/ChangeLog trunk/gcc/c-family/c-common.c trunk/gcc/doc/extend.texi trunk/gcc/fold-const.c trunk/gcc/testsuite/ChangeLog trunk/gcc/tree-vrp.c Propchange: trunk/gcc/testsuite/c-c++-common/pr20318.c ('svn:eol-style' added) Propchange: trunk/gcc/testsuite/c-c++-common/pr20318.c ('svn:keywords' added) Propchange: trunk/gcc/testsuite/gcc.dg/tree-ssa/pr20318.c ('svn:eol-style' added) Propchange: trunk/gcc/testsuite/gcc.dg/tree-ssa/pr20318.c ('svn:keywords' added)
Marking svn_fs_fs__id_parse and svn_error_create with the new __attribute__((__returns_nonnull__)) makes the warning about root_id disappear at -O3, so I am going to mark this bug as fixed, please reopen if necessary.