forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsimplifytypedef.cpp
3655 lines (3236 loc) · 152 KB
/
testsimplifytypedef.cpp
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2022 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 "errortypes.h"
#include "platform.h"
#include "settings.h"
#include "testsuite.h"
#include "token.h"
#include "tokenize.h"
#include "tokenlist.h"
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <simplecpp.h>
class TestSimplifyTypedef : public TestFixture {
public:
TestSimplifyTypedef() : TestFixture("TestSimplifyTypedef") {}
private:
Settings settings0;
Settings settings1;
Settings settings2;
void run() override {
settings0.severity.enable(Severity::style);
settings2.severity.enable(Severity::style);
// If there are unused templates, keep those
settings0.checkUnusedTemplates = true;
settings1.checkUnusedTemplates = true;
settings2.checkUnusedTemplates = true;
TEST_CASE(simplifyTypedef1);
TEST_CASE(simplifyTypedef2);
TEST_CASE(simplifyTypedef3);
TEST_CASE(simplifyTypedef4);
TEST_CASE(simplifyTypedef5);
TEST_CASE(simplifyTypedef6);
TEST_CASE(simplifyTypedef7);
TEST_CASE(simplifyTypedef8);
TEST_CASE(simplifyTypedef9);
TEST_CASE(simplifyTypedef10);
TEST_CASE(simplifyTypedef11);
TEST_CASE(simplifyTypedef12);
TEST_CASE(simplifyTypedef13);
TEST_CASE(simplifyTypedef14);
TEST_CASE(simplifyTypedef15);
TEST_CASE(simplifyTypedef16);
TEST_CASE(simplifyTypedef17);
TEST_CASE(simplifyTypedef18); // typedef vector<int[4]> a;
TEST_CASE(simplifyTypedef19);
TEST_CASE(simplifyTypedef20);
TEST_CASE(simplifyTypedef21);
TEST_CASE(simplifyTypedef22);
TEST_CASE(simplifyTypedef23);
TEST_CASE(simplifyTypedef24);
TEST_CASE(simplifyTypedef25);
TEST_CASE(simplifyTypedef26);
TEST_CASE(simplifyTypedef27);
TEST_CASE(simplifyTypedef28);
TEST_CASE(simplifyTypedef29);
TEST_CASE(simplifyTypedef30);
TEST_CASE(simplifyTypedef31);
TEST_CASE(simplifyTypedef32);
TEST_CASE(simplifyTypedef33);
TEST_CASE(simplifyTypedef34); // ticket #1411
TEST_CASE(simplifyTypedef35);
TEST_CASE(simplifyTypedef36); // ticket #1434
TEST_CASE(simplifyTypedef37); // ticket #1449
TEST_CASE(simplifyTypedef38);
TEST_CASE(simplifyTypedef43); // ticket #1588
TEST_CASE(simplifyTypedef44);
TEST_CASE(simplifyTypedef45); // ticket #1613
TEST_CASE(simplifyTypedef46);
TEST_CASE(simplifyTypedef47);
TEST_CASE(simplifyTypedef48); // ticket #1673
TEST_CASE(simplifyTypedef49); // ticket #1691
TEST_CASE(simplifyTypedef50);
TEST_CASE(simplifyTypedef51);
TEST_CASE(simplifyTypedef52); // ticket #1782
TEST_CASE(simplifyTypedef54); // ticket #1814
TEST_CASE(simplifyTypedef55);
TEST_CASE(simplifyTypedef56); // ticket #1829
TEST_CASE(simplifyTypedef57); // ticket #1846
TEST_CASE(simplifyTypedef58); // ticket #1963
TEST_CASE(simplifyTypedef59); // ticket #2011
TEST_CASE(simplifyTypedef60); // ticket #2035
TEST_CASE(simplifyTypedef61); // ticket #2074 and 2075
TEST_CASE(simplifyTypedef62); // ticket #2082
TEST_CASE(simplifyTypedef63); // ticket #2175 'typedef float x[3];'
TEST_CASE(simplifyTypedef64);
TEST_CASE(simplifyTypedef65); // ticket #2314
TEST_CASE(simplifyTypedef66); // ticket #2341
TEST_CASE(simplifyTypedef67); // ticket #2354
TEST_CASE(simplifyTypedef68); // ticket #2355
TEST_CASE(simplifyTypedef69); // ticket #2348
TEST_CASE(simplifyTypedef70); // ticket #2348
TEST_CASE(simplifyTypedef71); // ticket #2348
TEST_CASE(simplifyTypedef72); // ticket #2375
TEST_CASE(simplifyTypedef73); // ticket #2412
TEST_CASE(simplifyTypedef74); // ticket #2414
TEST_CASE(simplifyTypedef75); // ticket #2426
TEST_CASE(simplifyTypedef76); // ticket #2453
TEST_CASE(simplifyTypedef77); // ticket #2554
TEST_CASE(simplifyTypedef78); // ticket #2568
TEST_CASE(simplifyTypedef79); // ticket #2348
TEST_CASE(simplifyTypedef80); // ticket #2587
TEST_CASE(simplifyTypedef81); // ticket #2603
TEST_CASE(simplifyTypedef82); // ticket #2403
TEST_CASE(simplifyTypedef83); // ticket #2620
TEST_CASE(simplifyTypedef84); // ticket #2630
TEST_CASE(simplifyTypedef85); // ticket #2651
TEST_CASE(simplifyTypedef86); // ticket #2581
TEST_CASE(simplifyTypedef87); // ticket #2651
TEST_CASE(simplifyTypedef88); // ticket #2675
TEST_CASE(simplifyTypedef89); // ticket #2717
TEST_CASE(simplifyTypedef90); // ticket #2718
TEST_CASE(simplifyTypedef91); // ticket #2716
TEST_CASE(simplifyTypedef92); // ticket #2736
TEST_CASE(simplifyTypedef93); // ticket #2738
TEST_CASE(simplifyTypedef94); // ticket #1982
TEST_CASE(simplifyTypedef95); // ticket #2844
TEST_CASE(simplifyTypedef96); // ticket #2886
TEST_CASE(simplifyTypedef97); // ticket #2983 (segmentation fault)
TEST_CASE(simplifyTypedef99); // ticket #2999
TEST_CASE(simplifyTypedef100); // ticket #3000
TEST_CASE(simplifyTypedef101); // ticket #3003 (segmentation fault)
TEST_CASE(simplifyTypedef102); // ticket #3004
TEST_CASE(simplifyTypedef103); // ticket #3007
TEST_CASE(simplifyTypedef104); // ticket #3070
TEST_CASE(simplifyTypedef105); // ticket #3616
TEST_CASE(simplifyTypedef106); // ticket #3619
TEST_CASE(simplifyTypedef107); // ticket #3963 - bad code => segmentation fault
TEST_CASE(simplifyTypedef108); // ticket #4777
TEST_CASE(simplifyTypedef109); // ticket #1823 - rvalue reference
TEST_CASE(simplifyTypedef110); // ticket #6268
TEST_CASE(simplifyTypedef111); // ticket #6345
TEST_CASE(simplifyTypedef112); // ticket #6048
TEST_CASE(simplifyTypedef113); // ticket #7030
TEST_CASE(simplifyTypedef114); // ticket #7058 - skip "struct", AB::..
TEST_CASE(simplifyTypedef115); // ticket #6998
TEST_CASE(simplifyTypedef116); // ticket #5624
TEST_CASE(simplifyTypedef117); // ticket #6507
TEST_CASE(simplifyTypedef118); // ticket #5749
TEST_CASE(simplifyTypedef119); // ticket #7541
TEST_CASE(simplifyTypedef120); // ticket #8357
TEST_CASE(simplifyTypedef121); // ticket #5766
TEST_CASE(simplifyTypedef122); // segmentation fault
TEST_CASE(simplifyTypedef123); // ticket #7406
TEST_CASE(simplifyTypedef124); // ticket #7792
TEST_CASE(simplifyTypedef125); // #8749 - typedef char A[10]; p = new A[1];
TEST_CASE(simplifyTypedef126); // ticket #5953
TEST_CASE(simplifyTypedef127); // ticket #8878
TEST_CASE(simplifyTypedef128); // ticket #9053
TEST_CASE(simplifyTypedef129);
TEST_CASE(simplifyTypedef130); // ticket #9446
TEST_CASE(simplifyTypedef131); // ticket #9446
TEST_CASE(simplifyTypedef132); // ticket #9739 - using
TEST_CASE(simplifyTypedef133); // ticket #9812 - using
TEST_CASE(simplifyTypedef134);
TEST_CASE(simplifyTypedef135); // ticket #10068
TEST_CASE(simplifyTypedef136);
TEST_CASE(simplifyTypedef137);
TEST_CASE(simplifyTypedef138);
TEST_CASE(simplifyTypedef139);
TEST_CASE(simplifyTypedef140); // #10798
TEST_CASE(simplifyTypedefFunction1);
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
TEST_CASE(simplifyTypedefFunction3);
TEST_CASE(simplifyTypedefFunction4);
TEST_CASE(simplifyTypedefFunction5);
TEST_CASE(simplifyTypedefFunction6);
TEST_CASE(simplifyTypedefFunction7);
TEST_CASE(simplifyTypedefFunction8);
TEST_CASE(simplifyTypedefFunction9);
TEST_CASE(simplifyTypedefFunction10); // #5191
TEST_CASE(simplifyTypedefShadow); // #4445 - shadow variable
TEST_CASE(simplifyTypedefMacro);
}
#define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__)
std::string tok_(const char* file, int line, const char code[], bool simplify = true, Settings::PlatformType type = Settings::Native, bool debugwarnings = true) {
errout.str("");
settings0.certainty.enable(Certainty::inconclusive);
settings0.debugwarnings = debugwarnings; // show warnings about unhandled typedef
settings0.platform(type);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
return tokenizer.tokens()->stringifyList(nullptr, !simplify);
}
std::string simplifyTypedef(const char code[]) {
errout.str("");
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.list.createTokens(istr);
tokenizer.createLinks();
tokenizer.simplifyTypedef();
return tokenizer.tokens()->stringifyList(nullptr, false);
}
std::string simplifyTypedefP(const char code[]) {
// Clear the error buffer..
errout.str("");
// Raw tokens..
std::vector<std::string> files(1, "test.cpp");
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);
// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
// Tokenize..
Tokenizer tokenizer(&settings0, this);
tokenizer.createTokens(std::move(tokens2));
tokenizer.createLinks();
tokenizer.simplifyTypedef();
return tokenizer.tokens()->stringifyList(nullptr, false);
}
#define checkSimplifyTypedef(code) checkSimplifyTypedef_(code, __FILE__, __LINE__)
void checkSimplifyTypedef_(const char code[], const char* file, int line) {
errout.str("");
// Tokenize..
settings2.certainty.enable(Certainty::inconclusive);
settings2.debugwarnings = true; // show warnings about unhandled typedef
Tokenizer tokenizer(&settings2, this);
std::istringstream istr(code);
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
}
void simplifyTypedef1() {
const char code[] = "class A\n"
"{\n"
"public:\n"
" typedef wchar_t duplicate;\n"
" void foo() {}\n"
"};\n"
"typedef A duplicate;\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 simplifyTypedef2() {
const char code[] = "class A;\n"
"typedef A duplicate;\n"
"class A\n"
"{\n"
"public:\n"
"typedef wchar_t duplicate;\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 simplifyTypedef3() {
const char code[] = "class A {};\n"
"typedef A duplicate;\n"
"wchar_t foo()\n"
"{\n"
"typedef wchar_t duplicate;\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 simplifyTypedef4() {
const char code[] = "typedef int s32;\n"
"typedef unsigned int u32;\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 simplifyTypedef5() {
// ticket #780
const char code[] =
"typedef struct yy_buffer_state *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 simplifyTypedef6() {
// ticket #983
const char code[] =
"namespace VL {\n"
" typedef float float_t ;\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 simplifyTypedef7() {
const char code[] = "typedef int abc ; "
"Fred :: abc f ;";
const char expected[] = "Fred :: abc f ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef8() {
const char code[] = "typedef int INT;\n"
"typedef unsigned int UINT;\n"
"typedef int * PINT;\n"
"typedef unsigned int * PUINT;\n"
"typedef int & RINT;\n"
"typedef unsigned int & RUINT;\n"
"typedef const int & RCINT;\n"
"typedef const unsigned int & RCUINT;\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 simplifyTypedef9() {
const char code[] = "typedef struct s S, * PS;\n"
"typedef struct t { int a; } T, *TP;\n"
"typedef struct { int a; } U;\n"
"typedef struct { int a; } * V;\n"
"S s;\n"
"PS ps;\n"
"T t;\n"
"TP tp;\n"
"U u;\n"
"V v;";
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 Unnamed0 * v ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef10() {
const char code[] = "typedef union s S, * PS;\n"
"typedef union t { int a; float b ; } T, *TP;\n"
"typedef union { int a; float b; } U;\n"
"typedef union { int a; float b; } * V;\n"
"S s;\n"
"PS ps;\n"
"T t;\n"
"TP tp;\n"
"U u;\n"
"V v;";
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 Unnamed0 * v ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef11() {
const char code[] = "typedef enum { a = 0 , b = 1 , c = 2 } abc;\n"
"typedef enum xyz { x = 0 , y = 1 , z = 2 } XYZ;\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 } ; "
"abc e1 ; "
"enum xyz e2 ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef12() {
const char code[] = "typedef vector<int> V1;\n"
"typedef std::vector<int> V2;\n"
"typedef std::vector<std::vector<int> > V3;\n"
"typedef std::list<int>::iterator IntListIterator;\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 simplifyTypedef13() {
// ticket # 1167
const char code[] = "typedef std::pair<int(*)(void*), void*> Func;"
"typedef std::vector<Func> CallQueue;"
"int main() {}";
// Tokenize and check output..
tok(code);
ASSERT_EQUALS("", errout.str());
}
void simplifyTypedef14() {
// ticket # 1232
const char code[] = "template <typename F, unsigned int N> struct E"
"{"
" typedef E<F,(N>0)?(N-1):0> v;"
" typedef typename add<v,v>::val val;"
" FP_M(val);"
"};"
"template <typename F> struct E <F,0>"
"{"
" typedef typename D<1>::val val;"
" FP_M(val);"
"};";
// Tokenize and check output..
TODO_ASSERT_THROW(tok(code, true, Settings::Native, false), InternalError); // TODO: Do not throw exception
//ASSERT_EQUALS("", errout.str());
}
void simplifyTypedef15() {
{
const char code[] = "typedef char frame[10];\n"
"frame f;";
const char expected[] = "char f [ 10 ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "typedef unsigned char frame[10];\n"
"frame f;";
const char expected[] = "unsigned char f [ 10 ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
}
void simplifyTypedef16() {
// ticket # 1252
const char code[] = "typedef char MOT8;\n"
"typedef MOT8 CHFOO[4096];\n"
"typedef struct {\n"
" CHFOO freem;\n"
"} STRFOO;";
// Tokenize and check output..
tok(code);
ASSERT_EQUALS("", errout.str());
}
void simplifyTypedef17() {
const char code[] = "typedef char * PCHAR, CHAR;\n"
"PCHAR pc;\n"
"CHAR c;";
const char expected[] =
"char * pc ; "
"char c ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef18() {
const char code[] = "typedef vector<int[4]> a;\n"
"a b;";
ASSERT_EQUALS("vector < int [ 4 ] > b ;", tok(code));
}
void simplifyTypedef19() {
{
// ticket #1275
const char code[] = "typedef struct {} A, *B, **C;\n"
"A a;\n"
"B b;\n"
"C c;";
const char expected[] =
"struct A { } ; "
"struct A a ; "
"struct A * b ; "
"struct A * * c ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "typedef struct {} A, *********B;\n"
"A a;\n"
"B b;";
const char expected[] =
"struct A { } ; "
"struct A a ; "
"struct A * * * * * * * * * b ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "typedef struct {} **********A, *B, C;\n"
"A a;\n"
"B b;\n"
"C c;";
const char expected[] =
"struct Unnamed0 { } ; "
"struct Unnamed0 * * * * * * * * * * a ; "
"struct Unnamed0 * b ; "
"struct Unnamed0 c ;";
ASSERT_EQUALS(expected, tok(code, false));
}
}
void simplifyTypedef20() {
// ticket #1284
const char code[] = "typedef jobject invoke_t (jobject, Proxy *, Method *, JArray< jobject > *);";
ASSERT_EQUALS(";", tok(code));
}
void simplifyTypedef21() {
const char code[] = "typedef void (* PF)();\n"
"typedef void * (* PFV)(void *);\n"
"PF pf;\n"
"PFV pfv;";
const char expected[] =
""
""
"void ( * pf ) ( ) ; "
"void * ( * pfv ) ( void * ) ;";
ASSERT_EQUALS(expected, simplifyTypedef(code));
}
void simplifyTypedef22() {
{
const char code[] = "class Fred {\n"
" typedef void (*testfp)();\n"
" testfp get() { return test; }\n"
" static void test() { }\n"
"};";
const char expected[] =
"class Fred { "
""
"void * get ( ) { return test ; } "
"static void test ( ) { } "
"} ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "class Fred {\n"
" typedef void * (*testfp)(void *);\n"
" testfp get() { return test; }\n"
" static void * test(void * p) { return p; }\n"
"};";
const char expected[] =
"class Fred { "
""
"void * * get ( ) { return test ; } "
"static void * test ( void * p ) { return p ; } "
"} ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "class Fred {\n"
" typedef unsigned int * (*testfp)(unsigned int *);\n"
" testfp get() { return test; }\n"
" static unsigned int * test(unsigned int * p) { return p; }\n"
"};";
const char expected[] =
"class Fred { "
""
"unsigned int * * get ( ) { return test ; } "
"static unsigned int * test ( unsigned int * p ) { return p ; } "
"} ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "class Fred {\n"
" typedef const unsigned int * (*testfp)(const unsigned int *);\n"
" testfp get() { return test; }\n"
" static const unsigned int * test(const unsigned int * p) { return p; }\n"
"};";
// static const gets changed to const static
const char expected[] =
"class Fred { "
""
"const unsigned int * * get ( ) { return test ; } "
"static const unsigned int * test ( const unsigned int * p ) { return p ; } "
"} ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "class Fred {\n"
" typedef void * (*testfp)(void *);\n"
" testfp get(int i) { return test; }\n"
" static void * test(void * p) { return p; }\n"
"};";
const char expected[] =
"class Fred { "
""
"void * * get ( int i ) { return test ; } "
"static void * test ( void * p ) { return p ; } "
"} ;";
ASSERT_EQUALS(expected, tok(code, false));
}
}
void simplifyTypedef23() {
const char code[] = "typedef bool (*Callback) (int i);\n"
"void addCallback(Callback callback) { }\n"
"void addCallback1(Callback callback, int j) { }";
const char expected[] =
"void addCallback ( bool ( * callback ) ( ) ) { } "
"void addCallback1 ( bool ( * callback ) ( ) , int j ) { }";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef24() {
{
const char code[] = "typedef int (*fp)();\n"
"void g( fp f )\n"
"{\n"
" fp f2 = (fp)f;\n"
"}";
const char expected[] =
"void g ( int ( * f ) ( ) ) "
"{ "
"int ( * f2 ) ( ) ; f2 = ( int * ) f ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "typedef int (*fp)();\n"
"void g( fp f )\n"
"{\n"
" fp f2 = static_cast<fp>(f);\n"
"}";
const char expected[] =
"void g ( int ( * f ) ( ) ) "
"{ "
"int ( * f2 ) ( ) ; f2 = static_cast < int * > ( f ) ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
}
}
void simplifyTypedef25() {
{
// ticket #1298
const char code[] = "typedef void (*fill_names_f) (const char *);\n"
"struct vfs_class {\n"
" void (*fill_names) (struct vfs_class *me, fill_names_f);\n"
"}";
const char expected[] =
"struct vfs_class { "
"void ( * fill_names ) ( struct vfs_class * me , void ( * ) ( const char * ) ) ; "
"}";
ASSERT_EQUALS(expected, simplifyTypedef(code));
}
{
const char code[] = "typedef void (*fill_names_f) (const char *);\n"
"struct vfs_class {\n"
" void (*fill_names) (fill_names_f, struct vfs_class *me);\n"
"}";
const char expected[] =
"struct vfs_class { "
"void ( * fill_names ) ( void ( * ) ( const char * ) , struct vfs_class * me ) ; "
"}";
ASSERT_EQUALS(expected, simplifyTypedef(code));
}
}
void simplifyTypedef26() {
{
const char code[] = "typedef void (*Callback) ();\n"
"void addCallback(Callback (*callback)());";
const char expected[] = "void addCallback ( void ( * ( * callback ) ( ) ) ( ) ) ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
// ticket # 1307
const char code[] = "typedef void (*pc_video_update_proc)(bitmap_t *bitmap,\n"
"struct mscrtc6845 *crtc);\n"
"\n"
"struct mscrtc6845 *pc_video_start(pc_video_update_proc (*choosevideomode)(running_machine *machine, int *width, int *height, struct mscrtc6845 *crtc));";
const char expected[] = "struct mscrtc6845 * pc_video_start ( void ( * ( * choosevideomode ) ( running_machine * machine , int * width , int * height , struct mscrtc6845 * crtc ) ) ( bitmap_t * bitmap , struct mscrtc6845 * crtc ) ) ;";
ASSERT_EQUALS(expected, tok(code, false));
}
}
void simplifyTypedef27() {
// ticket #1316
const char code[] = "int main()\n"
"{\n"
" typedef int (*func_ptr)(float, double);\n"
" VERIFY((is_same<result_of<func_ptr(char, float)>::type, int>::value));\n"
"}";
const char expected[] =
"int main ( ) "
"{ "
""
"VERIFY ( is_same < result_of < int ( * ( char , float ) ) ( float , double ) > :: type , int > :: value ) ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef28() {
const char code[] = "typedef std::pair<double, double> (*F)(double);\n"
"F f;";
const char expected[] = "std :: pair < double , double > ( * f ) ( double ) ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef29() {
const char code[] = "typedef int array [ice_or<is_int<int>::value, is_int<UDT>::value>::value ? 1 : -1];\n"
"typedef int array1 [N];\n"
"typedef int array2 [N][M];\n"
"typedef int int_t, int_array[N];\n"
"array a;\n"
"array1 a1;\n"
"array2 a2;\n"
"int_t t;\n"
"int_array ia;";
const char expected[] =
"int a [ ice_or < is_int < int > :: value , is_int < UDT > :: value > :: value ? 1 : -1 ] ; "
"int a1 [ N ] ; "
"int a2 [ N ] [ M ] ; "
"int t ; "
"int ia [ N ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef30() {
const char code[] = "typedef ::std::list<int> int_list;\n"
"typedef ::std::list<int>::iterator int_list_iterator;\n"
"typedef ::std::list<int> int_list_array[10];\n"
"int_list il;\n"
"int_list_iterator ili;\n"
"int_list_array ila;";
const char expected[] =
":: std :: list < int > il ; "
":: std :: list < int > :: iterator ili ; "
":: std :: list < int > ila [ 10 ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef31() {
{
const char code[] = "class A {\n"
"public:\n"
" typedef int INT;\n"
" INT get() const;\n"
" void put(INT x) { a = x; }\n"
" INT a;\n"
"};\n"
"A::INT A::get() const { return a; }\n"
"A::INT i = A::a;";
const char expected[] = "class A { "
"public: "
""
"int get ( ) const ; "
"void put ( int x ) { a = x ; } "
"int a ; "
"} ; "
"int A :: get ( ) const { return a ; } "
"int i ; i = A :: a ;";
ASSERT_EQUALS(expected, tok(code, false));
}
{
const char code[] = "struct A {\n"
" typedef int INT;\n"
" INT get() const;\n"
" void put(INT x) { a = x; }\n"
" INT a;\n"
"};\n"
"A::INT A::get() const { return a; }\n"
"A::INT i = A::a;";
const char expected[] = "struct A { "
""
"int get ( ) const ; "
"void put ( int x ) { a = x ; } "
"int a ; "
"} ; "
"int A :: get ( ) const { return a ; } "
"int i ; i = A :: a ;";
ASSERT_EQUALS(expected, tok(code, false));
}
}
void simplifyTypedef32() {
const char code[] = "typedef char CHAR;\n"
"typedef CHAR * LPSTR;\n"
"typedef const CHAR * LPCSTR;\n"
"CHAR c;\n"
"LPSTR cp;\n"
"LPCSTR ccp;";
const char expected[] =
"char c ; "
"char * cp ; "
"const char * ccp ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef33() {
const char code[] = "class A {\n"
"public:\n"
" typedef char CHAR_A;\n"
" CHAR_A funA();\n"
" class B {\n"