[llvm-bugs] [Bug 25966] New: switch case with assert(false) causes -Wimplicit-fallthrough warnings in both debug and release builds

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Dec 29 20:11:48 PST 2015


https://llvm.org/bugs/show_bug.cgi?id=25966

            Bug ID: 25966
           Summary: switch case with assert(false) causes
                    -Wimplicit-fallthrough warnings in both debug and
                    release builds
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: cpeterson at mozilla.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

A not uncommon code pattern is to assert(false) in a switch statement's default
case but then fall through to a handled case.

Unfortunately, the assert(false) causes an "unannotated fall-through between
switch labels" warning in release (NDEBUG) builds and a "fallthrough annotation
in unreachable code" warning in DEBUG builds if you add a
[[clang::fallthrough]] annotation.

Possible workarounds:

1. Wrap the assert(false) in #ifdef DEBUG so there is no inert code in release
(NDEBUG) builds to cause an "unannotated fall-through between switch labels"
warning.

2. #define a special FALLTHROUGH_ASSERT() macro omits the unreachable
[[clang::fallthrough]] annotation in DEBUG builds. I took this approach in
Firefox, for the time being:

https://hg.mozilla.org/integration/mozilla-inbound/rev/d0a8d632dce5#l1.45

3. clang could allow a [[clang::fallthrough]] annotation after a call to a
noreturn function like abort(), but still warn about unreachable
[[clang::fallthrough]] annotations after an early break or return.

TEST CASE:

#include <assert.h>

int main(int argc, const char**)
{
    // clang -Wimplicit-fallthrough -std=c++11 -DNDEBUG test.cpp
    // warning: unannotated fall-through between switch labels
[-Wimplicit-fallthrough]
    switch (argc) {
        default:
            assert(false); // assert on an unexpected switch value
        case 0:
            break;
    }

    // clang -Wimplicit-fallthrough -std=c++11 -DDEBUG test.cpp
    // warning: fallthrough annotation in unreachable code
[-Wimplicit-fallthrough]
    switch (argc) {
        default:
            assert(false); // assert on an unexpected switch value
            [[clang::fallthrough]];
        case 0:
            break;
    }

    // Workaround #1:
    switch (argc) {
        default:
#ifdef DEBUG
            assert(false); // assert on an unexpected switch value
#endif
        case 0:
            break;
    }

    // Workaround #2:

#ifdef DEBUG
#define FALLTHROUGH_ASSERT() assert(false)
#else
#define FALLTHROUGH_ASSERT() [[clang::fallthrough]]
#endif

    switch (argc) {
        default:
            FALLTHROUGH_ASSERT(); // assert on an unexpected switch value
        case 0:
            break;
    }

    return 0;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20151230/70f91059/attachment.html>


More information about the llvm-bugs mailing list