Bug 54930 - Add warning switch for "returning reference to temporary" and similar
Summary: Add warning switch for "returning reference to temporary" and similar
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.4
: P3 enhancement
Target Milestone: 4.8.0
Assignee: Jonathan Wakely
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2012-10-15 07:49 UTC by Jonathan Wakely
Modified: 2012-10-29 23:22 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-10-15 00:00:00


Attachments
add -Wreturn-local-addr switch (813 bytes, patch)
2012-10-15 10:09 UTC, Jonathan Wakely
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2012-10-15 07:49:04 UTC
The various warnings issued by maybe_warn_about_returning_address_of_local() are enabled by default, so there is no way to make them (and only them) into errors.

If they were controlled by a switch such as -Wreturn-local-addr (enabled by default) then they could be promoted to an error with -Werror=return-local-addr (or disabled with -Wno-return-local-addr for the foolish.)
Comment 1 Jonathan Wakely 2012-10-15 10:09:01 UTC
Created attachment 28448 [details]
add -Wreturn-local-addr switch

This bootstraps, I'll regtest it tonight
Comment 2 Paolo Carlini 2012-10-15 10:11:15 UTC
Great. Remember the docs.
Comment 3 Jonathan Wakely 2012-10-15 10:24:14 UTC
Yep, and tests :)
Comment 4 Jonathan Wakely 2012-10-18 18:30:40 UTC
patch posted for review http://gcc.gnu.org/ml/gcc-patches/2012-10/msg01737.html
Comment 5 Jonathan Wakely 2012-10-29 23:21:42 UTC
Author: redi
Date: Mon Oct 29 23:21:35 2012
New Revision: 192968

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=192968
Log:
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.

Added:
    trunk/gcc/testsuite/g++.dg/warn/Wno-return-local-addr.C
    trunk/gcc/testsuite/g++.dg/warn/Wreturn-local-addr.C
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c.opt
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-typeck.c
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck.c
    trunk/gcc/doc/invoke.texi
    trunk/gcc/testsuite/ChangeLog
Comment 6 Jonathan Wakely 2012-10-29 23:22:51 UTC
fixed for 4.8.0