diff mbox series

Add warn_unused_result for malloc-like functions (PR tree-optimization/78902).

Message ID 023ffcfe-5967-4ff5-01fe-7e9523487c9b@suse.cz
State New
Headers show
Series Add warn_unused_result for malloc-like functions (PR tree-optimization/78902). | expand

Commit Message

Martin Liška June 6, 2019, 8:01 a.m. UTC
Hi.

The patch is about addition of warn_unused_attribute for malloc-like function.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

gcc/ChangeLog:

2019-06-06  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/78902
	* builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
	(ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
	(ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
	(ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
	(ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
	(ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
	(ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
	(ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
	New.
	(ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
	(ATTR_MALLOC_NOTHROW_NONNULL): Remove.
	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
	(ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
	* builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
	warn_unused_result attribute.
	(BUILT_IN_STRDUP): Likewise.
	(BUILT_IN_STRNDUP): Likewise.
	(BUILT_IN_ALLOCA): Likewise.
	(BUILT_IN_CALLOC): Likewise.
	(BUILT_IN_MALLOC): Likewise.
	(BUILT_IN_REALLOC): Likewise.

gcc/testsuite/ChangeLog:

2019-06-06  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/78902
	* c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
	of __builtin_alloca.
	* c-c++-common/asan/pr88619.c: Likewise.
	* g++.dg/overload/using2.C: Likewise for malloc.
	* gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
	* gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
	* gcc.dg/pr43643.c: Likewise.
	* gcc.dg/pr59717.c: Likewise for calloc.
	* gcc.dg/torture/pr71816.c: Likewise.
	* gcc.dg/tree-ssa/pr78886.c: Likewise.
	* gcc.dg/tree-ssa/pr79697.c: Likewise.
	* gcc.dg/pr78902.c: New test.
---
 gcc/builtin-attrs.def                         | 37 ++++++++++++-------
 gcc/builtins.def                              | 14 +++----
 .../asan/alloca_loop_unpoisoning.c            |  2 +-
 gcc/testsuite/c-c++-common/asan/pr88619.c     |  2 +-
 gcc/testsuite/g++.dg/overload/using2.C        |  2 +-
 gcc/testsuite/gcc.dg/attr-alloc_size-5.c      |  2 +-
 gcc/testsuite/gcc.dg/nonnull-3.c              |  4 +-
 gcc/testsuite/gcc.dg/pr43643.c                |  6 +--
 gcc/testsuite/gcc.dg/pr59717.c                |  8 ++--
 gcc/testsuite/gcc.dg/pr78902.c                | 14 +++++++
 gcc/testsuite/gcc.dg/torture/pr71816.c        |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/pr78886.c       |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/pr79697.c       |  6 +--
 13 files changed, 62 insertions(+), 39 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr78902.c

Comments

Martin Sebor June 6, 2019, 3:17 p.m. UTC | #1
On 6/6/19 2:01 AM, Martin Liška wrote:
> Hi.
> 
> The patch is about addition of warn_unused_attribute for malloc-like function.
> 
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

I like this change (as you know :)  Just one question: should
all allocation functions be also annotated, including the two
variants of __builtin_alloca_with_align and
__builtin_posix_memalign?

As a separate comment, to get the benefit of the attribute in
GCC we might want to also annotate the wrappers in libiberty.h
and perhaps also some (many?) of the functions in tree.h.
(This is just a suggestion to think about independent of your
change.)

Martin

> 
> Ready to be installed?
> Thanks,
> Martin
> 
> gcc/ChangeLog:
> 
> 2019-06-06  Martin Liska  <mliska@suse.cz>
> 
> 	PR tree-optimization/78902
> 	* builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
> 	(ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
> 	(ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
> 	(ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
> 	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
> 	(ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
> 	New.
> 	(ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_NOTHROW_NONNULL): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
> 	(ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
> 	* builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
> 	warn_unused_result attribute.
> 	(BUILT_IN_STRDUP): Likewise.
> 	(BUILT_IN_STRNDUP): Likewise.
> 	(BUILT_IN_ALLOCA): Likewise.
> 	(BUILT_IN_CALLOC): Likewise.
> 	(BUILT_IN_MALLOC): Likewise.
> 	(BUILT_IN_REALLOC): Likewise.
> 
> gcc/testsuite/ChangeLog:
> 
> 2019-06-06  Martin Liska  <mliska@suse.cz>
> 
> 	PR tree-optimization/78902
> 	* c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
> 	of __builtin_alloca.
> 	* c-c++-common/asan/pr88619.c: Likewise.
> 	* g++.dg/overload/using2.C: Likewise for malloc.
> 	* gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
> 	* gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
> 	* gcc.dg/pr43643.c: Likewise.
> 	* gcc.dg/pr59717.c: Likewise for calloc.
> 	* gcc.dg/torture/pr71816.c: Likewise.
> 	* gcc.dg/tree-ssa/pr78886.c: Likewise.
> 	* gcc.dg/tree-ssa/pr79697.c: Likewise.
> 	* gcc.dg/pr78902.c: New test.
> ---
>   gcc/builtin-attrs.def                         | 37 ++++++++++++-------
>   gcc/builtins.def                              | 14 +++----
>   .../asan/alloca_loop_unpoisoning.c            |  2 +-
>   gcc/testsuite/c-c++-common/asan/pr88619.c     |  2 +-
>   gcc/testsuite/g++.dg/overload/using2.C        |  2 +-
>   gcc/testsuite/gcc.dg/attr-alloc_size-5.c      |  2 +-
>   gcc/testsuite/gcc.dg/nonnull-3.c              |  4 +-
>   gcc/testsuite/gcc.dg/pr43643.c                |  6 +--
>   gcc/testsuite/gcc.dg/pr59717.c                |  8 ++--
>   gcc/testsuite/gcc.dg/pr78902.c                | 14 +++++++
>   gcc/testsuite/gcc.dg/torture/pr71816.c        |  2 +-
>   gcc/testsuite/gcc.dg/tree-ssa/pr78886.c       |  2 +-
>   gcc/testsuite/gcc.dg/tree-ssa/pr79697.c       |  6 +--
>   13 files changed, 62 insertions(+), 39 deletions(-)
>   create mode 100644 gcc/testsuite/gcc.dg/pr78902.c
> 
>
Jeff Law June 6, 2019, 4:58 p.m. UTC | #2
On 6/6/19 9:17 AM, Martin Sebor wrote:
> On 6/6/19 2:01 AM, Martin Liška wrote:
>> Hi.
>>
>> The patch is about addition of warn_unused_attribute for malloc-like
>> function.
>>
>> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> I like this change (as you know :)  Just one question: should
> all allocation functions be also annotated, including the two
> variants of __builtin_alloca_with_align and
> __builtin_posix_memalign?
> 
> As a separate comment, to get the benefit of the attribute in
> GCC we might want to also annotate the wrappers in libiberty.h
> and perhaps also some (many?) of the functions in tree.h.
> (This is just a suggestion to think about independent of your
> change.)
I like it conceptually as well -- I think the glibc guys had pretty good
results exposing latent issues when they went through and did the
necessary markup for WUR warnings.

Jeff
Jeff Law June 7, 2019, 12:02 a.m. UTC | #3
On 6/6/19 2:01 AM, Martin Liška wrote:
> Hi.
> 
> The patch is about addition of warn_unused_attribute for malloc-like function.
> 
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> Ready to be installed?
> Thanks,
> Martin
> 
> gcc/ChangeLog:
> 
> 2019-06-06  Martin Liska  <mliska@suse.cz>
> 
> 	PR tree-optimization/78902
> 	* builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
> 	(ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
> 	(ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
> 	(ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
> 	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
> 	(ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
> 	New.
> 	(ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_NOTHROW_NONNULL): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
> 	(ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
> 	* builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
> 	warn_unused_result attribute.
> 	(BUILT_IN_STRDUP): Likewise.
> 	(BUILT_IN_STRNDUP): Likewise.
> 	(BUILT_IN_ALLOCA): Likewise.
> 	(BUILT_IN_CALLOC): Likewise.
> 	(BUILT_IN_MALLOC): Likewise.
> 	(BUILT_IN_REALLOC): Likewise.
> 
> gcc/testsuite/ChangeLog:
> 
> 2019-06-06  Martin Liska  <mliska@suse.cz>
> 
> 	PR tree-optimization/78902
> 	* c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
> 	of __builtin_alloca.
> 	* c-c++-common/asan/pr88619.c: Likewise.
> 	* g++.dg/overload/using2.C: Likewise for malloc.
> 	* gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
> 	* gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
> 	* gcc.dg/pr43643.c: Likewise.
> 	* gcc.dg/pr59717.c: Likewise for calloc.
> 	* gcc.dg/torture/pr71816.c: Likewise.
> 	* gcc.dg/tree-ssa/pr78886.c: Likewise.
> 	* gcc.dg/tree-ssa/pr79697.c: Likewise.
> 	* gcc.dg/pr78902.c: New test.
OK.

Any thoughts on whether or not we'd want to IPA propagate this attribute
like we do for the malloc attribute?

jeff
> ---
Martin Liška June 7, 2019, 5:27 a.m. UTC | #4
On 6/6/19 5:17 PM, Martin Sebor wrote:
> On 6/6/19 2:01 AM, Martin Liška wrote:
>> Hi.
>>
>> The patch is about addition of warn_unused_attribute for malloc-like function.
>>
>> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> I like this change (as you know :)  Just one question: should
> all allocation functions be also annotated, including the two
> variants of __builtin_alloca_with_align and
> __builtin_posix_memalign?

Hi, we define the first one (and __builtin_alloca_with_align_and_max) in builtins.def
as: /* Implementing variable sized local variables.  */
and define just a stub: DEF_BUILTIN_STUB

Do you know why is that a variable sized function?
The later one is a bit different as it does not return memory, but return code.
Glibc does not mark it with warn_unused_result attr.

> 
> As a separate comment, to get the benefit of the attribute in
> GCC we might want to also annotate the wrappers in libiberty.h
> and perhaps also some (many?) of the functions in tree.h.
> (This is just a suggestion to think about independent of your
> change.)

Good idea, I can prepare a patch for it as well.

Martin

> 
> Martin
> 
>>
>> Ready to be installed?
>> Thanks,
>> Martin
>>
>> gcc/ChangeLog:
>>
>> 2019-06-06  Martin Liska  <mliska@suse.cz>
>>
>>     PR tree-optimization/78902
>>     * builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
>>     (ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
>>     (ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
>>     (ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
>>     (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
>>     (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
>>     (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
>>     (ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
>>     (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
>>     New.
>>     (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
>>     (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
>>     (ATTR_MALLOC_NOTHROW_NONNULL): Remove.
>>     (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
>>     (ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
>>     (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
>>     * builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
>>     warn_unused_result attribute.
>>     (BUILT_IN_STRDUP): Likewise.
>>     (BUILT_IN_STRNDUP): Likewise.
>>     (BUILT_IN_ALLOCA): Likewise.
>>     (BUILT_IN_CALLOC): Likewise.
>>     (BUILT_IN_MALLOC): Likewise.
>>     (BUILT_IN_REALLOC): Likewise.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 2019-06-06  Martin Liska  <mliska@suse.cz>
>>
>>     PR tree-optimization/78902
>>     * c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
>>     of __builtin_alloca.
>>     * c-c++-common/asan/pr88619.c: Likewise.
>>     * g++.dg/overload/using2.C: Likewise for malloc.
>>     * gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
>>     * gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
>>     * gcc.dg/pr43643.c: Likewise.
>>     * gcc.dg/pr59717.c: Likewise for calloc.
>>     * gcc.dg/torture/pr71816.c: Likewise.
>>     * gcc.dg/tree-ssa/pr78886.c: Likewise.
>>     * gcc.dg/tree-ssa/pr79697.c: Likewise.
>>     * gcc.dg/pr78902.c: New test.
>> ---
>>   gcc/builtin-attrs.def                         | 37 ++++++++++++-------
>>   gcc/builtins.def                              | 14 +++----
>>   .../asan/alloca_loop_unpoisoning.c            |  2 +-
>>   gcc/testsuite/c-c++-common/asan/pr88619.c     |  2 +-
>>   gcc/testsuite/g++.dg/overload/using2.C        |  2 +-
>>   gcc/testsuite/gcc.dg/attr-alloc_size-5.c      |  2 +-
>>   gcc/testsuite/gcc.dg/nonnull-3.c              |  4 +-
>>   gcc/testsuite/gcc.dg/pr43643.c                |  6 +--
>>   gcc/testsuite/gcc.dg/pr59717.c                |  8 ++--
>>   gcc/testsuite/gcc.dg/pr78902.c                | 14 +++++++
>>   gcc/testsuite/gcc.dg/torture/pr71816.c        |  2 +-
>>   gcc/testsuite/gcc.dg/tree-ssa/pr78886.c       |  2 +-
>>   gcc/testsuite/gcc.dg/tree-ssa/pr79697.c       |  6 +--
>>   13 files changed, 62 insertions(+), 39 deletions(-)
>>   create mode 100644 gcc/testsuite/gcc.dg/pr78902.c
>>
>>
>
Martin Liška June 7, 2019, 6:27 a.m. UTC | #5
On 6/7/19 2:02 AM, Jeff Law wrote:
> Any thoughts on whether or not we'd want to IPA propagate this attribute
> like we do for the malloc attribute?

If I see correctly, the pass_warn_unused_result is run very early in the early
tree optimizations. Thus we can't propagate that right now.

Martin
Martin Sebor June 7, 2019, 3:09 p.m. UTC | #6
On 6/6/19 11:27 PM, Martin Liška wrote:
> On 6/6/19 5:17 PM, Martin Sebor wrote:
>> On 6/6/19 2:01 AM, Martin Liška wrote:
>>> Hi.
>>>
>>> The patch is about addition of warn_unused_attribute for malloc-like function.
>>>
>>> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
>>
>> I like this change (as you know :)  Just one question: should
>> all allocation functions be also annotated, including the two
>> variants of __builtin_alloca_with_align and
>> __builtin_posix_memalign?
> 
> Hi, we define the first one (and __builtin_alloca_with_align_and_max) in builtins.def
> as: /* Implementing variable sized local variables.  */
> and define just a stub: DEF_BUILTIN_STUB
> 
> Do you know why is that a variable sized function?

__builtin_alloca_with_align implements VLAs.  I think the stubs
are used for intrnisics that were originally meant to be called
exclusively by GCC alone and not directly by programs.

> The later one is a bit different as it does not return memory, but return code.
> Glibc does not mark it with warn_unused_result attr.

My mistake.  I confused posix_memalign with aligned_alloc.

Martin


> 
>>
>> As a separate comment, to get the benefit of the attribute in
>> GCC we might want to also annotate the wrappers in libiberty.h
>> and perhaps also some (many?) of the functions in tree.h.
>> (This is just a suggestion to think about independent of your
>> change.)
> 
> Good idea, I can prepare a patch for it as well.
> 
> Martin
> 
>>
>> Martin
>>
>>>
>>> Ready to be installed?
>>> Thanks,
>>> Martin
>>>
>>> gcc/ChangeLog:
>>>
>>> 2019-06-06  Martin Liska  <mliska@suse.cz>
>>>
>>>      PR tree-optimization/78902
>>>      * builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
>>>      (ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
>>>      (ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
>>>      (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
>>>      (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
>>>      (ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
>>>      New.
>>>      (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
>>>      (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_MALLOC_NOTHROW_NONNULL): Remove.
>>>      (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
>>>      (ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
>>>      (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
>>>      * builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
>>>      warn_unused_result attribute.
>>>      (BUILT_IN_STRDUP): Likewise.
>>>      (BUILT_IN_STRNDUP): Likewise.
>>>      (BUILT_IN_ALLOCA): Likewise.
>>>      (BUILT_IN_CALLOC): Likewise.
>>>      (BUILT_IN_MALLOC): Likewise.
>>>      (BUILT_IN_REALLOC): Likewise.
>>>
>>> gcc/testsuite/ChangeLog:
>>>
>>> 2019-06-06  Martin Liska  <mliska@suse.cz>
>>>
>>>      PR tree-optimization/78902
>>>      * c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
>>>      of __builtin_alloca.
>>>      * c-c++-common/asan/pr88619.c: Likewise.
>>>      * g++.dg/overload/using2.C: Likewise for malloc.
>>>      * gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
>>>      * gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
>>>      * gcc.dg/pr43643.c: Likewise.
>>>      * gcc.dg/pr59717.c: Likewise for calloc.
>>>      * gcc.dg/torture/pr71816.c: Likewise.
>>>      * gcc.dg/tree-ssa/pr78886.c: Likewise.
>>>      * gcc.dg/tree-ssa/pr79697.c: Likewise.
>>>      * gcc.dg/pr78902.c: New test.
>>> ---
>>>    gcc/builtin-attrs.def                         | 37 ++++++++++++-------
>>>    gcc/builtins.def                              | 14 +++----
>>>    .../asan/alloca_loop_unpoisoning.c            |  2 +-
>>>    gcc/testsuite/c-c++-common/asan/pr88619.c     |  2 +-
>>>    gcc/testsuite/g++.dg/overload/using2.C        |  2 +-
>>>    gcc/testsuite/gcc.dg/attr-alloc_size-5.c      |  2 +-
>>>    gcc/testsuite/gcc.dg/nonnull-3.c              |  4 +-
>>>    gcc/testsuite/gcc.dg/pr43643.c                |  6 +--
>>>    gcc/testsuite/gcc.dg/pr59717.c                |  8 ++--
>>>    gcc/testsuite/gcc.dg/pr78902.c                | 14 +++++++
>>>    gcc/testsuite/gcc.dg/torture/pr71816.c        |  2 +-
>>>    gcc/testsuite/gcc.dg/tree-ssa/pr78886.c       |  2 +-
>>>    gcc/testsuite/gcc.dg/tree-ssa/pr79697.c       |  6 +--
>>>    13 files changed, 62 insertions(+), 39 deletions(-)
>>>    create mode 100644 gcc/testsuite/gcc.dg/pr78902.c
>>>
>>>
>>
>
diff mbox series

Patch

diff --git a/gcc/builtin-attrs.def b/gcc/builtin-attrs.def
index 204141f6c53..39d1395f42a 100644
--- a/gcc/builtin-attrs.def
+++ b/gcc/builtin-attrs.def
@@ -118,6 +118,7 @@  DEF_ATTR_IDENT (ATTR_TM_REGPARM, "*tm regparm")
 DEF_ATTR_IDENT (ATTR_TM_TMPURE, "transaction_pure")
 DEF_ATTR_IDENT (ATTR_RETURNS_TWICE, "returns_twice")
 DEF_ATTR_IDENT (ATTR_RETURNS_NONNULL, "returns_nonnull")
+DEF_ATTR_IDENT (ATTR_WARN_UNUSED_RESULT, "warn_unused_result")
 
 DEF_ATTR_TREE_LIST (ATTR_NOVOPS_LIST, ATTR_NOVOPS, ATTR_NULL, ATTR_NULL)
 
@@ -157,8 +158,10 @@  DEF_ATTR_TREE_LIST (ATTR_CONST_NORETURN_NOTHROW_LEAF_COLD_LIST, ATTR_COLD,\
 			ATTR_NULL, ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST)
 DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_LIST, ATTR_MALLOC,	\
 			ATTR_NULL, ATTR_NOTHROW_LIST)
-DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_LEAF_LIST, ATTR_MALLOC,	\
+DEF_ATTR_TREE_LIST (ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST, ATTR_WARN_UNUSED_RESULT,	\
 			ATTR_NULL, ATTR_NOTHROW_LEAF_LIST)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST, ATTR_MALLOC,	\
+			ATTR_NULL, ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST)
 DEF_ATTR_TREE_LIST (ATTR_SENTINEL_NOTHROW_LIST, ATTR_SENTINEL,	\
 			ATTR_NULL, ATTR_NOTHROW_LIST)
 DEF_ATTR_TREE_LIST (ATTR_SENTINEL_NOTHROW_LEAF_LIST, ATTR_SENTINEL,	\
@@ -170,24 +173,26 @@  DEF_ATTR_TREE_LIST (ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST, ATTR_CONST,\
    with _SIZE_1, or second argument with _SIZE_2, specifies the size
    of the allocated object.  */
 DEF_ATTR_TREE_LIST (ATTR_MALLOC_SIZE_1_NOTHROW_LIST, ATTR_ALLOC_SIZE,	\
-			ATTR_LIST_1, ATTR_MALLOC_NOTHROW_LIST)
-DEF_ATTR_TREE_LIST (ATTR_ALLOC_SIZE_2_NOTHROW_LIST, ATTR_ALLOC_SIZE,	\
-			ATTR_LIST_2, ATTR_MALLOC_NOTHROW_LIST)
-DEF_ATTR_TREE_LIST (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
-		        ATTR_LIST_1, ATTR_MALLOC_NOTHROW_LEAF_LIST)
+		    ATTR_LIST_1, ATTR_MALLOC_NOTHROW_LIST)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST, ATTR_WARN_UNUSED_RESULT,	\
+		    ATTR_NULL, ATTR_MALLOC_NOTHROW_LIST)
+DEF_ATTR_TREE_LIST (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST, ATTR_ALLOC_SIZE,	\
+		    ATTR_LIST_2, ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
+		    ATTR_LIST_1, ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST)
 /* Alloca is just like malloc except that it never returns null.  */
-DEF_ATTR_TREE_LIST (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST, ATTR_RETURNS_NONNULL,
-		    ATTR_NULL, ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST)
+DEF_ATTR_TREE_LIST (ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST, ATTR_RETURNS_NONNULL,
+		    ATTR_NULL, ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST)
 
 /* Allocation functions like calloc the product of whose first two arguments
    specifies the size of the allocated object.  */
-DEF_ATTR_TREE_LIST (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
-		        ATTR_LIST_1_2, ATTR_MALLOC_NOTHROW_LEAF_LIST)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
+		    ATTR_LIST_1_2, ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST)
 
 /* Allocation functions like realloc whose second argument specifies
    the size of the allocated object.  */
-DEF_ATTR_TREE_LIST (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
-		        ATTR_LIST_2, ATTR_NOTHROW_LEAF_LIST)
+DEF_ATTR_TREE_LIST (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
+		    ATTR_LIST_2, ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST)
 
 /* Functions whose pointer parameter(s) are all nonnull.  */
 DEF_ATTR_TREE_LIST (ATTR_NONNULL_LIST, ATTR_NONNULL, ATTR_NULL, ATTR_NULL)
@@ -290,11 +295,15 @@  DEF_ATTR_TREE_LIST (ATTR_PURE_NOTHROW_NONNULL, ATTR_PURE, ATTR_NULL, \
 DEF_ATTR_TREE_LIST (ATTR_PURE_NOTHROW_NONNULL_LEAF, ATTR_PURE, ATTR_NULL, \
 			ATTR_NOTHROW_NONNULL_LEAF)
 /* Nothrow malloc functions whose pointer parameter(s) are all nonnull.  */
-DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_NONNULL, ATTR_MALLOC, ATTR_NULL, \
+DEF_ATTR_TREE_LIST (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL, ATTR_WARN_UNUSED_RESULT, ATTR_NULL, \
 			ATTR_NOTHROW_NONNULL)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL, ATTR_MALLOC, ATTR_NULL, \
+			ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL)
 /* Nothrow malloc leaf functions whose pointer parameter(s) are all nonnull.  */
-DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_NONNULL_LEAF, ATTR_MALLOC, ATTR_NULL, \
+DEF_ATTR_TREE_LIST (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF, ATTR_WARN_UNUSED_RESULT, ATTR_NULL, \
 			ATTR_NOTHROW_NONNULL_LEAF)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF, ATTR_MALLOC, ATTR_NULL, \
+			ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF)
 
 /* Construct a tree for the format attribute (and implicitly nonnull).  */
 #define DEF_FORMAT_ATTRIBUTE(TYPE, FA, VALUES)				 \
diff --git a/gcc/builtins.def b/gcc/builtins.def
index c92292aeab9..6d41bdb4f44 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -288,7 +288,7 @@  DEF_C99_BUILTIN        (BUILT_IN_ACOSH, "acosh", BT_FN_DOUBLE_DOUBLE, ATTR_MATHF
 DEF_C99_BUILTIN        (BUILT_IN_ACOSHF, "acoshf", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_C99_BUILTIN        (BUILT_IN_ACOSHL, "acoshl", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_C99_C90RES_BUILTIN (BUILT_IN_ACOSL, "acosl", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
-DEF_C11_BUILTIN        (BUILT_IN_ALIGNED_ALLOC, "aligned_alloc", BT_FN_PTR_SIZE_SIZE, ATTR_ALLOC_SIZE_2_NOTHROW_LIST)
+DEF_C11_BUILTIN        (BUILT_IN_ALIGNED_ALLOC, "aligned_alloc", BT_FN_PTR_SIZE_SIZE, ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST)
 DEF_LIB_BUILTIN        (BUILT_IN_ASIN, "asin", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_C99_C90RES_BUILTIN (BUILT_IN_ASINF, "asinf", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_C99_BUILTIN        (BUILT_IN_ASINH, "asinh", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING)
@@ -702,8 +702,8 @@  DEF_LIB_BUILTIN        (BUILT_IN_STRCHR, "strchr", BT_FN_STRING_CONST_STRING_INT
 DEF_LIB_BUILTIN        (BUILT_IN_STRCMP, "strcmp", BT_FN_INT_CONST_STRING_CONST_STRING, ATTR_PURE_NOTHROW_NONNULL_LEAF)
 DEF_LIB_BUILTIN        (BUILT_IN_STRCPY, "strcpy", BT_FN_STRING_STRING_CONST_STRING, ATTR_RET1_NOTHROW_NONNULL_LEAF)
 DEF_LIB_BUILTIN        (BUILT_IN_STRCSPN, "strcspn", BT_FN_SIZE_CONST_STRING_CONST_STRING, ATTR_PURE_NOTHROW_NONNULL_LEAF)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRDUP, "strdup", BT_FN_STRING_CONST_STRING, ATTR_MALLOC_NOTHROW_NONNULL_LEAF)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRNDUP, "strndup", BT_FN_STRING_CONST_STRING_SIZE, ATTR_MALLOC_NOTHROW_NONNULL_LEAF)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRDUP, "strdup", BT_FN_STRING_CONST_STRING, ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRNDUP, "strndup", BT_FN_STRING_CONST_STRING_SIZE, ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF)
 DEF_LIB_BUILTIN        (BUILT_IN_STRLEN, "strlen", BT_FN_SIZE_CONST_STRING, ATTR_PURE_NOTHROW_NONNULL_LEAF)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRNCASECMP, "strncasecmp", BT_FN_INT_CONST_STRING_CONST_STRING_SIZE, ATTR_PURE_NOTHROW_NONNULL_LEAF)
 DEF_LIB_BUILTIN        (BUILT_IN_STRNCAT, "strncat", BT_FN_STRING_STRING_CONST_STRING_SIZE, ATTR_RET1_NOTHROW_NONNULL_LEAF)
@@ -811,7 +811,7 @@  DEF_GCC_BUILTIN        (BUILT_IN_UMULLL_OVERFLOW, "umulll_overflow", BT_FN_BOOL_
 DEF_LIB_BUILTIN        (BUILT_IN_ABORT, "abort", BT_FN_VOID, ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST)
 DEF_LIB_BUILTIN        (BUILT_IN_ABS, "abs", BT_FN_INT_INT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_AGGREGATE_INCOMING_ADDRESS, "aggregate_incoming_address", BT_FN_PTR_VAR, ATTR_LEAF_LIST)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_ALLOCA, "alloca", BT_FN_PTR_SIZE, ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_ALLOCA, "alloca", BT_FN_PTR_SIZE, ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_APPLY, "apply", BT_FN_PTR_PTR_FN_VOID_VAR_PTR_SIZE, ATTR_NULL)
 DEF_GCC_BUILTIN        (BUILT_IN_APPLY_ARGS, "apply_args", BT_FN_PTR_VAR, ATTR_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_BSWAP16, "bswap16", BT_FN_UINT16_UINT16, ATTR_CONST_NOTHROW_LEAF_LIST)
@@ -819,7 +819,7 @@  DEF_GCC_BUILTIN        (BUILT_IN_BSWAP32, "bswap32", BT_FN_UINT32_UINT32, ATTR_C
 DEF_GCC_BUILTIN        (BUILT_IN_BSWAP64, "bswap64", BT_FN_UINT64_UINT64, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_CLEAR_CACHE, "__clear_cache", BT_FN_VOID_PTR_PTR, ATTR_NOTHROW_LEAF_LIST)
 /* [trans-mem]: Adjust BUILT_IN_TM_CALLOC if BUILT_IN_CALLOC is changed.  */
-DEF_LIB_BUILTIN        (BUILT_IN_CALLOC, "calloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_CALLOC, "calloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_CLASSIFY_TYPE, "classify_type", BT_FN_INT_VAR, ATTR_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_CLZ, "clz", BT_FN_INT_UINT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_CLZIMAX, "clzimax", BT_FN_INT_UINTMAX, ATTR_CONST_NOTHROW_LEAF_LIST)
@@ -896,7 +896,7 @@  DEF_LIB_BUILTIN        (BUILT_IN_LABS, "labs", BT_FN_LONG_LONG, ATTR_CONST_NOTHR
 DEF_C99_BUILTIN        (BUILT_IN_LLABS, "llabs", BT_FN_LONGLONG_LONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_LONGJMP, "longjmp", BT_FN_VOID_PTR_INT, ATTR_NORETURN_NOTHROW_LIST)
 /* [trans-mem]: Adjust BUILT_IN_TM_MALLOC if BUILT_IN_MALLOC is changed.  */
-DEF_LIB_BUILTIN        (BUILT_IN_MALLOC, "malloc", BT_FN_PTR_SIZE, ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_MALLOC, "malloc", BT_FN_PTR_SIZE, ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_NEXT_ARG, "next_arg", BT_FN_PTR_VAR, ATTR_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_PARITY, "parity", BT_FN_INT_UINT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_PARITYIMAX, "parityimax", BT_FN_INT_UINTMAX, ATTR_CONST_NOTHROW_LEAF_LIST)
@@ -908,7 +908,7 @@  DEF_GCC_BUILTIN        (BUILT_IN_POPCOUNTL, "popcountl", BT_FN_INT_ULONG, ATTR_C
 DEF_GCC_BUILTIN        (BUILT_IN_POPCOUNTLL, "popcountll", BT_FN_INT_ULONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_POSIX_MEMALIGN, "posix_memalign", BT_FN_INT_PTRPTR_SIZE_SIZE, ATTR_NOTHROW_NONNULL_LEAF)
 DEF_GCC_BUILTIN        (BUILT_IN_PREFETCH, "prefetch", BT_FN_VOID_CONST_PTR_VAR, ATTR_NOVOPS_LEAF_LIST)
-DEF_LIB_BUILTIN        (BUILT_IN_REALLOC, "realloc", BT_FN_PTR_PTR_SIZE, ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_REALLOC, "realloc", BT_FN_PTR_PTR_SIZE, ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_RETURN, "return", BT_FN_VOID_PTR, ATTR_NORETURN_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_RETURN_ADDRESS, "return_address", BT_FN_PTR_UINT, ATTR_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_SAVEREGS, "saveregs", BT_FN_PTR_VAR, ATTR_NULL)
diff --git a/gcc/testsuite/c-c++-common/asan/alloca_loop_unpoisoning.c b/gcc/testsuite/c-c++-common/asan/alloca_loop_unpoisoning.c
index 55345fdb3a5..3050d3ba94b 100644
--- a/gcc/testsuite/c-c++-common/asan/alloca_loop_unpoisoning.c
+++ b/gcc/testsuite/c-c++-common/asan/alloca_loop_unpoisoning.c
@@ -17,7 +17,7 @@  __attribute__((noinline)) void foo(int len) {
   top = &x;
   volatile char array[len];
   assert(!((uintptr_t) array & 31L));
-  __builtin_alloca(len);
+  void *p = __builtin_alloca(len);
   for (int i = 0; i < thirty_two; ++i) {
     char array[i];
     bot = array;
diff --git a/gcc/testsuite/c-c++-common/asan/pr88619.c b/gcc/testsuite/c-c++-common/asan/pr88619.c
index aab8a4c4c9c..fd3fbf1a73a 100644
--- a/gcc/testsuite/c-c++-common/asan/pr88619.c
+++ b/gcc/testsuite/c-c++-common/asan/pr88619.c
@@ -10,5 +10,5 @@  main ()
   A b;
   int *p = &b;
   *(p - 1) = 123;
-  __builtin_alloca (b);
+  void *p2 = __builtin_alloca (b);
 }
diff --git a/gcc/testsuite/g++.dg/overload/using2.C b/gcc/testsuite/g++.dg/overload/using2.C
index b1d52f7cf43..edfd1a10ac4 100644
--- a/gcc/testsuite/g++.dg/overload/using2.C
+++ b/gcc/testsuite/g++.dg/overload/using2.C
@@ -69,7 +69,7 @@  using std::C3; using other::C3;
   long C3 (long) throw ();
 
 int main () {
-  malloc (0);
+  void *p = malloc (0);
   exit (0);
 
   _exit (0); // { dg-error "ambiguous" }
diff --git a/gcc/testsuite/gcc.dg/attr-alloc_size-5.c b/gcc/testsuite/gcc.dg/attr-alloc_size-5.c
index 26ee43f87de..7aa7cbf0c72 100644
--- a/gcc/testsuite/gcc.dg/attr-alloc_size-5.c
+++ b/gcc/testsuite/gcc.dg/attr-alloc_size-5.c
@@ -230,5 +230,5 @@  test_alloca (size_t n)
 {
   extern void* alloca (size_t);
 
-  alloca (0);
+  alloca (0); /* { dg-warning "ignoring return value of '.*' declared with attribute 'warn_unused_result'" } */
 }
diff --git a/gcc/testsuite/gcc.dg/nonnull-3.c b/gcc/testsuite/gcc.dg/nonnull-3.c
index 040248c53aa..6f7bc4f4295 100644
--- a/gcc/testsuite/gcc.dg/nonnull-3.c
+++ b/gcc/testsuite/gcc.dg/nonnull-3.c
@@ -56,8 +56,8 @@  foo (void *p, char *s)
   __builtin_strspn (s, NULL);  /* { dg-warning "null" "null pointer check" } */
   __builtin_strchr (NULL, 16);  /* { dg-warning "null" "null pointer check" } */
   __builtin_strrchr (NULL, 16);  /* { dg-warning "null" "null pointer check" } */
-  __builtin_strdup (NULL);  /* { dg-warning "null" "null pointer check" } */
-  __builtin_strndup (NULL, 16);  /* { dg-warning "null" "null pointer check" } */
+  void *p1 = __builtin_strdup (NULL);  /* { dg-warning "null" "null pointer check" } */
+  void *p2 = __builtin_strndup (NULL, 16);  /* { dg-warning "null" "null pointer check" } */
 
   __builtin_nan (NULL);  /* { dg-warning "null" "null pointer check" } */
   __builtin_nanf (NULL);  /* { dg-warning "null" "null pointer check" } */
diff --git a/gcc/testsuite/gcc.dg/pr43643.c b/gcc/testsuite/gcc.dg/pr43643.c
index b0c57c0a6dc..43896abd85a 100644
--- a/gcc/testsuite/gcc.dg/pr43643.c
+++ b/gcc/testsuite/gcc.dg/pr43643.c
@@ -10,9 +10,9 @@  extern char *strdup (const char *);
 void
 func(char *a, char *b, char *c)
 {
-  strdup(a);
-  strdup(b);
-  strdup(c);
+  void *p = strdup(a);
+  p = strdup(b);
+  p = strdup(c);
 }
 
 int
diff --git a/gcc/testsuite/gcc.dg/pr59717.c b/gcc/testsuite/gcc.dg/pr59717.c
index e49020aa063..64e4f32197f 100644
--- a/gcc/testsuite/gcc.dg/pr59717.c
+++ b/gcc/testsuite/gcc.dg/pr59717.c
@@ -242,7 +242,7 @@  stdlib (void *p, void *q, __SIZE_TYPE__ sz)
 {
   abort (); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
-  calloc (sz, 1); /* { dg-warning "incompatible implicit" } */
+  void *p1 = calloc (sz, 1); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
   exit (1); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
@@ -252,11 +252,11 @@  stdlib (void *p, void *q, __SIZE_TYPE__ sz)
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
   llabs (1LL); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
-  malloc (sz); /* { dg-warning "incompatible implicit" } */
+  void *p2 = malloc (sz); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
-  realloc (p, sz); /* { dg-warning "incompatible implicit" } */
+  void *p3 = realloc (p, sz); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
-  aligned_alloc (sz, sz); /* { dg-warning "incompatible implicit" } */
+  void *p4 = aligned_alloc (sz, sz); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
 }
 
diff --git a/gcc/testsuite/gcc.dg/pr78902.c b/gcc/testsuite/gcc.dg/pr78902.c
new file mode 100644
index 00000000000..49efc970475
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr78902.c
@@ -0,0 +1,14 @@ 
+/* { dg-do compile } */
+
+void *ptr;
+
+void foo(void)
+{
+ __builtin_malloc (1); /* { dg-warning "ignoring return value of '__builtin_malloc' declared with attribute 'warn_unused_result'" } */
+ __builtin_calloc (10, 20); /* { dg-warning "ignoring return value of '__builtin_calloc' declared with attribute 'warn_unused_result'" } */
+ __builtin_alloca (10); /* { dg-warning "ignoring return value of '__builtin_alloca' declared with attribute 'warn_unused_result'" } */
+ __builtin_realloc (ptr, 100); /* { dg-warning "ignoring return value of '__builtin_realloc' declared with attribute 'warn_unused_result'" } */
+ __builtin_aligned_alloc (10, 16); /* { dg-warning "ignoring return value of '__builtin_aligned_alloc' declared with attribute 'warn_unused_result'" } */
+ __builtin_strdup ("pes"); /* { dg-warning "ignoring return value of '__builtin_strdup' declared with attribute 'warn_unused_result'" } */
+ __builtin_strndup ("pes", 10); /* { dg-warning "ignoring return value of '__builtin_strndup' declared with attribute 'warn_unused_result'" } */
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr71816.c b/gcc/testsuite/gcc.dg/torture/pr71816.c
index 179fad85cd1..be37ad921bd 100644
--- a/gcc/testsuite/gcc.dg/torture/pr71816.c
+++ b/gcc/testsuite/gcc.dg/torture/pr71816.c
@@ -11,7 +11,7 @@  void *realloc();
 int ext2fs_resize_mem(void *p1) {
     int size = 0;
     memcpy(&ext2fs_resize_mem_p, p1, sizeof(ext2fs_resize_mem_p));
-    realloc(&ext2fs_resize_mem_p, size);
+    void *p = realloc(&ext2fs_resize_mem_p, size);
     return 0;
 }
 struct ext2_icount_el *insert_icount_el() {
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr78886.c b/gcc/testsuite/gcc.dg/tree-ssa/pr78886.c
index de22b83ad25..7f62891b77b 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr78886.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr78886.c
@@ -7,6 +7,6 @@  void *malloc(size_t x);
 void foo(void)
 {
  volatile int i;
- malloc(1);
+ void *p = malloc(1);
  i;
 }
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
index 973ec0dc193..e62efd5de61 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
@@ -3,17 +3,17 @@ 
 
 void f(void)
 {
-  __builtin_strdup ("abc");
+  __builtin_strdup ("abc"); /* { dg-warning "ignoring return value of '__builtin_strdup' declared with attribute 'warn_unused_result'" } */
 }
 
 void g(void)
 {
-  __builtin_strndup ("abc", 3);
+  __builtin_strndup ("abc", 3); /* { dg-warning "ignoring return value of '__builtin_strndup' declared with attribute 'warn_unused_result'" } */
 }
 
 void h(void)
 {
-  __builtin_realloc (0, 10);
+  __builtin_realloc (0, 10); /* { dg-warning "ignoring return value of '__builtin_realloc' declared with attribute 'warn_unused_result'" } */
 }
 
 /* { dg-final { scan-tree-dump "Deleting : __builtin_strdup" "cddce1" } } */