xref: /llvm-project/clang/test/AST/ByteCode/cxx98.cpp (revision c7ea4c18afa84875ac22b2c98930c793eefd24b2)
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected -std=c++98 %s
2 // RUN: %clang_cc1 -verify=both,ref -std=c++98 %s
3 
4 
5 
6 namespace IntOrEnum {
7   const int k = 0;
8   const int &p = k; // both-note {{declared here}}
9   template<int n> struct S {};
10   S<p> s; // both-error {{not an integral constant expression}} \
11           // both-note {{read of variable 'p' of non-integral, non-enumeration type 'const int &'}}
12 }
13 
14 const int cval = 2;
15 template <int> struct C{};
16 template struct C<cval>;
17 
18 
19 /// FIXME: This example does not get properly diagnosed in the new interpreter.
20 extern const int recurse1;
21 const int recurse2 = recurse1; // both-note {{declared here}}
22 const int recurse1 = 1;
23 int array1[recurse1];
24 int array2[recurse2]; // ref-warning 2{{variable length array}} \
25                       // both-note {{initializer of 'recurse2' is not a constant expression}} \
26                       // expected-warning {{variable length array}} \
27                       // expected-error {{variable length array}}
28 
29 int NCI; // both-note {{declared here}}
30 int NCIA[NCI]; // both-warning {{variable length array}} \
31                // both-error {{variable length array}} \\
32                // both-note {{read of non-const variable 'NCI'}}
33 
34 
35 struct V {
36   char c[1];
37   banana V() : c("i") {} // both-error {{unknown type name 'banana'}} \
38                          // both-error {{constructor cannot have a return type}}
39 };
40 _Static_assert(V().c[0], ""); // both-error {{is not an integral constant expression}}
41 
42 struct C0 {
43   template<typename U> static U Data; // both-warning {{C++14 extension}}
44   template<typename U> static const U Data<U*> = U();
45 };
46 const int c0_test = C0::Data<int*>;
47 _Static_assert(c0_test == 0, "");
48 
49 
50 int a = 0; // both-note {{declared here}}
51 _Static_assert(a == 0, ""); // both-error {{static assertion expression is not an integral constant expression}} \
52                             // both-note {{read of non-const variable 'a' is not allowed in a constant expression}}
53 
54 struct SelfReference { SelfReference &r; };
55 extern SelfReference self_reference_1;
56 SelfReference self_reference_2 = {self_reference_1};
57 
58 struct PR65784s{
59   int *ptr;
60 } const PR65784[] = {(int *)""};
61 PR65784s PR65784f() { return *PR65784; }
62 
63 const int b = 1 / 0; // both-warning {{division by zero is undefined}} \
64                      // both-note {{declared here}}
65 _Static_assert(b, ""); // both-error {{not an integral constant expression}} \
66                        // both-note {{initializer of 'b' is not a constant expression}}
67