Fix APInt value initialization to give a zero value as any sane integer type
authorRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 4 Sep 2015 04:08:36 +0000 (04:08 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 4 Sep 2015 04:08:36 +0000 (04:08 +0000)
should, rather than giving a broken value that doesn't even zero/sign-extend
properly.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246836 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/APInt.h
unittests/ADT/APIntTest.cpp

index ea67e7928a7e0124b76314e2c748cb3270ebb6d3..e2a0cb5e69dc0d67f9eac4c17bfc7858eef8eab7 100644 (file)
@@ -294,11 +294,12 @@ public:
       delete[] pVal;
   }
 
-  /// \brief Default constructor that creates an uninitialized APInt.
+  /// \brief Default constructor that creates an uninteresting APInt
+  /// representing a 1-bit zero value.
   ///
   /// This is useful for object deserialization (pair this with the static
   ///  method Read).
-  explicit APInt() : BitWidth(1) {}
+  explicit APInt() : BitWidth(1), VAL(0) {}
 
   /// \brief Returns whether this instance allocated memory.
   bool needsCleanup() const { return !isSingleWord(); }
index ffba7b1633698f7ee838b7db6681fa363653e708..0002dad8555f719da44ba3ab1993c474599625c3 100644 (file)
@@ -17,6 +17,13 @@ using namespace llvm;
 
 namespace {
 
+TEST(APIntTest, ValueInit) {
+  APInt Zero = APInt();
+  EXPECT_TRUE(!Zero);
+  EXPECT_TRUE(!Zero.zext(64));
+  EXPECT_TRUE(!Zero.sext(64));
+}
+
 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
 TEST(APIntTest, ShiftLeftByZero) {
   APInt One = APInt::getNullValue(65) + 1;