<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 - warnings for untaken branch of ?: expression, at file scope"
   href="https://bugs.llvm.org/show_bug.cgi?id=38789">38789</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>warnings for untaken branch of ?: expression, at file scope
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>arnd@linaro.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I have encountered a couple of warnings in the Linux kernel for range checking
of shift counts. The common macro

#define DMA_BIT_MASK(n)        (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))

produces a warning at file scope for a '64' argument, but not inside of
a function. I tried using __builtin_choose_expr() to avoid that, which
made it worse: that version always warns, regardless of the scope (it
also does this with gcc).

The same thing happened with a less common macro, which I've simplifed
into 'COND_BIT()' in the test case below.

The behavior is the same for all clang versions I tried (3.8 through 8),
but my expectation was to see no warning at all.

$ clang-8 -Wall -O2  -c -xc -  << EOF
#define DMA_BIT_MASK(n)        (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
#define DMA_BIT_MASK_2(n) __builtin_choose_expr((n) == 64, ~0ULL,
(1ULL<<(n))-1)
#define COND_BIT(x) (((x) >= 0) ? (1ul << x) : 0ul)
#define COND_BIT_2(x) __builtin_choose_expr(x >= 0, 1ul << x, 0ul)

// warning: shift count >= width of type [-Wshift-count-overflow]
long long a = DMA_BIT_MASK(64);

// warning: shift count >= width of type [-Wshift-count-overflow]
long long b = DMA_BIT_MASK_2(64);

// warning: shift count is negative [-Wshift-count-negative]
// [gcc: no warning]
unsigned long c = COND_BIT(-1);

// warning: shift count is negative [-Wshift-count-negative]
unsigned long d = COND_BIT_2(-1);

int f(void)
{
    // no warning
    long long a = DMA_BIT_MASK(64);

    // warning: shift count >= width of type [-Wshift-count-overflow]
    long long b = DMA_BIT_MASK_2(64);

    // no warning
    unsigned long c = COND_BIT(-1);

    // warning: shift count is negative [-Wshift-count-negative]
    unsigned long d = COND_BIT_2(-1);

    // prevent unused variable wanings
    return a & b & c & d;
}
EOF</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>