1// RUN: %check_clang_tidy %s modernize-use-using %t -- -- -fno-delayed-template-parsing -I %S/Inputs/use-using/
2
3typedef int Type;
4// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' [modernize-use-using]
5// CHECK-FIXES: using Type = int;
6
7typedef long LL;
8// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
9// CHECK-FIXES: using LL = long;
10
11typedef int Bla;
12// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
13// CHECK-FIXES: using Bla = int;
14
15typedef Bla Bla2;
16// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
17// CHECK-FIXES: using Bla2 = Bla;
18
19typedef void (*type)(int, int);
20// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
21// CHECK-FIXES: using type = void (*)(int, int);
22
23typedef void (*type2)();
24// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
25// CHECK-FIXES: using type2 = void (*)();
26
27class Class {
28 typedef long long Type;
29 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
30 // CHECK-FIXES: using Type = long long;
31};
32
33typedef void (Class::*MyPtrType)(Bla) const;
34// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
35// CHECK-FIXES: using MyPtrType = void (Class::*)(Bla)[[ATTR:( __attribute__\(\(thiscall\)\))?]] const;
36
37class Iterable {
38public:
39 class Iterator {};
40};
41
42template <typename T>
43class Test {
44 typedef typename T::iterator Iter;
45 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
46 // CHECK-FIXES: using Iter = typename T::iterator;
47};
48
49using balba = long long;
50
51union A {};
52
53typedef void (A::*PtrType)(int, int) const;
54// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
55// CHECK-FIXES: using PtrType = void (A::*)(int, int)[[ATTR]] const;
56
57typedef Class some_class;
58// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
59// CHECK-FIXES: using some_class = Class;
60
61typedef Class Cclass;
62// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
63// CHECK-FIXES: using Cclass = Class;
64
65typedef Cclass cclass2;
66// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
67// CHECK-FIXES: using cclass2 = Cclass;
68
69class cclass {};
70
71typedef void (cclass::*MyPtrType3)(Bla);
72// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
73// CHECK-FIXES: using MyPtrType3 = void (cclass::*)(Bla)[[ATTR]];
74
75using my_class = int;
76
77typedef Test<my_class *> another;
78// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
79// CHECK-FIXES: using another = Test<my_class *>;
80
81typedef int* PInt;
82// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
83// CHECK-FIXES: using PInt = int *;
84
85typedef int bla1, bla2, bla3;
86// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
87// CHECK-MESSAGES: :[[@LINE-2]]:17: warning: use 'using' instead of 'typedef'
88// CHECK-MESSAGES: :[[@LINE-3]]:23: warning: use 'using' instead of 'typedef'
89// CHECK-FIXES: using bla1 = int;
90// CHECK-FIXES-NEXT: using bla2 = int;
91// CHECK-FIXES-NEXT: using bla3 = int;
92
93#define CODE typedef int INT
94
95CODE;
96// CHECK-FIXES: #define CODE typedef int INT
97// CHECK-FIXES: CODE;
98
99struct Foo;
100#define Bar Baz
101typedef Foo Bar;
102// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
103// CHECK-FIXES: #define Bar Baz
104// CHECK-FIXES: using Baz = Foo;
105
106#define TYPEDEF typedef
107TYPEDEF Foo Bak;
108// CHECK-FIXES: #define TYPEDEF typedef
109// CHECK-FIXES: TYPEDEF Foo Bak;
110
111#define FOO Foo
112typedef FOO Bam;
113// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
114// CHECK-FIXES: #define FOO Foo
115// CHECK-FIXES: using Bam = Foo;
116
117typedef struct Foo Bap;
118// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
119// CHECK-FIXES: using Bap = struct Foo;
120
121struct Foo typedef Bap2;
122// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
123// CHECK-FIXES: using Bap2 = struct Foo;
124
125Foo typedef Bap3;
126// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
127// CHECK-FIXES: using Bap3 = Foo;
128
129typedef struct Unknown Baq;
130// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
131// CHECK-FIXES: using Baq = struct Unknown;
132
133struct Unknown2 typedef Baw;
134// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
135// CHECK-FIXES: using Baw = struct Unknown2;
136
137int typedef Bax;
138// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
139// CHECK-FIXES: using Bax = int;
140
141typedef struct Q1 { int a; } S1;
142// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
143// CHECK-FIXES: using S1 = struct Q1 { int a; };
144typedef struct { int b; } S2;
145// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
146// CHECK-FIXES: using S2 = struct { int b; };
147struct Q2 { int c; } typedef S3;
148// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
149// CHECK-FIXES: using S3 = struct Q2 { int c; };
150struct { int d; } typedef S4;
151// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
152// CHECK-FIXES: using S4 = struct { int d; };
153
154namespace my_space {
155 class my_cclass {};
156 typedef my_cclass FuncType;
157// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
158// CHECK-FIXES: using FuncType = my_cclass;
159}
160
161#define lol 4
162typedef unsigned Map[lol];
163// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
164// CHECK-FIXES: typedef unsigned Map[lol];
165
166typedef void (*fun_type)();
167// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
168// CHECK-FIXES: using fun_type = void (*)();
169
170namespace template_instantiations {
171template <typename T>
172class C {
173 protected:
174 typedef C<T> super;
175 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
176 // CHECK-FIXES: using super = C<T>;
177 virtual void f();
178
179public:
180 virtual ~C();
181};
182
183class D : public C<D> {
184 void f() override { super::f(); }
185};
186class E : public C<E> {
187 void f() override { super::f(); }
188};
189}
190
191template <typename T1, typename T2>
192class TwoArgTemplate {
193 typedef TwoArgTemplate<T1, T2> self;
194 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
195 // CHECK-FIXES: using self = TwoArgTemplate<T1, T2>;
196};
197
198template <bool B, typename T>
199struct S {};
200
201typedef S<(0 > 0), int> S_t, *S_p;
202// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
203// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use 'using' instead of 'typedef'
204// CHECK-FIXES: using S_t = S<(0 > 0), int>;
205// CHECK-FIXES-NEXT: using S_p = S_t*;
206
207typedef S<(0 < 0), int> S2_t, *S2_p;
208// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
209// CHECK-MESSAGES: :[[@LINE-2]]:29: warning: use 'using' instead of 'typedef'
210// CHECK-FIXES: using S2_t = S<(0 < 0), int>;
211// CHECK-FIXES-NEXT: using S2_p = S2_t*;
212
213typedef S<(0 > 0 && (3 > 1) && (1 < 1)), int> S3_t;
214// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
215// CHECK-FIXES: using S3_t = S<(0 > 0 && (3 > 1) && (1 < 1)), int>;
216
217template <bool B>
218struct Q {};
219
220constexpr bool b[1] = {true};
221
222typedef Q<b[0 < 0]> Q_t, *Q_p;
223// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
224// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: use 'using' instead of 'typedef'
225// CHECK-FIXES: using Q_t = Q<b[0 < 0]>;
226// CHECK-FIXES-NEXT: using Q_p = Q_t*;
227
228typedef Q<b[0 < 0]> Q2_t;
229// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
230// CHECK-FIXES: using Q2_t = Q<b[0 < 0]>;
231
232struct T {
233 constexpr T(bool) {}
234
235 static constexpr bool b = true;
236};
237
238typedef Q<T{0 < 0}.b> Q3_t, *Q3_p;
239// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
240// CHECK-MESSAGES: :[[@LINE-2]]:27: warning: use 'using' instead of 'typedef'
241// CHECK-FIXES: using Q3_t = Q<T{0 < 0}.b>;
242// CHECK-FIXES-NEXT: using Q3_p = Q3_t*;
243
244typedef Q<T{0 < 0}.b> Q3_t;
245// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
246// CHECK-FIXES: using Q3_t = Q<T{0 < 0}.b>;
247
248typedef TwoArgTemplate<TwoArgTemplate<int, Q<T{0 < 0}.b> >, S<(0 < 0), Q<b[0 < 0]> > > Nested_t;
249// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
250// CHECK-FIXES: using Nested_t = TwoArgTemplate<TwoArgTemplate<int, Q<T{0 < 0}.b>>, S<(0 < 0), Q<b[0 < 0]>>>;
251
252template <typename a>
253class TemplateKeyword {
254 typedef typename a::template b<> d;
255 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
256 // CHECK-FIXES: using d = typename a::template b<>;
257
258 typedef typename a::template b<>::c d2;
259 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
260 // CHECK-FIXES: using d2 = typename a::template b<>::c;
261};
262
263template <typename... Args>
264class Variadic {};
265
266typedef Variadic<Variadic<int, bool, Q<T{0 < 0}.b> >, S<(0 < 0), Variadic<Q<b[0 < 0]> > > > Variadic_t;
267// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
268// CHECK-FIXES: using Variadic_t = Variadic<Variadic<int, bool, Q<T{0 < 0}.b>>, S<(0 < 0), Variadic<Q<b[0 < 0]>>>>
269
270typedef Variadic<Variadic<int, bool, Q<T{0 < 0}.b> >, S<(0 < 0), Variadic<Q<b[0 < 0]> > > > Variadic_t, *Variadic_p;
271// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
272// CHECK-MESSAGES: :[[@LINE-2]]:103: warning: use 'using' instead of 'typedef'
273// CHECK-FIXES: using Variadic_t = Variadic<Variadic<int, bool, Q<T{0 < 0}.b>>, S<(0 < 0), Variadic<Q<b[0 < 0]>>>>;
274// CHECK-FIXES-NEXT: using Variadic_p = Variadic_t*;
275
276typedef struct { int a; } R_t, *R_p;
277// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
278// CHECK-MESSAGES: :[[@LINE-2]]:30: warning: use 'using' instead of 'typedef'
279// CHECK-FIXES: using R_t = struct { int a; };
280// CHECK-FIXES-NEXT: using R_p = R_t*;
281
282typedef enum { ea1, eb1 } EnumT1;
283// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
284// CHECK-FIXES: using EnumT1 = enum { ea1, eb1 };
285
286#include "modernize-use-using.h"
287
288typedef enum { ea2, eb2 } EnumT2_CheckTypedefImpactFromAnotherFile;
289// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
290// CHECK-FIXES: using EnumT2_CheckTypedefImpactFromAnotherFile = enum { ea2, eb2 };
291
292template <int A>
293struct InjectedClassName {
294 typedef InjectedClassName b;
295 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
296 // CHECK-FIXES: using b = InjectedClassName;
297};
298
299template <int>
300struct InjectedClassNameWithUnnamedArgument {
301 typedef InjectedClassNameWithUnnamedArgument b;
302 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
303 // CHECK-FIXES: using b = InjectedClassNameWithUnnamedArgument;
304};
305
306typedef struct { int a; union { int b; }; } PR50990;
307// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
308// CHECK-FIXES: using PR50990 = struct { int a; union { int b; }; };
309
310typedef struct { struct { int a; struct { struct { int b; } c; int d; } e; } f; int g; } PR50990_nested;
311// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
312// CHECK-FIXES: using PR50990_nested = struct { struct { int a; struct { struct { int b; } c; int d; } e; } f; int g; };
313
314typedef struct { struct { int a; } b; union { int c; float d; struct { int e; }; }; struct { double f; } g; } PR50990_siblings;
315// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
316// CHECK-FIXES: using PR50990_siblings = struct { struct { int a; } b; union { int c; float d; struct { int e; }; }; struct { double f; } g; };
317
318typedef void (*ISSUE_65055_1)(int);
319typedef bool (*ISSUE_65055_2)(int);
320// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: use 'using' instead of 'typedef'
321// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: use 'using' instead of 'typedef'
322// CHECK-FIXES: {{^}}using ISSUE_65055_1 = void (*)(int);{{$}}
323// CHECK-FIXES: {{^}}using ISSUE_65055_2 = bool (*)(int);{{$}}
324
325typedef class ISSUE_67529_1 *ISSUE_67529;
326// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
327// CHECK-FIXES: using ISSUE_67529 = class ISSUE_67529_1 *;
328
329// Some Header
330extern "C" {
331
332typedef int InExternC;
333// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' [modernize-use-using]
334// CHECK-FIXES: using InExternC = int;
335
336}
337
338extern "C++" {
339
340typedef int InExternCPP;
341// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' [modernize-use-using]
342// CHECK-FIXES: using InExternCPP = int;
343
344}
345
346namespace ISSUE_72179
347{
348 void foo()
349 {
350 typedef int a;
351 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 'typedef' [modernize-use-using]
352 // CHECK-FIXES: using a = int;
353
354 }
355
356 void foo2()
357 {
358 typedef struct { int a; union { int b; }; } c;
359 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 'typedef' [modernize-use-using]
360 // CHECK-FIXES: using c = struct { int a; union { int b; }; };
361 }
362
363 template <typename T>
364 void foo3()
365 {
366 typedef T b;
367 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 'typedef' [modernize-use-using]
368 // CHECK-FIXES: using b = T;
369 }
370
371 template <typename T>
372 class MyClass
373 {
374 void foo()
375 {
376 typedef MyClass c;
377 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use 'using' instead of 'typedef' [modernize-use-using]
378 // CHECK-FIXES: using c = MyClass;
379 }
380 };
381
382 const auto foo4 = [](int a){typedef int d;};
383 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: use 'using' instead of 'typedef' [modernize-use-using]
384 // CHECK-FIXES: const auto foo4 = [](int a){using d = int;};
385}
386

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp