Debian Codegraph @

clang/test/AST/Interp/eval-order.cpp

raw
1 // RUN: %clang_cc1 -std=c++1z -verify=ref,both %s -fcxx-exceptions -triple=x86_64-linux-gnu
2 // RUN: %clang_cc1 -std=c++1z -verify=expected,both %s -fcxx-exceptions -triple=x86_64-linux-gnu -fexperimental-new-constant-interpreter
3
4 // ref-no-diagnostics
5
6 /// Check that assignment operators evaluate their operands right-to-left.
7 /// Copied from test/SemaCXX/constant-expression-cxx1z.cpp
8 ///
9 /// As you can see from the FIXME comments, some of these are not yet working correctly
10 /// in the new interpreter.
11 namespace EvalOrder {
12 template<typename T> struct lvalue {
13 T t;
14 constexpr T &get() { return t; }
15 };
16
17 struct UserDefined {
18 int n = 0;
19 constexpr UserDefined &operator=(const UserDefined&) { return *this; }
20 constexpr UserDefined &operator+=(const UserDefined&) { return *this; }
21 constexpr void operator<<(const UserDefined&) const {}
22 constexpr void operator>>(const UserDefined&) const {}
23 constexpr void operator+(const UserDefined&) const {}
24 constexpr void operator[](int) const {}
25 };
26 constexpr UserDefined ud;
27
28 struct NonMember {};
29 constexpr void operator+=(NonMember, NonMember) {}
30 constexpr void operator<<(NonMember, NonMember) {}
31 constexpr void operator>>(NonMember, NonMember) {}
32 constexpr void operator+(NonMember, NonMember) {}
33 constexpr NonMember nm;
34
35 constexpr void f(...) {}
36
37 // Helper to ensure that 'a' is evaluated before 'b'.
38 struct seq_checker {
39 bool done_a = false;
40 bool done_b = false;
41
42 template <typename T> constexpr T &&a(T &&v) {
43 done_a = true;
44 return (T &&)v;
45 }
46 template <typename T> constexpr T &&b(T &&v) {
47 if (!done_a)
48 throw "wrong"; // expected-note 7{{not valid}}
49 done_b = true;
50 return (T &&)v;
51 }
52
53 constexpr bool ok() { return done_a && done_b; }
54 };
55
56 // SEQ(expr), where part of the expression is tagged A(...) and part is
57 // tagged B(...), checks that A is evaluated before B.
58 #define A sc.a
59 #define B sc.b
60 #define SEQ(...) static_assert([](seq_checker sc) { void(__VA_ARGS__); return sc.ok(); }({}))
61
62 // Longstanding sequencing rules.
63 SEQ((A(1), B(2)));
64 SEQ((A(true) ? B(2) : throw "huh?"));
65 SEQ((A(false) ? throw "huh?" : B(2)));
66 SEQ(A(true) && B(true));
67 SEQ(A(false) || B(true));
68
69 // From P0145R3:
70
71 // Rules 1 and 2 have no effect ('b' is not an expression).
72
73 // Rule 3: a->*b
74 SEQ(A(ud).*B(&UserDefined::n));
75 SEQ(A(&ud)->*B(&UserDefined::n));
76
77 // Rule 4: a(b1, b2, b3)
78 SEQ(A(f)(B(1), B(2), B(3))); // expected-error {{not an integral constant expression}} FIXME \
79 // expected-note 2{{in call to}}
80
81 // Rule 5: b = a, b @= a
82 SEQ(B(lvalue<int>().get()) = A(0)); // expected-error {{not an integral constant expression}} FIXME \
83 // expected-note 2{{in call to}}
84 SEQ(B(lvalue<UserDefined>().get()) = A(ud)); // expected-error {{not an integral constant expression}} FIXME \
85 // expected-note 2{{in call to}}
86 SEQ(B(lvalue<int>().get()) += A(0));
87 SEQ(B(lvalue<UserDefined>().get()) += A(ud)); // expected-error {{not an integral constant expression}} FIXME \
88 // expected-note 2{{in call to}}
89
90 SEQ(B(lvalue<NonMember>().get()) += A(nm)); // expected-error {{not an integral constant expression}} FIXME \
91 // expected-note 2{{in call to}}
92
93
94 // Rule 6: a[b]
95 constexpr int arr[3] = {};
96 SEQ(A(arr)[B(0)]);
97 SEQ(A(+arr)[B(0)]);
98 SEQ(A(0)[B(arr)]); // expected-error {{not an integral constant expression}} FIXME \
99 // expected-note 2{{in call to}}
100 SEQ(A(0)[B(+arr)]); // expected-error {{not an integral constant expression}} FIXME \
101 // expected-note 2{{in call to}}
102 SEQ(A(ud)[B(0)]);
103
104 // Rule 7: a << b
105 SEQ(A(1) << B(2));
106 SEQ(A(ud) << B(ud));
107 SEQ(A(nm) << B(nm));
108
109 // Rule 8: a >> b
110 SEQ(A(1) >> B(2));
111 SEQ(A(ud) >> B(ud));
112 SEQ(A(nm) >> B(nm));
113
114 // No particular order of evaluation is specified in other cases, but we in
115 // practice evaluate left-to-right.
116 // FIXME: Technically we're expected to check for undefined behavior due to
117 // unsequenced read and modification and treat it as non-constant due to UB.
118 SEQ(A(1) + B(2));
119 SEQ(A(ud) + B(ud));
120 SEQ(A(nm) + B(nm));
121 SEQ(f(A(1), B(2)));
122 #undef SEQ
123 #undef A
124 #undef B
125 }