ahatanak wrote:

> > Could you help me understand why clang is emitting the warning?
> > Isn't it semantically equivalent to the following code?
> > ```
> > unsigned int foo(unsigned char x)
> > {
> >      int i = ~(1<<x); // no -Wimplicit-int-conversion warning.
> >      return i; // no -Wimplicit-int-conversion warning.
> > }
> > ```
> 
> The conversion isn't implicit in the latter case.

It looks like the warning is emitted because of the following code in 
`TryGetExprRange`:

```
    case BO_Shl:
      // ...except that we want to treat '1 << (blah)' as logically
      // positive.  It's an important idiom.
      if (IntegerLiteral *I
            = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
        if (I->getValue() == 1) {
          IntRange R = IntRange::forValueOfType(C, GetExprType(E));
          return IntRange(R.Width, /*NonNegative*/ true);
        }
      }
```

`1<<x` is treated as non-negative even though it can be negative when `x` is 
equal to 31.

https://github.com/llvm/llvm-project/pull/126846
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to