This is with gcc 4.3-20070914 snapshot.

The attached program has four sites with compile-time integer overflow: the
initializations of h4, h8, bp_i:h, and bp_lli:h.  Only the last of these draws
a warning.

[EMAIL PROTECTED]:~/exp-integer-overflow$ cat z5.cc
#include <stdio.h>

int l4 = 0x01010101;
int h4 = l4 * 0x80;

long long int l8 = 0x0101010101010101ll;
long long int h8 = l8 * 0x80;

template <class T> struct BitPattern {
  // For int32:
  //   half_ones = 0x00001111
  //   l         = 0x01010101
  //   h         = 0x80808080
  static const T half_ones = (static_cast<T>(1) << (sizeof(T)*4)) - 1;
  static const T l = (sizeof(T) == 1) ? 1 : (half_ones / 0xff * (half_ones +
2));
  static const T h = l * 0x80;
};

BitPattern<int> bp_i;
BitPattern<long long int> bp_lli;

int main() {
  printf("%16.16llx\n", bp_lli.half_ones);
  printf("%16.16llx\n", bp_lli.l);
  printf("%16.16llx\n", bp_lli.h);
  return 0;
}

[EMAIL PROTECTED]:~/exp-integer-overflow$
/home/mec/gcc-4.3-20070914/install/bin/g++ -Wall -S z5.cc
z5.cc: In instantiation of 'const long long int BitPattern<long long int>::h':
z5.cc:25:   instantiated from here
z5.cc:16: warning: integer overflow in expression


-- 
           Summary: Inconsistent warning for compile-time integer overflow
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mec at google dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33514

Reply via email to