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
Created attachment 8331 [details]
Pre-processed SVN code.
Subject: Re: RFE: add attribute to specify that a function never
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
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.
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>;
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)>;
[ ... ]
*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.
Here is a reduced testcase for the SVN code:
static void *count_and_verify_instructions(int *ninst,const unsigned char *p)
if (p == ((void *)0))
*ninst = 0;
void *decode_window(const unsigned char *data)
void *svn_err__temp = count_and_verify_instructions(&ninst, data);
----- 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.
I have a patch already.
No longer working on this, I am too busy working on the gfortran front-end.
Date: Wed Oct 9 13:03:13 2013
New Revision: 203316
2013-10-09 Marc Glisse <email@example.com>
* c-common.c (handle_returns_nonnull_attribute): New function.
(c_common_attribute_table): Add returns_nonnull.
* doc/extend.texi (returns_nonnull): New function attribute.
* fold-const.c (tree_expr_nonzero_warnv_p): Look for returns_nonnull
* tree-vrp.c (gimple_stmt_nonzero_warnv_p): Likewise.
(stmt_interesting_for_vrp): Accept all GIMPLE_CALL.
* c-c++-common/pr20318.c: New file.
* gcc.dg/tree-ssa/pr20318.c: New file.
trunk/gcc/testsuite/c-c++-common/pr20318.c (with props)
trunk/gcc/testsuite/gcc.dg/tree-ssa/pr20318.c (with props)
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.