[cfe-users] Issue in -Wtautological-constant-out-of-range-compare?

David Blaikie dblaikie at gmail.com
Sat Dec 14 10:42:11 PST 2013


I believe this does also happen in ToT Clang & there's been some discussion
(perhaps it's on an internal Google bug, I forget) about the right thing to
do here.

I think one of the issues is that your code isn't guaranteed to behave that
way - it's possible for an implementation to map -1 onto one of the
existing 0/1 bit values of the enum (the enum only needs to be one bit wide
(obviously sizeof won't reflect that, it still has to take at least a byte
of storage)). Arguably, though, it's still possibel on some implementations
(including Clang) for it to have such a value...

The wording is tricky - between underlying types and ranges, etc, I don't
have it all perfectly straight in my head.


On Sat, Dec 14, 2013 at 3:17 AM, Fred <fredm at spamcop.net> wrote:

> Hi all,
> I am seeing the -Wtautological-constant-out-of-range-compare warning for
> the first time.
>
> With this Apple clang version:
> $ clang --version
> Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
> Target: x86_64-apple-darwin13.0.0
> Thread model: posix
>
> In this program:
> #include <stdio.h>
>
> typedef enum o {
>     O_1 = 0,
>     O_2,
> } o;
>
> int main(int argc, char** argv) {
>   enum o e1 = -1;
>
>   if( e1 >= 2 ) {
>     printf( "That's not right\n" );
>   }
>
>   printf("e1:%d\n",e1);
>
>   return 0;
> }
>
> Compiled thus, I get the warning that my if test above will always
> evaluate as false:
> $ clang -Wall -std=c89 t1.c
> t1.c:11:10: warning: comparison of constant 2 with expression of type
> 'enum o'
>       is always false [-Wtautological-constant-out-of-range-compare]
>   if( e1 >= 2 ) {
>       ~~ ^  ~
> 1 warning generated.
>
> That is demonstrably untrue:
> $ ./a.out
> That's not right
> e1:-1
>
> Does this error also occur in the mainline clang? I am unsure whether to
> raise the issue with Apple or the clang developers.
>
> Thanks,
> Fred
> _______________________________________________
> cfe-users mailing list
> cfe-users at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20131214/5734eb13/attachment.html>


More information about the cfe-users mailing list