Bug 81392 - Improve diagnostics for [[fallthrough]] attribute that is missing a semicolon
Summary: Improve diagnostics for [[fallthrough]] attribute that is missing a semicolon
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: 9.4
Assignee: David Malcolm
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2017-07-11 10:42 UTC by Cody Gray
Modified: 2020-03-12 11:58 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-07-11 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Cody Gray 2017-07-11 10:42:20 UTC
The C++1z/C++17 draft standard introduces a [[fallthrough]] attribute to explicitly document that fall-through behavior is intended in a switch-case block.

This works in conjunction with G++'s -Wimplicit-fallthrough option, which gives a warning about potentially unintended fall-through behaviors.

The [[fallthrough]] attribute is required to be applied to an empty statement (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0188r1.pdf), and therefore requires a terminating semicolon.

However, forgetting that semicolon is a common error. With the following code:

    #include <iostream>

    int main() {
        switch (0) {
            case 0:
                std::cout << "a\n";
                [[fallthrough]]
            case 1:
                std::cout << "b\n";
                break;
        }
    }

G++ (7.1 and the current trunk of 8.0) issues the following warning:

    warning: this statement may fall through [-Wimplicit-fallthrough=]
        std::cout << "a\n";
        ~~~~~~~~~~^~~~~~~~

This is less helpful than it could be. The current Clang trunk provides a substantially more helpful error message in this case:

    error: fallthrough attribute is only allowed on empty statements
        [[fallthrough]]
          ^
    note: did you forget ';'?
        [[fallthrough]]
                       ^
                       ;

It would be nice to have something similar in G++.
Comment 1 Jakub Jelinek 2019-05-03 09:14:27 UTC
GCC 9.1 has been released.
Comment 2 Jakub Jelinek 2019-08-12 08:53:47 UTC
GCC 9.2 has been released.
Comment 3 Jakub Jelinek 2020-03-12 11:58:42 UTC
GCC 9.3.0 has been released, adjusting target milestone.