[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gcc-patches
Subject:    [PATCH] Fix -Wreturn-type with switches (PR sanitizer/81275)
From:       Jakub Jelinek <jakub () redhat ! com>
Date:       2017-11-30 22:55:42
Message-ID: 20171130225542.GP2353 () tucnak
[Download RAW message or body]

Hi!

Now that the C++ FE emits __builtin_unreachable with BUILTINS_LOCATION
for fallthrough into end of function without returning value, we need to
see those in the warn_function_return pass which is right after building
cfg.  While GIMPLE_CONDs conditionally jumping to __builtin_unreachable are
only optimized during evrp and later, GIMPLE_SWITCH cases that branch to
__builtin_unreachable are unfortunately removed already during the cfg
cleanup in the cfg pass and thus the immediately following
warn_function_return pass can't warn.  Fixed by postponing that, e.g.
the evrp pass does unconditional TODO_cleanup_cfg, so those will be
optimized away pretty early (often already during ccp1).

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2017-11-30  Jakub Jelinek  <jakub@redhat.com>

	PR sanitizer/81275
	* tree-cfg.c (group_case_labels_stmt): Don't optimize away
	C++ FE implicitly added __builtin_unreachable () until -Wreturn-type
	is diagnosed.

	* c-c++-common/tsan/pr81275.c: New test.

--- gcc/tree-cfg.c.jj	2017-11-30 11:40:38.000000000 +0100
+++ gcc/tree-cfg.c	2017-11-30 12:01:39.496311219 +0100
@@ -1750,7 +1750,14 @@ group_case_labels_stmt (gswitch *stmt)
 
       /* Discard cases that have an unreachable destination block.  */
       if (EDGE_COUNT (base_bb->succs) == 0
-	  && gimple_seq_unreachable_p (bb_seq (base_bb)))
+	  && gimple_seq_unreachable_p (bb_seq (base_bb))
+	  /* Don't optimize this if __builtin_unreachable () is the
+	     implicitly added one by the C++ FE too early, before
+	     -Wreturn-type can be diagnosed.  We'll optimize it later
+	     during switchconv pass or any other cfg cleanup.  */
+	  && (gimple_in_ssa_p (cfun)
+	      || (LOCATION_LOCUS (gimple_location (last_stmt (base_bb)))
+		  != BUILTINS_LOCATION)))
 	{
 	  edge base_edge = find_edge (gimple_bb (stmt), base_bb);
 	  if (base_edge != NULL)
--- gcc/testsuite/c-c++-common/tsan/pr81275.c.jj	2017-11-28 22:23:34.000000000 +0100
+++ gcc/testsuite/c-c++-common/tsan/pr81275.c	2017-11-30 11:50:56.185274379 +0100
@@ -1,7 +1,6 @@
 /* PR sanitizer/81275 */
 /* { dg-do compile } */
 /* { dg-options "-Wreturn-type -fsanitize=thread" } */
-/* { dg-skip-if "" { c++ } { "*" } { "-O0" } } */
 
 int
 f1 (int a, int b)

	Jakub
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic