[PATCH] D42938: [Sema] Emit -Winteger-overflow for arguments in function calls, ObjC messages.

Jan Korous via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 2 11:05:39 PST 2018


jkorous-apple added a comment.

Sorry, my bad, I tried just older clang version which didn't produce any error.

With reasonably up to date clang I get these:

  > cat tmpl_overflow.hpp



  template<int N> struct foo { short a; };
  
  template<> struct foo<1000000000000000000 * 10240> { bool a; };
  
  int main() {
      foo<1000000000000000000 * 10240> a;
  }



  > ~/src/oss/llvm at master/build/bin/clang++ tmpl_overflow.hpp
  tmpl_overflow.hpp:3:23: error: non-type template argument is not a constant expression
  template<> struct foo<1000000000000000000 * 10240> { bool a; };
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  tmpl_overflow.hpp:3:43: note: value 10240000000000000000000 is outside the range of representable values of type 'long'
  template<> struct foo<1000000000000000000 * 10240> { bool a; };
                                            ^
  tmpl_overflow.hpp:6:9: error: non-type template argument is not a constant expression
      foo<1000000000000000000 * 10240> a;
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  tmpl_overflow.hpp:6:29: note: value 10240000000000000000000 is outside the range of representable values of type 'long'
      foo<1000000000000000000 * 10240> a;
                              ^
  2 errors generated.

I am not actually sure those error messages are correct (not constant? type 'long'?) but I am not sure it's still within the scope of your patch either.

What is interesting is that by using lower value (which is still to big for short) I probably hit different diagnostic check and get different error:

  > cat tmpl_overflow.hpp

  template<int N> struct foo { short a; };
  
  template<> struct foo<100000000000000 * 10240> { bool a; };
  
  int main() {
      foo<100000000000000 * 10240> a;
  }



                                                                                                                                                                                                                                                                                                                              jankorous @ jans-imac /Users/jankorous/tmp
  > ~/src/oss/llvm at master/build/bin/clang++ tmpl_overflow.hpp
  tmpl_overflow.hpp:3:23: error: non-type template argument evaluates to 1024000000000000000, which cannot be narrowed to type 'int' [-Wc++11-narrowing]
  template<> struct foo<100000000000000 * 10240> { bool a; };
                        ^
  tmpl_overflow.hpp:6:9: error: non-type template argument evaluates to 1024000000000000000, which cannot be narrowed to type 'int' [-Wc++11-narrowing]
      foo<100000000000000 * 10240> a;
          ^
  2 errors generated.


https://reviews.llvm.org/D42938





More information about the cfe-commits mailing list