On 6/12/19 1:22 PM, Jakub Jelinek wrote:
> On Wed, Jun 12, 2019 at 01:11:09PM +0200, Martin Liška wrote:
>> 2019-06-12  Martin Liska  <mli...@suse.cz>
>>
>>      * calls.c (special_function_p): Make it global.
>>      * calls.h (special_function_p): Declare.
> 
> Why?

Not needed any longer.

> 
>>      * tree-cfg.c (do_warn_unused_result): Do not
>>      warn for alloca(0).
>> --- a/gcc/tree-cfg.c
>> +++ b/gcc/tree-cfg.c
>> @@ -63,6 +63,7 @@ along with GCC; see the file COPYING3.  If not see
>>  #include "opts.h"
>>  #include "asan.h"
>>  #include "profile.h"
>> +#include "calls.h"
>>  
>>  /* This file contains functions for building the Control Flow Graph (CFG)
>>     for a function tree.  */
>> @@ -9447,10 +9448,17 @@ do_warn_unused_result (gimple_seq seq)
>>            location_t loc = gimple_location (g);
>>  
>>            if (fdecl)
>> -            warning_at (loc, OPT_Wunused_result,
>> -                        "ignoring return value of %qD "
>> -                        "declared with attribute %<warn_unused_result%>",
>> -                        fdecl);
>> +            {
>> +              if ((special_function_p (fdecl, 0) & ECF_MAY_BE_ALLOCA)
> 
> Why not instead gimple_maybe_alloca_call_p (g) ?

Because I was not aware of the function ;)

> On the other side, you want && gimple_call_num_args (g) == 1,

Sure.

> if some alloca call had wrong declaration, you might ICE otherwise.
> 
>> +                  && TREE_CODE (gimple_call_arg (g, 0)) == INTEGER_CST
>> +                  && wi::to_wide (gimple_call_arg (g, 0)) == 0)
> 
> && integer_zerop (gimple_call_arg (g, 0))
> 
> instead?

Yep.

> 
> Plus you need a comment explaining why we don't warn about alloca with
> constant 0 argument.

Also done.

Is it fine now?
Martin

> 
>       Jakub
> 

>From dfdb688206cadd7fb9450ba005e8aa465ae61f7e Mon Sep 17 00:00:00 2001
From: Martin Liska <mli...@suse.cz>
Date: Wed, 12 Jun 2019 12:22:36 +0200
Subject: [PATCH] Do not warn with warn_unused_result for alloca(0).

gcc/ChangeLog:

2019-06-12  Martin Liska  <mli...@suse.cz>

	* tree-cfg.c (do_warn_unused_result): Do not
	warn for alloca(0) as it's used by some C
	libraries to release previously allocated memory
	via alloca calls.

gcc/testsuite/ChangeLog:

2019-06-12  Martin Liska  <mli...@suse.cz>

	* gcc.dg/pr78902.c: Add testing of alloca(0).
	* gcc.dg/attr-alloc_size-5.c: Do not warn
	about alloca(0).
---
 gcc/testsuite/gcc.dg/attr-alloc_size-5.c |  2 +-
 gcc/testsuite/gcc.dg/pr78902.c           |  1 +
 gcc/tree-cfg.c                           | 18 ++++++++++++++----
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/attr-alloc_size-5.c b/gcc/testsuite/gcc.dg/attr-alloc_size-5.c
index 7aa7cbf0c72..26ee43f87de 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); /* { dg-warning "ignoring return value of '.*' declared with attribute 'warn_unused_result'" } */
+  alloca (0);
 }
diff --git a/gcc/testsuite/gcc.dg/pr78902.c b/gcc/testsuite/gcc.dg/pr78902.c
index 49efc970475..f0f4314d684 100644
--- a/gcc/testsuite/gcc.dg/pr78902.c
+++ b/gcc/testsuite/gcc.dg/pr78902.c
@@ -7,6 +7,7 @@ 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_alloca (0);
  __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'" } */
diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c
index a585efea3d8..0d21f3624d7 100644
--- a/gcc/tree-cfg.c
+++ b/gcc/tree-cfg.c
@@ -63,6 +63,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "opts.h"
 #include "asan.h"
 #include "profile.h"
+#include "calls.h"
 
 /* This file contains functions for building the Control Flow Graph (CFG)
    for a function tree.  */
@@ -9447,10 +9448,19 @@ do_warn_unused_result (gimple_seq seq)
 	      location_t loc = gimple_location (g);
 
 	      if (fdecl)
-		warning_at (loc, OPT_Wunused_result,
-			    "ignoring return value of %qD "
-			    "declared with attribute %<warn_unused_result%>",
-			    fdecl);
+		{
+		  /* Some C libraries use alloca(0) in order to free previously
+		     allocated memory by alloca calls.  */
+		  if (gimple_maybe_alloca_call_p (g)
+		      && gimple_call_num_args (g) == 1
+		      && integer_zerop (gimple_call_arg (g, 0)))
+		    ;
+		  else
+		    warning_at (loc, OPT_Wunused_result,
+				"ignoring return value of %qD declared "
+				"with attribute %<warn_unused_result%>",
+				fdecl);
+		}
 	      else
 		warning_at (loc, OPT_Wunused_result,
 			    "ignoring return value of function "
-- 
2.21.0

Reply via email to