diff options
author | redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-10-29 23:21:35 +0000 |
---|---|---|
committer | redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-10-29 23:21:35 +0000 |
commit | 8b447d3f4f8509f90a13d4aa2fd435b84ff517f4 (patch) | |
tree | cc94a67e7b17460335b8b668eac1f55a166543ed | |
parent | d75cece1a80023500084e21c3dc9209d6891fb60 (diff) | |
download | gcc-8b447d3f4f8509f90a13d4aa2fd435b84ff517f4.tar.gz |
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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@192968 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r-- | gcc/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/c-family/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/c-family/c.opt | 4 | ||||
-rw-r--r-- | gcc/c/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/c/c-typeck.c | 3 | ||||
-rw-r--r-- | gcc/cp/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/cp/typeck.c | 8 | ||||
-rw-r--r-- | gcc/doc/invoke.texi | 8 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C | 26 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C | 20 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Wreturn-local-addr.c | 9 |
12 files changed, 100 insertions, 6 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 33f3497da42..9daf727ed80 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2012-10-29 Jonathan Wakely <jwakely.gcc@gmail.com> + + PR c++/54930 + * doc/invoke.texi (Warning Options): Document -Wno-return-local-addr. + 2012-10-29 H.J. Lu <hongjiu.lu@intel.com> * lra-assigns.c: Remove trailing white spaces. diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index f97057ec2c5..f7c942216bc 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,8 @@ +2012-10-29 Jonathan Wakely <jwakely.gcc@gmail.com> + + PR c++/54930 + * c.opt (Wreturn_local_addr): Define new option. + 2012-10-25 Jason Merrill <jason@redhat.com> * c.opt (Wvirtual-move-assign): New. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 7eb66c6175b..06d6e363c98 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -613,6 +613,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 LangEnabledBy(C ObjC C++ ObjC++,Wall) Warn whenever a function's return type defaults to \"int\" (C), or about inconsistent return types (C++) diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index f44131b74a1..cd3a457d953 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,8 @@ +2012-10-29 Jonathan Wakely <jwakely.gcc@gmail.com> + + PR c++/54930 + * c-typeck.c (c_finish_return): Use OPT_Wreturn_local_addr. + 2012-10-29 Manuel López-Ibáñez <manu@gcc.gnu.org> PR c/53066 diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 4855933c181..cf63355f614 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/ChangeLog b/gcc/cp/ChangeLog index 86050dafde3..1eb1fe4f2c1 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2012-10-29 Jonathan Wakely <jwakely.gcc@gmail.com> + + PR c++/54930 + * typeck.c (maybe_warn_about_returning_address_of_local): Use + OPT_Wreturn_local_addr. + 2012-10-26 Jakub Jelinek <jakub@redhat.com> PR c++/55081 diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 2514b6fc741..5d8c27d534c 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -8020,14 +8020,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; } } @@ -8043,10 +8043,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 15ecaf12703..720d42de101 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 @@ -3535,6 +3535,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/ChangeLog b/gcc/testsuite/ChangeLog index 80e9289f4e8..f6a81221005 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2012-10-29 Jonathan Wakely <jwakely.gcc@gmail.com> + + 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. + 2012-10-29 H.J. Lu <hongjiu.lu@intel.com> PR middle-end/55116 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 00000000000..e15bfa24f54 --- /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 00000000000..faa3a345440 --- /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 00000000000..d496d205e89 --- /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" } */ +} |