[webkit-dev] Heads up: FALLTHROUGH annotations for switches with fallthroughs

Joseph Pecoraro pecoraro at apple.com
Sun Jan 26 01:41:50 PST 2014


Hello!

I'm slowly enabling -Wimplicit-fallthrough on projects in the Mac / iOS ports.
<http://trac.webkit.org/changeset/162793>
<http://clang.llvm.org/docs/LanguageExtensions.html#the-clang-fallthrough-attribute>

This means that if you write any switch statements with implicit fallthroughs it will produce build errors in projects with the warning enabled.

If you're writing a switch with a fallthrough, use the "FALLTHROUGH;" annotation (which expands to [[clang::fallthrough]];) to appease the warning.

The intent of this warning is to catch at compile time any accidental switch fallthroughs, and therefore explicitly annotate everywhere a fallthrough was intended.

Thanks,
- JoePeck

--

For example. Here is a switch with an implicit fallthrough but no compiler recognized annotation:

switch (input) {
case 1:
    output -= 5;
    // fallthrough
default:
    output += 5;
}

When the warning is enabled you will see a build error like:

main.cpp:9:5: warning: unannotated fall-through between switch labels [-Wimplicit-fallthrough]
    default:
    ^
main.cpp:9:5: note: insert 'FALLTHROUGH;' to silence this warning
    default:
    ^
    FALLTHROUGH; 
main.cpp:9:5: note: insert 'break;' to avoid fall-through
    default:
    ^
    break; 

Use "FALLTHROUGH;" to annotate the fallthrough and build without errors:

switch (input) {
case 1:
    output -= 5;
    FALLTHROUGH;
default:
    output += 5;
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.webkit.org/pipermail/webkit-dev/attachments/20140126/6547bf35/attachment.html>


More information about the webkit-dev mailing list