forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestsimplifyusing.cpp
More file actions
624 lines (541 loc) · 21.5 KB
/
testsimplifyusing.cpp
File metadata and controls
624 lines (541 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2019 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "platform.h"
#include "settings.h"
#include "testsuite.h"
#include "token.h"
#include "tokenize.h"
#include "tokenlist.h"
struct InternalError;
class TestSimplifyUsing : public TestFixture {
public:
TestSimplifyUsing() : TestFixture("TestSimplifyUsing") {
}
private:
Settings settings0;
Settings settings1;
Settings settings2;
void run() OVERRIDE {
settings0.addEnabled("style");
settings2.addEnabled("style");
TEST_CASE(simplifyUsing1);
TEST_CASE(simplifyUsing2);
TEST_CASE(simplifyUsing3);
TEST_CASE(simplifyUsing4);
TEST_CASE(simplifyUsing5);
TEST_CASE(simplifyUsing6);
TEST_CASE(simplifyUsing7);
TEST_CASE(simplifyUsing8);
TEST_CASE(simplifyUsing9);
TEST_CASE(simplifyUsing10);
TEST_CASE(simplifyUsing11);
TEST_CASE(simplifyUsing12);
TEST_CASE(simplifyUsing13);
TEST_CASE(simplifyUsing14);
TEST_CASE(simplifyUsing15);
TEST_CASE(simplifyUsing16);
TEST_CASE(simplifyUsing8970);
TEST_CASE(simplifyUsing8971);
TEST_CASE(simplifyUsing8976);
TEST_CASE(simplifyUsing9040);
TEST_CASE(simplifyUsing9042);
TEST_CASE(simplifyUsing9191);
TEST_CASE(simplifyUsing9381);
TEST_CASE(simplifyUsing9385);
TEST_CASE(simplifyUsing9388);
}
std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Native, bool debugwarnings = true) {
errout.str("");
settings0.inconclusive = true;
settings0.debugwarnings = debugwarnings;
settings0.platform(type);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
if (simplify)
tokenizer.simplifyTokenList2();
return tokenizer.tokens()->stringifyList(nullptr, !simplify);
}
void simplifyUsing1() {
const char code[] = "class A\n"
"{\n"
"public:\n"
" using duplicate = wchar_t;\n"
" void foo() {}\n"
"};\n"
"using duplicate = A;\n"
"int main()\n"
"{\n"
" duplicate a;\n"
" a.foo();\n"
" A::duplicate c = 0;\n"
"}";
const char expected[] =
"class A "
"{ "
"public: "
""
"void foo ( ) { } "
"} ; "
"int main ( ) "
"{ "
"A a ; "
"a . foo ( ) ; "
"wchar_t c ; c = 0 ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing2() {
const char code[] = "class A;\n"
"using duplicate = A;\n"
"class A\n"
"{\n"
"public:\n"
"using duplicate = wchar_t;\n"
"duplicate foo() { wchar_t b; return b; }\n"
"};";
const char expected[] =
"class A ; "
"class A "
"{ "
"public: "
""
"wchar_t foo ( ) { wchar_t b ; return b ; } "
"} ;";
ASSERT_EQUALS(expected, tok(code));
}
void simplifyUsing3() {
const char code[] = "class A {};\n"
"using duplicate = A;\n"
"wchar_t foo()\n"
"{\n"
"using duplicate = wchar_t;\n"
"duplicate b;\n"
"return b;\n"
"}\n"
"int main()\n"
"{\n"
"duplicate b;\n"
"}";
const char expected[] =
"class A { } ; "
"wchar_t foo ( ) "
"{ "
""
"wchar_t b ; "
"return b ; "
"} "
"int main ( ) "
"{ "
"A b ; "
"}";
ASSERT_EQUALS(expected, tok(code));
}
void simplifyUsing4() {
const char code[] = "using s32 = int;\n"
"using u32 = unsigned int;\n"
"void f()\n"
"{\n"
" s32 ivar = -2;\n"
" u32 uvar = 2;\n"
" return uvar / ivar;\n"
"}";
const char expected[] =
"void f ( ) "
"{ "
"int ivar ; ivar = -2 ; "
"unsigned int uvar ; uvar = 2 ; "
"return uvar / ivar ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing5() {
const char code[] =
"using YY_BUFFER_STATE = struct yy_buffer_state *;\n"
"void f()\n"
"{\n"
" YY_BUFFER_STATE state;\n"
"}";
const char expected[] =
"void f ( ) "
"{ "
"struct yy_buffer_state * state ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing6() {
const char code[] =
"namespace VL {\n"
" using float_t = float;\n"
" inline VL::float_t fast_atan2(VL::float_t y, VL::float_t x){}\n"
"}";
const char expected[] =
"namespace VL { "
""
"float fast_atan2 ( float y , float x ) { } "
"}";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing7() {
const char code[] = "using abc = int; "
"Fred :: abc f ;";
const char expected[] = "Fred :: abc f ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing8() {
const char code[] = "using INT = int;\n"
"using UINT = unsigned int;\n"
"using PINT = int *;\n"
"using PUINT = unsigned int *;\n"
"using RINT = int &;\n"
"using RUINT = unsigned int &;\n"
"using RCINT = const int &;\n"
"using RCUINT = const unsigned int &;\n"
"INT ti;\n"
"UINT tui;\n"
"PINT tpi;\n"
"PUINT tpui;\n"
"RINT tri;\n"
"RUINT trui;\n"
"RCINT trci;\n"
"RCUINT trcui;";
const char expected[] =
"int ti ; "
"unsigned int tui ; "
"int * tpi ; "
"unsigned int * tpui ; "
"int & tri ; "
"unsigned int & trui ; "
"const int & trci ; "
"const unsigned int & trcui ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing9() {
const char code[] = "using S = struct s;\n"
"using PS = S *;\n"
"using T = struct t { int a; };\n"
"using TP = T *;\n"
"using U = struct { int a; };\n"
"using V = U *;\n"
"using W = struct { int a; } *;\n"
"S s;\n"
"PS ps;\n"
"T t;\n"
"TP tp;\n"
"U u;\n"
"V v;\n"
"W w;";
const char expected[] =
"struct t { int a ; } ; "
"struct U { int a ; } ; "
"struct Unnamed0 { int a ; } ; "
"struct s s ; "
"struct s * ps ; "
"struct t t ; "
"struct t * tp ; "
"struct U u ; "
"struct U * v ; "
"struct Unnamed0 * w ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing10() {
const char code[] = "using S = union s;\n"
"using PS = S *;\n"
"using T = union t { int a; float b ; };\n"
"using TP = T *;\n"
"using U = union { int a; float b; };\n"
"using V = U *;\n"
"using W = union { int a; float b; } *;\n"
"S s;\n"
"PS ps;\n"
"T t;\n"
"TP tp;\n"
"U u;\n"
"V v;\n"
"W w;";
const char expected[] =
"union t { int a ; float b ; } ; "
"union U { int a ; float b ; } ; "
"union Unnamed0 { int a ; float b ; } ; "
"union s s ; "
"union s * ps ; "
"union t t ; "
"union t * tp ; "
"union U u ; "
"union U * v ; "
"union Unnamed0 * w ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing11() {
const char code[] = "using abc = enum { a = 0 , b = 1 , c = 2 };\n"
"using XYZ = enum xyz { x = 0 , y = 1 , z = 2 };\n"
"abc e1;\n"
"XYZ e2;";
const char expected[] = "enum abc { a = 0 , b = 1 , c = 2 } ; "
"enum xyz { x = 0 , y = 1 , z = 2 } ; "
"enum abc e1 ; "
"enum xyz e2 ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing12() {
const char code[] = "using V1 = vector<int>;\n"
"using V2 = std::vector<int>;\n"
"using V3 = std::vector<std::vector<int> >;\n"
"using IntListIterator = std::list<int>::iterator;\n"
"V1 v1;\n"
"V2 v2;\n"
"V3 v3;\n"
"IntListIterator iter;";
const char expected[] = "vector < int > v1 ; "
"std :: vector < int > v2 ; "
"std :: vector < std :: vector < int > > v3 ; "
"std :: list < int > :: iterator iter ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing13() {
const char code[] = "using Func = std::pair<int(*)(void*), void*>;\n"
"using CallQueue = std::vector<Func>;\n"
"int main() {\n"
" CallQueue q;\n"
"}";
const char expected[] = "int main ( ) { "
"std :: vector < std :: pair < int ( * ) ( void * ) , void * > > q ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
ASSERT_EQUALS("", errout.str());
}
void simplifyUsing14() {
const char code[] = "template <typename F, unsigned int N> struct E"
"{"
" using v = E<F,(N>0)?(N-1):0>;"
" using val = typename add<v,v>::val;"
" FP_M(val);"
"};"
"template <typename F> struct E <F,0>"
"{"
" using nal = typename D<1>::val;"
" FP_M(val);"
"};";
tok(code, true, Settings::Native, false);
ASSERT_EQUALS("", errout.str());
}
void simplifyUsing15() {
{
const char code[] = "using frame = char [10];\n"
"frame f;";
const char expected[] = "char f [ 10 ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "using frame = unsigned char [10];\n"
"frame f;";
const char expected[] = "unsigned char f [ 10 ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
}
void simplifyUsing16() {
const char code[] = "using MOT8 = char;\n"
"using CHFOO = MOT8 [4096];\n"
"using STRFOO = struct {\n"
" CHFOO freem;\n"
"};\n"
"STRFOO s;";
const char expected[] = "struct STRFOO { "
"char freem [ 4096 ] ; "
"} ; "
"struct STRFOO s ;";
ASSERT_EQUALS(expected, tok(code, false));
ASSERT_EQUALS("", errout.str());
}
void simplifyUsing8970() {
const char code[] = "using V = std::vector<int>;\n"
"struct A {\n"
" V p;\n"
"};";
const char expected[] = "struct A { "
"std :: vector < int > p ; "
"} ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing8971() {
const char code[] = "class A {\n"
"public:\n"
" using V = std::vector<double>;\n"
"};\n"
"using V = std::vector<int>;\n"
"class I {\n"
"private:\n"
" A::V v_;\n"
" V v2_;\n"
"};";
const char expected[] = "class A { "
"public: "
"} ; "
"class I { "
"private: "
"std :: vector < double > v_ ; "
"std :: vector < int > v2_ ; "
"} ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyUsing8976() {
const char code[] = "using mystring = std::string;";
const char exp[] = ";";
ASSERT_EQUALS(exp, tok(code));
}
void simplifyUsing9040() {
const char code[] = "using BOOL = unsigned; int i;";
const char exp[] = "int i ;";
ASSERT_EQUALS(exp, tok(code, true, Settings::Unix32));
ASSERT_EQUALS(exp, tok(code, true, Settings::Unix64));
ASSERT_EQUALS(exp, tok(code, true, Settings::Win32A));
ASSERT_EQUALS(exp, tok(code, true, Settings::Win32W));
ASSERT_EQUALS(exp, tok(code, true, Settings::Win64));
}
void simplifyUsing9042() {
const char code[] = "template <class T>\n"
"class c {\n"
" int i = 0;\n"
" c() { i--; }\n"
"};\n"
"template <class T>\n"
"class s {};\n"
"using BOOL = char;";
const char exp[] = "template < class T > "
"class c { "
"int i ; i = 0 ; "
"c ( ) { i -- ; } "
"} ;";
ASSERT_EQUALS(exp, tok(code, true, Settings::Win64));
}
void simplifyUsing9191() {
const char code[] = "namespace NS1 {\n"
" namespace NS2 {\n"
" using _LONG = signed long long;\n"
" }\n"
"}\n"
"void f1() {\n"
" using namespace NS1;\n"
" NS2::_LONG A;\n"
"}\n"
"void f2() {\n"
" using namespace NS1::NS2;\n"
" _LONG A;\n"
"}";
const char exp[] = "namespace NS1 { "
"} "
"void f1 ( ) { "
"using namespace NS1 ; "
"signed long long A ; "
"} "
"void f2 ( ) { "
"using namespace NS1 :: NS2 ; "
"signed long long A ; "
"}";
ASSERT_EQUALS(exp, tok(code, false));
}
void simplifyUsing9381() {
const char code[] = "namespace ns {\n"
" class Result;\n"
" using UniqueResultPtr = std::unique_ptr<Result>;\n"
" class A {\n"
" public:\n"
" void func(UniqueResultPtr);\n"
" };\n"
" void A::func(UniqueResultPtr) {\n"
" }\n"
"}";
const char exp[] = "namespace ns { "
"class Result ; "
"class A { "
"public: "
"void func ( std :: unique_ptr < Result > ) ; "
"} ; "
"void A :: func ( std :: unique_ptr < Result > ) { "
"} "
"}";
ASSERT_EQUALS(exp, tok(code, false));
}
void simplifyUsing9385() {
{
const char code[] = "class A {\n"
"public:\n"
" using Foo = int;\n"
" A(Foo foo);\n"
" ~A();\n"
" void func(Foo foo);\n"
"};\n"
"A::A(Foo) { }\n"
"A::~A() { Foo foo; }\n"
"void A::func(Foo) { }";
const char exp[] = "class A { "
"public: "
"A ( int foo ) ; "
"~ A ( ) ; "
"void func ( int foo ) ; "
"} ; "
"A :: A ( int ) { } "
"A :: ~ A ( ) { int foo ; } "
"void A :: func ( int ) { }";
ASSERT_EQUALS(exp, tok(code, false));
}
{
const char code[] = "class A {\n"
"public:\n"
" struct B {\n"
" using Foo = int;\n"
" B(Foo foo);\n"
" ~B();\n"
" void func(Foo foo);\n"
" };\n"
"};\n"
"A::B::B(Foo) { }\n"
"A::B::~B() { Foo foo; }\n"
"void A::B::func(Foo) { }";
const char exp[] = "class A { "
"public: "
"struct B { "
"B ( int foo ) ; "
"~ B ( ) ; "
"void func ( int foo ) ; "
"} ; "
"} ; "
"A :: B :: B ( int ) { } "
"A :: B :: ~ B ( ) { int foo ; } "
"void A :: B :: func ( int ) { }";
ASSERT_EQUALS(exp, tok(code, false));
}
}
void simplifyUsing9388() {
const char code[] = "class A {\n"
"public:\n"
" using Type = int;\n"
" A(Type&);\n"
" Type& t_;\n"
"};\n"
"A::A(Type& t) : t_(t) { }";
const char exp[] = "class A { "
"public: "
"A ( int & ) ; "
"int & t_ ; "
"} ; "
"A :: A ( int & t ) : t_ ( t ) { }";
ASSERT_EQUALS(exp, tok(code, false));
}
};
REGISTER_TEST(TestSimplifyUsing)