[llvm] f60e319 - ADT: Fix that APSInt's string constructor claims it requires 5 bits to store a zero
Raphael Isemann via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 10 07:37:15 PDT 2020
Author: Raphael Isemann
Date: 2020-06-10T16:36:33+02:00
New Revision: f60e319419b848b5789d4713de38df381e4fe7b4
URL: https://github.com/llvm/llvm-project/commit/f60e319419b848b5789d4713de38df381e4fe7b4
DIFF: https://github.com/llvm/llvm-project/commit/f60e319419b848b5789d4713de38df381e4fe7b4.diff
LOG: ADT: Fix that APSInt's string constructor claims it requires 5 bits to store a zero
Summary:
When constructing an APSInt from a string, the constructor doesn't correctly
truncate the bit width of the result if the passed in string was "0" (or any
alternative way to express 0 like "-0" or "000"). Instead of 1 (which is the
smallest allowed bit width) it returns an APSInt with a bit width of 5.
The reason is that the constructor checks that it never truncates the result to
the invalid bit width of 0, so when it calculates that storing a "0" doesn't
require any bits it just keeps the original overestimated bit width (which
happens to be 5).
This patch just sets the bit width of the result to 1 if the required bit width
is 0.
Reviewers: arphaman, dexonsmith
Reviewed By: dexonsmith
Subscribers: hiraditya, dexonsmith, JDevlieghere, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D81329
Added:
Modified:
llvm/lib/Support/APSInt.cpp
llvm/unittests/ADT/APSIntTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/APSInt.cpp b/llvm/lib/Support/APSInt.cpp
index 6805e06df333..b65b6824eaf8 100644
--- a/llvm/lib/Support/APSInt.cpp
+++ b/llvm/lib/Support/APSInt.cpp
@@ -26,14 +26,14 @@ APSInt::APSInt(StringRef Str) {
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);
}
diff --git a/llvm/unittests/ADT/APSIntTest.cpp b/llvm/unittests/ADT/APSIntTest.cpp
index 9045f3e48f7d..932f54c19f18 100644
--- a/llvm/unittests/ADT/APSIntTest.cpp
+++ b/llvm/unittests/ADT/APSIntTest.cpp
@@ -150,6 +150,29 @@ TEST(APSIntTest, FromString) {
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) {
More information about the llvm-commits
mailing list