-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][Interp] Fix truncateCast() #69911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesThe added test case used to fail because we converted the LHS to Full diff: https://github.com/llvm/llvm-project/pull/69911.diff 2 Files Affected:
diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index 45e5b49546270aa..98d4dae8fa4df0e 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -35,10 +35,18 @@ template <bool Signed> class IntegralAP final {
friend IntegralAP<!Signed>;
APInt V;
- template <typename T> static T truncateCast(const APInt &V) {
+ template <typename T, bool InputSigned>
+ static T truncateCast(const APInt &V) {
constexpr unsigned BitSize = sizeof(T) * 8;
- if (BitSize >= V.getBitWidth())
- return std::is_signed_v<T> ? V.getSExtValue() : V.getZExtValue();
+ if (BitSize >= V.getBitWidth()) {
+ APInt Extended;
+ if constexpr (InputSigned)
+ Extended = V.sext(BitSize);
+ else
+ Extended = V.zext(BitSize);
+ return std::is_signed_v<T> ? Extended.getSExtValue()
+ : Extended.getZExtValue();
+ }
return std::is_signed_v<T> ? V.trunc(BitSize).getSExtValue()
: V.trunc(BitSize).getZExtValue();
@@ -81,14 +89,20 @@ template <bool Signed> class IntegralAP final {
}
explicit operator bool() const { return !V.isZero(); }
- explicit operator int8_t() const { return truncateCast<int8_t>(V); }
- explicit operator uint8_t() const { return truncateCast<uint8_t>(V); }
- explicit operator int16_t() const { return truncateCast<int16_t>(V); }
- explicit operator uint16_t() const { return truncateCast<uint16_t>(V); }
- explicit operator int32_t() const { return truncateCast<int32_t>(V); }
- explicit operator uint32_t() const { return truncateCast<uint32_t>(V); }
- explicit operator int64_t() const { return truncateCast<int64_t>(V); }
- explicit operator uint64_t() const { return truncateCast<uint64_t>(V); }
+ explicit operator int8_t() const { return truncateCast<int8_t, Signed>(V); }
+ explicit operator uint8_t() const { return truncateCast<uint8_t, Signed>(V); }
+ explicit operator int16_t() const { return truncateCast<int16_t, Signed>(V); }
+ explicit operator uint16_t() const {
+ return truncateCast<uint16_t, Signed>(V);
+ }
+ explicit operator int32_t() const { return truncateCast<int32_t, Signed>(V); }
+ explicit operator uint32_t() const {
+ return truncateCast<uint32_t, Signed>(V);
+ }
+ explicit operator int64_t() const { return truncateCast<int64_t, Signed>(V); }
+ explicit operator uint64_t() const {
+ return truncateCast<uint64_t, Signed>(V);
+ }
template <typename T> static IntegralAP from(T Value, unsigned NumBits = 0) {
assert(NumBits > 0);
diff --git a/clang/test/AST/Interp/intap.cpp b/clang/test/AST/Interp/intap.cpp
index 27fae1b904351ce..02a860eb0986c15 100644
--- a/clang/test/AST/Interp/intap.cpp
+++ b/clang/test/AST/Interp/intap.cpp
@@ -27,6 +27,9 @@ static_assert(BitIntZero2 == 0, "");
constexpr unsigned _BitInt(1) UBitIntZero1{};
static_assert(UBitIntZero1 == 0, "");
+constexpr unsigned _BitInt(2) BI1 = 3u;
+static_assert(BI1 == 3, "");
+
#ifdef __SIZEOF_INT128__
namespace i128 {
|
6bae86e
to
78422ff
Compare
Ping |
cor3ntin
approved these changes
Oct 30, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The added test case used to fail because we converted the LHS to
-1
.