<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Misleading warning when switch controlling expression is promoted from type narrower than int"
   href="https://bugs.llvm.org/show_bug.cgi?id=41306">41306</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Misleading warning when switch controlling expression is promoted from type narrower than int
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>7.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>cuoq@trust-in-soft.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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: <a href="https://gcc.godbolt.org/z/cbjQSu">https://gcc.godbolt.org/z/cbjQSu</a> )

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

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.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>