FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppcheck/test/testsimplifytemplate.cpp at master · napcode/cppcheck · GitHub
cppcheck/test/testsimplifytemplate.cpp at master · napcode/cppcheck · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
Accelerator
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
napcode
/
cppcheck
Public
forked from
cppcheck-opensource/cppcheck
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppcheck
/
test
/
testsimplifytemplate.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
5108 lines (4771 loc) · 245 KB
Breadcrumbs
cppcheck
/
test
/
testsimplifytemplate.cpp
Copy path
File metadata and controls
5108 lines (4771 loc) · 245 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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-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
"
config.h
"
#
include
"
platform.h
"
#
include
"
settings.h
"
#
include
"
templatesimplifier.h
"
#
include
"
testsuite.h
"
#
include
"
token.h
"
#
include
"
tokenize.h
"
struct
InternalError
;
class
TestSimplifyTemplate
:
public
TestFixture
{
public:
TestSimplifyTemplate
() : TestFixture(
"
TestSimplifyTemplate
"
) {
}
private:
Settings settings;
void
run
() OVERRIDE {
settings.
addEnabled
(
"
portability
"
);
TEST_CASE
(template1);
TEST_CASE
(template2);
TEST_CASE
(template3);
TEST_CASE
(template4);
TEST_CASE
(template5);
TEST_CASE
(template6);
TEST_CASE
(template7);
TEST_CASE
(template8);
TEST_CASE
(template9);
TEST_CASE
(template10);
TEST_CASE
(template11);
TEST_CASE
(template12);
TEST_CASE
(template13);
TEST_CASE
(template14);
TEST_CASE
(template15);
//
recursive templates
TEST_CASE
(template16);
TEST_CASE
(template17);
TEST_CASE
(template18);
TEST_CASE
(template19);
TEST_CASE
(template20);
TEST_CASE
(template21);
TEST_CASE
(template22);
TEST_CASE
(template23);
TEST_CASE
(template24);
//
#2648 - using sizeof in template parameter
TEST_CASE
(template25);
//
#2648 - another test for sizeof template parameter
TEST_CASE
(template26);
//
#2721 - passing 'char[2]' as template parameter
TEST_CASE
(template27);
//
#3350 - removing unused template in macro call
TEST_CASE
(template28);
TEST_CASE
(template30);
//
#3529 - template < template < ..
TEST_CASE
(template31);
//
#4010 - reference type
TEST_CASE
(template32);
//
#3818 - mismatching template not handled well
TEST_CASE
(template33);
//
#3818,#4544 - inner templates in template instantiation not handled well
TEST_CASE
(template34);
//
#3706 - namespace => hang
TEST_CASE
(template35);
//
#4074 - A<'x'> a;
TEST_CASE
(template36);
//
#4310 - passing unknown template instantiation as template argument
TEST_CASE
(template37);
//
#4544 - A<class B> a;
TEST_CASE
(template38);
//
#4832 - crash on C++11 right angle brackets
TEST_CASE
(template39);
//
#4742 - freeze
TEST_CASE
(template40);
//
#5055 - template specialization outside struct
TEST_CASE
(template41);
//
#4710 - const in instantiation not handled perfectly
TEST_CASE
(template42);
//
#4878 - variadic templates
TEST_CASE
(template43);
//
#5097 - assert due to '>>' not treated as end of template instantiation
TEST_CASE
(template44);
//
#5297 - TemplateSimplifier::simplifyCalculations not eager enough
TEST_CASE
(template45);
//
#5814 - syntax error reported for valid code
TEST_CASE
(template46);
//
#5816 - syntax error reported for valid code
TEST_CASE
(template47);
//
#6023 - syntax error reported for valid code
TEST_CASE
(template48);
//
#6134 - 100% CPU upon invalid code
TEST_CASE
(template49);
//
#6237 - template instantiation
TEST_CASE
(template50);
//
#4272 - simple partial specialization
TEST_CASE
(template51);
//
#6172 - crash upon valid code
TEST_CASE
(template52);
//
#6437 - crash upon valid code
TEST_CASE
(template53);
//
#4335 - bail out for valid code
TEST_CASE
(template54);
//
#6587 - memory corruption upon valid code
TEST_CASE
(template55);
//
#6604 - simplify "const const" to "const" in template instantiations
TEST_CASE
(template56);
//
#7117 - const ternary operator simplification as template parameter
TEST_CASE
(template57);
//
#7891
TEST_CASE
(template58);
//
#6021 - use after free (deleted tokens in simplifyCalculations)
TEST_CASE
(template59);
//
#8051 - TemplateSimplifier::simplifyTemplateInstantiation failure
TEST_CASE
(template60);
//
handling of methods outside template definition
TEST_CASE
(template61);
//
daca2, kodi
TEST_CASE
(template62);
//
#8314 - inner template instantiation
TEST_CASE
(template63);
//
#8576 - qualified type
TEST_CASE
(template64);
//
#8683
TEST_CASE
(template65);
//
#8321
TEST_CASE
(template66);
//
#8725
TEST_CASE
(template67);
//
#8122
TEST_CASE
(template68);
//
union
TEST_CASE
(template69);
//
#8791
TEST_CASE
(template70);
//
#5289
TEST_CASE
(template71);
//
#8821
TEST_CASE
(template72);
TEST_CASE
(template73);
TEST_CASE
(template74);
TEST_CASE
(template75);
TEST_CASE
(template76);
TEST_CASE
(template77);
TEST_CASE
(template78);
TEST_CASE
(template79);
//
#5133
TEST_CASE
(template80);
TEST_CASE
(template81);
TEST_CASE
(template82);
//
#8603
TEST_CASE
(template83);
//
#8867
TEST_CASE
(template84);
//
#8880
TEST_CASE
(template85);
//
#8902 crash
TEST_CASE
(template86);
//
crash
TEST_CASE
(template87);
TEST_CASE
(template88);
//
#6183
TEST_CASE
(template89);
//
#8917
TEST_CASE
(template90);
//
crash
TEST_CASE
(template91);
TEST_CASE
(template92);
TEST_CASE
(template93);
//
crash
TEST_CASE
(template94);
//
#8927 crash
TEST_CASE
(template95);
//
#7417
TEST_CASE
(template96);
//
#7854
TEST_CASE
(template97);
TEST_CASE
(template98);
//
#8959
TEST_CASE
(template99);
//
#8960
TEST_CASE
(template100);
//
#8967
TEST_CASE
(template101);
//
#8968
TEST_CASE
(template102);
//
#9005
TEST_CASE
(template103);
TEST_CASE
(template104);
//
#9021
TEST_CASE
(template105);
//
#9076
TEST_CASE
(template106);
TEST_CASE
(template107);
//
#8663
TEST_CASE
(template108);
//
#9109
TEST_CASE
(template109);
//
#9144
TEST_CASE
(template110);
TEST_CASE
(template111);
//
crash
TEST_CASE
(template112);
//
#9146 syntax error
TEST_CASE
(template113);
TEST_CASE
(template114);
//
#9155
TEST_CASE
(template115);
//
#9153
TEST_CASE
(template116);
//
#9178
TEST_CASE
(template117);
TEST_CASE
(template118);
TEST_CASE
(template119);
//
#9186
TEST_CASE
(template120);
TEST_CASE
(template121);
//
#9193
TEST_CASE
(template122);
//
#9147
TEST_CASE
(template123);
//
#9183
TEST_CASE
(template124);
//
#9197
TEST_CASE
(template125);
TEST_CASE
(template126);
//
#9217
TEST_CASE
(template127);
//
#9225
TEST_CASE
(template128);
//
#9224
TEST_CASE
(template129);
TEST_CASE
(template130);
//
#9246
TEST_CASE
(template131);
//
#9249
TEST_CASE
(template132);
//
#9250
TEST_CASE
(template133);
TEST_CASE
(template134);
TEST_CASE
(template135);
TEST_CASE
(template136);
//
#9287
TEST_CASE
(template137);
//
#9288
TEST_CASE
(template138);
TEST_CASE
(template139);
TEST_CASE
(template140);
TEST_CASE
(template141);
//
#9337
TEST_CASE
(template142);
//
#9338
TEST_CASE
(template143);
TEST_CASE
(template144);
//
#9046
TEST_CASE
(template145);
//
syntax error
TEST_CASE
(template146);
//
syntax error
TEST_CASE
(template147);
//
syntax error
TEST_CASE
(template148);
//
syntax error
TEST_CASE
(template149);
//
unknown macro
TEST_CASE
(template150);
//
syntax error
TEST_CASE
(template151);
//
crash
TEST_CASE
(template152);
//
#9467
TEST_CASE
(template153);
//
#9483
TEST_CASE
(template154);
//
#9495
TEST_CASE
(template155);
//
#9539
TEST_CASE
(template_specialization_1);
//
#7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE
(template_specialization_2);
//
#7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE
(template_enum);
//
#6299 Syntax error in complex enum declaration (including template)
TEST_CASE
(template_unhandled);
TEST_CASE
(template_default_parameter);
TEST_CASE
(template_forward_declared_default_parameter);
TEST_CASE
(template_default_type);
TEST_CASE
(template_typename);
TEST_CASE
(template_constructor);
//
#3152 - template constructor is removed
TEST_CASE
(syntax_error_templates_1);
TEST_CASE
(template_member_ptr);
//
Ticket #5786 - crash upon valid code
TEST_CASE
(template_namespace_1);
TEST_CASE
(template_namespace_2);
TEST_CASE
(template_namespace_3);
TEST_CASE
(template_namespace_4);
TEST_CASE
(template_namespace_5);
TEST_CASE
(template_namespace_6);
TEST_CASE
(template_namespace_7);
//
#8768
TEST_CASE
(template_namespace_8);
TEST_CASE
(template_namespace_9);
TEST_CASE
(template_namespace_10);
TEST_CASE
(template_namespace_11);
//
#7145
//
Test TemplateSimplifier::templateParameters
TEST_CASE
(templateParameters);
TEST_CASE
(templateNamePosition);
TEST_CASE
(findTemplateDeclarationEnd);
TEST_CASE
(expandSpecialized1);
TEST_CASE
(expandSpecialized2);
TEST_CASE
(expandSpecialized3);
//
#8671
TEST_CASE
(expandSpecialized4);
TEST_CASE
(templateAlias1);
TEST_CASE
(templateAlias2);
TEST_CASE
(templateAlias3);
//
#8315
TEST_CASE
(templateAlias4);
//
#9070
TEST_CASE
(templateAlias5);
//
Test TemplateSimplifier::instantiateMatch
TEST_CASE
(instantiateMatch);
TEST_CASE
(templateParameterWithoutName);
//
#8602 Template default parameter without name yields syntax error
TEST_CASE
(templateTypeDeduction1);
//
#8962
TEST_CASE
(templateTypeDeduction2);
TEST_CASE
(simplifyTemplateArgs1);
TEST_CASE
(simplifyTemplateArgs2);
TEST_CASE
(template_variadic_1);
//
#9144
TEST_CASE
(template_variable_1);
TEST_CASE
(template_variable_2);
TEST_CASE
(template_variable_3);
TEST_CASE
(template_variable_4);
TEST_CASE
(simplifyDecltype);
}
std::string
tok
(
const
char
code[],
bool
debugwarnings =
false
, Settings::PlatformType type = Settings::Native) {
errout.
str
(
"
"
);
settings.
debugwarnings
= debugwarnings;
settings.
platform
(type);
Tokenizer
tokenizer
(&settings,
this
);
std::istringstream
istr
(code);
tokenizer.
tokenize
(istr,
"
test.cpp
"
);
return
tokenizer.
tokens
()->
stringifyList
(
nullptr
,
true
);
}
void
template1
() {
const
char
code[] =
"
template <class T> T f(T val) { T a; }
\n
"
"
f<int>(10);
"
;
const
char
expected[] =
"
int f<int> ( int val ) ;
"
"
f<int> ( 10 ) ;
"
"
int f<int> ( int val ) { int a ; }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template2
() {
const
char
code[] =
"
template <class T> class Fred { T a; };
\n
"
"
Fred<int> fred;
"
;
const
char
expected[] =
"
class Fred<int> ;
"
"
Fred<int> fred ;
"
"
class Fred<int> { int a ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template3
() {
const
char
code[] =
"
template <class T, int sz> class Fred { T data[sz]; };
\n
"
"
Fred<float,4> fred;
"
;
const
char
expected[] =
"
class Fred<float,4> ;
"
"
Fred<float,4> fred ;
"
"
class Fred<float,4> { float data [ 4 ] ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template4
() {
const
char
code[] =
"
template <class T> class Fred { Fred(); };
\n
"
"
Fred<float> fred;
"
;
const
char
expected[] =
"
class Fred<float> ;
"
"
Fred<float> fred ;
"
"
class Fred<float> { Fred<float> ( ) ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template5
() {
const
char
code[] =
"
template <class T> class Fred { };
\n
"
"
template <class T> Fred<T>::Fred() { }
\n
"
"
Fred<float> fred;
"
;
const
char
expected[] =
"
class Fred<float> ;
"
"
Fred<float> fred ;
"
"
class Fred<float> { } ;
"
"
Fred<float> :: Fred<float> ( ) { }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template6
() {
const
char
code[] =
"
template <class T> class Fred { };
\n
"
"
Fred<float> fred1;
\n
"
"
Fred<float> fred2;
"
;
const
char
expected[] =
"
class Fred<float> ;
"
"
Fred<float> fred1 ;
"
"
Fred<float> fred2 ;
"
"
class Fred<float> { } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template7
() {
//
A template class that is not used => no simplification
{
const
char
code[] =
"
template <class T>
\n
"
"
class ABC
\n
"
"
{
\n
"
"
public:
\n
"
"
typedef ABC<T> m;
\n
"
"
};
\n
"
;
const
char
expected[] =
"
template < class T > class ABC { public: } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
{
const
char
code[] =
"
template <typename T> class ABC {
\n
"
"
public:
\n
"
"
typedef std::vector<T> type;
\n
"
"
};
\n
"
"
int main() {
\n
"
"
ABC<int>::type v;
\n
"
"
v.push_back(4);
\n
"
"
return 0;
\n
"
"
}
\n
"
;
const
char
wanted[] =
"
class ABC<int> ;
"
"
int main ( ) {
"
"
std :: vector < int > v ;
"
"
v . push_back ( 4 ) ;
"
"
return 0 ;
"
"
}
"
"
class ABC<int> { public: } ;
"
;
const
char
current[] =
"
class ABC<int> ;
"
"
int main ( ) {
"
"
ABC<int> :: type v ;
"
"
v . push_back ( 4 ) ;
"
"
return 0 ;
"
"
}
"
"
class ABC<int> { public: } ;
"
;
TODO_ASSERT_EQUALS
(wanted, current,
tok
(code));
}
{
const
char
code[] =
"
template <typename T> class ABC {
\n
"
"
public:
\n
"
"
typedef std::vector<T> type;
\n
"
"
void f()
\n
"
"
{
\n
"
"
ABC<int>::type v;
\n
"
"
v.push_back(4);
\n
"
"
}
\n
"
"
};
\n
"
;
const
char
expected[] =
"
template < typename T > class ABC {
"
"
public: void f ( ) {
"
"
ABC < int > :: type v ;
"
"
v . push_back ( 4 ) ;
"
"
}
"
"
} ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
}
//
Template definitions but no usage => no expansion
void
template8
() {
const
char
code[] =
"
template<typename T> class A;
\n
"
"
template<typename T> class B;
\n
"
"
\n
"
"
typedef A<int> x;
\n
"
"
typedef B<int> y;
\n
"
"
\n
"
"
template<typename T> class A {
\n
"
"
void f() {
\n
"
"
B<T> a = B<T>::g();
\n
"
"
T b = 0;
\n
"
"
if (b)
\n
"
"
b = 0;
\n
"
"
}
\n
"
"
};
\n
"
"
\n
"
"
template<typename T> inline B<T> h() { return B<T>(); }
\n
"
;
ASSERT_EQUALS
(
"
template < typename T > class A ;
"
"
template < typename T > class B ;
"
"
template < typename T > class A { void f ( ) { B < T > a ; a = B < T > :: g ( ) ; T b ; b = 0 ; if ( b ) { b = 0 ; } } } ;
"
"
template < typename T > B < T > h ( ) { return B < T > ( ) ; }
"
,
tok
(code));
ASSERT_EQUALS
(
"
class A { template < typename T > int foo ( T d ) ; } ;
"
,
tok
(
"
class A{ template<typename T> int foo(T d);};
"
));
}
void
template9
() {
const
char
code[] =
"
template < typename T > class A { } ;
\n
"
"
\n
"
"
void f ( ) {
\n
"
"
A < int > a ;
\n
"
"
}
\n
"
"
\n
"
"
template < typename T >
\n
"
"
class B {
\n
"
"
void g ( ) {
\n
"
"
A < T > b = A < T > :: h ( ) ;
\n
"
"
}
\n
"
"
} ;
\n
"
;
//
The expected result..
const
char
expected[] =
"
class A<int> ;
"
"
void f ( ) { A<int> a ; } ;
"
"
class A<int> { } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template10
() {
const
char
code[] =
"
template <int ui, typename T> T * foo()
\n
"
"
{ return new T[ui]; }
\n
"
"
\n
"
"
void f ( )
\n
"
"
{
\n
"
"
foo<3,int>();
\n
"
"
}
\n
"
;
//
The expected result..
const
char
expected[] =
"
int * foo<3,int> ( ) ;
"
"
void f ( )
"
"
{
"
"
foo<3,int> ( ) ;
"
"
}
"
"
int * foo<3,int> ( ) { return new int [ 3 ] ; }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template11
() {
const
char
code[] =
"
template <int ui, typename T> T * foo()
\n
"
"
{ return new T[ui]; }
\n
"
"
\n
"
"
void f ( )
\n
"
"
{
\n
"
"
char * p = foo<3,char>();
\n
"
"
}
\n
"
;
//
The expected result..
const
char
expected[] =
"
char * foo<3,char> ( ) ;
"
"
void f ( )
"
"
{
"
"
char * p ; p = foo<3,char> ( ) ;
"
"
}
"
"
char * foo<3,char> ( ) { return new char [ 3 ] ; }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template12
() {
const
char
code[] =
"
template <int x, int y, int z>
\n
"
"
class A : public B<x, y, (x - y) ? ((y < z) ? 1 : -1) : 0>
\n
"
"
{ };
\n
"
"
\n
"
"
void f()
\n
"
"
{
\n
"
"
A<12,12,11> a;
\n
"
"
}
\n
"
;
const
char
expected[] =
"
class A<12,12,11> ;
"
"
void f ( )
"
"
{
"
"
A<12,12,11> a ;
"
"
}
"
"
class A<12,12,11> : public B < 12 , 12 , 0 >
"
"
{ } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template13
() {
const
char
code[] =
"
class BB {};
\n
"
"
\n
"
"
template <class T>
\n
"
"
class AA {
\n
"
"
public:
\n
"
"
static AA<T> create(T* newObject);
\n
"
"
static int size();
\n
"
"
};
\n
"
"
\n
"
"
class CC { public: CC(AA<BB>, int) {} };
\n
"
"
\n
"
"
class XX {
\n
"
"
AA<CC> y;
\n
"
"
public:
\n
"
"
XX();
\n
"
"
};
\n
"
"
\n
"
"
XX::XX():
\n
"
"
y(AA<CC>::create(new CC(AA<BB>(), 0)))
\n
"
"
{}
\n
"
"
\n
"
"
int yy[AA<CC>::size()];
"
;
const
char
expected[] =
"
class BB { } ;
"
"
class AA<BB> ;
"
"
class AA<CC> ;
"
"
class CC { public: CC ( AA<BB> , int ) { } } ;
"
"
class XX {
"
"
AA<CC> y ;
"
"
public:
"
"
XX ( ) ;
"
"
} ;
"
"
XX :: XX ( ) :
"
"
y ( AA<CC> :: create ( new CC ( AA<BB> ( ) , 0 ) ) )
"
"
{ }
"
"
int yy [ AA<CC> :: size ( ) ] ;
"
"
class AA<BB> {
"
"
public:
"
"
static AA<BB> create ( BB * newObject ) ;
"
"
static int size ( ) ;
"
"
} ;
"
"
class AA<CC> {
"
"
public:
"
"
static AA<CC> create ( CC * newObject ) ;
"
"
static int size ( ) ;
"
"
} ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template14
() {
const
char
code[] =
"
template <> void foo<int *>()
\n
"
"
{ x(); }
\n
"
"
\n
"
"
int main()
\n
"
"
{
\n
"
"
foo<int*>();
\n
"
"
}
\n
"
;
const
char
expected[] =
"
void foo<int*> ( ) ;
"
"
void foo<int*> ( )
"
"
{ x ( ) ; }
"
"
int main ( )
"
"
{ foo<int*> ( ) ; }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template15
() {
//
recursive templates #3130 etc
const
char
code[] =
"
template <unsigned int i> void a()
\n
"
"
{
\n
"
"
a<i-1>();
\n
"
"
}
\n
"
"
\n
"
"
template <> void a<0>()
\n
"
"
{ }
\n
"
"
\n
"
"
int main()
\n
"
"
{
\n
"
"
a<2>();
\n
"
"
return 0;
\n
"
"
}
\n
"
;
//
The expected result..
const
char
expected[] =
"
void a<0> ( ) ;
"
"
void a<2> ( ) ;
"
"
void a<1> ( ) ;
"
"
void a<0> ( ) { }
"
"
int main ( )
"
"
{ a<2> ( ) ; return 0 ; }
"
"
void a<2> ( ) { a<1> ( ) ; }
"
"
void a<1> ( ) { a<0> ( ) ; }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
//
#3130
const
char
code2[] =
"
template <int n> struct vec {
\n
"
"
vec() {}
\n
"
"
vec(const vec<n-1>& v) {}
\n
"
//
<- never used don't instantiate
"
};
\n
"
"
\n
"
"
vec<4> v;
"
;
const
char
expected2[] =
"
struct vec<4> ;
"
"
vec<4> v ;
"
"
struct vec<4> {
"
"
vec<4> ( ) { }
"
"
vec<4> ( const vec < 4 - 1 > & v ) { }
"
"
} ;
"
;
ASSERT_EQUALS
(expected2,
tok
(code2));
}
void
template16
() {
const
char
code[] =
"
template <unsigned int i> void a()
\n
"
"
{ }
\n
"
"
\n
"
"
template <unsigned int i> void b()
\n
"
"
{ a<i>(); }
\n
"
"
\n
"
"
int main()
\n
"
"
{
\n
"
"
b<2>();
\n
"
"
return 0;
\n
"
"
}
\n
"
;
const
char
expected[] =
"
void a<2> ( ) ;
"
"
void b<2> ( ) ;
"
"
int main ( ) { b<2> ( ) ; return 0 ; }
"
"
void b<2> ( ) { a<2> ( ) ; }
"
"
void a<2> ( ) { }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template17
() {
const
char
code[] =
"
template<class T>
\n
"
"
class Fred
\n
"
"
{
\n
"
"
template<class T>
\n
"
"
static shared_ptr< Fred<T> > CreateFred()
\n
"
"
{
\n
"
"
}
\n
"
"
};
\n
"
"
\n
"
"
shared_ptr<int> i;
\n
"
;
const
char
expected[] =
"
template < class T >
"
"
class Fred
"
"
{
"
"
template < class T >
"
"
static shared_ptr < Fred < T > > CreateFred ( )
"
"
{
"
"
}
"
"
} ;
"
"
shared_ptr < int > i ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template18
() {
const
char
code[] =
"
template <class T> class foo { T a; };
\n
"
"
foo<int> *f;
"
;
const
char
expected[] =
"
class foo<int> ;
"
"
foo<int> * f ;
"
"
class foo<int> { int a ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template19
() {
const
char
code[] =
"
template <typename T> T & foo()
\n
"
"
{ static T temp; return temp; }
\n
"
"
\n
"
"
void f ( )
\n
"
"
{
\n
"
"
char p = foo<char>();
\n
"
"
}
\n
"
;
//
The expected result..
const
char
expected[] =
"
char & foo<char> ( ) ;
"
"
void f ( )
"
"
{
"
"
char p ; p = foo<char> ( ) ;
"
"
}
"
"
char & foo<char> ( ) { static char temp ; return temp ; }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template20
() {
//
Ticket #1788 - the destructor implementation is lost
const
char
code[] =
"
template <class T> class A { public: ~A(); };
\n
"
"
template <class T> A<T>::~A() {}
\n
"
"
A<int> a;
\n
"
;
//
The expected result..
const
char
expected[] =
"
class A<int> ;
"
"
A<int> a ;
"
"
class A<int> { public: ~ A<int> ( ) ; } ;
"
"
A<int> :: ~ A<int> ( ) { }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template21
() {
{
const
char
code[] =
"
template <class T> struct Fred { T a; };
\n
"
"
Fred<int> fred;
"
;
const
char
expected[] =
"
struct Fred<int> ;
"
"
Fred<int> fred ;
"
"
struct Fred<int> { int a ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
{
const
char
code[] =
"
template <class T, int sz> struct Fred { T data[sz]; };
\n
"
"
Fred<float,4> fred;
"
;
const
char
expected[] =
"
struct Fred<float,4> ;
"
"
Fred<float,4> fred ;
"
"
struct Fred<float,4> { float data [ 4 ] ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
{
const
char
code[] =
"
template <class T> struct Fred { Fred(); };
\n
"
"
Fred<float> fred;
"
;
const
char
expected[] =
"
struct Fred<float> ;
"
"
Fred<float> fred ;
"
"
struct Fred<float> { Fred<float> ( ) ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
{
const
char
code[] =
"
template <class T> struct Fred { };
\n
"
"
Fred<float> fred1;
\n
"
"
Fred<float> fred2;
"
;
const
char
expected[] =
"
struct Fred<float> ;
"
"
Fred<float> fred1 ;
"
"
Fred<float> fred2 ;
"
"
struct Fred<float> { } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
}
void
template22
() {
const
char
code[] =
"
template <class T> struct Fred { T a; };
\n
"
"
Fred<std::string> fred;
"
;
const
char
expected[] =
"
struct Fred<std::string> ;
"
"
Fred<std::string> fred ;
"
"
struct Fred<std::string> { std :: string a ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template23
() {
const
char
code[] =
"
template <class T> void foo() { }
\n
"
"
void bar() {
\n
"
"
std::cout << (foo<double>());
\n
"
"
}
"
;
const
char
expected[] =
"
void foo<double> ( ) ;
"
"
void bar ( ) {
"
"
std :: cout << ( foo<double> ( ) ) ;
"
"
}
"
"
void foo<double> ( ) { }
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template24
() {
//
#2648
const
char
code[] =
"
template<int n> struct B
\n
"
"
{
\n
"
"
int a[n];
\n
"
"
};
\n
"
"
\n
"
"
template<int x> class bitset: B<sizeof(int)>
\n
"
"
{};
\n
"
"
\n
"
"
bitset<1> z;
"
;
const
char
expected[] =
"
struct B<4> ;
"
"
class bitset<1> ;
"
"
bitset<1> z ;
"
"
class bitset<1> : B<4> { } ;
"
"
struct B<4> { int a [ 4 ] ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template25
() {
const
char
code[] =
"
template<int n> struct B
\n
"
"
{
\n
"
"
int a[n];
\n
"
"
};
\n
"
"
\n
"
"
template<int x> class bitset: B<((sizeof(int)) ? : 1)>
\n
"
"
{};
\n
"
"
\n
"
"
bitset<1> z;
"
;
const
char
expected[] =
"
struct B<4> ;
"
"
class bitset<1> ;
"
"
bitset<1> z ;
"
"
class bitset<1> : B<4> { } ;
"
"
struct B<4> { int a [ 4 ] ; } ;
"
;
ASSERT_EQUALS
(expected,
tok
(code));
}
void
template26
() {
//
#2721
const
char
code[] =
"
template<class T>
\n
"
"
class A { public: T x; };
\n
"
"
\n
"
"
template<class M>
\n
"
"
class C: public A<char[M]> {};
\n
"
"
\n
"
"
C<2> a;
\n
"
;
//
TODO: expand A also
ASSERT_EQUALS
(
"
template < class T > class A { public: T x ; } ; class C<2> ; C<2> a ; class C<2> : public A < char [ 2 ] > { } ;
"
,
tok
(code));
}
void
template27
() {
//
#3350 - template inside macro call
const
char
code[] =
"
X(template<class T> class Fred);
"
;
ASSERT_THROW
(
tok
(code), InternalError);
}
void
template28
() {
//
#3226 - inner template
const
char
code[] =
"
template<class A, class B> class Fred {};
\n
"
"
Fred<int,Fred<int,int> > x;
\n
"
;
ASSERT_EQUALS
(
"
class Fred<int,int> ;
"
"
class Fred<int,Fred<int,int>> ;
"
"
Fred<int,Fred<int,int>> x ;
"
"
class Fred<int,int> { } ;
"
"
class Fred<int,Fred<int,int>> { } ;
"
,
tok
(code));
}
void
template30
() {
//
#3529 - template < template < ..
const
char
code[] =
"
template<template<class> class A, class B> void f(){}
"
;
ASSERT_EQUALS
(
"
template < template < class > class A , class B > void f ( ) { }
"
,
tok
(code));
}
void
template31
() {
//
#4010 - template reference type
const
char
code[] =
"
template<class T> struct A{}; A<int&> a;
"
;
ASSERT_EQUALS
(
"
struct A<int&> ;
"
"
A<int&> a ;
"
"
struct A<int&> { } ;
"
,
tok
(code));
//
#7409 - rvalue
const
char
code2[] =
"
template<class T> struct A{}; A<int&&> a;
"
;
ASSERT_EQUALS
(
"
struct A<int&&> ;
"
"
A<int&&> a ;
"
"
struct A<int&&> { } ;
"
,
tok
(code2));
}
void
template32
() {
//
#3818 - mismatching template not handled well
const
char
code[] =
"
template <class T1, class T2, class T3, class T4 > struct A { };
\n
"
"
\n
"
"
template <class T>
\n
"
"
struct B
\n
"
"
{
\n
"
"
public:
\n
"
"
A < int, Pair<T, int>, int > a;
\n
"
//
mismatching parameters => don't instantiate
"
};
\n
"
"
\n
"
"
B<int> b;
\n
"
;
ASSERT_EQUALS
(
"
template < class T1 , class T2 , class T3 , class T4 > struct A { } ;
"
"
struct B<int> ;
"
"
B<int> b ;
"
"
struct B<int> { public: A < int , Pair < int , int > , int > a ; } ;
"
,
tok
(code));
}
void
template33
() {
{
//
#3818 - inner templates in template instantiation not handled well
const
char
code[] =
"
template<class T> struct A { };
\n
"
"
template<class T> struct B { };
\n
"
"
template<class T> struct C { A<B<X<T> > > ab; };
\n
"
"
C<int> c;
"
;
ASSERT_EQUALS
(
"
struct A<B<X<int>>> ;
"
"
struct B<X<int>> ;
"
"
struct C<int> ;
"
"
C<int> c ;
"
"
struct C<int> { A<B<X<int>>> ab ; } ;
"
"
struct B<X<int>> { } ;
"
//
<- redundant.. but nevermind
"
struct A<B<X<int>>> { } ;
"
,
tok
(code));
}
{
//
#4544
const
char
code[] =
"
struct A { };
\n
"
"
template<class T> struct B { };
\n
"
"
template<class T> struct C { };
\n
"
"
C< B<A> > c;
"
;
ASSERT_EQUALS
(
"
struct A { } ;
"
"
template < class T > struct B { } ;
"
//
<- redundant.. but nevermind
"
struct C<B<A>> ;
"
"
C<B<A>> c ;
"
"
struct C<B<A>> { } ;
"
,
tok
(code));
}
}
void
template34
() {
//
#3706 - namespace => hang
const
char
code[] =
"
namespace abc {
\n
"
"
template <typename T> struct X { void f(X<T> &x) {} };
\n
"
"
}
\n
"
"
template <> int X<int>::Y(0);
"
;
tok
(code);
}
void
template35
() {
//
#4074 - "A<'x'> a;" is not recognized as template instantiation
const
char
code[] =
"
template <char c> class A {};
\n
"
"
A <'x'> a;
"
;
ASSERT_EQUALS
(
"
class A<'x'> ;
"
"
A<'x'> a ;
"
"
class A<'x'> { } ;
"
,
tok
(code));
}
void
template36
() {
//
#4310 - Passing unknown template instantiation as template argument
const
char
code[] =
"
template <class T> struct X { T t; };
\n
"
"
template <class C> struct Y { Foo < X< Bar<C> > > _foo; };
\n
"
//
<- Bar is unknown
"
Y<int> bar;
"
;
ASSERT_EQUALS
(
"
struct X<Bar<int>> ;
"
"
struct Y<int> ;
"
"
Y<int> bar ;
"
"
struct Y<int> { Foo < X<Bar<int>> > _foo ; } ;
"
"
struct X<Bar<int>> { Bar < int > t ; } ;
"
,
tok
(code));
}
void
template37
() {
//
#4544 - A<class B> a;
{
const
char
code[] =
"
class A { };
\n
"
"
template<class T> class B {};
\n
"
"
B<class A> b1;
\n
"
"
B<A> b2;
"
;
ASSERT_EQUALS
(
"
class A { } ; class B<A> ; B<A> b1 ; B<A> b2 ; class B<A> { } ;
"
,
tok
(code));
}
{
const
char
code[] =
"
struct A { };
\n
"
"
template<class T> class B {};
\n
"
"
B<struct A> b1;
\n
"
"
B<A> b2;
"
;
ASSERT_EQUALS
(
"
struct A { } ; class B<A> ; B<A> b1 ; B<A> b2 ; class B<A> { } ;
"
,
tok
(code));
}
{
const
char
code[] =
"
enum A { };
\n
"
"
template<class T> class B {};
\n
"
"
B<enum A> b1;
\n
"
"
B<A> b2;
"
;
ASSERT_EQUALS
(
"
enum A { } ; class B<A> ; B<A> b1 ; B<A> b2 ; class B<A> { } ;
"
,
tok
(code));
}
}
void
template_unhandled
() {
//
An unhandled template usage should be simplified..
ASSERT_EQUALS
(
"
x<int> ( ) ;
"
,
tok
(
"
x<int>();
"
));
}
void
template38
() {
//
#4832 - Crash on C++11 right angle brackets
const
char
code[] =
"
template <class T> class A {
\n
"
"
T mT;
\n
"
"
public:
\n
"
"
void foo() {}
\n
"
"
};
\n
"
"
\n
"
"
int main() {
\n
"
"
A<A<BLA>> gna1;
\n
"
"
A<BLA> gna2;
\n
"
"
}
\n
"
;
const
char
expected[] =
"
class A<BLA> ;
"
"
class A<A<BLA>> ;
"
"
int main ( ) {
"
"
A<A<BLA>> gna1 ;
"
"
A<BLA> gna2 ;
"
"
}
"
"
class A<BLA> {
"
"
BLA mT ;
"
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL