diff options
author | Nikita Popov <npopov@redhat.com> | 2024-09-02 09:48:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-02 09:48:54 +0200 |
commit | 30cc198c2d4ad784f18cc10a03d45a19145357af (patch) | |
tree | 04c64c8d8bf268218368efe07a6f5f8753595a98 /llvm/unittests/ADT/APIntTest.cpp | |
parent | e4e0dfb0c24c9bcd4bef835bd6a162967f097584 (diff) | |
download | llvm-30cc198c2d4ad784f18cc10a03d45a19145357af.zip llvm-30cc198c2d4ad784f18cc10a03d45a19145357af.tar.gz llvm-30cc198c2d4ad784f18cc10a03d45a19145357af.tar.bz2 |
[APInt] Add default-disabled assertion to APInt constructor (#106524)
If the uint64_t constructor is used, assert that the value is actually a
signed or unsigned N-bit integer depending on whether the isSigned flag
is set. Provide an implicitTrunc flag to restore the previous behavior,
where the argument is silently truncated instead.
In this commit, implicitTrunc is enabled by default, which means that
the new assertions are disabled and no actual change in behavior occurs.
The plan is to flip the default once all places violating the assertion
have been fixed. See #80309 for the scope of the necessary changes.
The primary motivation for this change is to avoid incorrectly specified
isSigned flags. A recurring problem we have is that people write
something like `APInt(BW, -1)` and this works perfectly fine -- until
the code path is hit with `BW > 64`. Most of our i128 specific
miscompilations are caused by variants of this issue.
The cost of the change is that we have to specify the correct isSigned
flag (and make sure there are no excess bits) for uses where BW is
always <= 64 as well.
Diffstat (limited to 'llvm/unittests/ADT/APIntTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/APIntTest.cpp | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index eb4b847..fff29d2 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -220,11 +220,12 @@ TEST(APIntTest, i256) { } TEST(APIntTest, i1) { - const APInt neg_two(1, static_cast<uint64_t>(-2), true); + const APInt neg_two(1, static_cast<uint64_t>(-2), true, + /*implicitTrunc=*/true); const APInt neg_one(1, static_cast<uint64_t>(-1), true); const APInt zero(1, 0); const APInt one(1, 1); - const APInt two(1, 2); + const APInt two(1, 2, false, /*implicitTrunc=*/true); EXPECT_EQ(0, neg_two.getSExtValue()); EXPECT_EQ(-1, neg_one.getSExtValue()); |