File: testsimplifyusing.cpp

package info (click to toggle)
cppcheck 2.17.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 25,384 kB
  • sloc: cpp: 263,341; python: 19,737; ansic: 7,953; sh: 1,018; makefile: 996; xml: 994; cs: 291
file content (1620 lines) | stat: -rw-r--r-- 69,322 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
/*
 * Cppcheck - A tool for static C/C++ code analysis
 * Copyright (C) 2007-2025 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 "fixture.h"
#include "helpers.h"
#include "platform.h"
#include "settings.h"
#include "token.h"
#include "tokenize.h"
#include "tokenlist.h"
#include "utils.h"

#include <cstddef>
#include <sstream>
#include <string>
#include <vector>

class TestSimplifyUsing : public TestFixture {
public:
    TestSimplifyUsing() : TestFixture("TestSimplifyUsing") {}


private:
    const Settings settings0 = settingsBuilder().severity(Severity::style).build();

    void run() override {
        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(simplifyUsing17);
        TEST_CASE(simplifyUsing18);
        TEST_CASE(simplifyUsing19);
        TEST_CASE(simplifyUsing20);
        TEST_CASE(simplifyUsing21);
        TEST_CASE(simplifyUsing22);
        TEST_CASE(simplifyUsing23);
        TEST_CASE(simplifyUsing24);
        TEST_CASE(simplifyUsing25);
        TEST_CASE(simplifyUsing26); // #11090
        TEST_CASE(simplifyUsing27);
        TEST_CASE(simplifyUsing28);
        TEST_CASE(simplifyUsing29);
        TEST_CASE(simplifyUsing30);
        TEST_CASE(simplifyUsing31);
        TEST_CASE(simplifyUsing32);
        TEST_CASE(simplifyUsing33);
        TEST_CASE(simplifyUsing34);
        TEST_CASE(simplifyUsing35);

        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);
        TEST_CASE(simplifyUsing9518);
        TEST_CASE(simplifyUsing9757);
        TEST_CASE(simplifyUsing10008);
        TEST_CASE(simplifyUsing10054);
        TEST_CASE(simplifyUsing10136);
        TEST_CASE(simplifyUsing10171);
        TEST_CASE(simplifyUsing10172);
        TEST_CASE(simplifyUsing10173);
        TEST_CASE(simplifyUsing10335);
        TEST_CASE(simplifyUsing10720);

        TEST_CASE(scopeInfo1);
        TEST_CASE(scopeInfo2);
    }

    struct TokOptions
    {
        TokOptions() = default;
        Platform::Type type = Platform::Type::Native;
        bool debugwarnings = true;
        bool preprocess = false;
    };

#define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__)
    template<size_t size>
    std::string tok_(const char* file, int line, const char (&code)[size], const TokOptions& options = make_default_obj()) {
        const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive).debugwarnings(options.debugwarnings).platform(options.type).build();

        if (options.preprocess) {
            Tokenizer tokenizer(settings, *this);
            std::vector<std::string> files(1, "test.cpp");
            PreprocessorHelper::preprocess(code, files, tokenizer, *this);
            std::istringstream istr(code);
            ASSERT_LOC(tokenizer.list.createTokens(istr, "test.cpp"), file, line); // TODO: this creates the tokens a second time
            ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
            return tokenizer.tokens()->stringifyList(nullptr);
        }

        SimpleTokenizer tokenizer(settings, *this);
        ASSERT_LOC(tokenizer.tokenize(code), file, line);

        return tokenizer.tokens()->stringifyList(nullptr);
    }

    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));
    }

    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));
    }

    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 ( ) "
            "{ "
            "yy_buffer_state * state ; "
            "}";

        ASSERT_EQUALS(expected, tok(code));
    }

    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));
    }

    void simplifyUsing7() {
        const char code[] = "using abc = int; "
                            "Fred :: abc f ;";
        const char expected[] = "Fred :: abc f ;";
        ASSERT_EQUALS(expected, tok(code));
    }

    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));
    }

    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 ; } ; "
            "s s ; "
            "s * ps ; "
            "t t ; "
            "t * tp ; "
            "U u ; "
            "U * v ; "
            "Unnamed0 * w ;";

        ASSERT_EQUALS(expected, tok(code));
    }

    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 ; } ; "
            "s s ; "
            "s * ps ; "
            "t t ; "
            "t * tp ; "
            "U u ; "
            "U * v ; "
            "Unnamed0 * w ;";

        ASSERT_EQUALS(expected, tok(code));
    }

    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 } ; "
                                "abc e1 ; "
                                "xyz e2 ;";

        ASSERT_EQUALS(expected, tok(code));
    }

    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));
    }

    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));
        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);"
                            "};";

        TODO_ASSERT_THROW(tok(code, dinit(TokOptions, $.debugwarnings = false)), InternalError); // TODO: Do not throw AST validation exception
        //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));
        }

        {
            const char code[] = "using frame = unsigned char [10];\n"
                                "frame f;";

            const char expected[] = "unsigned char f [ 10 ] ;";

            ASSERT_EQUALS(expected, tok(code));
        }
    }

    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 ] ; "
                                "} ; "
                                "STRFOO s ;";

        ASSERT_EQUALS(expected, tok(code));
        ASSERT_EQUALS("", errout_str());
    }

    void simplifyUsing17() {
        const char code[] = "class C1 {};\n"
                            "using S1 = class S1 {};\n"
                            "using S2 = class S2 : public C1 {};\n"
                            "using S3 = class {};\n"
                            "using S4 = class : public C1 {};\n"
                            "S1 s1;\n"
                            "S2 s2;\n"
                            "S3 s3;\n"
                            "S4 s4;";

        const char expected[] = "class C1 { } ; "
                                "class S1 { } ; "
                                "class S2 : public C1 { } ; "
                                "class S3 { } ; "
                                "class S4 : public C1 { } ; "
                                "S1 s1 ; "
                                "S2 s2 ; "
                                "S3 s3 ; "
                                "S4 s4 ;";

        ASSERT_EQUALS(expected, tok(code));
    }

    void simplifyUsing18() {
        const char code[] = "{ { { using a = a; using a; } } }";
        (void)tok(code); // don't crash
    }

    void simplifyUsing19() {
        const char code[] = "namespace a {\n"
                            "using b = int;\n"
                            "void foo::c() { }\n"
                            "void foo::d() { }\n"
                            "void foo::e() {\n"
                            "   using b = float;\n"
                            "}\n"
                            "}";
        (void)tok(code); // don't hang
        ignore_errout(); // we are not interested in the output
    }

    void simplifyUsing20() {
        const char code[] = "namespace a {\n"
                            "namespace b {\n"
                            "namespace c {\n"
                            "namespace d {\n"
                            "namespace e {\n"
                            "namespace f {\n"
                            "namespace g {\n"
                            "using Changeset = ::IAdaptionCallback::Changeset;\n"
                            "using EProperty = searches::EProperty;\n"
                            "using ChangesetValueType = Changeset::value_type;\n"
                            "namespace {\n"
                            "   template <class T>\n"
                            "   auto modify(std::shared_ptr<T>& p) -> boost::optional<decltype(p->modify())> {\n"
                            "      return nullptr;\n"
                            "   }\n"
                            "   template <class T>\n"
                            "   std::list<T> getValidElements() {\n"
                            "      return nullptr;\n"
                            "   }\n"
                            "}\n"
                            "std::shared_ptr<ResourceConfiguration>\n"
                            "foo::getConfiguration() {\n"
                            "   return nullptr;\n"
                            "}\n"
                            "void\n"
                            "foo::doRegister(const Input & Input) {\n"
                            "   UNUSED( Input );\n"
                            "}\n"
                            "foo::MicroServiceReturnValue\n"
                            "foo::post(SearchesPtr element, const Changeset& changeset)\n"
                            "{\n"
                            "   using EProperty = ab::ep;\n"
                            "   static std::map<EProperty, std::pair<ElementPropertyHandler, CheckComponentState>> updateHandlers =\n"
                            "   {\n"
                            "      {EProperty::Needle, {&RSISearchesResource::updateNeedle,         &RSISearchesResource::isSearcherReady}},\n"
                            "      {EProperty::SortBy, {&RSISearchesResource::updateResultsSorting, &RSISearchesResource::isSearcherReady}}\n"
                            "   };\n"
                            "   return nullptr;\n"
                            "}\n"
                            "}}}}}}}";
        (void)tok(code); // don't hang
        ignore_errout(); // we do not care about the output
    }

    void simplifyUsing21() {
        const char code[] = "using a = b;\n"
                            "enum {}";
        (void)tok(code); // don't crash
    }

    void simplifyUsing22() {
        const char code[] = "namespace aa { namespace bb { namespace cc { namespace dd { namespace ee {\n"
                            "class fff {\n"
                            "public:\n"
                            "    using icmsp   = std::shared_ptr<aa::bb::ee::cm::api::icm>;\n"
                            "private:\n"
                            "    using Connection = boost::signals2::connection;\n"
                            "    using ESdk = sdk::common::api::Sdk::ESdk;\n"
                            "    using co = aa::bb::ee::com::api::com2;\n"
                            "};\n"
                            "fff::fff() : m_icm(icm) {\n"
                            "    using ESdk = aa::bb::sdk::common::api::Sdk::ESdk;\n"
                            "}\n"
                            "}}}}}";
        const char expected[] = "namespace aa { namespace bb { namespace cc { namespace dd { namespace ee { "
                                "class fff { "
                                "public: "
                                "private: "
                                "} ; "
                                "fff :: fff ( ) : m_icm ( icm ) { "
                                "} "
                                "} } } } }";
        ASSERT_EQUALS(expected, tok(code)); // don't hang
        ignore_errout(); // we do not care about the output
    }

    void simplifyUsing23() {
        const char code[] = "class cmcch {\n"
                            "public:\n"
                            "   cmcch(icmsp const& icm, Rtnf&& rtnf = {});\n"
                            "private:\n"
                            "    using escs = aa::bb::cc::dd::ee;\n"
                            "private:\n"
                            "   icmsp m_icm;\n"
                            "   mutable std::atomic<rt> m_rt;\n"
                            "};\n"
                            "cmcch::cmcch(cmcch::icmsp const& icm, Rtnf&& rtnf)\n"
                            "   : m_icm(icm)\n"
                            "   , m_rt{rt::UNKNOWN_} {\n"
                            "  using escs = yy::zz::aa::bb::cc::dd::ee;\n"
                            "}";
        const char expected[] = "class cmcch { "
                                "public: "
                                "cmcch ( const icmsp & icm , Rtnf && rtnf = { } ) ; "
                                "private: "
                                "private: "
                                "icmsp m_icm ; "
                                "mutable std :: atomic < rt > m_rt ; "
                                "} ; "
                                "cmcch :: cmcch ( const cmcch :: icmsp & icm , Rtnf && rtnf ) "
                                ": m_icm ( icm ) "
                                ", m_rt { rt :: UNKNOWN_ } { "
                                "}";
        ASSERT_EQUALS(expected, tok(code)); // don't hang
    }

    void simplifyUsing24() {
        const char code[] = "using value_type = const ValueFlow::Value;\n"
                            "value_type vt;";
        const char expected[] = "const ValueFlow :: Value vt ;";
        ASSERT_EQUALS(expected, tok(code));
    }

    void simplifyUsing25() {
        const char code[] = "struct UnusualType {\n"
                            "  using T = vtkm::Id;\n"
                            "  T X;\n"
                            "};\n"
                            "namespace vtkm {\n"
                            "template <>\n"
                            "struct VecTraits<UnusualType> : VecTraits<UnusualType::T> { };\n"
                            "}";
        const char expected[] = "struct UnusualType { "
                                "vtkm :: Id X ; "
                                "} ; "
                                "namespace vtkm { "
                                "struct VecTraits<UnusualType> : VecTraits < vtkm :: Id > { } ; "
                                "}";
        ASSERT_EQUALS(expected, tok(code));
    }

    void simplifyUsing26() { // #11090
        const char code[] = "namespace M {\n"
                            "    struct A;\n"
                            "    struct B;\n"
                            "    struct C;\n"
                            "    template<typename T>\n"
                            "    struct F {};\n"
                            "    template<>\n"
                            "    struct F<B> : F<A> {};\n"
                            "    template<>\n"
                            "    struct F<C> : F<A> {};\n"
                            "}\n"
                            "namespace N {\n"
                            "    using namespace M;\n"
                            "    using A = void;\n"
                            "}\n";
        const char expected[] = "namespace M { "
                                "struct A ; struct B ; struct C ; "
                                "struct F<C> ; struct F<B> ; struct F<A> ; "
                                "struct F<B> : F<A> { } ; struct F<C> : F<A> { } ; "
                                "} "
                                "namespace N { "
                                "using namespace M ; "
                                "} "
                                "struct M :: F<A> { } ;";
        ASSERT_EQUALS(expected, tok(code));
    }

    void simplifyUsing27() { // #11670
        const char code[] = "namespace N {\n"
                            "    template <class T>\n"
                            "    struct S {\n"
                            "        using iterator = T*;\n"
                            "        iterator begin();\n"
                            "    };\n"
                            "}\n"
                            "using I = N::S<int>;\n"
                            "void f() {\n"
                            "    I::iterator iter;\n"
                            "}\n";
        const char expected[] = "namespace N { struct S<int> ; } "
                                "void f ( ) { int * iter ; } "
                                "struct N :: S<int> { int * begin ( ) ; } ;";
        ASSERT_EQUALS(expected, tok(code));
    }

    void simplifyUsing28() { // #11795
        const char code[] = "void f() {\n"
                            "    using T = int;\n"
                            "    T* p{ new T };\n"
                            "}\n";
        const char expected[] = "void f ( ) { int * p { new int } ; }";
        ASSERT_EQUALS(expected, tok(code));
        ASSERT_EQUALS("", errout_str());
    }

    void simplifyUsing29() { // #11981
        const char code[] = "using T = int*;\n"
                            "void f(T = T()) {}\n";
        const char expected[] = "void f ( int * = ( int * ) 0 ) { }";
        ASSERT_EQUALS(expected, tok(code));
        ASSERT_EQUALS("", errout_str());
    }

    void simplifyUsing30() {
        {
            const char code[] = "using std::to_string;\n" // #8454
                                "void f() {\n"
                                "    std::string str = to_string(1);\n"
                                "}\n";
            const char expected[] = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }";
            ASSERT_EQUALS(expected, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "using std::cout, std::endl, std::cerr, std::ostringstream;\n"
                                "cerr << \"abc\";\n";
            const char expected[] = "std :: cerr << \"abc\" ;";
            ASSERT_EQUALS(expected, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "using std::string_view_literals::operator\"\"sv;\n";
            const char expected[] = "using std :: string_view_literals :: operator\"\"sv ;";
            ASSERT_EQUALS(expected, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "template <typename T>\n"
                                "class vector : public ::std::vector<T> {\n"
                                "public:\n"
                                "    using ::std::vector<T>::vector;\n"
                                "    vector() {}\n"
                                "};\n"
                                "vector <int> v;\n";
            const char expected[] = "class vector<int> ; "
                                    "vector<int> v ; "
                                    "class vector<int> : public :: std :: vector<int> { "
                                    "public: "
                                    "using vector<int> = :: std :: vector<int> :: vector<int> ; "
                                    "vector<int> ( ) { } "
                                    "} ;";
            ASSERT_EQUALS(expected, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "using namespace ::std;\n"
                                "void f(const char* c) {\n"
                                "    cout << std::string(c) << \"abc\";\n"
                                "}\n";
            const char expected[] = "using namespace :: std ; " // TODO: simplify cout?
                                    "void f ( const char * c ) { "
                                    "cout << std :: string ( c ) << \"abc\" ; "
                                    "}";
            ASSERT_EQUALS(expected, tok(code));
            ASSERT_EQUALS(
                "[test.cpp:3]: (debug) valueFlowConditionExpressions bailout: Skipping function due to incomplete variable cout\n",
                errout_str());
        }
        {
            const char code[] = "class T : private std::vector<std::pair<std::string, const int*>> {\n" // #12521
                                "    using std::vector<std::pair<std::string, const int*>>::empty;\n"
                                "};\n";
            const char expected[] = "class T : private std :: vector < std :: pair < std :: string , const int * > > { "
                                    "using empty = std :: vector < std :: pair < std :: string , const int * > > :: empty ; "
                                    "} ;";
            ASSERT_EQUALS(expected, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
    }

    void simplifyUsing31() { // #11899
        const char code[] = "struct B {\n"
                            "    B();\n"
                            "    void f();\n"
                            "};\n"
                            "struct D : B {\n"
                            "    using B::B;\n"
                            "    void g() {\n"
                            "        B::f();\n"
                            "    }\n"
                            "    B b;\n"
                            "};\n";
        const char expected[] = "struct B { "
                                "B ( ) ; "
                                "void f ( ) ; "
                                "} ; "
                                "struct D : B { "
                                "using B = B :: B ; "
                                "void g ( ) { "
                                "B :: f ( ) ; "
                                "} "
                                "B b ; "
                                "} ;";
        ASSERT_EQUALS(expected, tok(code));
        ASSERT_EQUALS("", errout_str());
    }

    void simplifyUsing32() {
        const char code[] = "using T = int*;\n" // #11430
                            "T f() { return T{}; }\n"
                            "T g() { return T(malloc(4)); }\n";
        const char expected[] = "int * f ( ) { return ( int * ) 0 ; } "
                                "int * g ( ) { return ( int * ) ( malloc ( 4 ) ) ; }";
        ASSERT_EQUALS(expected, tok(code));
        ASSERT_EQUALS("", errout_str());

        const char code2[] = "struct S {\n" // #13095
                             "    using reference_type = int&;\n"
                             "    reference_type get();\n"
                             "    int i;\n"
                             "};\n"
                             "auto S::get() -> reference_type {\n"
                             "    return i;\n"
                             "}\n";
        const char expected2[] = "struct S { "
                                 "int & get ( ) ; "
                                 "int i ; "
                                 "} ; "
                                 "auto S :: get ( ) . int & { return i ; }";
        ASSERT_EQUALS(expected2, tok(code2));
        TODO_ASSERT_EQUALS("",
                           "[test.cpp:6]: (debug) auto token with no type.\n"
                           "", errout_str());

        const char code3[] = "using V = int*;\n"
                             "auto g() -> const volatile V { return {}; }\n";
        const char expected3[] = "auto g ( ) . const volatile int * { return { } ; }";
        ASSERT_EQUALS(expected3, tok(code3));
        TODO_ASSERT_EQUALS("",
                           "[test.cpp:2]: (debug) auto token with no type.\n"
                           "", errout_str());
    }

    void simplifyUsing33() { // #13090
        const char code[] = "namespace N {\n"
                            "    using T = int;\n"
                            "    T f() { return (T)0; }\n"
                            "}\n"
                            "struct S {\n"
                            "    using U = int;\n"
                            "    U g() { return (U)0; }\n"
                            "};\n";
        const char expected[] = "namespace N { "
                                "int f ( ) { return ( int ) 0 ; } "
                                "} "
                                "struct S { "
                                "int g ( ) { return ( int ) 0 ; } "
                                "} ;";
        ASSERT_EQUALS(expected, tok(code));
        ASSERT_EQUALS("", errout_str());
    }

    void simplifyUsing34() { // #13295
        const char code[] = "struct A {\n"
                            "    static const int a = 1;\n"
                            "};\n"
                            "using B = struct A;\n"
                            "void g(int);\n"
                            "void f() {\n"
                            "    g({ B::a });\n"
                            "}\n";
        const char expected[] = "struct A { "
                                "static const int a = 1 ; "
                                "} ; "
                                "void g ( int ) ; "
                                "void f ( ) { "
                                "g ( { A :: a } ) ; "
                                "}";
        ASSERT_EQUALS(expected, tok(code));
        ASSERT_EQUALS("", errout_str());
    }

    void simplifyUsing35() { // #13492
        const char code[] = "using a = b;\n"
                            "using c = d;\n";
        const char expected[] = ";";
        ASSERT_EQUALS(expected, tok(code));
        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));
    }

    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));
    }

    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, dinit(TokOptions, $.type = Platform::Unix32)));
        ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::Unix64)));
        ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::Win32A)));
        ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::Win32W)));
        ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::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 -- ; } "
                           "} ; "
                           "template < class T > class s { } ;";

        ASSERT_EQUALS(exp, tok(code, dinit(TokOptions, $.type = Platform::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[] = "void f1 ( ) { "
                           "using namespace NS1 ; "
                           "signed long long A ; "
                           "} "
                           "void f2 ( ) { "
                           "using namespace NS1 :: NS2 ; "
                           "signed long long A ; "
                           "}";

        ASSERT_EQUALS(exp, tok(code));
        ASSERT_EQUALS("", errout_str());
    }

    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));
    }

    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));
        }
        {
            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));
        }
    }

    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));
    }

    void simplifyUsing9518() {
        const char code[] = "namespace a {\n"
                            "using a = enum {};\n"
                            "}";
        const char exp[] = "namespace a { "
                           "enum a { } ; "
                           "}";
        ASSERT_EQUALS(exp, tok(code));
    }

    void simplifyUsing9757() {
        const char code[] = "enum class Type_t { Nil = 0 };\n"
                            "template<Type_t type> class MappedType { };\n"
                            "template<> class MappedType<Type_t::Nil> { using type = void; };\n"
                            "std::string to_string (Example::Type_t type) {\n"
                            "   switch (type) {}\n"
                            "}";
        const char exp[] = "enum class Type_t { Nil = 0 } ; "
                           "class MappedType<Type_t::Nil> ; "
                           "template < Type_t type > class MappedType { } ; "
                           "class MappedType<Type_t::Nil> { } ; "
                           "std :: string to_string ( Example :: Type_t type ) { "
                           "switch ( type ) { } }";
        ASSERT_EQUALS(exp, tok(code));
    }

    void simplifyUsing10008() {
        const char code[] = "namespace ns {\n"
                            "    using ArrayType = std::vector<int>;\n"
                            "}\n"
                            "using namespace ns;\n"
                            "static void f() {\n"
                            "    const ArrayType arr;\n"
                            "}";
        const char exp[] = "using namespace ns ; "
                           "static void f ( ) { "
                           "const std :: vector < int > arr ; "
                           "}";
        ASSERT_EQUALS(exp, tok(code));
    }

    void simplifyUsing10054() { // debug: Executable scope 'x' with unknown function.
        {
            // original example: using "namespace external::ns1;" but redundant qualification
            const char code[] = "namespace external {\n"
                                "    namespace ns1 {\n"
                                "        template <int size> struct B { };\n"
                                "        using V = B<sizeof(bool)>;\n"
                                "    }\n"
                                "}\n"
                                "namespace ns {\n"
                                "    struct A {\n"
                                "        void f(external::ns1::V);\n"
                                "    };\n"
                                "}\n"
                                "using namespace external::ns1;\n"
                                "namespace ns {\n"
                                "    void A::f(external::ns1::V) {}\n"
                                "}";
            const char exp[]  = "namespace external { "
                                "namespace ns1 { "
                                "struct B<1> ; "
                                "} "
                                "} "
                                "namespace ns { "
                                "struct A { "
                                "void f ( external :: ns1 :: B<1> ) ; "
                                "} ; "
                                "} "
                                "using namespace external :: ns1 ; "
                                "namespace ns { "
                                "void A :: f ( external :: ns1 :: B<1> ) { } "
                                "} "
                                "struct external :: ns1 :: B<1> { } ;";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            // no using "namespace external::ns1;"
            const char code[] = "namespace external {\n"
                                "    namespace ns1 {\n"
                                "        template <int size> struct B { };\n"
                                "        using V = B<sizeof(bool)>;\n"
                                "    }\n"
                                "}\n"
                                "namespace ns {\n"
                                "    struct A {\n"
                                "        void f(external::ns1::V);\n"
                                "    };\n"
                                "}\n"
                                "namespace ns {\n"
                                "    void A::f(external::ns1::V) {}\n"
                                "}";
            const char exp[]  = "namespace external { "
                                "namespace ns1 { "
                                "struct B<1> ; "
                                "} "
                                "} "
                                "namespace ns { "
                                "struct A { "
                                "void f ( external :: ns1 :: B<1> ) ; "
                                "} ; "
                                "} "
                                "namespace ns { "
                                "void A :: f ( external :: ns1 :: B<1> ) { } "
                                "} "
                                "struct external :: ns1 :: B<1> { } ;";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            // using "namespace external::ns1;" without redundant qualification
            const char code[] = "namespace external {\n"
                                "    namespace ns1 {\n"
                                "        template <int size> struct B { };\n"
                                "        using V = B<sizeof(bool)>;\n"
                                "    }\n"
                                "}\n"
                                "namespace ns {\n"
                                "    struct A {\n"
                                "        void f(external::ns1::V);\n"
                                "    };\n"
                                "}\n"
                                "using namespace external::ns1;\n"
                                "namespace ns {\n"
                                "    void A::f(V) {}\n"
                                "}";
            const char exp[]  = "namespace external { "
                                "namespace ns1 { "
                                "struct B<1> ; "
                                "} "
                                "} "
                                "namespace ns { "
                                "struct A { "
                                "void f ( external :: ns1 :: B<1> ) ; "
                                "} ; "
                                "} "
                                "using namespace external :: ns1 ; "
                                "namespace ns { "
                                "void A :: f ( external :: ns1 :: B<1> ) { } "
                                "} "
                                "struct external :: ns1 :: B<1> { } ;";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            // using "namespace external::ns1;" without redundant qualification on declaration and definition
            const char code[] = "namespace external {\n"
                                "    namespace ns1 {\n"
                                "        template <int size> struct B { };\n"
                                "        using V = B<sizeof(bool)>;\n"
                                "    }\n"
                                "}\n"
                                "using namespace external::ns1;\n"
                                "namespace ns {\n"
                                "    struct A {\n"
                                "        void f(V);\n"
                                "    };\n"
                                "}\n"
                                "namespace ns {\n"
                                "    void A::f(V) {}\n"
                                "}";
            const char exp[]  = "namespace external { "
                                "namespace ns1 { "
                                "struct B<1> ; "
                                "} "
                                "} "
                                "using namespace external :: ns1 ; "
                                "namespace ns { "
                                "struct A { "
                                "void f ( external :: ns1 :: B<1> ) ; "
                                "} ; "
                                "} "
                                "namespace ns { "
                                "void A :: f ( external :: ns1 :: B<1> ) { } "
                                "} "
                                "struct external :: ns1 :: B<1> { } ;";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "namespace external {\n"
                                "    template <int size> struct B { };\n"
                                "    namespace ns1 {\n"
                                "        using V = B<sizeof(bool)>;\n"
                                "    }\n"
                                "}\n"
                                "namespace ns {\n"
                                "    struct A {\n"
                                "        void f(external::ns1::V);\n"
                                "    };\n"
                                "}\n"
                                "namespace ns {\n"
                                "    void A::f(external::ns1::V) {}\n"
                                "}";
            const char exp[]  = "namespace external { "
                                "struct B<1> ; "
                                "} "
                                "namespace ns { "
                                "struct A { "
                                "void f ( external :: B<1> ) ; "
                                "} ; "
                                "} "
                                "namespace ns { "
                                "void A :: f ( external :: B<1> ) { } "
                                "} "
                                "struct external :: B<1> { } ;";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "template <int size> struct B { };\n"
                                "namespace external {\n"
                                "    namespace ns1 {\n"
                                "        using V = B<sizeof(bool)>;\n"
                                "    }\n"
                                "}\n"
                                "namespace ns {\n"
                                "    struct A {\n"
                                "        void f(external::ns1::V);\n"
                                "    };\n"
                                "}\n"
                                "namespace ns {\n"
                                "    void A::f(external::ns1::V) {}\n"
                                "}";
            const char exp[]  = "struct B<1> ; "
                                "namespace ns { "
                                "struct A { "
                                "void f ( B<1> ) ; "
                                "} ; "
                                "} "
                                "namespace ns { "
                                "void A :: f ( B<1> ) { } "
                                "} "
                                "struct B<1> { } ;";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
    }

    void simplifyUsing10136() {
        {
            const char code[] = "class B {\n"
                                "public:\n"
                                "    using V = std::vector<char>;\n"
                                "    virtual void f(const V&) const = 0;\n"
                                "};\n"
                                "class A final : public B {\n"
                                "public:\n"
                                "    void f(const V&) const override;\n"
                                "};\n"
                                "void A::f(const std::vector<char>&) const { }";
            const char exp[]  = "class B { "
                                "public: "
                                "virtual void f ( const std :: vector < char > & ) const = 0 ; "
                                "} ; "
                                "class A : public B { "
                                "public: "
                                "void f ( const std :: vector < char > & ) const override ; "
                                "} ; "
                                "void A :: f ( const std :: vector < char > & ) const { }";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "namespace NS1 {\n"
                                "    class B {\n"
                                "    public:\n"
                                "        using V = std::vector<char>;\n"
                                "        virtual void f(const V&) const = 0;\n"
                                "    };\n"
                                "}\n"
                                "namespace NS2 {\n"
                                "    class A : public NS1::B {\n"
                                "    public:\n"
                                "        void f(const V&) const override;\n"
                                "    };\n"
                                "    namespace NS3 {\n"
                                "        class C : public A {\n"
                                "        public:\n"
                                "            void f(const V&) const override;\n"
                                "        };\n"
                                "        void C::f(const V&) const { }\n"
                                "    }\n"
                                "    void A::f(const V&) const { }\n"
                                "}\n"
                                "void foo() {\n"
                                "    NS2::A a;\n"
                                "    NS2::NS3::C c;\n"
                                "    NS1::B::V v;\n"
                                "    a.f(v);\n"
                                "    c.f(v);\n"
                                "}";
            const char exp[]  = "namespace NS1 { "
                                "class B { "
                                "public: "
                                "virtual void f ( const std :: vector < char > & ) const = 0 ; "
                                "} ; "
                                "} "
                                "namespace NS2 { "
                                "class A : public NS1 :: B { "
                                "public: "
                                "void f ( const std :: vector < char > & ) const override ; "
                                "} ; "
                                "namespace NS3 { "
                                "class C : public A { "
                                "public: "
                                "void f ( const std :: vector < char > & ) const override ; "
                                "} ; "
                                "void C :: f ( const std :: vector < char > & ) const { } "
                                "} "
                                "void A :: f ( const std :: vector < char > & ) const { } "
                                "} "
                                "void foo ( ) { "
                                "NS2 :: A a ; "
                                "NS2 :: NS3 :: C c ; "
                                "std :: vector < char > v ; "
                                "a . f ( v ) ; "
                                "c . f ( v ) ; "
                                "}";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "foo::ResultCodes_e\n"
                                "GemImpl::setR(const ::foo::s _ipSource)\n"
                                "{\n"
                                "   M3_LOG_EE_DEBUG();\n"
                                "   using MLSource = foo::s::Literal;\n"
                                "   auto ret = foo::ResultCodes_e::NO_ERROR;\n"
                                "   M3_LOG_INFO(\"foo(\" << static_cast<int>(_ipSource) << \")\");\n"
                                "   return ret;\n"
                                "}\n"
                                "foo::ResultCodes_e\n"
                                "GemImpl::getF(::foo::s &_ipSource)\n"
                                "{\n"
                                "   M3_LOG_EE_DEBUG();\n"
                                "   auto ret = foo::ResultCodes_e::NO_ERROR;\n"
                                "   return ret;\n"
                                "}\n"
                                "foo::ResultCodes_e\n"
                                "GemImpl::setF(const ::foo::s _ipSource)\n"
                                "{\n"
                                "   M3_LOG_EE_DEBUG();\n"
                                "   using MLSource = foo::s::Literal;\n"
                                "   auto ret = foo::ResultCodes_e::NO_ERROR;\n"
                                "   return ret;\n"
                                "}";
            (void)tok(code); // don't crash
            ignore_errout(); // we do not care about the output
        }
    }

    void simplifyUsing10171() {
        const char code[] = "namespace ns {\n"
                            "    class A {\n"
                            "    public:\n"
                            "        using V = std::vector<unsigned char>;\n"
                            "        virtual void f(const V&) const = 0;\n"
                            "    };\n"
                            "    class B : public A {\n"
                            "    public:\n"
                            "        void f(const V&) const override;\n"
                            "    };\n"
                            "}\n"
                            "namespace ns {\n"
                            "    void B::f(const std::vector<unsigned char>&) const { }\n"
                            "}";
        const char exp[] = "namespace ns { "
                           "class A { "
                           "public: "
                           "virtual void f ( const std :: vector < unsigned char > & ) const = 0 ; "
                           "} ; "
                           "class B : public A { "
                           "public: "
                           "void f ( const std :: vector < unsigned char > & ) const override ; "
                           "} ; "
                           "} "
                           "namespace ns { "
                           "void B :: f ( const std :: vector < unsigned char > & ) const { } "
                           "}";
        ASSERT_EQUALS(exp, tok(code));
        ASSERT_EQUALS("", errout_str());
    }

    void simplifyUsing10172() {
        {
            const char code[] = "namespace ns {\n"
                                "    class A {\n"
                                "    public:\n"
                                "        using h = std::function<void()>;\n"
                                "    };\n"
                                "    class B : public A {\n"
                                "        void f(h);\n"
                                "    };\n"
                                "}\n"
                                "namespace ns {\n"
                                "    void B::f(h) { }\n"
                                "}";
            const char exp[] = "namespace ns { "
                               "class A { "
                               "public: "
                               "} ; "
                               "class B : public A { "
                               "void f ( std :: function < void ( ) > ) ; "
                               "} ; "
                               "} "
                               "namespace ns { "
                               "void B :: f ( std :: function < void ( ) > ) { } "
                               "}";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "namespace ns {\n"
                                "namespace external {\n"
                                "class A {\n"
                                "public: \n"
                                "  using h = std::function<void()>;\n"
                                "};\n"
                                "class B : public A {\n"
                                "  void f(h);\n"
                                "};\n"
                                "}\n"
                                "}\n"
                                "namespace ns {\n"
                                "namespace external {\n"
                                "void B::f(h) {}\n"
                                "}\n"
                                "}";
            const char exp[] = "namespace ns { "
                               "namespace external { "
                               "class A { "
                               "public: "
                               "} ; "
                               "class B : public A { "
                               "void f ( std :: function < void ( ) > ) ; "
                               "} ; "
                               "} "
                               "} "
                               "namespace ns { "
                               "namespace external { "
                               "void B :: f ( std :: function < void ( ) > ) { } "
                               "} "
                               "}";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
    }

    void simplifyUsing10173() {
        {
            const char code[] = "std::ostream & operator<<(std::ostream &s, const Pr<st> p) {\n"
                                "    return s;\n"
                                "}\n"
                                "void foo() {\n"
                                "    using Pr = d::Pr<st>;\n"
                                "    Pr p;\n"
                                "}\n"
                                "void bar() {\n"
                                "   Pr<st> p;\n"
                                "}";
            const char exp[]  = "std :: ostream & operator<< ( std :: ostream & s , const Pr < st > p ) { "
                                "return s ; "
                                "} "
                                "void foo ( ) { "
                                "d :: Pr < st > p ; "
                                "} "
                                "void bar ( ) { "
                                "Pr < st > p ; "
                                "}";
            ASSERT_EQUALS(exp, tok(code));
            ASSERT_EQUALS("", errout_str());
        }
        {
            const char code[] = "namespace defsa {\n"
                                "void xxx::foo() {\n"
                                "   using NS1 = v1::l;\n"
                                "}\n"
                                "void xxx::bar() {\n"
                                "   using NS1 = v1::l;\n"
                                "}\n"
                                "void xxx::foobar() {\n"
                                "   using NS1 = v1::l;\n"
                                "}\n"
                                "}";
            const char exp[]  = "namespace defsa { "
                                "void xxx :: foo ( ) { "
                                "} "
                                "void xxx :: bar ( ) { "
                                "} "
                                "void xxx :: foobar ( ) { "
                                "} "
                                "}";
            ASSERT_EQUALS(exp, tok(code));
            ignore_errout(); // we do not care about the output
        }
    }

    void simplifyUsing10335() {
        const char code[] = "using uint8_t = unsigned char;\n"
                            "enum E : uint8_t { E0 };";
        const char exp[]  = "enum E : unsigned char { E0 } ;";
        ASSERT_EQUALS(exp, tok(code));
    }

    void simplifyUsing10720() { // hang/segmentation fault
        const char code[] = "template <typename... Ts>\n"
                            "struct S {};\n"
                            "#define STAMP(thiz, prev) using thiz = S<prev, prev, prev, prev, prev, prev, prev, prev, prev, prev>;\n"
                            "STAMP(A, int);\n"
                            "STAMP(B, A);\n"
                            "STAMP(C, B);\n";
        (void)tok(code, dinit(TokOptions, $.preprocess = true));
        ASSERT(startsWith(errout_str(), "[test.cpp:6]: (debug) Failed to parse 'using C = S < S < S < int"));
    }

    void scopeInfo1() {
        const char code[] = "struct A {\n"
                            "    enum class Mode { UNKNOWN, ENABLED, NONE, };\n"
                            "};\n"
                            "\n"
                            "namespace spdlog { class logger; }\n"
                            "using LoggerPtr = std::shared_ptr<spdlog::logger>;";
        (void)tok(code);
        ASSERT_EQUALS("", errout_str());
    }

    void scopeInfo2() {
        const char code[] = "struct A {\n"
                            "    using Map = std::map<int, int>;\n"
                            "    Map values;\n"
                            "};\n"
                            "\n"
                            "static void getInitialProgramState(const A::Map& vars = A::Map {})\n"
                            "{}\n";
        (void)tok(code);
        ASSERT_EQUALS("", errout_str());
    }
};

REGISTER_TEST(TestSimplifyUsing)