Skip to content

Commit 30b1577

Browse files
committed
Add more casting tests
1 parent d5894a4 commit 30b1577

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ bool CastFloatingIntegral(InterpState &S, CodePtr OpPC) {
15541554
S.Stk.push<T>(T(F.isNonZero()));
15551555
return true;
15561556
} else {
1557-
APSInt Result(std::max(8u, T::bitWidth() + 1),
1557+
APSInt Result(std::max(8u, T::bitWidth()),
15581558
/*IsUnsigned=*/!T::isSigned());
15591559
auto Status = F.convertToInteger(Result);
15601560

clang/test/AST/Interp/literals.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,42 @@ namespace i128 {
5050
// ref-note {{outside the range}}
5151
constexpr int128_t Two = (int128_t)1 << 1ul;
5252
static_assert(Two == 2, "");
53+
54+
#if __cplusplus >= 201402L
55+
template <typename T>
56+
constexpr T CastFrom(__int128_t A) {
57+
T B = (T)A;
58+
return B;
59+
}
60+
static_assert(CastFrom<char>(12) == 12, "");
61+
static_assert(CastFrom<unsigned char>(12) == 12, "");
62+
static_assert(CastFrom<long>(12) == 12, "");
63+
static_assert(CastFrom<unsigned short>(12) == 12, "");
64+
static_assert(CastFrom<int128_t>(12) == 12, "");
65+
static_assert(CastFrom<float>(12) == 12, "");
66+
static_assert(CastFrom<double>(12) == 12, "");
67+
static_assert(CastFrom<long double>(12) == 12, "");
68+
69+
template <typename T>
70+
constexpr __int128 CastTo(T A) {
71+
int128_t B = (int128_t)A;
72+
return B;
73+
}
74+
static_assert(CastTo<char>(12) == 12, "");
75+
static_assert(CastTo<unsigned char>(12) == 12, "");
76+
static_assert(CastTo<long>(12) == 12, "");
77+
static_assert(CastTo<unsigned long long>(12) == 12, "");
78+
static_assert(CastTo<float>(12) == 12, "");
79+
static_assert(CastTo<double>(12) == 12, "");
80+
static_assert(CastTo<long double>(12) == 12, "");
81+
#endif
82+
83+
constexpr int128_t Error = __LDBL_MAX__; // ref-warning {{implicit conversion of out of range value}} \
84+
// ref-error {{must be initialized by a constant expression}} \
85+
// ref-note {{is outside the range of representable values of type}} \
86+
// expected-warning {{implicit conversion of out of range value}} \
87+
// expected-error {{must be initialized by a constant expression}} \
88+
// expected-note {{is outside the range of representable values of type}}
5389
}
5490

5591
constexpr bool b = number;

0 commit comments

Comments
 (0)