xref: /llvm-project/clang/test/SemaCXX/attr-unavailable.cpp (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
3 int &foo(int);
4 double &foo(double);
5 void foo(...) __attribute__((__unavailable__)); // \
6 // expected-note 2 {{'foo' has been explicitly marked unavailable here}}
7 
8 void bar(...) __attribute__((__unavailable__)); // expected-note 4 {{explicitly marked unavailable}}
9 
test_foo(short * sp)10 void test_foo(short* sp) {
11   int &ir = foo(1);
12   double &dr = foo(1.0);
13   foo(sp); // expected-error{{'foo' is unavailable}}
14 
15   void (*fp)(...) = &bar; // expected-error{{'bar' is unavailable}}
16   void (*fp2)(...) = bar; // expected-error{{'bar' is unavailable}}
17 
18   int &(*fp3)(int) = foo;
19   void (*fp4)(...) = foo; // expected-error{{'foo' is unavailable}}
20 }
21 
22 namespace radar9046492 {
23 #define FOO __attribute__((unavailable("not available - replaced")))
24 
25 void foo() FOO; // expected-note{{'foo' has been explicitly marked unavailable here}}
bar()26 void bar() {
27   foo(); // expected-error {{'foo' is unavailable: not available - replaced}}
28 }
29 }
30 
31 void unavail(short* sp)  __attribute__((__unavailable__));
unavail(short * sp)32 void unavail(short* sp) {
33   // No complains inside an unavailable function.
34   int &ir = foo(1);
35   double &dr = foo(1.0);
36   foo(sp);
37   foo();
38 }
39 
40 // Show that delayed processing of 'unavailable' is the same
41 // delayed process for 'deprecated'.
42 enum DeprecatedEnum { DE_A, DE_B } __attribute__((deprecated)); // expected-note {{'DeprecatedEnum' has been explicitly marked deprecated here}}
43 typedef enum DeprecatedEnum AnotherDeprecatedEnum; // expected-warning {{'DeprecatedEnum' is deprecated}}
44 
45 __attribute__((deprecated)) typedef enum DeprecatedEnum DeprecatedEnum;
46 __attribute__((deprecated))
testDeprecated(DeprecatedEnum X)47 DeprecatedEnum testDeprecated(DeprecatedEnum X) { return X; }
48 
49 
50 enum UnavailableEnum { UE_A, UE_B } __attribute__((unavailable)); // expected-note {{'UnavailableEnum' has been explicitly marked unavailable here}}
51 typedef enum UnavailableEnum AnotherUnavailableEnum; // expected-error {{'UnavailableEnum' is unavailable}}
52                                                      //
53 
54 __attribute__((unavailable)) typedef enum UnavailableEnum UnavailableEnum;
55 __attribute__((unavailable))
testUnavailable(UnavailableEnum X)56 UnavailableEnum testUnavailable(UnavailableEnum X) { return X; }
57 
58 
59 // Check that unavailable classes can be used as arguments to unavailable
60 // function, particularly in template functions.
61 #if !__has_feature(attribute_availability_in_templates)
62 #error "Missing __has_feature"
63 #endif
64 class __attribute((unavailable)) UnavailableClass; // \
65         expected-note 3{{'UnavailableClass' has been explicitly marked unavailable here}}
66 void unavail_class(UnavailableClass&); // expected-error {{'UnavailableClass' is unavailable}}
67 void unavail_class_marked(UnavailableClass&) __attribute__((unavailable));
68 template <class T> void unavail_class(UnavailableClass&); // expected-error {{'UnavailableClass' is unavailable}}
69 template <class T> void unavail_class_marked(UnavailableClass&) __attribute__((unavailable));
70 template <class T> void templated(T&);
untemplated(UnavailableClass & UC)71 void untemplated(UnavailableClass &UC) {  // expected-error {{'UnavailableClass' is unavailable}}
72   templated(UC);
73 }
untemplated_marked(UnavailableClass & UC)74 void untemplated_marked(UnavailableClass &UC) __attribute__((unavailable)) {
75   templated(UC);
76 }
77 
templated_calls_bar()78 template <class T> void templated_calls_bar() { bar(); } // \
79            // expected-error{{'bar' is unavailable}}
templated_calls_bar_arg(T v)80 template <class T> void templated_calls_bar_arg(T v) { bar(v); } // \
81            // expected-error{{'bar' is unavailable}}
templated_calls_bar_arg_never_called(T v)82 template <class T> void templated_calls_bar_arg_never_called(T v) { bar(v); }
83 
84 template <class T>
unavail_templated_calls_bar()85 void unavail_templated_calls_bar() __attribute__((unavailable)) { //  \
86 // expected-note {{'unavail_templated_calls_bar<int>' has been explicitly marked unavailable here}}
87   bar(5);
88 }
89 template <class T>
unavail_templated_calls_bar_arg(T v)90 void unavail_templated_calls_bar_arg(T v) __attribute__((unavailable)) {
91 // expected-note@-1 {{'unavail_templated_calls_bar_arg<int>' has been explicitly marked unavailable here}}
92   bar(v);
93 }
94 
calls_templates_which_call_bar()95 void calls_templates_which_call_bar() {
96   templated_calls_bar<int>();
97 
98   templated_calls_bar_arg(5); // \
99   expected-note{{in instantiation of function template specialization 'templated_calls_bar_arg<int>' requested here}}
100 
101   unavail_templated_calls_bar<int>(); // \
102   expected-error{{'unavail_templated_calls_bar<int>' is unavailable}}
103 
104   unavail_templated_calls_bar_arg(5); // \
105   expected-error{{'unavail_templated_calls_bar_arg<int>' is unavailable}}
106 }
107 
108 template <class T> void unavail_templated(T) __attribute__((unavailable));
109 // expected-note@-1 {{'unavail_templated<int>' has been explicitly marked unavailable here}}
calls_unavail_templated()110 void calls_unavail_templated() {
111   unavail_templated(5); // expected-error{{'unavail_templated<int>' is unavailable}}
112 }
unavail_calls_unavail_templated()113 void unavail_calls_unavail_templated() __attribute__((unavailable)) {
114   unavail_templated(5);
115 }
116 
117 void unavailable() __attribute((unavailable));
118 // expected-note@-1 4 {{'unavailable' has been explicitly marked unavailable here}}
119 struct AvailableStruct {
calls_unavailableAvailableStruct120   void calls_unavailable() { unavailable(); } // \
121   expected-error{{'unavailable' is unavailable}}
calls_unavailableAvailableStruct122   template <class U> void calls_unavailable() { unavailable(); } // \
123   expected-error{{'unavailable' is unavailable}}
124 };
125 template <class T> struct AvailableStructTemplated {
calls_unavailableAvailableStructTemplated126   void calls_unavailable() { unavailable(); } // \
127   expected-error{{'unavailable' is unavailable}}
calls_unavailableAvailableStructTemplated128   template <class U> void calls_unavailable() { unavailable(); } // \
129   expected-error{{'unavailable' is unavailable}}
130 };
131 struct __attribute__((unavailable)) UnavailableStruct {
calls_unavailableUnavailableStruct132   void calls_unavailable() { unavailable(); }
calls_unavailableUnavailableStruct133   template <class U> void calls_unavailable() { unavailable(); }
134 };
135 template <class T> struct __attribute__((unavailable)) UnavailableStructTemplated {
calls_unavailableUnavailableStructTemplated136   void calls_unavailable() { unavailable(); }
calls_unavailableUnavailableStructTemplated137   template <class U> void calls_unavailable() { unavailable(); }
138 };
139 
140 int unavailable_int() __attribute__((unavailable)); // expected-note 2 {{'unavailable_int' has been explicitly marked unavailable here}}
has_default_arg(int x=unavailable_int ())141 int has_default_arg(int x = unavailable_int()) { // expected-error{{'unavailable_int' is unavailable}}
142   return x;
143 }
144 
has_default_arg2(int x=unavailable_int ())145 int has_default_arg2(int x = unavailable_int()) __attribute__((unavailable)) {
146   return x;
147 }
148 
149 template <class T>
150 T unavailable_template() __attribute__((unavailable));
151 // expected-note@-1 {{'unavailable_template<int>' has been explicitly marked unavailable here}}
152 
153 template <class T>
has_default_arg_template(T x=unavailable_template<T> ())154 int has_default_arg_template(T x = unavailable_template<T>()) {}
155 // expected-error@-1 {{'unavailable_template<int>' is unavailable}}
156 
157 int instantiate_it = has_default_arg_template<int>();
158 // expected-note@-1 {{in instantiation of default function argument expression for 'has_default_arg_template<int>' required here}}
159 
160 template <class T>
has_default_arg_template2(T x=unavailable_template<T> ())161 int has_default_arg_template2(T x = unavailable_template<T>())
162     __attribute__((unavailable)) {}
163 
164 __attribute__((unavailable))
165 int instantiate_it2 = has_default_arg_template2<int>();
166 
167 template <class T>
phase_one_unavailable(int x=unavailable_int ())168 int phase_one_unavailable(int x = unavailable_int()) {}
169 // expected-error@-1 {{'unavailable_int' is unavailable}}
170 
171 template <class T>
phase_one_unavailable2(int x=unavailable_int ())172 int phase_one_unavailable2(int x = unavailable_int()) __attribute__((unavailable)) {}
173 
174 namespace GH61815 {
175 template <class _ValueType = int>
176 class __attribute__((unavailable)) polymorphic_allocator {}; // expected-note 2 {{'polymorphic_allocator<void>' has been explicitly marked unavailable here}}
177 
f()178 void f() {
179   polymorphic_allocator<void> a; // expected-error {{'polymorphic_allocator<void>' is unavailable}}
180   polymorphic_allocator<void> b; // expected-error {{'polymorphic_allocator<void>' is unavailable}}
181 }
182 }
183