This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix -Wreturn-type with -fsanitize=return (PR c++/81212)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Richard Biener <rguenther at suse dot de>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Sat, 2 Dec 2017 01:14:10 +0100
- Subject: [PATCH] Fix -Wreturn-type with -fsanitize=return (PR c++/81212)
- Authentication-results: sourceware.org; auth=none
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
With -fsanitize=return, we add __builtin_ubsan_handle_missing_return
instead of __builtin_unreachable with BUILTINS_LOCATION locus,
the following patch teaches warn function return pass to handle that too.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2017-12-01 Jakub Jelinek <jakub@redhat.com>
PR c++/81212
* tree-cfg.c (pass_warn_function_return::execute): Handle
__builtin_ubsan_handle_missing_return like __builtin_unreachable
with BUILTINS_LOCATION.
* g++.dg/ubsan/pr81212.C: New test.
* g++.dg/ubsan/return-1.C: Add -Wno-return-type to dg-options.
* g++.dg/ubsan/return-2.C: Add -Wno-return-type to dg-options.
* g++.dg/ubsan/return-7.C: Add -Wno-return-type to dg-options.
--- gcc/tree-cfg.c.jj 2017-12-01 09:10:12.000000000 +0100
+++ gcc/tree-cfg.c 2017-12-01 19:38:21.933049446 +0100
@@ -9151,10 +9151,13 @@ pass_warn_function_return::execute (func
if (EDGE_COUNT (bb->succs) == 0)
{
gimple *last = last_stmt (bb);
+ const enum built_in_function ubsan_missing_ret
+ = BUILT_IN_UBSAN_HANDLE_MISSING_RETURN;
if (last
- && (LOCATION_LOCUS (gimple_location (last))
- == BUILTINS_LOCATION)
- && gimple_call_builtin_p (last, BUILT_IN_UNREACHABLE))
+ && ((LOCATION_LOCUS (gimple_location (last))
+ == BUILTINS_LOCATION
+ && gimple_call_builtin_p (last, BUILT_IN_UNREACHABLE))
+ || gimple_call_builtin_p (last, ubsan_missing_ret)))
{
gimple_stmt_iterator gsi = gsi_for_stmt (last);
gsi_prev_nondebug (&gsi);
--- gcc/testsuite/g++.dg/ubsan/pr81212.C.jj 2017-12-01 19:30:43.251747933 +0100
+++ gcc/testsuite/g++.dg/ubsan/pr81212.C 2017-12-01 19:30:25.000000000 +0100
@@ -0,0 +1,16 @@
+// PR c++/81212
+// { dg-do compile }
+// { dg-options "-Wreturn-type -fsanitize=return" }
+
+struct S
+{
+ S (void *);
+ void *s;
+};
+
+S
+foo (bool x, void *y)
+{
+ if (x)
+ return S (y);
+} // { dg-warning "control reaches end of non-void function" }
--- gcc/testsuite/g++.dg/ubsan/return-1.C.jj 2013-11-22 21:05:59.000000000 +0100
+++ gcc/testsuite/g++.dg/ubsan/return-1.C 2017-12-01 22:23:17.053866660 +0100
@@ -1,5 +1,5 @@
// { dg-do run }
-// { dg-options "-fsanitize=return" }
+// { dg-options "-fsanitize=return -Wno-return-type" }
// { dg-shouldfail "ubsan" }
struct S { S (); ~S (); };
--- gcc/testsuite/g++.dg/ubsan/return-2.C.jj 2014-10-22 15:52:16.000000000 +0200
+++ gcc/testsuite/g++.dg/ubsan/return-2.C 2017-12-01 22:23:32.059679081 +0100
@@ -1,5 +1,5 @@
// { dg-do run }
-// { dg-options "-fsanitize=return -fno-sanitize-recover=return" }
+// { dg-options "-fsanitize=return -fno-sanitize-recover=return -Wno-return-type" }
struct S { S (); ~S (); };
--- gcc/testsuite/g++.dg/ubsan/return-7.C.jj 2016-11-23 20:50:51.000000000 +0100
+++ gcc/testsuite/g++.dg/ubsan/return-7.C 2017-12-01 22:24:03.894281135 +0100
@@ -1,5 +1,5 @@
// { dg-do run }
-// { dg-options "-fsanitize=undefined" }
+// { dg-options "-fsanitize=undefined -Wno-return-type" }
// { dg-shouldfail "ubsan" }
struct S { S (); ~S (); };
Jakub