Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

An overflow in a constant expression is not detected #14082

Open Quuxplusone opened 12 years ago

Quuxplusone commented 12 years ago
Bugzilla Link PR14043
Status NEW
Importance P enhancement
Reported by Tomasz Mikolajczyk (tmmikolajczyk@gmail.com)
Reported on 2012-10-08 16:55:19 -0700
Last modified on 2012-10-09 09:18:39 -0700
Version 3.1
Hardware PC Linux
CC efriedma@quicinc.com, llvm-bugs@lists.llvm.org, pawel@32bitmicro.com, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments fib.cpp (885 bytes, text/x-c++src)
Blocks
Blocked by
See also
Created attachment 9321
Application calculating Fibonacci seq at compile time

Consider this trivial Fibonacci sequence calculation at compile time:

template<int v>
struct fib
{
    static_assert(v > 1, "fib<0> and fib<1> are special");
    enum { value = fib<v-1>::value + fib<v-2>::value };
};

template<>
struct fib<0> { enum { value = 0 }; };

template<>
struct fib<1> { enum { value = 1 }; };

std::cout << fib<0>::value << std::endl;
std::cout << fib<1>::value << std::endl;
std::cout << fib<2>::value << std::endl;
std::cout << fib<3>::value << std::endl;
std::cout << fib<4>::value << std::endl;
std::cout << fib<5>::value << std::endl;

std::cout << fib<45>::value << std::endl; // 1134903170
std::cout << fib<46>::value << std::endl; // 1836311903
std::cout << fib<47>::value << std::endl; // integer overflow

gcc (4.7.1) complains when tries to instantiate the fib<47>:
~$ g++ -std=c++11 fib.cpp
fib.cpp: In instantiation of `struct fib<47>':
fib.cpp:30:25:   required from here
fib.cpp:7:10: warning: integer overflow in expression [-Woverflow]
fib.cpp:7:10: error: overflow in constant expression [-fpermissive]
fib.cpp:7:10: error: enumerator value for `value' is not an integer constant

However clang keeps going silently:
~$ clang++ -std=c++11 fib.cpp
~$ ./a.out
0
1
1
2
3
5
8
13
21
34
1134903170
1836311903
-1323752223
~$

Would be good to have the overflow detected as well. The fib.cpp file from the
attachment contains the whole app.
Quuxplusone commented 12 years ago

Attached fib.cpp (885 bytes, text/x-c++src): Application calculating Fibonacci seq at compile time

Quuxplusone commented 12 years ago

Hmm... we do print a warning with -pedantic, but it might be a good idea to print something by default in this case.

Quuxplusone commented 12 years ago

Ironically, we accept this by default for GCC compatibility. Looks like they upgraded this from pedwarn to permerror for C++ in 4.5 or 4.6. Bumping this from Extension to ExtWarn seems sensible to me.

GCC still treats this as a pedwarn in C, but it seems reasonable to me for us to give a warning by default in all language modes (though for C, we still wouldn't catch overflow, because the C ICE checker doesn't evaluate).