[PATCH] D81329: ADT: Fix that APSInt's string constructor claims it requires 5 bits to store a zero

Raphael Isemann via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 10 07:39:31 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rGf60e319419b8: ADT: Fix that APSInt's string constructor claims it requires 5 bits to store a… (authored by teemperor).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81329/new/

https://reviews.llvm.org/D81329

Files:
  llvm/lib/Support/APSInt.cpp
  llvm/unittests/ADT/APSIntTest.cpp


Index: llvm/unittests/ADT/APSIntTest.cpp
===================================================================
--- llvm/unittests/ADT/APSIntTest.cpp
+++ llvm/unittests/ADT/APSIntTest.cpp
@@ -150,6 +150,29 @@
   EXPECT_EQ(APSInt("-1234").getExtValue(), -1234);
 }
 
+TEST(APSIntTest, FromStringBitWidth) {
+  EXPECT_EQ(APSInt("0").getBitWidth(), 1U);
+  EXPECT_EQ(APSInt("000").getBitWidth(), 1U);
+  EXPECT_EQ(APSInt("1").getBitWidth(), 1U);
+  EXPECT_EQ(APSInt("2").getBitWidth(), 2U);
+  EXPECT_EQ(APSInt("3").getBitWidth(), 2U);
+  EXPECT_EQ(APSInt("003").getBitWidth(), 2U);
+  EXPECT_EQ(APSInt("15").getBitWidth(), 4U);
+  EXPECT_EQ(APSInt("16").getBitWidth(), 5U);
+  EXPECT_EQ(APSInt("17").getBitWidth(), 5U);
+
+  EXPECT_EQ(APSInt("-0").getBitWidth(), 1U);
+  EXPECT_EQ(APSInt("-000").getBitWidth(), 1U);
+  EXPECT_EQ(APSInt("-1").getBitWidth(), 1U);
+  EXPECT_EQ(APSInt("-2").getBitWidth(), 2U);
+  EXPECT_EQ(APSInt("-3").getBitWidth(), 3U);
+  EXPECT_EQ(APSInt("-003").getBitWidth(), 3U);
+  EXPECT_EQ(APSInt("-5").getBitWidth(), 4U);
+  EXPECT_EQ(APSInt("-15").getBitWidth(), 5U);
+  EXPECT_EQ(APSInt("-16").getBitWidth(), 5U);
+  EXPECT_EQ(APSInt("-17").getBitWidth(), 6U);
+}
+
 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
 
 TEST(APSIntTest, StringDeath) {
Index: llvm/lib/Support/APSInt.cpp
===================================================================
--- llvm/lib/Support/APSInt.cpp
+++ llvm/lib/Support/APSInt.cpp
@@ -26,14 +26,14 @@
   APInt Tmp(NumBits, Str, /*radix=*/10);
   if (Str[0] == '-') {
     unsigned MinBits = Tmp.getMinSignedBits();
-    if (MinBits > 0 && MinBits < NumBits)
-      Tmp = Tmp.trunc(MinBits);
+    if (MinBits < NumBits)
+      Tmp = Tmp.trunc(std::max<unsigned>(1, MinBits));
     *this = APSInt(Tmp, /*isUnsigned=*/false);
     return;
   }
   unsigned ActiveBits = Tmp.getActiveBits();
-  if (ActiveBits > 0 && ActiveBits < NumBits)
-    Tmp = Tmp.trunc(ActiveBits);
+  if (ActiveBits < NumBits)
+    Tmp = Tmp.trunc(std::max<unsigned>(1, ActiveBits));
   *this = APSInt(Tmp, /*isUnsigned=*/true);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81329.269849.patch
Type: text/x-patch
Size: 2069 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200610/288cef97/attachment.bin>


More information about the llvm-commits mailing list