Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / AST / Interp / bitfields.cpp
blobd3a8a083063ab47f4023ed9798a32edfd164262f
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify %s
2 // RUN: %clang_cc1 -verify=ref -Wno-bitfield-constant-conversion %s
3 // RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify %s
4 // RUN: %clang_cc1 -std=c++20 -verify=ref -Wno-bitfield-constant-conversion %s
6 // expected-no-diagnostics
7 // ref-no-diagnostics
9 namespace Basic {
10 struct A {
11 unsigned int a : 2;
12 constexpr A() : a(0) {}
13 constexpr A(int a) : a(a) {}
16 constexpr A a{1};
17 static_assert(a.a == 1, "");
19 constexpr A a2{10};
20 static_assert(a2.a == 2, "");
23 constexpr int storeA() {
24 A a;
25 a.a = 10;
27 return a.a;
29 static_assert(storeA() == 2, "");
31 constexpr int storeA2() {
32 A a;
33 return a.a = 10;
35 static_assert(storeA2() == 2, "");
37 #if __cplusplus >= 202002
38 struct Init1 {
39 unsigned a : 2 = 1;
41 constexpr Init1 I1{};
42 static_assert(I1.a == 1, "");
44 struct Init2 {
45 unsigned a : 2 = 100;
47 constexpr Init2 I2{};
48 static_assert(I2.a == 0, "");
49 #endif
51 struct Init3 {
52 unsigned a : 2;
53 constexpr Init3() : a(100) {}
55 constexpr Init3 I3{};
56 static_assert(I3.a == 0, "");
59 namespace Overflow {
60 struct A {int c:3;};
62 constexpr int f() {
63 A a1{3};
64 return a1.c++;
67 static_assert(f() == 3, "");
70 namespace Compound {
71 struct A {
72 unsigned int a : 2;
73 constexpr A() : a(0) {}
74 constexpr A(int a) : a(a) {}
77 constexpr unsigned add() {
78 A a;
79 a.a += 10;
80 return a.a;
82 static_assert(add() == 2, "");
84 constexpr unsigned sub() {
85 A a;
86 a.a -= 10;
87 return a.a;
89 static_assert(sub() == 2, "");
91 constexpr unsigned mul() {
92 A a(1);
93 a.a *= 5;
94 return a.a;
96 static_assert(mul() == 1, "");
98 constexpr unsigned div() {
99 A a(2);
100 a.a /= 2;
101 return a.a;
103 static_assert(div() == 1, "");