Skip to content

Commit 1e9895c

Browse files
committed
fix test for builtin-bswapg
1 parent c0d0f3c commit 1e9895c

File tree

3 files changed

+87
-123
lines changed

3 files changed

+87
-123
lines changed

clang/test/CodeGen/builtins.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ int main(void) {
132132
// Whatever
133133
P(bswapg, ((char)N));
134134
P(bswapg, ((short)N));
135-
P(bswapg, ((int)N));
136-
P(bswapg, ((unsigned long)N));
135+
P(bswapg, ((int)N));
136+
P(bswapg, ((unsigned long)N));
137137
P(bswap16, (N));
138138
P(bswap32, (N));
139139
P(bswap64, (N));

clang/test/Sema/builtin-bswapg.cpp

Lines changed: 0 additions & 121 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
3+
// expected-no-diagnostics
4+
5+
void test_basic_type_checks() {
6+
static_assert(__is_same(char, decltype(__builtin_bswapg((char)0))), "");
7+
static_assert(__is_same(unsigned char, decltype(__builtin_bswapg((unsigned char)0))), "");
8+
static_assert(__is_same(short, decltype(__builtin_bswapg((short)0))), "");
9+
static_assert(__is_same(unsigned short, decltype(__builtin_bswapg((unsigned short)0))), "");
10+
static_assert(__is_same(int, decltype(__builtin_bswapg((int)0))), "");
11+
static_assert(__is_same(unsigned int, decltype(__builtin_bswapg((unsigned int)0))), "");
12+
static_assert(__is_same(long, decltype(__builtin_bswapg((long)0))), "");
13+
static_assert(__is_same(unsigned long, decltype(__builtin_bswapg((unsigned long)0))), "");
14+
}
15+
16+
template<typename T>
17+
void test_template_type_check() {
18+
static_assert(__is_same(T, decltype(__builtin_bswapg(T{}))),
19+
"bswapg should return the same type as its argument");
20+
constexpr T zero{};
21+
constexpr T max = ~T{};
22+
constexpr T one = T{1};
23+
24+
static_assert(__is_same(T, decltype(__builtin_bswapg(zero))), "");
25+
static_assert(__is_same(T, decltype(__builtin_bswapg(max))), "");
26+
static_assert(__is_same(T, decltype(__builtin_bswapg(one))), "");
27+
}
28+
template void test_template_type_check<char>();
29+
template void test_template_type_check<unsigned char>();
30+
template void test_template_type_check<short>();
31+
template void test_template_type_check<unsigned short>();
32+
template void test_template_type_check<int>();
33+
template void test_template_type_check<unsigned int>();
34+
template void test_template_type_check<long>();
35+
template void test_template_type_check<unsigned long>();
36+
37+
void test_lambda_type_checks() {
38+
auto lambda = [](auto x) {
39+
static_assert(__is_same(decltype(x), decltype(__builtin_bswapg(x))),
40+
"bswapg in lambda should preserve type");
41+
return __builtin_bswapg(x);
42+
};
43+
auto result_long = lambda(42UL);
44+
static_assert(__is_same(unsigned long, decltype(result_long)), "");
45+
46+
auto result_int = lambda(42);
47+
static_assert(__is_same(int, decltype(result_int)), "");
48+
49+
auto result_short = lambda(static_cast<short>(42));
50+
static_assert(__is_same(short, decltype(result_short)), "");
51+
52+
auto result_char = lambda(static_cast<char>(42));
53+
static_assert(__is_same(char, decltype(result_char)), "");
54+
}
55+
56+
decltype(auto) test_decltype_auto(int x) {
57+
return __builtin_bswapg(x);
58+
}
59+
60+
void test_decltype_auto_check() {
61+
int x = 42;
62+
auto result = test_decltype_auto(x);
63+
static_assert(__is_same(int, decltype(result)), "");
64+
}
65+
66+
template<auto Value>
67+
struct ValueTemplateTypeTest {
68+
using value_type = decltype(Value);
69+
using result_type = decltype(__builtin_bswapg(Value));
70+
71+
static constexpr bool type_matches = __is_same(value_type, result_type);
72+
static_assert(type_matches, "Value template bswapg should preserve type");
73+
74+
static constexpr auto swapped_value = __builtin_bswapg(Value);
75+
};
76+
77+
template<auto... Values>
78+
void test_template_pack_types() {
79+
static_assert((__is_same(decltype(Values), decltype(__builtin_bswapg(Values))) && ...), "All pack elements should preserve type");
80+
}
81+
82+
template struct ValueTemplateTypeTest<0x1234>;
83+
template struct ValueTemplateTypeTest<0x12345678UL>;
84+
template struct ValueTemplateTypeTest<(short)0x1234>;
85+
template struct ValueTemplateTypeTest<(char)0x12>;

0 commit comments

Comments
 (0)