* [patch] PR 54930: add -Wreturn-local-addr
@ 2012-10-18 18:47 Jonathan Wakely
2012-10-19 16:24 ` Joseph S. Myers
2012-10-21 21:44 ` Dodji Seketeli
0 siblings, 2 replies; 6+ messages in thread
From: Jonathan Wakely @ 2012-10-18 18:47 UTC (permalink / raw)
To: gcc-patches
[-- Attachment #1: Type: text/plain, Size: 963 bytes --]
This adds a warning switch for the existing "returning address of
local variable" warnings in the C and C++ FEs which are enabled by
default but have no switch controlling them. Adding a switch allows it
to be turned into an error with -Werror=return-local-addr. I've added
two tests checking the -Werror form works.
gcc/c-family:
PR c++/54930
* c.opt (Wreturn_local_addr): Define new option.
gcc/c:
PR c++/54930
* c-typeck.c (c_finish_return): Use OPT_Wreturn_local_addr.
gcc/cp:
PR c++/54930
* typeck.c (maybe_warn_about_returning_address_of_local): Use
OPT_Wreturn_local_addr.
gcc:
PR c++/54930
* doc/invoke.texi (Warning Options): Document -Wno-return-local-addr.
gcc/testsuite:
PR c++/54930
* gcc.dg/Wreturn-local-addr.c: New.
* g++.dg/warn/Wno-return-local-addr.C: New.
* g++.dg/warn/Wreturn-local-addr.C: New.
Tested x86_64-linux, OK for trunk?
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 5840 bytes --]
commit 77abe727e837e052680be8020fa2990cce4d1e9a
Author: Jonathan Wakely <jwakely.gcc@gmail.com>
Date: Wed Oct 17 19:32:10 2012 +0100
gcc/c-family:
PR c++/54930
* c.opt (Wreturn_local_addr): Define new option.
gcc/c:
PR c++/54930
* c-typeck.c (c_finish_return): Use OPT_Wreturn_local_addr.
gcc/cp:
PR c++/54930
* typeck.c (maybe_warn_about_returning_address_of_local): Use
OPT_Wreturn_local_addr.
gcc:
PR c++/54930
* doc/invoke.texi (Warning Options): Document -Wno-return-local-addr.
gcc/testsuite:
PR c++/54930
* gcc.dg/Wreturn-local-addr.c: New.
* g++.dg/warn/Wno-return-local-addr.C: New.
* g++.dg/warn/Wreturn-local-addr.C: New.
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 3166984..f75b58c 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -601,6 +601,10 @@ Wreorder
C++ ObjC++ Var(warn_reorder) Warning LangEnabledBy(C++ ObjC++,Wall)
Warn when the compiler reorders code
+Wreturn-local-addr
+C ObjC C++ ObjC++ Var(warn_return_local_addr) Init(1) Warning
+Warn about returning a pointer/reference to a local or temporary variable.
+
Wreturn-type
C ObjC C++ ObjC++ Var(warn_return_type) Warning
Warn whenever a function's return type defaults to \"int\" (C), or about inconsistent return types (C++)
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 4855933..cf63355 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -8742,7 +8742,8 @@ c_finish_return (location_t loc, tree retval, tree origtype)
&& !TREE_STATIC (inner)
&& DECL_CONTEXT (inner) == current_function_decl)
warning_at (loc,
- 0, "function returns address of local variable");
+ OPT_Wreturn_local_addr, "function returns address "
+ "of local variable");
break;
default:
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index eaa0935..d2944d3 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -8019,14 +8019,14 @@ maybe_warn_about_returning_address_of_local (tree retval)
if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
|| TREE_CODE (whats_returned) == TARGET_EXPR)
{
- warning (0, "returning reference to temporary");
+ warning (OPT_Wreturn_local_addr, "returning reference to temporary");
return;
}
if (TREE_CODE (whats_returned) == VAR_DECL
&& DECL_NAME (whats_returned)
&& TEMP_NAME_P (DECL_NAME (whats_returned)))
{
- warning (0, "reference to non-lvalue returned");
+ warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
return;
}
}
@@ -8042,10 +8042,10 @@ maybe_warn_about_returning_address_of_local (tree retval)
|| TREE_PUBLIC (whats_returned)))
{
if (TREE_CODE (valtype) == REFERENCE_TYPE)
- warning (0, "reference to local variable %q+D returned",
+ warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
whats_returned);
else
- warning (0, "address of local variable %q+D returned",
+ warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
whats_returned);
return;
}
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index f8c9230..2e36de1 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -261,7 +261,7 @@ Objective-C and Objective-C++ Dialects}.
-Woverlength-strings -Wpacked -Wpacked-bitfield-compat -Wpadded @gol
-Wparentheses -Wpedantic-ms-format -Wno-pedantic-ms-format @gol
-Wpointer-arith -Wno-pointer-to-int-cast @gol
--Wredundant-decls @gol
+-Wredundant-decls -Wno-return-local-addr @gol
-Wreturn-type -Wsequence-point -Wshadow @gol
-Wsign-compare -Wsign-conversion -Wsizeof-pointer-memaccess @gol
-Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol
@@ -3526,6 +3526,12 @@ definitions, may be found on the GCC readings page, at
This warning is enabled by @option{-Wall} for C and C++.
+@item -Wno-return-local-addr
+@opindex Wno-return-local-addr
+@opindex Wreturn-local-addr
+Do not warn about returning a pointer (or in C++, a reference) to a
+variable that goes out of scope after the function returns.
+
@item -Wreturn-type
@opindex Wreturn-type
@opindex Wno-return-type
diff --git a/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C b/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C
new file mode 100644
index 0000000..e15bfa2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C
@@ -0,0 +1,26 @@
+// { dg-do assemble }
+// { dg-options "-Wno-return-local-addr" }
+
+int& bad1()
+{
+ int x = 0;
+ return x;
+}
+
+int* bad2()
+{
+ int x = 0;
+ return &x;
+}
+
+int f();
+
+const int& bad3()
+{
+ return f();
+}
+
+const int& bad4()
+{
+ return int();
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C
new file mode 100644
index 0000000..faa3a34
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C
@@ -0,0 +1,20 @@
+// { dg-do assemble }
+// { dg-options "-Werror=return-local-addr" }
+// { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 }
+
+int& bad1()
+{
+ int x = 0; // { dg-error "reference to local variable" }
+ return x;
+}
+
+int* bad2()
+{
+ int x = 0; // { dg-error "address of local variable" }
+ return &x;
+}
+
+const int& bad4()
+{
+ return int(); // { dg-error "returning reference to temporary" }
+}
diff --git a/gcc/testsuite/gcc.dg/Wreturn-local-addr.c b/gcc/testsuite/gcc.dg/Wreturn-local-addr.c
new file mode 100644
index 0000000..d496d20
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wreturn-local-addr.c
@@ -0,0 +1,9 @@
+/* { dg-do assemble } */
+/* { dg-options "-Werror=return-local-addr" } */
+/* { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 } */
+
+int* bad()
+{
+ int x = 0;
+ return &x; /* { dg-error "address of local variable" } */
+}
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [patch] PR 54930: add -Wreturn-local-addr
2012-10-18 18:47 [patch] PR 54930: add -Wreturn-local-addr Jonathan Wakely
@ 2012-10-19 16:24 ` Joseph S. Myers
2012-10-21 21:44 ` Dodji Seketeli
1 sibling, 0 replies; 6+ messages in thread
From: Joseph S. Myers @ 2012-10-19 16:24 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: gcc-patches
On Thu, 18 Oct 2012, Jonathan Wakely wrote:
> This adds a warning switch for the existing "returning address of
> local variable" warnings in the C and C++ FEs which are enabled by
> default but have no switch controlling them. Adding a switch allows it
> to be turned into an error with -Werror=return-local-addr. I've added
> two tests checking the -Werror form works.
The C front-end changes are OK.
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [patch] PR 54930: add -Wreturn-local-addr
2012-10-18 18:47 [patch] PR 54930: add -Wreturn-local-addr Jonathan Wakely
2012-10-19 16:24 ` Joseph S. Myers
@ 2012-10-21 21:44 ` Dodji Seketeli
2012-10-29 21:26 ` Jonathan Wakely
1 sibling, 1 reply; 6+ messages in thread
From: Dodji Seketeli @ 2012-10-21 21:44 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: gcc-patches, Jason Merrill
The C++ bits look good to my casual non-maintainer eyes. Let's CC
Jason.
Thanks.
Jonathan Wakely <jwakely.gcc@gmail.com> a écrit:
> This adds a warning switch for the existing "returning address of
> local variable" warnings in the C and C++ FEs which are enabled by
> default but have no switch controlling them. Adding a switch allows it
> to be turned into an error with -Werror=return-local-addr. I've added
> two tests checking the -Werror form works.
>
> gcc/c-family:
> PR c++/54930
> * c.opt (Wreturn_local_addr): Define new option.
>
> gcc/c:
> PR c++/54930
> * c-typeck.c (c_finish_return): Use OPT_Wreturn_local_addr.
>
> gcc/cp:
> PR c++/54930
> * typeck.c (maybe_warn_about_returning_address_of_local): Use
> OPT_Wreturn_local_addr.
>
> gcc:
> PR c++/54930
> * doc/invoke.texi (Warning Options): Document -Wno-return-local-addr.
>
> gcc/testsuite:
> PR c++/54930
> * gcc.dg/Wreturn-local-addr.c: New.
> * g++.dg/warn/Wno-return-local-addr.C: New.
> * g++.dg/warn/Wreturn-local-addr.C: New.
>
> Tested x86_64-linux, OK for trunk?
>
> commit 77abe727e837e052680be8020fa2990cce4d1e9a
> Author: Jonathan Wakely <jwakely.gcc@gmail.com>
> Date: Wed Oct 17 19:32:10 2012 +0100
>
> gcc/c-family:
> PR c++/54930
> * c.opt (Wreturn_local_addr): Define new option.
>
> gcc/c:
> PR c++/54930
> * c-typeck.c (c_finish_return): Use OPT_Wreturn_local_addr.
>
> gcc/cp:
> PR c++/54930
> * typeck.c (maybe_warn_about_returning_address_of_local): Use
> OPT_Wreturn_local_addr.
>
> gcc:
> PR c++/54930
> * doc/invoke.texi (Warning Options): Document -Wno-return-local-addr.
>
> gcc/testsuite:
> PR c++/54930
> * gcc.dg/Wreturn-local-addr.c: New.
> * g++.dg/warn/Wno-return-local-addr.C: New.
> * g++.dg/warn/Wreturn-local-addr.C: New.
>
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index 3166984..f75b58c 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -601,6 +601,10 @@ Wreorder
> C++ ObjC++ Var(warn_reorder) Warning LangEnabledBy(C++ ObjC++,Wall)
> Warn when the compiler reorders code
>
> +Wreturn-local-addr
> +C ObjC C++ ObjC++ Var(warn_return_local_addr) Init(1) Warning
> +Warn about returning a pointer/reference to a local or temporary variable.
> +
> Wreturn-type
> C ObjC C++ ObjC++ Var(warn_return_type) Warning
> Warn whenever a function's return type defaults to \"int\" (C), or about inconsistent return types (C++)
> diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
> index 4855933..cf63355 100644
> --- a/gcc/c/c-typeck.c
> +++ b/gcc/c/c-typeck.c
> @@ -8742,7 +8742,8 @@ c_finish_return (location_t loc, tree retval, tree origtype)
> && !TREE_STATIC (inner)
> && DECL_CONTEXT (inner) == current_function_decl)
> warning_at (loc,
> - 0, "function returns address of local variable");
> + OPT_Wreturn_local_addr, "function returns address "
> + "of local variable");
> break;
>
> default:
> diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
> index eaa0935..d2944d3 100644
> --- a/gcc/cp/typeck.c
> +++ b/gcc/cp/typeck.c
> @@ -8019,14 +8019,14 @@ maybe_warn_about_returning_address_of_local (tree retval)
> if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
> || TREE_CODE (whats_returned) == TARGET_EXPR)
> {
> - warning (0, "returning reference to temporary");
> + warning (OPT_Wreturn_local_addr, "returning reference to temporary");
> return;
> }
> if (TREE_CODE (whats_returned) == VAR_DECL
> && DECL_NAME (whats_returned)
> && TEMP_NAME_P (DECL_NAME (whats_returned)))
> {
> - warning (0, "reference to non-lvalue returned");
> + warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
> return;
> }
> }
> @@ -8042,10 +8042,10 @@ maybe_warn_about_returning_address_of_local (tree retval)
> || TREE_PUBLIC (whats_returned)))
> {
> if (TREE_CODE (valtype) == REFERENCE_TYPE)
> - warning (0, "reference to local variable %q+D returned",
> + warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
> whats_returned);
> else
> - warning (0, "address of local variable %q+D returned",
> + warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
> whats_returned);
> return;
> }
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index f8c9230..2e36de1 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -261,7 +261,7 @@ Objective-C and Objective-C++ Dialects}.
> -Woverlength-strings -Wpacked -Wpacked-bitfield-compat -Wpadded @gol
> -Wparentheses -Wpedantic-ms-format -Wno-pedantic-ms-format @gol
> -Wpointer-arith -Wno-pointer-to-int-cast @gol
> --Wredundant-decls @gol
> +-Wredundant-decls -Wno-return-local-addr @gol
> -Wreturn-type -Wsequence-point -Wshadow @gol
> -Wsign-compare -Wsign-conversion -Wsizeof-pointer-memaccess @gol
> -Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol
> @@ -3526,6 +3526,12 @@ definitions, may be found on the GCC readings page, at
>
> This warning is enabled by @option{-Wall} for C and C++.
>
> +@item -Wno-return-local-addr
> +@opindex Wno-return-local-addr
> +@opindex Wreturn-local-addr
> +Do not warn about returning a pointer (or in C++, a reference) to a
> +variable that goes out of scope after the function returns.
> +
> @item -Wreturn-type
> @opindex Wreturn-type
> @opindex Wno-return-type
> diff --git a/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C b/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C
> new file mode 100644
> index 0000000..e15bfa2
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C
> @@ -0,0 +1,26 @@
> +// { dg-do assemble }
> +// { dg-options "-Wno-return-local-addr" }
> +
> +int& bad1()
> +{
> + int x = 0;
> + return x;
> +}
> +
> +int* bad2()
> +{
> + int x = 0;
> + return &x;
> +}
> +
> +int f();
> +
> +const int& bad3()
> +{
> + return f();
> +}
> +
> +const int& bad4()
> +{
> + return int();
> +}
> diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C
> new file mode 100644
> index 0000000..faa3a34
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C
> @@ -0,0 +1,20 @@
> +// { dg-do assemble }
> +// { dg-options "-Werror=return-local-addr" }
> +// { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 }
> +
> +int& bad1()
> +{
> + int x = 0; // { dg-error "reference to local variable" }
> + return x;
> +}
> +
> +int* bad2()
> +{
> + int x = 0; // { dg-error "address of local variable" }
> + return &x;
> +}
> +
> +const int& bad4()
> +{
> + return int(); // { dg-error "returning reference to temporary" }
> +}
> diff --git a/gcc/testsuite/gcc.dg/Wreturn-local-addr.c b/gcc/testsuite/gcc.dg/Wreturn-local-addr.c
> new file mode 100644
> index 0000000..d496d20
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/Wreturn-local-addr.c
> @@ -0,0 +1,9 @@
> +/* { dg-do assemble } */
> +/* { dg-options "-Werror=return-local-addr" } */
> +/* { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 } */
> +
> +int* bad()
> +{
> + int x = 0;
> + return &x; /* { dg-error "address of local variable" } */
> +}
--
Dodji
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [patch] PR 54930: add -Wreturn-local-addr
2012-10-21 21:44 ` Dodji Seketeli
@ 2012-10-29 21:26 ` Jonathan Wakely
2012-10-29 21:59 ` Jason Merrill
0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2012-10-29 21:26 UTC (permalink / raw)
To: Dodji Seketeli; +Cc: gcc-patches, Jason Merrill
Ping - can I get this reviewed before stage 1 ends?
On 21 October 2012 22:25, Dodji Seketeli <dodji@seketeli.org> wrote:
> The C++ bits look good to my casual non-maintainer eyes. Let's CC
> Jason.
>
> Thanks.
>
> Jonathan Wakely <jwakely.gcc@gmail.com> a écrit:
>
>> This adds a warning switch for the existing "returning address of
>> local variable" warnings in the C and C++ FEs which are enabled by
>> default but have no switch controlling them. Adding a switch allows it
>> to be turned into an error with -Werror=return-local-addr. I've added
>> two tests checking the -Werror form works.
>>
>> gcc/c-family:
>> PR c++/54930
>> * c.opt (Wreturn_local_addr): Define new option.
>>
>> gcc/c:
>> PR c++/54930
>> * c-typeck.c (c_finish_return): Use OPT_Wreturn_local_addr.
>>
>> gcc/cp:
>> PR c++/54930
>> * typeck.c (maybe_warn_about_returning_address_of_local): Use
>> OPT_Wreturn_local_addr.
>>
>> gcc:
>> PR c++/54930
>> * doc/invoke.texi (Warning Options): Document -Wno-return-local-addr.
>>
>> gcc/testsuite:
>> PR c++/54930
>> * gcc.dg/Wreturn-local-addr.c: New.
>> * g++.dg/warn/Wno-return-local-addr.C: New.
>> * g++.dg/warn/Wreturn-local-addr.C: New.
>>
>> Tested x86_64-linux, OK for trunk?
>>
>> commit 77abe727e837e052680be8020fa2990cce4d1e9a
>> Author: Jonathan Wakely <jwakely.gcc@gmail.com>
>> Date: Wed Oct 17 19:32:10 2012 +0100
>>
>> gcc/c-family:
>> PR c++/54930
>> * c.opt (Wreturn_local_addr): Define new option.
>>
>> gcc/c:
>> PR c++/54930
>> * c-typeck.c (c_finish_return): Use OPT_Wreturn_local_addr.
>>
>> gcc/cp:
>> PR c++/54930
>> * typeck.c (maybe_warn_about_returning_address_of_local): Use
>> OPT_Wreturn_local_addr.
>>
>> gcc:
>> PR c++/54930
>> * doc/invoke.texi (Warning Options): Document -Wno-return-local-addr.
>>
>> gcc/testsuite:
>> PR c++/54930
>> * gcc.dg/Wreturn-local-addr.c: New.
>> * g++.dg/warn/Wno-return-local-addr.C: New.
>> * g++.dg/warn/Wreturn-local-addr.C: New.
>>
>> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
>> index 3166984..f75b58c 100644
>> --- a/gcc/c-family/c.opt
>> +++ b/gcc/c-family/c.opt
>> @@ -601,6 +601,10 @@ Wreorder
>> C++ ObjC++ Var(warn_reorder) Warning LangEnabledBy(C++ ObjC++,Wall)
>> Warn when the compiler reorders code
>>
>> +Wreturn-local-addr
>> +C ObjC C++ ObjC++ Var(warn_return_local_addr) Init(1) Warning
>> +Warn about returning a pointer/reference to a local or temporary variable.
>> +
>> Wreturn-type
>> C ObjC C++ ObjC++ Var(warn_return_type) Warning
>> Warn whenever a function's return type defaults to \"int\" (C), or about inconsistent return types (C++)
>> diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
>> index 4855933..cf63355 100644
>> --- a/gcc/c/c-typeck.c
>> +++ b/gcc/c/c-typeck.c
>> @@ -8742,7 +8742,8 @@ c_finish_return (location_t loc, tree retval, tree origtype)
>> && !TREE_STATIC (inner)
>> && DECL_CONTEXT (inner) == current_function_decl)
>> warning_at (loc,
>> - 0, "function returns address of local variable");
>> + OPT_Wreturn_local_addr, "function returns address "
>> + "of local variable");
>> break;
>>
>> default:
>> diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
>> index eaa0935..d2944d3 100644
>> --- a/gcc/cp/typeck.c
>> +++ b/gcc/cp/typeck.c
>> @@ -8019,14 +8019,14 @@ maybe_warn_about_returning_address_of_local (tree retval)
>> if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
>> || TREE_CODE (whats_returned) == TARGET_EXPR)
>> {
>> - warning (0, "returning reference to temporary");
>> + warning (OPT_Wreturn_local_addr, "returning reference to temporary");
>> return;
>> }
>> if (TREE_CODE (whats_returned) == VAR_DECL
>> && DECL_NAME (whats_returned)
>> && TEMP_NAME_P (DECL_NAME (whats_returned)))
>> {
>> - warning (0, "reference to non-lvalue returned");
>> + warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
>> return;
>> }
>> }
>> @@ -8042,10 +8042,10 @@ maybe_warn_about_returning_address_of_local (tree retval)
>> || TREE_PUBLIC (whats_returned)))
>> {
>> if (TREE_CODE (valtype) == REFERENCE_TYPE)
>> - warning (0, "reference to local variable %q+D returned",
>> + warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
>> whats_returned);
>> else
>> - warning (0, "address of local variable %q+D returned",
>> + warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
>> whats_returned);
>> return;
>> }
>> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
>> index f8c9230..2e36de1 100644
>> --- a/gcc/doc/invoke.texi
>> +++ b/gcc/doc/invoke.texi
>> @@ -261,7 +261,7 @@ Objective-C and Objective-C++ Dialects}.
>> -Woverlength-strings -Wpacked -Wpacked-bitfield-compat -Wpadded @gol
>> -Wparentheses -Wpedantic-ms-format -Wno-pedantic-ms-format @gol
>> -Wpointer-arith -Wno-pointer-to-int-cast @gol
>> --Wredundant-decls @gol
>> +-Wredundant-decls -Wno-return-local-addr @gol
>> -Wreturn-type -Wsequence-point -Wshadow @gol
>> -Wsign-compare -Wsign-conversion -Wsizeof-pointer-memaccess @gol
>> -Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol
>> @@ -3526,6 +3526,12 @@ definitions, may be found on the GCC readings page, at
>>
>> This warning is enabled by @option{-Wall} for C and C++.
>>
>> +@item -Wno-return-local-addr
>> +@opindex Wno-return-local-addr
>> +@opindex Wreturn-local-addr
>> +Do not warn about returning a pointer (or in C++, a reference) to a
>> +variable that goes out of scope after the function returns.
>> +
>> @item -Wreturn-type
>> @opindex Wreturn-type
>> @opindex Wno-return-type
>> diff --git a/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C b/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C
>> new file mode 100644
>> index 0000000..e15bfa2
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C
>> @@ -0,0 +1,26 @@
>> +// { dg-do assemble }
>> +// { dg-options "-Wno-return-local-addr" }
>> +
>> +int& bad1()
>> +{
>> + int x = 0;
>> + return x;
>> +}
>> +
>> +int* bad2()
>> +{
>> + int x = 0;
>> + return &x;
>> +}
>> +
>> +int f();
>> +
>> +const int& bad3()
>> +{
>> + return f();
>> +}
>> +
>> +const int& bad4()
>> +{
>> + return int();
>> +}
>> diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C
>> new file mode 100644
>> index 0000000..faa3a34
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C
>> @@ -0,0 +1,20 @@
>> +// { dg-do assemble }
>> +// { dg-options "-Werror=return-local-addr" }
>> +// { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 }
>> +
>> +int& bad1()
>> +{
>> + int x = 0; // { dg-error "reference to local variable" }
>> + return x;
>> +}
>> +
>> +int* bad2()
>> +{
>> + int x = 0; // { dg-error "address of local variable" }
>> + return &x;
>> +}
>> +
>> +const int& bad4()
>> +{
>> + return int(); // { dg-error "returning reference to temporary" }
>> +}
>> diff --git a/gcc/testsuite/gcc.dg/Wreturn-local-addr.c b/gcc/testsuite/gcc.dg/Wreturn-local-addr.c
>> new file mode 100644
>> index 0000000..d496d20
>> --- /dev/null
>> +++ b/gcc/testsuite/gcc.dg/Wreturn-local-addr.c
>> @@ -0,0 +1,9 @@
>> +/* { dg-do assemble } */
>> +/* { dg-options "-Werror=return-local-addr" } */
>> +/* { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 } */
>> +
>> +int* bad()
>> +{
>> + int x = 0;
>> + return &x; /* { dg-error "address of local variable" } */
>> +}
>
> --
> Dodji
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [patch] PR 54930: add -Wreturn-local-addr
2012-10-29 21:26 ` Jonathan Wakely
@ 2012-10-29 21:59 ` Jason Merrill
2012-10-29 23:46 ` Jonathan Wakely
0 siblings, 1 reply; 6+ messages in thread
From: Jason Merrill @ 2012-10-29 21:59 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: Dodji Seketeli, gcc-patches
OK.
Jason
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [patch] PR 54930: add -Wreturn-local-addr
2012-10-29 21:59 ` Jason Merrill
@ 2012-10-29 23:46 ` Jonathan Wakely
0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Wakely @ 2012-10-29 23:46 UTC (permalink / raw)
To: Jason Merrill; +Cc: Dodji Seketeli, gcc-patches
On 29 October 2012 21:26, Jason Merrill wrote:
> OK.
Thanks, committed.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-10-29 23:23 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-18 18:47 [patch] PR 54930: add -Wreturn-local-addr Jonathan Wakely
2012-10-19 16:24 ` Joseph S. Myers
2012-10-21 21:44 ` Dodji Seketeli
2012-10-29 21:26 ` Jonathan Wakely
2012-10-29 21:59 ` Jason Merrill
2012-10-29 23:46 ` Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).