xref: /llvm-project/clang/test/SemaCXX/attr-annotate.cpp (revision 9d739e54f4506bf9bd220c5d65f710b86a39f6d5)
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 
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>
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 {
32     [[clang::annotate("test", fail_on_int(v))]] void t() {}
33     // expected-error@-1 {{no matching function for call to 'fail_on_int'}}
34     [[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 {
45     [[clang::annotate("test", ((void)T{}, 9))]] void t() {}
46     // expected-error@-1 {{cannot create object of function 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 
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}}
69     [[clang::annotate("qdwqwd", cf, cb)]] void t() {}
70     [[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}}
73     [[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}}
76     [[clang::annotate("jui", ((void)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>
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 
97 void test() {}
98 }
99 
100 namespace test3 {
101 
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 
115 void self() {
116   [[clang::annotate("function designators?", self)]] int k = 0;
117 }
118 
119 }
120 
121 namespace test4 {
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 
128 [[clang::annotate("", foldable_but_invalid())]] void f1() {}
129 // expected-error@-1 {{'annotate' attribute requires parameter 1 to be a constant expression}}
130 
131 [[clang::annotate()]] void f2() {}
132 // expected-error@-1 {{'annotate' attribute takes at least 1 argument}}
133 
134 template <typename T> [[clang::annotate()]] void f2() {}
135 // expected-error@-1 {{'annotate' attribute takes at least 1 argument}}
136 }
137