Bug 84203 - add -Wsuggest-attribute=returns_nonnull
Summary: add -Wsuggest-attribute=returns_nonnull
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 8.0
: P3 enhancement
Target Milestone: 14.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: 58689 new-warning, new_warning
  Show dependency treegraph
 
Reported: 2018-02-04 23:23 UTC by Martin Sebor
Modified: 2024-06-18 06:21 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-08-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2018-02-04 23:23:24 UTC
The option -Wsuggest-attribute= helps improve code generation by annotating function declarations with attributes like cold, const, pure, malloc and noreturn.  However, the option doesn't make it possible to request suggestions for the returns_nonnull attribute.  Especially in C++ where functions returning newly allocated memory often never return null (because they throw on failure to allocate), making use of the attribute could further improve generated code.

$ cat t.C && gcc -Wall -Wextra -O -S -Wsuggest-attribute=noreturn t.C
int f ()   // -Wsuggest-attribute=noreturn here (good)
{
  throw "not implemented";
}


void* f (unsigned n)   // missing -Wsuggest-attribute=returns_nonnull
{
  return new char[n];
}

t.C: In function ‘int f()’:
t.C:1:5: warning: function might be candidate for attribute ‘noreturn’ [-Wsuggest-attribute=noreturn]
 int f ()   // -Wsuggest-attribute=noreturn here (good)
     ^
Comment 1 Martin Sebor 2018-02-04 23:28:34 UTC
See also bug 84202.
Comment 2 Martin Sebor 2018-02-05 00:16:22 UTC
I just noticed -Wsuggest-attribute=returns_nonnull mentioned in bug 58689 comment #8.  I'm on the fence between resolving this as a duplicate of that bug and treating pr58689 as a meta-bug.  Let me go with the latter for now and make this a blocker of it.
Comment 3 Eric Gallager 2018-08-05 02:41:01 UTC
(In reply to Martin Sebor from comment #2)
> I just noticed -Wsuggest-attribute=returns_nonnull mentioned in bug 58689
> comment #8.  I'm on the fence between resolving this as a duplicate of that
> bug and treating pr58689 as a meta-bug.  Let me go with the latter for now
> and make this a blocker of it.

ok confirming this one too then
Comment 4 Peter Eisentraut 2024-06-18 05:48:22 UTC
This is implemented in gcc 14:

commit 53ba8d66955
Author: Jan Hubicka <jh@suse.cz>
Date:   Mon Nov 20 19:35:53 2023

    inter-procedural value range propagation

    implement very basic propapgation of return value ranges from VRP
    pass.  This helps std::vector's push_back since we work out value range of
    allocated block.  This propagates only within single translation unit.  I hoped
    we will also do the propagation at WPA stage, but that needs more work on
    ipa-cp side.

    I also added code auto-detecting return_nonnull and corresponding -Wsuggest-attribute.

[It is misspelled in the commit message.]
Comment 5 Sam James 2024-06-18 06:21:15 UTC
.