llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.96k stars 11.54k forks source link

A `long long` constant is out of range #29953

Open llvmbot opened 7 years ago

llvmbot commented 7 years ago
Bugzilla Link 30605
Version 3.8
OS Windows NT
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@hfinkel,@zygoloid,@rnk

Extended Description

In the following code the <= expression is being evaluated as false:

///////////////////////////////////
#include <stdio.h>

int main()
{
    short  sh = 9L;
    unsigned int ui1 = (sh <= 0xD48FBCB67A80957DLL);
    printf("%X\n", ui1); 
}
////////////////////////////////////

According to C++11 standard 2.14.2 [lex.icon]: hexadecimal constant suffixed with "LL" that does not "fit" into long long int, but does "fit" into unsigned long long int, should be represented by the latter. In this case our constant: 0xD48FBCB67A80957D < 2^64 - 1 That means that it can be represented by unsigned long long int, and the <= expression should be evaluated as true;

The command used to compile is: clang file.cpp -std=c++11 -o file.exe Compiler version:

clang version 3.8.1 (branches/release_38)
Target: i686-pc-windows-msvc
Thread model: posix

The compiler prints out the following warning:

//////////////////////////////////////////////////////
clang+vs.cpp:8:25: warning: comparison of constant -3130075724073888387 with expression of type 'short' is always false
      [-Wtautological-constant-out-of-range-compare]
        unsigned int ui1 = (sh <= 0xD48FBCB67A80957DLL);
                            ~~ ^  ~~~~~~~~~~~~~~~~~~~~
1 warning generated.
///////////////////////////////////////////////////////////
rnk commented 7 years ago

This non-conforming behavior is enabled by -fms-extensions, which is enabled by default when targeting Windows. You should be able to disable this with -fno-ms-extensions.

Reid, presumably this should be behind -fms-compatibility instead, since it's a non-conforming language change?

Sure, r283227. However, Iliya will have to pass -fno-ms-compatibility to get the right behavior on Windows.

You can also pass -fno-ms-extensions as a workaround to get the right behavior today, but this will probably break parsing most MSVC system headers.

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 7 years ago

This non-conforming behavior is enabled by -fms-extensions, which is enabled by default when targeting Windows. You should be able to disable this with -fno-ms-extensions.

Reid, presumably this should be behind -fms-compatibility instead, since it's a non-conforming language change?