Debian Codegraph @

clang/test/AST/Interp/cxx17.cpp

raw
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -verify=expected,both %s
2 // RUN: %clang_cc1 -std=c++17 -verify=ref,both %s
3
4 struct F { int a; int b;};
5 constexpr F getF() {
6 return {12, 3};
7 }
8 constexpr int f() {
9 auto [a1, b1] = getF();
10 auto [a2, b2] = getF();
11
12 return a1 + a2 + b1 + b2;
13 }
14 static_assert(f() == 30);
15
16
17 constexpr int structRefs() {
18 const auto &[a, b] = getF();
19
20 return a + b;
21 }
22 static_assert(structRefs() == 15);
23
24 constexpr int structRefs2() {
25 F f = getF();
26 const auto &[a, b] = f;
27
28 return a + b;
29 }
30 static_assert(structRefs2() == 15);
31
32
33 template<typename T>
34 struct Tuple {
35 T first;
36 T second;
37 constexpr Tuple(T a, T b) : first(a), second(b) {}
38 };
39 template<typename T>
40 constexpr T addTuple(const Tuple<T> &Tu) {
41 auto [a, b] = Tu;
42 return a + b;
43 }
44
45 template<typename T>
46 constexpr T addTuple2(const Tuple<T> &Tu) {
47 auto [a, b] = Tu;
48 return Tu.first + Tu.second;
49 }
50
51 constexpr Tuple<int> T1 = Tuple(1,2);
52 static_assert(addTuple(T1) == 3);
53 static_assert(addTuple2(T1) == 3);
54
55 constexpr Tuple<short> T2 = Tuple<short>(11,2);
56 static_assert(addTuple(T2) == 13);
57 static_assert(addTuple2(T2) == 13);
58
59 constexpr int Modify() {
60 auto T = Tuple<int>(10, 20);
61 auto &[x, y] = T;
62 x += 1;
63 y += 1;
64 return T.first + T.second;
65 }
66 static_assert(Modify() == 32, "");
67
68 constexpr int a() {
69 int a[2] = {5, 3};
70 auto [x, y] = a;
71 return x + y;
72 }
73 static_assert(a() == 8);
74
75 constexpr int b() {
76 int a[2] = {5, 3};
77 auto &[x, y] = a;
78 x += 1;
79 y += 2;
80 return a[0] + a[1];
81 }
82 static_assert(b() == 11);
83
84 /// The diagnostics between the two interpreters used to be different here.
85 struct S { int a; };
86 constexpr S getS() { // both-error {{constexpr function never produces a constant expression}}
87 (void)(1/0); // both-note 2{{division by zero}} \
88 // both-warning {{division by zero}}
89 return S{12};
90 }
91 constexpr S s = getS(); // both-error {{must be initialized by a constant expression}} \
92 // both-note {{in call to 'getS()'}} \
93 // both-note {{declared here}}
94 static_assert(s.a == 12, ""); // both-error {{not an integral constant expression}} \
95 // both-note {{initializer of 's' is not a constant expression}}