diff options
author | Siddhesh Poyarekar <siddhesh@sourceware.org> | 2018-02-20 11:11:31 +0000 |
---|---|---|
committer | Siddhesh Poyarekar <siddhesh@gcc.gnu.org> | 2018-02-20 11:11:31 +0000 |
commit | d3eb902f7f45cc20b5802b2ef3c85b88fdec52c0 (patch) | |
tree | d2a38f0b7b33dc609127d7b35d5e8333ffcf2b21 /gcc | |
parent | 5bbccd92506c4d260eca29d12fa30c75aaaee65b (diff) | |
download | gcc-d3eb902f7f45cc20b5802b2ef3c85b88fdec52c0.zip gcc-d3eb902f7f45cc20b5802b2ef3c85b88fdec52c0.tar.gz gcc-d3eb902f7f45cc20b5802b2ef3c85b88fdec52c0.tar.bz2 |
c++: Fix spurious fallthrough warning on break
The C++ frontend generates a break that results in the fallthrough
warning misfiring in nested switch blocks where cases in the inner
switch block return, rendering the break pointless. The fallthrough
detection in finish_break_stmt does not work either because the
condition is encoded as an IF_STMT and not a COND_EXPR.
Fix this by adding a condition for IF_STMT in the
langhooks.block_may_fallthru for C++. Fix tested on x86_64.
gcc/cp
* cp-objcp-common.c (cxx_block_may_fallthru): Add case for
IF_STMT.
gcc/testsuite
* g++.dg/nested-switch.C: New test case.
From-SVN: r257843
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/cp/cp-objcp-common.c | 5 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 4 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wimplicit-fallthrough-3.C | 31 |
4 files changed, 45 insertions, 0 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index f2bcce9..dd408e0 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2018-02-20 Siddhesh Poyarekar <siddhesh@sourceware.org> + + * cp-objcp-common.c (cxx_block_may_fallthru): Add case for + IF_STMT. + 2018-02-20 Paolo Carlini <paolo.carlini@oracle.com> PR c++/84446 diff --git a/gcc/cp/cp-objcp-common.c b/gcc/cp/cp-objcp-common.c index a45dda4..5289a89 100644 --- a/gcc/cp/cp-objcp-common.c +++ b/gcc/cp/cp-objcp-common.c @@ -349,6 +349,11 @@ cxx_block_may_fallthru (const_tree stmt) case THROW_EXPR: return false; + case IF_STMT: + if (block_may_fallthru (THEN_CLAUSE (stmt))) + return true; + return block_may_fallthru (ELSE_CLAUSE (stmt)); + case SWITCH_STMT: return (!SWITCH_STMT_ALL_CASES_P (stmt) || !SWITCH_STMT_NO_BREAK_P (stmt) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 476374a..cfae982 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2018-02-20 Siddhesh Poyarekar <siddhesh@sourceware.org> + + * g++.dg/warn/Wimplicit-fallthrough-3.C: New test case. + 2018-02-20 Martin Liska <mliska@suse.cz> PR c/84310 diff --git a/gcc/testsuite/g++.dg/warn/Wimplicit-fallthrough-3.C b/gcc/testsuite/g++.dg/warn/Wimplicit-fallthrough-3.C new file mode 100644 index 0000000..2f06817 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wimplicit-fallthrough-3.C @@ -0,0 +1,31 @@ +// Verify that there are no spurious warnings in nested switch statements due +// to the unnecessary break in the inner switch block. +// { dg-do compile } +// { dg-options "-Wimplicit-fallthrough" } */ + +int +foo (int c1, int c2, int c3) +{ + switch (c2) + { + case 0: + switch (c3) // { dg-bogus "may fall through" } + { + case 0: + if (c1) + return 1; + else + return 2; + break; + + default: + return 3; + } + + case 1: + return 4; + default: + return 5; + break; + } +} |