1 // RUN: %clang_cc1 -std=gnu++20 -fsyntax-only -verify %s
2
3 template<bool If, typename Type>
4 struct enable_if {
5 using type= Type;
6 };
7
8 template<typename Type>
9 struct enable_if<false, Type> {};
10
11 template<typename T1, typename T2>
12 struct is_same {
13 static constexpr bool value = false;
14 };
15
16 template<typename T1>
17 struct is_same<T1, T1> {
18 static constexpr bool value = true;
19 };
20
str()21 constexpr const char *str() {
22 return "abc";
23 }
24
25 template<typename T, typename enable_if<!is_same<int, T>::value, int>::type = 0>
fail_on_int(T t)26 constexpr T fail_on_int(T t) {return t;}
27 // expected-note@-1 {{candidate template ignored: requirement}}
28
29 namespace test0 {
30 template<typename T, T v>
31 struct A {
ttest0::A32 [[clang::annotate("test", fail_on_int(v))]] void t() {}
33 // expected-error@-1 {{no matching function for call to 'fail_on_int'}}
t1test0::A34 [[clang::annotate("test", (typename enable_if<!is_same<long, T>::value, int>::type)v)]] void t1() {}
35 // expected-error@-1 {{failed requirement}}
36 };
37 A<int, 9> a;
38 // expected-note@-1 {{in instantiation of template class}}
39 A<long, 7> a1;
40 // expected-note@-1 {{in instantiation of template class}}
41 A<unsigned long, 6> a2;
42
43 template<typename T>
44 struct B {
ttest0::B45 [[clang::annotate("test", (T{}, 9))]] void t() {}
46 // expected-error@-1 {{illegal initializer type 'void'}}
47 };
48 B<int> b;
49 B<void> b1;
50 // expected-note@-1 {{in instantiation of template class}}
51 }
52
53 namespace test1 {
54 int g_i; // expected-note {{declared here}}
55
t3()56 [[clang::annotate("test", "arg")]] void t3() {}
57
58 template <typename T, T V>
59 struct B {
60 static T b; // expected-note {{declared here}}
61 static constexpr T cb = V;
62 template <typename T1, T1 V1>
63 struct foo {
64 static T1 f; // expected-note {{declared here}}
65 static constexpr T1 cf = V1;
66 int v __attribute__((annotate("v_ann_0", str(), 90, V, g_i))) __attribute__((annotate("v_ann_1", V1)));
67 // expected-error@-1 {{'annotate' attribute requires parameter 4 to be a constant expression}}
68 // expected-note@-2 {{is not allowed in a constant expression}}
ttest1::B::foo69 [[clang::annotate("qdwqwd", cf, cb)]] void t() {}
t1test1::B::foo70 [[clang::annotate("qdwqwd", f, cb)]] void t1() {}
71 // expected-error@-1 {{'annotate' attribute requires parameter 1 to be a constant expression}}
72 // expected-note@-2 {{is not allowed in a constant expression}}
t2test1::B::foo73 [[clang::annotate("jui", b, cf)]] void t2() {}
74 // expected-error@-1 {{'annotate' attribute requires parameter 1 to be a constant expression}}
75 // expected-note@-2 {{is not allowed in a constant expression}}
t3test1::B::foo76 [[clang::annotate("jui", (b, 0), cf)]] [[clang::annotate("jui", &b, cf, &foo::t2, str())]] void t3() {}
77 };
78 };
79
80 static B<int long, -1>::foo<unsigned, 9> gf; // expected-note {{in instantiation of}}
81 static B<int long, -2> gf1;
82
83 } // namespace test1
84
85 namespace test2 {
86
87 template<int I>
f()88 int f() {
89 [[clang::annotate("test", I)]] int v = 0; // expected-note {{declared here}}
90 [[clang::annotate("test", v)]] int v2 = 0;
91 // expected-error@-1 {{'annotate' attribute requires parameter 1 to be a constant expression}}
92 // expected-note@-2 {{is not allowed in a constant expression}}
93 [[clang::annotate("test", rtyui)]] int v3 = 0;
94 // expected-error@-1 {{use of undeclared identifier 'rtyui'}}
95 }
96
test()97 void test() {}
98 }
99
100 namespace test3 {
101
f()102 void f() {
103 int n = 10;
104 int vla[n];
105
106 [[clang::annotate("vlas are awful", sizeof(vla))]] int i = 0; // reject, the sizeof is not unevaluated
107 // expected-error@-1 {{'annotate' attribute requires parameter 1 to be a constant expression}}
108 // expected-note@-2 {{subexpression not valid in a constant expression}}
109 [[clang::annotate("_Generic selection expression should be fine", _Generic(n, int : 0, default : 1))]]
110 int j = 0; // second arg should resolve to 0 fine
111 }
112 void designator();
113 [[clang::annotate("function designators?", designator)]] int k = 0; // Should work?
114
self()115 void self() {
116 [[clang::annotate("function designators?", self)]] int k = 0;
117 }
118
119 }
120
121 namespace test4 {
foldable_but_invalid()122 constexpr int foldable_but_invalid() {
123 int *A = new int(0);
124 // expected-note@-1 {{allocation performed here was not deallocated}}
125 return *A;
126 }
127
f1()128 [[clang::annotate("", foldable_but_invalid())]] void f1() {}
129 // expected-error@-1 {{'annotate' attribute requires parameter 1 to be a constant expression}}
130 }
131