[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / clang / test / SemaCXX / warn-unused-local-typedef.cpp
blob554ea37eeb28207bb25adee4a7410722ceeb8001
1 // RUN: %clang_cc1 -fsyntax-only -Wunused-local-typedef -verify -std=c++1y %s
3 struct S {
4 typedef int Foo; // no diag
5 };
7 namespace N {
8 typedef int Foo; // no diag
9 typedef int Foo2; // no diag
12 template <class T> class Vec {};
14 typedef int global_foo; // no diag
16 void f() {
17 typedef int foo0; // expected-warning {{unused typedef 'foo0'}}
18 using foo0alias = int ; // expected-warning {{unused type alias 'foo0alias'}}
20 typedef int foo1 __attribute__((unused)); // no diag
22 typedef int foo2;
24 typedef int foo2; // expected-warning {{unused typedef 'foo2'}}
26 typedef foo2 foo3; // expected-warning {{unused typedef 'foo3'}}
28 typedef int foo2_2; // expected-warning {{unused typedef 'foo2_2'}}
30 typedef int foo2_2;
31 typedef foo2_2 foo3_2; // expected-warning {{unused typedef 'foo3_2'}}
34 typedef int foo4;
35 foo4 the_thing;
37 typedef int* foo5;
38 typedef foo5* foo6; // no diag
39 foo6 *myptr;
41 struct S2 {
42 typedef int Foo; // no diag
43 typedef int Foo2; // expected-warning {{unused typedef 'Foo2'}}
45 struct Deeper {
46 typedef int DeepFoo; // expected-warning {{unused typedef 'DeepFoo'}}
50 S2::Foo s2foo;
52 typedef struct {} foostruct; // expected-warning {{unused typedef 'foostruct'}}
54 typedef struct {} foostruct2; // no diag
55 foostruct2 fs2;
57 typedef int vecint; // no diag
58 Vec<vecint> v;
60 N::Foo nfoo;
62 typedef int ConstExprInt;
63 static constexpr int a = (ConstExprInt)4;
66 int printf(char const *, ...);
68 void test() {
69 typedef signed long int superint; // no diag
70 printf("%ld", (superint)42);
72 typedef signed long int superint2; // no diag
73 printf("%ld", static_cast<superint2>(42));
75 #pragma clang diagnostic push
76 #pragma clang diagnostic ignored "-Wunused-local-typedef"
77 typedef int trungl_bot_was_here; // no diag
78 #pragma clang diagnostic pop
80 typedef int foo; // expected-warning {{unused typedef 'foo'}}
83 template <class T>
84 void template_fun(T t) {
85 typedef int foo; // expected-warning {{unused typedef 'foo'}}
86 typedef int bar; // no-diag
87 bar asdf;
89 struct S2 {
90 typedef int Foo; // no diag
92 typedef int Foo2; // expected-warning {{unused typedef 'Foo2'}}
94 typedef int Foo3; // no diag
97 typename S2::Foo s2foo;
98 typename T::Foo s3foo;
100 typedef typename S2::Foo3 TTSF; // expected-warning {{unused typedef 'TTSF'}}
102 void template_fun_user() {
103 struct Local {
104 typedef int Foo; // no-diag
105 typedef int Bar; // expected-warning {{unused typedef 'Bar'}}
106 } p;
107 template_fun(p);
110 void typedef_in_nested_name() {
111 typedef struct { // expected-warning {{add a tag name}}
112 typedef int Foo; // expected-note {{}}
113 } A; // expected-note {{}}
114 A::Foo adsf;
116 using A2 = struct { // expected-warning {{add a tag name}} expected-note {{this alias declaration}}
117 typedef int Foo; // expected-note {{}}
119 A2::Foo adsf2;
122 auto sneaky() {
123 struct S {
124 // Local typedefs can be used after the scope they were in has closed:
125 typedef int t;
127 // Even if they aren't, this could be an inline function that could be used
128 // in another TU, so this shouldn't warn either:
129 typedef int s;
131 private:
132 typedef int p; // expected-warning{{unused typedef 'p'}}
134 return S();
136 auto x = sneaky();
137 decltype(x)::t y;
139 static auto static_sneaky() {
140 struct S {
141 typedef int t;
142 // This function has internal linkage, so we can warn:
143 typedef int s; // expected-warning {{unused typedef 's'}}
145 return S();
147 auto sx = static_sneaky();
148 decltype(sx)::t sy;
150 auto sneaky_with_friends() {
151 struct S {
152 private:
153 friend class G;
154 // Can't warn if we have friends:
155 typedef int p;
157 return S();
160 namespace {
161 auto nstatic_sneaky() {
162 struct S {
163 typedef int t;
164 // This function has internal linkage, so we can warn:
165 typedef int s; // expected-warning {{unused typedef 's'}}
167 return S();
169 auto nsx = nstatic_sneaky();
170 decltype(nsx)::t nsy;
173 // Like sneaky(), but returning pointer to local type
174 template<typename T>
175 struct remove_reference { typedef T type; };
176 template<typename T> struct remove_reference<T&> { typedef T type; };
177 auto pointer_sneaky() {
178 struct S {
179 typedef int t;
180 typedef int s;
182 return (S*)nullptr;
184 remove_reference<decltype(*pointer_sneaky())>::type::t py;
186 // Like sneaky(), but returning templated struct referencing local type.
187 template <class T> struct container { int a; T t; };
188 auto template_sneaky() {
189 struct S {
190 typedef int t;
191 typedef int s;
193 return container<S>();
195 auto tx = template_sneaky();
196 decltype(tx.t)::t ty;
198 // Like sneaky(), but doing its sneakiness by returning a member function
199 // pointer.
200 auto sneaky_memfun() {
201 struct S {
202 typedef int type;
203 int n;
205 return &S::n;
208 template <class T> void sneaky_memfun_g(int T::*p) {
209 typename T::type X;
212 void sneaky_memfun_h() {
213 sneaky_memfun_g(sneaky_memfun());
216 void typedefs_in_constructors() {
217 struct A {};
218 struct B : public A {
219 // Neither of these two should warn:
220 typedef A INHERITED;
221 B() : INHERITED() {}
223 typedef B SELF;
224 B(int) : SELF() {}
228 void *operator new(__SIZE_TYPE__, void *p) throw() { return p; }
229 void placement_new_and_delete() {
230 struct MyStruct { };
231 char memory[sizeof(MyStruct)];
232 void *p = memory;
234 typedef MyStruct A_t1;
235 MyStruct *a = new (p) A_t1();
237 typedef MyStruct A_t2;
238 a->~A_t2();
241 // This should not disable any warnings:
242 #pragma clang diagnostic ignored "-Wunused-local-typedef"