[llvm-bugs] [Bug 41306] New: Misleading warning when switch controlling expression is promoted from type narrower than int

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Mar 29 08:52:27 PDT 2019


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

            Bug ID: 41306
           Summary: Misleading warning when switch controlling expression
                    is promoted from type narrower than int
           Product: clang
           Version: 7.0
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: cuoq at trust-in-soft.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

Consider the C function:

long long X;

void f(unsigned char x)
{
  switch(x) {
    case -1: X=-1; break;
    case 0xff: X=0xff; break;
  }
}

Clang 7 emits the following warning for the constant -1:

<source>:6:10: warning: overflow converting case value to switch condition type
(-1 to 255) [-Wswitch]
    case -1: X=-1; break;
         ^
1 warning generated.

(Compiler Explorer link: https://gcc.godbolt.org/z/cbjQSu )

This bug is related to C11's 6.8.4.2:5 clause
(https://port70.net/~nsz/c/c11/n1570.html#6.8.4.2p5 ).

Because the controlling expression “x” is promoted before being used as
reference for the type the constants such as “-1” should be converted to, the
warning emitted by Clang is misleading in this case. “-1” will NOT, in fact, be
converted to unsigned char nor be changed to 255. The code generation handles
this situation correctly (the case “-1” can never happen and is correctly
eliminated). The only issue is the warning, which gives the impression that the
value 255 for x would make the first branch taken.

GCC's warning is better worded in this particular case.

-- 
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/20190329/a0a17f47/attachment.html>


More information about the llvm-bugs mailing list