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
| //===-- TweakTests.cpp ------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Annotations.h"
#include "SourceCode.h"
#include "TestFS.h"
#include "TestTU.h"
#include "TweakTesting.h"
#include "refactor/Tweak.h"
#include "clang/AST/Expr.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/FileSystemOptions.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock-matchers.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cassert>
#include <string>
#include <utility>
#include <vector>
using ::testing::AllOf;
using ::testing::HasSubstr;
using ::testing::StartsWith;
using ::testing::ElementsAre;
namespace clang {
namespace clangd {
namespace {
MATCHER_P2(FileWithContents, FileName, Contents, "") {
return arg.first() == FileName && arg.second == Contents;
}
TEST(FileEdits, AbsolutePath) {
auto RelPaths = {"a.h", "foo.cpp", "test/test.cpp"};
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
new llvm::vfs::InMemoryFileSystem);
MemFS->setCurrentWorkingDirectory(testRoot());
for (auto Path : RelPaths)
MemFS->addFile(Path, 0, llvm::MemoryBuffer::getMemBuffer("", Path));
FileManager FM(FileSystemOptions(), MemFS);
DiagnosticsEngine DE(new DiagnosticIDs, new DiagnosticOptions);
SourceManager SM(DE, FM);
for (auto Path : RelPaths) {
auto FID = SM.createFileID(*FM.getFile(Path), SourceLocation(),
clang::SrcMgr::C_User);
auto Res = Tweak::Effect::fileEdit(SM, FID, tooling::Replacements());
ASSERT_THAT_EXPECTED(Res, llvm::Succeeded());
EXPECT_EQ(Res->first, testPath(Path));
}
}
TWEAK_TEST(SwapIfBranches);
TEST_F(SwapIfBranchesTest, Test) {
Context = Function;
EXPECT_EQ(apply("^if (true) {return 100;} else {continue;}"),
"if (true) {continue;} else {return 100;}");
EXPECT_EQ(apply("^if () {return 100;} else {continue;}"),
"if () {continue;} else {return 100;}")
<< "broken condition";
EXPECT_AVAILABLE("^i^f^^(^t^r^u^e^) { return 100; } ^e^l^s^e^ { continue; }");
EXPECT_UNAVAILABLE("if (true) {^return ^100;^ } else { ^continue^;^ }");
// Available in subexpressions of the condition;
EXPECT_THAT("if(2 + [[2]] + 2) { return 2 + 2 + 2; } else {continue;}",
isAvailable());
// But not as part of the branches.
EXPECT_THAT("if(2 + 2 + 2) { return 2 + [[2]] + 2; } else { continue; }",
Not(isAvailable()));
// Range covers the "else" token, so available.
EXPECT_THAT("if(2 + 2 + 2) { return 2 + [[2 + 2; } else {continue;]]}",
isAvailable());
// Not available in compound statements in condition.
EXPECT_THAT(
"if([]{return [[true]];}()) { return 2 + 2 + 2; } else { continue; }",
Not(isAvailable()));
// Not available if both sides aren't braced.
EXPECT_THAT("^if (1) return; else { return; }", Not(isAvailable()));
// Only one if statement is supported!
EXPECT_THAT("[[if(1){}else{}if(2){}else{}]]", Not(isAvailable()));
}
TWEAK_TEST(RawStringLiteral);
TEST_F(RawStringLiteralTest, Test) {
Context = Expression;
EXPECT_AVAILABLE(R"cpp(^"^f^o^o^\^n^")cpp");
EXPECT_AVAILABLE(R"cpp(R"(multi )" ^"token " "str\ning")cpp");
EXPECT_UNAVAILABLE(R"cpp(^"f^o^o^o")cpp"); // no chars need escaping
EXPECT_UNAVAILABLE(R"cpp(R"(multi )" ^"token " u8"str\ning")cpp"); // nonascii
EXPECT_UNAVAILABLE(R"cpp(^R^"^(^multi )" "token " "str\ning")cpp"); // raw
EXPECT_UNAVAILABLE(R"cpp(^"token\n" __FILE__)cpp"); // chunk is macro
EXPECT_UNAVAILABLE(R"cpp(^"a\r\n";)cpp"); // forbidden escape char
const char *Input = R"cpp(R"(multi
token)" "\nst^ring\n" "literal")cpp";
const char *Output = R"cpp(R"(multi
token
string
literal)")cpp";
EXPECT_EQ(apply(Input), Output);
}
TWEAK_TEST(DumpAST);
TEST_F(DumpASTTest, Test) {
EXPECT_AVAILABLE("^int f^oo() { re^turn 2 ^+ 2; }");
EXPECT_UNAVAILABLE("/*c^omment*/ int foo() return 2 ^ + 2; }");
EXPECT_THAT(apply("int x = 2 ^+ 2;"),
AllOf(StartsWith("message:"), HasSubstr("BinaryOperator"),
HasSubstr("'+'"), HasSubstr("|-IntegerLiteral"),
HasSubstr("<col:9> 'int' 2\n`-IntegerLiteral"),
HasSubstr("<col:13> 'int' 2")));
}
TWEAK_TEST(ShowSelectionTree);
TEST_F(ShowSelectionTreeTest, Test) {
EXPECT_AVAILABLE("^int f^oo() { re^turn 2 ^+ 2; }");
EXPECT_AVAILABLE("/*c^omment*/ int foo() return 2 ^ + 2; }");
const char *Output = R"(message:
TranslationUnitDecl
VarDecl int x = fcall(2 + 2)
.CallExpr fcall(2 + 2)
ImplicitCastExpr fcall
.DeclRefExpr fcall
.BinaryOperator 2 + 2
*IntegerLiteral 2
)";
EXPECT_EQ(apply("int fcall(int); int x = fca[[ll(2 +]]2);"), Output);
}
TWEAK_TEST(DumpRecordLayout);
TEST_F(DumpRecordLayoutTest, Test) {
EXPECT_AVAILABLE("^s^truct ^X ^{ int x; ^};");
EXPECT_THAT("struct X { int ^a; };", Not(isAvailable()));
EXPECT_THAT("struct ^X;", Not(isAvailable()));
EXPECT_THAT("template <typename T> struct ^X { T t; };", Not(isAvailable()));
EXPECT_THAT("enum ^X {};", Not(isAvailable()));
EXPECT_THAT(apply("struct ^X { int x; int y; }"),
AllOf(StartsWith("message:"), HasSubstr("0 | int x")));
}
TWEAK_TEST(ExtractVariable);
TEST_F(ExtractVariableTest, Test) {
const char *AvailableCases = R"cpp(
int xyz(int a = 1) {
struct T {
int bar(int a = 1);
int z;
} t;
// return statement
return [[[[t.b[[a]]r]](t.z)]];
}
void f() {
int a = [[5 +]] [[4 * [[[[xyz]]()]]]];
// multivariable initialization
if(1)
int x = [[1]], y = [[a + 1]], a = [[1]], z = a + 1;
// if without else
if([[1]])
a = [[1]];
// if with else
if(a < [[3]])
if(a == [[4]])
a = [[5]];
else
a = [[5]];
else if (a < [[4]])
a = [[4]];
else
a = [[5]];
// for loop
for(a = [[1]]; a > [[[[3]] + [[4]]]]; a++)
a = [[2]];
// while
while(a < [[1]])
a = [[1]];
// do while
do
a = [[1]];
while(a < [[3]]);
}
)cpp";
EXPECT_AVAILABLE(AvailableCases);
const char *NoCrashCases = R"cpp(
template<typename T, typename ...Args>
struct Test<T, Args...> {
Test(const T &v) :val[[(^]]) {}
T val;
};
)cpp";
EXPECT_UNAVAILABLE(NoCrashCases);
const char *UnavailableCases = R"cpp(
int xyz(int a = [[1]]) {
struct T {
int bar(int a = [[1]]);
int z = [[1]];
} t;
return [[t]].bar([[[[t]].z]]);
}
void v() { return; }
// function default argument
void f(int b = [[1]]) {
// empty selection
int a = ^1 ^+ ^2;
// void expressions
auto i = new int, j = new int;
[[[[delete i]], delete j]];
[[v]]();
// if
if(1)
int x = 1, y = a + 1, a = 1, z = [[a + 1]];
if(int a = 1)
if([[a + 1]] == 4)
a = [[[[a]] +]] 1;
// for loop
for(int a = 1, b = 2, c = 3; a > [[b + c]]; [[a++]])
a = [[a + 1]];
// lambda
auto lamb = [&[[a]], &[[b]]](int r = [[1]]) {return 1;}
// assigment
xyz([[a = 5]]);
xyz([[a *= 5]]);
// Variable DeclRefExpr
a = [[b]];
// statement expression
[[xyz()]];
while (a)
[[++a]];
// label statement
goto label;
label:
a = [[1]];
}
)cpp";
EXPECT_UNAVAILABLE(UnavailableCases);
// vector of pairs of input and output strings
const std::vector<std::pair<llvm::StringLiteral, llvm::StringLiteral>>
InputOutputs = {
// extraction from variable declaration/assignment
{R"cpp(void varDecl() {
int a = 5 * (4 + (3 [[- 1)]]);
})cpp",
R"cpp(void varDecl() {
auto dummy = (3 - 1); int a = 5 * (4 + dummy);
})cpp"},
// FIXME: extraction from switch case
/*{R"cpp(void f(int a) {
if(1)
while(a < 1)
switch (1) {
case 1:
a = [[1 + 2]];
break;
default:
break;
}
})cpp",
R"cpp(void f(int a) {
auto dummy = 1 + 2; if(1)
while(a < 1)
switch (1) {
case 1:
a = dummy;
break;
default:
break;
}
})cpp"},*/
// Macros
{R"cpp(#define PLUS(x) x++
void f(int a) {
int y = PLUS([[1+a]]);
})cpp",
/*FIXME: It should be extracted like this.
R"cpp(#define PLUS(x) x++
void f(int a) {
auto dummy = 1+a; int y = PLUS(dummy);
})cpp"},*/
R"cpp(#define PLUS(x) x++
void f(int a) {
auto dummy = PLUS(1+a); int y = dummy;
})cpp"},
// ensure InsertionPoint isn't inside a macro
{R"cpp(#define LOOP(x) while (1) {a = x;}
void f(int a) {
if(1)
LOOP(5 + [[3]])
})cpp",
/*FIXME: It should be extracted like this. SelectionTree needs to be
* fixed for macros.
R"cpp(#define LOOP(x) while (1) {a = x;}
void f(int a) {
auto dummy = 3; if(1)
LOOP(5 + dummy)
})cpp"},*/
R"cpp(#define LOOP(x) while (1) {a = x;}
void f(int a) {
auto dummy = LOOP(5 + 3); if(1)
dummy
})cpp"},
{R"cpp(#define LOOP(x) do {x;} while(1);
void f(int a) {
if(1)
LOOP(5 + [[3]])
})cpp",
R"cpp(#define LOOP(x) do {x;} while(1);
void f(int a) {
auto dummy = 3; if(1)
LOOP(5 + dummy)
})cpp"},
// attribute testing
{R"cpp(void f(int a) {
[ [gsl::suppress("type")] ] for (;;) a = [[1]];
})cpp",
R"cpp(void f(int a) {
auto dummy = 1; [ [gsl::suppress("type")] ] for (;;) a = dummy;
})cpp"},
// MemberExpr
{R"cpp(class T {
T f() {
return [[T().f()]].f();
}
};)cpp",
R"cpp(class T {
T f() {
auto dummy = T().f(); return dummy.f();
}
};)cpp"},
// Function DeclRefExpr
{R"cpp(int f() {
return [[f]]();
})cpp",
R"cpp(int f() {
auto dummy = f(); return dummy;
})cpp"},
// FIXME: Wrong result for \[\[clang::uninitialized\]\] int b = [[1]];
// since the attr is inside the DeclStmt and the bounds of
// DeclStmt don't cover the attribute.
// Binary subexpressions
{R"cpp(void f() {
int x = 1 + [[2 + 3 + 4]] + 5;
})cpp",
R"cpp(void f() {
auto dummy = 2 + 3 + 4; int x = 1 + dummy + 5;
})cpp"},
{R"cpp(void f() {
int x = [[1 + 2 + 3]] + 4 + 5;
})cpp",
R"cpp(void f() {
auto dummy = 1 + 2 + 3; int x = dummy + 4 + 5;
})cpp"},
{R"cpp(void f() {
int x = 1 + 2 + [[3 + 4 + 5]];
})cpp",
R"cpp(void f() {
auto dummy = 3 + 4 + 5; int x = 1 + 2 + dummy;
})cpp"},
// Non-associative operations have no special support
{R"cpp(void f() {
int x = 1 - [[2 - 3 - 4]] - 5;
})cpp",
R"cpp(void f() {
auto dummy = 1 - 2 - 3 - 4; int x = dummy - 5;
})cpp"},
// A mix of associative operators isn't associative.
{R"cpp(void f() {
int x = 0 + 1 * [[2 + 3]] * 4 + 5;
})cpp",
R"cpp(void f() {
auto dummy = 1 * 2 + 3 * 4; int x = 0 + dummy + 5;
})cpp"},
// Overloaded operators are supported, we assume associativity
// as if they were built-in.
{R"cpp(struct S {
S(int);
};
S operator+(S, S);
void f() {
S x = S(1) + [[S(2) + S(3) + S(4)]] + S(5);
})cpp",
R"cpp(struct S {
S(int);
};
S operator+(S, S);
void f() {
auto dummy = S(2) + S(3) + S(4); S x = S(1) + dummy + S(5);
})cpp"},
// Don't try to analyze across macro boundaries
// FIXME: it'd be nice to do this someday (in a safe way)
{R"cpp(#define ECHO(X) X
void f() {
int x = 1 + [[ECHO(2 + 3) + 4]] + 5;
})cpp",
R"cpp(#define ECHO(X) X
void f() {
auto dummy = 1 + ECHO(2 + 3) + 4; int x = dummy + 5;
})cpp"},
{R"cpp(#define ECHO(X) X
void f() {
int x = 1 + [[ECHO(2) + ECHO(3) + 4]] + 5;
})cpp",
R"cpp(#define ECHO(X) X
void f() {
auto dummy = 1 + ECHO(2) + ECHO(3) + 4; int x = dummy + 5;
})cpp"},
};
for (const auto &IO : InputOutputs) {
EXPECT_EQ(IO.second, apply(IO.first)) << IO.first;
}
}
TWEAK_TEST(AnnotateHighlightings);
TEST_F(AnnotateHighlightingsTest, Test) {
EXPECT_AVAILABLE("^vo^id^ ^f(^) {^}^"); // available everywhere.
EXPECT_AVAILABLE("[[int a; int b;]]");
EXPECT_EQ("void /* entity.name.function.cpp */f() {}", apply("void ^f() {}"));
EXPECT_EQ(apply("[[void f1(); void f2();]]"),
"void /* entity.name.function.cpp */f1(); "
"void /* entity.name.function.cpp */f2();");
EXPECT_EQ(apply("void f1(); void f2() {^}"),
"void f1(); "
"void /* entity.name.function.cpp */f2() {}");
}
TWEAK_TEST(ExpandMacro);
TEST_F(ExpandMacroTest, Test) {
Header = R"cpp(
#define FOO 1 2 3
#define FUNC(X) X+X+X
#define EMPTY
#define EMPTY_FN(X)
)cpp";
// Available on macro names, not available anywhere else.
EXPECT_AVAILABLE("^F^O^O^ BAR ^F^O^O^");
EXPECT_AVAILABLE("^F^U^N^C^(1)");
EXPECT_UNAVAILABLE("^#^d^efine^ ^XY^Z 1 ^2 ^3^");
EXPECT_UNAVAILABLE("FOO ^B^A^R^ FOO ^");
EXPECT_UNAVAILABLE("FUNC(^1^)^");
// Works as expected on object-like macros.
EXPECT_EQ(apply("^FOO BAR FOO"), "1 2 3 BAR FOO");
EXPECT_EQ(apply("FOO BAR ^FOO"), "FOO BAR 1 2 3");
// And function-like macros.
EXPECT_EQ(apply("F^UNC(2)"), "2 + 2 + 2");
// Works on empty macros.
EXPECT_EQ(apply("int a ^EMPTY;"), "int a ;");
EXPECT_EQ(apply("int a ^EMPTY_FN(1 2 3);"), "int a ;");
EXPECT_EQ(apply("int a = 123 ^EMPTY EMPTY_FN(1);"),
"int a = 123 EMPTY_FN(1);");
EXPECT_EQ(apply("int a = 123 ^EMPTY_FN(1) EMPTY;"), "int a = 123 EMPTY;");
EXPECT_EQ(apply("int a = 123 EMPTY_FN(1) ^EMPTY;"),
"int a = 123 EMPTY_FN(1) ;");
}
TWEAK_TEST(ExpandAutoType);
TEST_F(ExpandAutoTypeTest, Test) {
Header = R"cpp(
namespace ns {
struct Class {
struct Nested {};
}
void Func();
}
inline namespace inl_ns {
namespace {
struct Visible {};
}
}
)cpp";
EXPECT_AVAILABLE("^a^u^t^o^ i = 0;");
EXPECT_UNAVAILABLE("auto ^i^ ^=^ ^0^;^");
// check primitive type
EXPECT_EQ(apply("[[auto]] i = 0;"), "int i = 0;");
EXPECT_EQ(apply("au^to i = 0;"), "int i = 0;");
// check classes and namespaces
EXPECT_EQ(apply("^auto C = ns::Class::Nested();"),
"ns::Class::Nested C = ns::Class::Nested();");
// check that namespaces are shortened
EXPECT_EQ(apply("namespace ns { void f() { ^auto C = Class(); } }"),
"namespace ns { void f() { Class C = Class(); } }");
// undefined functions should not be replaced
EXPECT_THAT(apply("au^to x = doesnt_exist();"),
StartsWith("fail: Could not deduce type for 'auto' type"));
// function pointers should not be replaced
EXPECT_THAT(apply("au^to x = &ns::Func;"),
StartsWith("fail: Could not expand type of function pointer"));
// lambda types are not replaced
EXPECT_THAT(apply("au^to x = []{};"),
StartsWith("fail: Could not expand type of lambda expression"));
// inline namespaces
EXPECT_EQ(apply("au^to x = inl_ns::Visible();"),
"Visible x = inl_ns::Visible();");
// local class
EXPECT_EQ(apply("namespace x { void y() { struct S{}; ^auto z = S(); } }"),
"namespace x { void y() { struct S{}; S z = S(); } }");
// replace array types
EXPECT_EQ(apply(R"cpp(au^to x = "test")cpp"),
R"cpp(const char * x = "test")cpp");
EXPECT_UNAVAILABLE("dec^ltype(au^to) x = 10;");
// FIXME: Auto-completion in a template requires disabling delayed template
// parsing.
ExtraArgs.push_back("-fno-delayed-template-parsing");
// unknown types in a template should not be replaced
EXPECT_THAT(apply("template <typename T> void x() { ^auto y = T::z(); }"),
StartsWith("fail: Could not deduce type for 'auto' type"));
}
TWEAK_TEST(ExtractFunction);
TEST_F(ExtractFunctionTest, FunctionTest) {
Context = Function;
// Root statements should have common parent.
EXPECT_EQ(apply("for(;;) [[1+2; 1+2;]]"), "unavailable");
// Expressions aren't extracted.
EXPECT_EQ(apply("int x = 0; [[x++;]]"), "unavailable");
// We don't support extraction from lambdas.
EXPECT_EQ(apply("auto lam = [](){ [[int x;]] }; "), "unavailable");
// Partial statements aren't extracted.
EXPECT_THAT(apply("int [[x = 0]];"), "unavailable");
// Ensure that end of Zone and Beginning of PostZone being adjacent doesn't
// lead to break being included in the extraction zone.
EXPECT_THAT(apply("for(;;) { [[int x;]]break; }"), HasSubstr("extracted"));
// FIXME: ExtractFunction should be unavailable inside loop construct
// initalizer/condition.
EXPECT_THAT(apply(" for([[int i = 0;]];);"), HasSubstr("extracted"));
// Don't extract because needs hoisting.
EXPECT_THAT(apply(" [[int a = 5;]] a++; "), StartsWith("fail"));
// Don't extract return
EXPECT_THAT(apply(" if(true) [[return;]] "), StartsWith("fail"));
}
TEST_F(ExtractFunctionTest, FileTest) {
// Check all parameters are in order
std::string ParameterCheckInput = R"cpp(
struct Foo {
int x;
};
void f(int a) {
int b;
int *ptr = &a;
Foo foo;
[[a += foo.x + b;
*ptr++;]]
})cpp";
std::string ParameterCheckOutput = R"cpp(
struct Foo {
int x;
};
void extracted(int &a, int &b, int * &ptr, Foo &foo) {
a += foo.x + b;
*ptr++;
}
void f(int a) {
int b;
int *ptr = &a;
Foo foo;
extracted(a, b, ptr, foo);
})cpp";
EXPECT_EQ(apply(ParameterCheckInput), ParameterCheckOutput);
// Check const qualifier
std::string ConstCheckInput = R"cpp(
void f(const int c) {
[[while(c) {}]]
})cpp";
std::string ConstCheckOutput = R"cpp(
void extracted(const int &c) {
while(c) {}
}
void f(const int c) {
extracted(c);
})cpp";
EXPECT_EQ(apply(ConstCheckInput), ConstCheckOutput);
// Don't extract when we need to make a function as a parameter.
EXPECT_THAT(apply("void f() { [[int a; f();]] }"), StartsWith("fail"));
// We don't extract from methods for now since they may involve multi-file
// edits
std::string MethodFailInput = R"cpp(
class T {
void f() {
[[int x;]]
}
};
)cpp";
EXPECT_EQ(apply(MethodFailInput), "unavailable");
// We don't extract from templated functions for now as templates are hard
// to deal with.
std::string TemplateFailInput = R"cpp(
template<typename T>
void f() {
[[int x;]]
}
)cpp";
EXPECT_EQ(apply(TemplateFailInput), "unavailable");
// FIXME: This should be extractable after selectionTree works correctly for
// macros (currently it doesn't select anything for the following case)
std::string MacroFailInput = R"cpp(
#define F(BODY) void f() { BODY }
F ([[int x = 0;]])
)cpp";
EXPECT_EQ(apply(MacroFailInput), "unavailable");
// Shouldn't crash.
EXPECT_EQ(apply("void f([[int a]]);"), "unavailable");
// Don't extract if we select the entire function body (CompoundStmt).
std::string CompoundFailInput = R"cpp(
void f() [[{
int a;
}]]
)cpp";
EXPECT_EQ(apply(CompoundFailInput), "unavailable");
}
TEST_F(ExtractFunctionTest, ControlFlow) {
Context = Function;
// We should be able to extract break/continue with a parent loop/switch.
EXPECT_THAT(apply(" [[for(;;) if(1) break;]] "), HasSubstr("extracted"));
EXPECT_THAT(apply(" for(;;) [[while(1) break;]] "), HasSubstr("extracted"));
EXPECT_THAT(apply(" [[switch(1) { break; }]]"), HasSubstr("extracted"));
EXPECT_THAT(apply(" [[while(1) switch(1) { continue; }]]"),
HasSubstr("extracted"));
// Don't extract break and continue without a loop/switch parent.
EXPECT_THAT(apply(" for(;;) [[if(1) continue;]] "), StartsWith("fail"));
EXPECT_THAT(apply(" while(1) [[if(1) break;]] "), StartsWith("fail"));
EXPECT_THAT(apply(" switch(1) { [[break;]] }"), StartsWith("fail"));
EXPECT_THAT(apply(" for(;;) { [[while(1) break; break;]] }"),
StartsWith("fail"));
}
TWEAK_TEST(RemoveUsingNamespace);
TEST_F(RemoveUsingNamespaceTest, All) {
std::pair<llvm::StringRef /*Input*/, llvm::StringRef /*Expected*/> Cases[] = {
{// Remove all occurrences of ns. Qualify only unqualified.
R"cpp(
namespace ns1 { struct vector {}; }
namespace ns2 { struct map {}; }
using namespace n^s1;
using namespace ns2;
using namespace ns1;
int main() {
ns1::vector v1;
vector v2;
map m1;
}
)cpp",
R"cpp(
namespace ns1 { struct vector {}; }
namespace ns2 { struct map {}; }
using namespace ns2;
int main() {
ns1::vector v1;
ns1::vector v2;
map m1;
}
)cpp"},
{// Ident to be qualified is a macro arg.
R"cpp(
#define DECLARE(x, y) x y
namespace ns { struct vector {}; }
using namespace n^s;
int main() {
DECLARE(ns::vector, v1);
DECLARE(vector, v2);
}
)cpp",
R"cpp(
#define DECLARE(x, y) x y
namespace ns { struct vector {}; }
int main() {
DECLARE(ns::vector, v1);
DECLARE(ns::vector, v2);
}
)cpp"},
{// Nested namespace: Fully qualify ident from inner ns.
R"cpp(
namespace aa { namespace bb { struct map {}; }}
using namespace aa::b^b;
int main() {
map m;
}
)cpp",
R"cpp(
namespace aa { namespace bb { struct map {}; }}
int main() {
aa::bb::map m;
}
)cpp"},
{// Nested namespace: Fully qualify ident from inner ns.
R"cpp(
namespace aa { namespace bb { struct map {}; }}
using namespace a^a;
int main() {
bb::map m;
}
)cpp",
R"cpp(
namespace aa { namespace bb { struct map {}; }}
int main() {
aa::bb::map m;
}
)cpp"},
{// Typedef.
R"cpp(
namespace aa { namespace bb { struct map {}; }}
using namespace a^a;
typedef bb::map map;
int main() { map M; }
)cpp",
R"cpp(
namespace aa { namespace bb { struct map {}; }}
typedef aa::bb::map map;
int main() { map M; }
)cpp"},
{// FIXME: Nested namespaces: Not aware of using ns decl of outer ns.
R"cpp(
namespace aa { namespace bb { struct map {}; }}
using name[[space aa::b]]b;
using namespace aa;
int main() {
map m;
}
)cpp",
R"cpp(
namespace aa { namespace bb { struct map {}; }}
using namespace aa;
int main() {
aa::bb::map m;
}
)cpp"},
{// Does not qualify ident from inner namespace.
R"cpp(
namespace aa { namespace bb { struct map {}; }}
using namespace aa::bb;
using namespace a^a;
int main() {
map m;
}
)cpp",
R"cpp(
namespace aa { namespace bb { struct map {}; }}
using namespace aa::bb;
int main() {
map m;
}
)cpp"},
{// Available only for top level namespace decl.
R"cpp(
namespace aa {
namespace bb { struct map {}; }
using namespace b^b;
}
int main() { aa::map m; }
)cpp",
"unavailable"},
{// FIXME: Unavailable for namespaces containing using-namespace decl.
R"cpp(
namespace aa {
namespace bb { struct map {}; }
using namespace bb;
}
using namespace a^a;
int main() {
map m;
}
)cpp",
"unavailable"},
{R"cpp(
namespace a::b { struct Foo {}; }
using namespace a;
using namespace a::[[b]];
using namespace b;
int main() { Foo F;}
)cpp",
R"cpp(
namespace a::b { struct Foo {}; }
using namespace a;
int main() { a::b::Foo F;}
)cpp"},
{R"cpp(
namespace a::b { struct Foo {}; }
using namespace a;
using namespace a::b;
using namespace [[b]];
int main() { Foo F;}
)cpp",
R"cpp(
namespace a::b { struct Foo {}; }
using namespace a;
int main() { b::Foo F;}
)cpp"},
{// Enumerators.
R"cpp(
namespace tokens {
enum Token {
comma, identifier, numeric
};
}
using namespace tok^ens;
int main() {
auto x = comma;
}
)cpp",
R"cpp(
namespace tokens {
enum Token {
comma, identifier, numeric
};
}
int main() {
auto x = tokens::comma;
}
)cpp"},
{// inline namespaces.
R"cpp(
namespace std { inline namespace ns1 { inline namespace ns2 { struct vector {}; }}}
using namespace st^d;
int main() {
vector V;
}
)cpp",
R"cpp(
namespace std { inline namespace ns1 { inline namespace ns2 { struct vector {}; }}}
int main() {
std::vector V;
}
)cpp"}};
for (auto C : Cases)
EXPECT_EQ(C.second, apply(C.first)) << C.first;
}
TWEAK_TEST(DefineInline);
TEST_F(DefineInlineTest, TriggersOnFunctionDecl) {
// Basic check for function body and signature.
EXPECT_AVAILABLE(R"cpp(
class Bar {
void baz();
};
[[void [[Bar::[[b^a^z]]]]() [[{
return;
}]]]]
void foo();
[[void [[f^o^o]]() [[{
return;
}]]]]
)cpp");
EXPECT_UNAVAILABLE(R"cpp(
// Not a definition
vo^i[[d^ ^f]]^oo();
[[vo^id ]]foo[[()]] {[[
[[(void)(5+3);
return;]]
}]]
)cpp");
}
TEST_F(DefineInlineTest, NoForwardDecl) {
Header = "void bar();";
EXPECT_UNAVAILABLE(R"cpp(
void bar() {
return;
}
// FIXME: Generate a decl in the header.
void fo^o() {
return;
})cpp");
}
TEST_F(DefineInlineTest, ReferencedDecls) {
EXPECT_AVAILABLE(R"cpp(
void bar();
void foo(int test);
void fo^o(int baz) {
int x = 10;
bar();
})cpp");
// Internal symbol usage.
Header = "void foo(int test);";
EXPECT_UNAVAILABLE(R"cpp(
void bar();
void fo^o(int baz) {
int x = 10;
bar();
})cpp");
// Becomes available after making symbol visible.
Header = "void bar();" + Header;
EXPECT_AVAILABLE(R"cpp(
void fo^o(int baz) {
int x = 10;
bar();
})cpp");
// FIXME: Move declaration below bar to make it visible.
Header.clear();
EXPECT_UNAVAILABLE(R"cpp(
void foo();
void bar();
void fo^o() {
bar();
})cpp");
// Order doesn't matter within a class.
EXPECT_AVAILABLE(R"cpp(
class Bar {
void foo();
void bar();
};
void Bar::fo^o() {
bar();
})cpp");
// FIXME: Perform include insertion to make symbol visible.
ExtraFiles["a.h"] = "void bar();";
Header = "void foo(int test);";
EXPECT_UNAVAILABLE(R"cpp(
#include "a.h"
void fo^o(int baz) {
int x = 10;
bar();
})cpp");
}
TEST_F(DefineInlineTest, TemplateSpec) {
EXPECT_UNAVAILABLE(R"cpp(
template <typename T> void foo();
template<> void foo<char>();
template<> void f^oo<int>() {
})cpp");
EXPECT_UNAVAILABLE(R"cpp(
template <typename T> void foo();
template<> void f^oo<int>() {
})cpp");
EXPECT_UNAVAILABLE(R"cpp(
template <typename T> struct Foo { void foo(); };
template <typename T> void Foo<T>::f^oo() {
})cpp");
EXPECT_AVAILABLE(R"cpp(
template <typename T> void foo();
void bar();
template <> void foo<int>();
template<> void f^oo<int>() {
bar();
})cpp");
}
TEST_F(DefineInlineTest, CheckForCanonDecl) {
EXPECT_UNAVAILABLE(R"cpp(
void foo();
void bar() {}
void f^oo() {
// This bar normally refers to the definition just above, but it is not
// visible from the forward declaration of foo.
bar();
})cpp");
// Make it available with a forward decl.
EXPECT_AVAILABLE(R"cpp(
void bar();
void foo();
void bar() {}
void f^oo() {
bar();
})cpp");
}
TEST_F(DefineInlineTest, UsingShadowDecls) {
// Template body is not parsed until instantiation time on windows, which
// results in arbitrary failures as function body becomes NULL.
ExtraArgs.push_back("-fno-delayed-template-parsing");
EXPECT_UNAVAILABLE(R"cpp(
namespace ns1 { void foo(int); }
namespace ns2 { void foo(int*); }
template <typename T>
void bar();
using ns1::foo;
using ns2::foo;
template <typename T>
void b^ar() {
foo(T());
})cpp");
}
TEST_F(DefineInlineTest, TransformNestedNamespaces) {
auto Test = R"cpp(
namespace a {
void bar();
namespace b {
void baz();
namespace c {
void aux();
}
}
}
void foo();
using namespace a;
using namespace b;
using namespace c;
void f^oo() {
bar();
a::bar();
baz();
b::baz();
a::b::baz();
aux();
c::aux();
b::c::aux();
a::b::c::aux();
})cpp";
auto Expected = R"cpp(
namespace a {
void bar();
namespace b {
void baz();
namespace c {
void aux();
}
}
}
void foo(){
a::bar();
a::bar();
a::b::baz();
a::b::baz();
a::b::baz();
a::b::c::aux();
a::b::c::aux();
a::b::c::aux();
a::b::c::aux();
}
using namespace a;
using namespace b;
using namespace c;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformUsings) {
auto Test = R"cpp(
namespace a { namespace b { namespace c { void aux(); } } }
void foo();
void f^oo() {
using namespace a;
using namespace b;
using namespace c;
using c::aux;
namespace d = c;
})cpp";
auto Expected = R"cpp(
namespace a { namespace b { namespace c { void aux(); } } }
void foo(){
using namespace a;
using namespace a::b;
using namespace a::b::c;
using a::b::c::aux;
namespace d = a::b::c;
}
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformDecls) {
auto Test = R"cpp(
void foo();
void f^oo() {
class Foo {
public:
void foo();
int x;
static int y;
};
Foo::y = 0;
enum En { Zero, One };
En x = Zero;
enum class EnClass { Zero, One };
EnClass y = EnClass::Zero;
})cpp";
auto Expected = R"cpp(
void foo(){
class Foo {
public:
void foo();
int x;
static int y;
};
Foo::y = 0;
enum En { Zero, One };
En x = Zero;
enum class EnClass { Zero, One };
EnClass y = EnClass::Zero;
}
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformTemplDecls) {
auto Test = R"cpp(
namespace a {
template <typename T> class Bar {
public:
void bar();
};
template <typename T> T bar;
template <typename T> void aux() {}
}
void foo();
using namespace a;
void f^oo() {
bar<Bar<int>>.bar();
aux<Bar<int>>();
})cpp";
auto Expected = R"cpp(
namespace a {
template <typename T> class Bar {
public:
void bar();
};
template <typename T> T bar;
template <typename T> void aux() {}
}
void foo(){
a::bar<a::Bar<int>>.bar();
a::aux<a::Bar<int>>();
}
using namespace a;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformMembers) {
auto Test = R"cpp(
class Foo {
void foo();
};
void Foo::f^oo() {
return;
})cpp";
auto Expected = R"cpp(
class Foo {
void foo(){
return;
}
};
)cpp";
EXPECT_EQ(apply(Test), Expected);
ExtraFiles["a.h"] = R"cpp(
class Foo {
void foo();
};)cpp";
llvm::StringMap<std::string> EditedFiles;
Test = R"cpp(
#include "a.h"
void Foo::f^oo() {
return;
})cpp";
Expected = R"cpp(
#include "a.h"
)cpp";
EXPECT_EQ(apply(Test, &EditedFiles), Expected);
Expected = R"cpp(
class Foo {
void foo(){
return;
}
};)cpp";
EXPECT_THAT(EditedFiles,
ElementsAre(FileWithContents(testPath("a.h"), Expected)));
}
TEST_F(DefineInlineTest, TransformDependentTypes) {
auto Test = R"cpp(
namespace a {
template <typename T> class Bar {};
}
template <typename T>
void foo();
using namespace a;
template <typename T>
void f^oo() {
Bar<T> B;
Bar<Bar<T>> q;
})cpp";
auto Expected = R"cpp(
namespace a {
template <typename T> class Bar {};
}
template <typename T>
void foo(){
a::Bar<T> B;
a::Bar<a::Bar<T>> q;
}
using namespace a;
)cpp";
// Template body is not parsed until instantiation time on windows, which
// results in arbitrary failures as function body becomes NULL.
ExtraArgs.push_back("-fno-delayed-template-parsing");
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformFunctionTempls) {
// Check we select correct specialization decl.
std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
{R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p);
template <>
void foo<char>(char p);
template <>
void fo^o<int>(int p) {
return;
})cpp",
R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p){
return;
}
template <>
void foo<char>(char p);
)cpp"},
{// Make sure we are not selecting the first specialization all the time.
R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p);
template <>
void foo<char>(char p);
template <>
void fo^o<char>(char p) {
return;
})cpp",
R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p);
template <>
void foo<char>(char p){
return;
}
)cpp"},
{R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p);
template <typename T>
void fo^o(T p) {
return;
})cpp",
R"cpp(
template <typename T>
void foo(T p){
return;
}
template <>
void foo<int>(int p);
)cpp"},
};
// Template body is not parsed until instantiation time on windows, which
// results in arbitrary failures as function body becomes NULL.
ExtraArgs.push_back("-fno-delayed-template-parsing");
for(const auto &Case : Cases)
EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
}
TEST_F(DefineInlineTest, TransformTypeLocs) {
auto Test = R"cpp(
namespace a {
template <typename T> class Bar {
public:
template <typename Q> class Baz {};
};
class Foo{};
}
void foo();
using namespace a;
void f^oo() {
Bar<int> B;
Foo foo;
a::Bar<Bar<int>>::Baz<Bar<int>> q;
})cpp";
auto Expected = R"cpp(
namespace a {
template <typename T> class Bar {
public:
template <typename Q> class Baz {};
};
class Foo{};
}
void foo(){
a::Bar<int> B;
a::Foo foo;
a::Bar<a::Bar<int>>::Baz<a::Bar<int>> q;
}
using namespace a;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformDeclRefs) {
auto Test =R"cpp(
namespace a {
template <typename T> class Bar {
public:
void foo();
static void bar();
int x;
static int y;
};
void bar();
void test();
}
void foo();
using namespace a;
void f^oo() {
a::Bar<int> B;
B.foo();
a::bar();
Bar<Bar<int>>::bar();
a::Bar<int>::bar();
B.x = Bar<int>::y;
Bar<int>::y = 3;
bar();
a::test();
})cpp";
auto Expected = R"cpp(
namespace a {
template <typename T> class Bar {
public:
void foo();
static void bar();
int x;
static int y;
};
void bar();
void test();
}
void foo(){
a::Bar<int> B;
B.foo();
a::bar();
a::Bar<a::Bar<int>>::bar();
a::Bar<int>::bar();
B.x = a::Bar<int>::y;
a::Bar<int>::y = 3;
a::bar();
a::test();
}
using namespace a;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, StaticMembers) {
auto Test = R"cpp(
namespace ns { class X { static void foo(); void bar(); }; }
void ns::X::b^ar() {
foo();
})cpp";
auto Expected = R"cpp(
namespace ns { class X { static void foo(); void bar(){
foo();
} }; }
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformInlineNamespaces) {
auto Test = R"cpp(
namespace a { inline namespace b { namespace { struct Foo{}; } } }
void foo();
using namespace a;
void ^foo() {Foo foo;})cpp";
auto Expected = R"cpp(
namespace a { inline namespace b { namespace { struct Foo{}; } } }
void foo(){a::Foo foo;}
using namespace a;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TokensBeforeSemicolon) {
std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
{R"cpp(
void foo() /*Comment -_-*/ /*Com 2*/ ;
void fo^o() { return ; })cpp",
R"cpp(
void foo() /*Comment -_-*/ /*Com 2*/ { return ; }
)cpp"},
{R"cpp(
void foo();
void fo^o() { return ; })cpp",
R"cpp(
void foo(){ return ; }
)cpp"},
{R"cpp(
#define SEMI ;
void foo() SEMI
void fo^o() { return ; })cpp",
"fail: Couldn't find semicolon for target declaration."},
};
for(const auto& Case: Cases)
EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
}
TEST_F(DefineInlineTest, HandleMacros) {
EXPECT_UNAVAILABLE(R"cpp(
#define BODY { return; }
void foo();
void f^oo()BODY)cpp");
EXPECT_UNAVAILABLE(R"cpp(
#define BODY void foo(){ return; }
void foo();
[[BODY]])cpp");
std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
// We don't qualify declarations coming from macros.
{R"cpp(
#define BODY Foo
namespace a { class Foo{}; }
void foo();
using namespace a;
void f^oo(){BODY})cpp",
R"cpp(
#define BODY Foo
namespace a { class Foo{}; }
void foo(){BODY}
using namespace a;
)cpp"},
// Macro is not visible at declaration location, but we proceed.
{R"cpp(
void foo();
#define BODY return;
void f^oo(){BODY})cpp",
R"cpp(
void foo(){BODY}
#define BODY return;
)cpp"},
{R"cpp(
#define TARGET void foo()
TARGET;
void f^oo(){ return; })cpp",
R"cpp(
#define TARGET void foo()
TARGET{ return; }
)cpp"},
{R"cpp(
#define TARGET foo
void TARGET();
void f^oo(){ return; })cpp",
R"cpp(
#define TARGET foo
void TARGET(){ return; }
)cpp"},
};
for(const auto& Case: Cases)
EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
}
} // namespace
} // namespace clangd
} // namespace clang
|