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
|
//===-- ExtractFunctionTests.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 "TweakTesting.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::HasSubstr;
using ::testing::StartsWith;
namespace clang {
namespace clangd {
namespace {
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");
// FIXME: Support hoisting.
EXPECT_THAT(apply(" [[int a = 5;]] a++; "), "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
// initializer/condition.
EXPECT_THAT(apply(" for([[int i = 0;]];);"), HasSubstr("extracted"));
// Extract certain return
EXPECT_THAT(apply(" if(true) [[{ return; }]] "), HasSubstr("extracted"));
// Don't extract uncertain return
EXPECT_THAT(apply(" if(true) [[if (false) return;]] "),
StartsWith("unavailable"));
EXPECT_THAT(
apply("#define RETURN_IF_ERROR(x) if (x) return\nRETU^RN_IF_ERROR(4);"),
StartsWith("unavailable"));
FileName = "a.c";
EXPECT_THAT(apply(" for([[int i = 0;]];);"), HasSubstr("unavailable"));
}
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);
// Check const qualifier with namespace
std::string ConstNamespaceCheckInput = R"cpp(
namespace X { struct Y { int z; }; }
int f(const X::Y &y) {
[[return y.z + y.z;]]
})cpp";
std::string ConstNamespaceCheckOutput = R"cpp(
namespace X { struct Y { int z; }; }
int extracted(const X::Y &y) {
return y.z + y.z;
}
int f(const X::Y &y) {
return extracted(y);
})cpp";
EXPECT_EQ(apply(ConstNamespaceCheckInput), ConstNamespaceCheckOutput);
// Don't extract when we need to make a function as a parameter.
EXPECT_THAT(apply("void f() { [[int a; f();]] }"), StartsWith("fail"));
std::string MethodInput = R"cpp(
class T {
void f() {
[[int x;]]
}
};
)cpp";
std::string MethodCheckOutput = R"cpp(
class T {
void extracted() {
int x;
}
void f() {
extracted();
}
};
)cpp";
EXPECT_EQ(apply(MethodInput), MethodCheckOutput);
std::string OutOfLineMethodInput = R"cpp(
class T {
void f();
};
void T::f() {
[[int x;]]
}
)cpp";
std::string OutOfLineMethodCheckOutput = R"cpp(
class T {
void extracted();
void f();
};
void T::extracted() {
int x;
}
void T::f() {
extracted();
}
)cpp";
EXPECT_EQ(apply(OutOfLineMethodInput), OutOfLineMethodCheckOutput);
// 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");
std::string MacroInput = R"cpp(
#define F(BODY) void f() { BODY }
F ([[int x = 0;]])
)cpp";
std::string MacroOutput = R"cpp(
#define F(BODY) void f() { BODY }
void extracted() {
int x = 0;
}
F (extracted();)
)cpp";
EXPECT_EQ(apply(MacroInput), MacroOutput);
// Shouldn't crash.
EXPECT_EQ(apply("void f([[int a]]);"), "unavailable");
EXPECT_EQ(apply("void f(int a = [[1]]);"), "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");
ExtraArgs.push_back("-std=c++14");
// FIXME: Expressions are currently not extracted
EXPECT_EQ(apply(R"cpp(
void call() { [[1+1]]; }
)cpp"),
"unavailable");
// FIXME: Single expression statements are currently not extracted
EXPECT_EQ(apply(R"cpp(
void call() { [[1+1;]] }
)cpp"),
"unavailable");
}
TEST_F(ExtractFunctionTest, DifferentHeaderSourceTest) {
Header = R"cpp(
class SomeClass {
void f();
};
)cpp";
std::string OutOfLineSource = R"cpp(
void SomeClass::f() {
[[int x;]]
}
)cpp";
std::string OutOfLineSourceOutputCheck = R"cpp(
void SomeClass::extracted() {
int x;
}
void SomeClass::f() {
extracted();
}
)cpp";
std::string HeaderOutputCheck = R"cpp(
class SomeClass {
void extracted();
void f();
};
)cpp";
llvm::StringMap<std::string> EditedFiles;
EXPECT_EQ(apply(OutOfLineSource, &EditedFiles), OutOfLineSourceOutputCheck);
EXPECT_EQ(EditedFiles.begin()->second, HeaderOutputCheck);
}
TEST_F(ExtractFunctionTest, DifferentFilesNestedTest) {
Header = R"cpp(
class T {
class SomeClass {
void f();
};
};
)cpp";
std::string NestedOutOfLineSource = R"cpp(
void T::SomeClass::f() {
[[int x;]]
}
)cpp";
std::string NestedOutOfLineSourceOutputCheck = R"cpp(
void T::SomeClass::extracted() {
int x;
}
void T::SomeClass::f() {
extracted();
}
)cpp";
std::string NestedHeaderOutputCheck = R"cpp(
class T {
class SomeClass {
void extracted();
void f();
};
};
)cpp";
llvm::StringMap<std::string> EditedFiles;
EXPECT_EQ(apply(NestedOutOfLineSource, &EditedFiles),
NestedOutOfLineSourceOutputCheck);
EXPECT_EQ(EditedFiles.begin()->second, NestedHeaderOutputCheck);
}
TEST_F(ExtractFunctionTest, ConstexprDifferentHeaderSourceTest) {
Header = R"cpp(
class SomeClass {
constexpr void f() const;
};
)cpp";
std::string OutOfLineSource = R"cpp(
constexpr void SomeClass::f() const {
[[int x;]]
}
)cpp";
std::string OutOfLineSourceOutputCheck = R"cpp(
constexpr void SomeClass::extracted() const {
int x;
}
constexpr void SomeClass::f() const {
extracted();
}
)cpp";
std::string HeaderOutputCheck = R"cpp(
class SomeClass {
constexpr void extracted() const;
constexpr void f() const;
};
)cpp";
llvm::StringMap<std::string> EditedFiles;
EXPECT_EQ(apply(OutOfLineSource, &EditedFiles), OutOfLineSourceOutputCheck);
EXPECT_NE(EditedFiles.begin(), EditedFiles.end())
<< "The header should be edited and receives the declaration of the new "
"function";
if (EditedFiles.begin() != EditedFiles.end()) {
EXPECT_EQ(EditedFiles.begin()->second, HeaderOutputCheck);
}
}
TEST_F(ExtractFunctionTest, ConstevalDifferentHeaderSourceTest) {
ExtraArgs.push_back("--std=c++20");
Header = R"cpp(
class SomeClass {
consteval void f() const;
};
)cpp";
std::string OutOfLineSource = R"cpp(
consteval void SomeClass::f() const {
[[int x;]]
}
)cpp";
std::string OutOfLineSourceOutputCheck = R"cpp(
consteval void SomeClass::extracted() const {
int x;
}
consteval void SomeClass::f() const {
extracted();
}
)cpp";
std::string HeaderOutputCheck = R"cpp(
class SomeClass {
consteval void extracted() const;
consteval void f() const;
};
)cpp";
llvm::StringMap<std::string> EditedFiles;
EXPECT_EQ(apply(OutOfLineSource, &EditedFiles), OutOfLineSourceOutputCheck);
EXPECT_NE(EditedFiles.begin(), EditedFiles.end())
<< "The header should be edited and receives the declaration of the new "
"function";
if (EditedFiles.begin() != EditedFiles.end()) {
EXPECT_EQ(EditedFiles.begin()->second, HeaderOutputCheck);
}
}
TEST_F(ExtractFunctionTest, ConstDifferentHeaderSourceTest) {
Header = R"cpp(
class SomeClass {
void f() const;
};
)cpp";
std::string OutOfLineSource = R"cpp(
void SomeClass::f() const {
[[int x;]]
}
)cpp";
std::string OutOfLineSourceOutputCheck = R"cpp(
void SomeClass::extracted() const {
int x;
}
void SomeClass::f() const {
extracted();
}
)cpp";
std::string HeaderOutputCheck = R"cpp(
class SomeClass {
void extracted() const;
void f() const;
};
)cpp";
llvm::StringMap<std::string> EditedFiles;
EXPECT_EQ(apply(OutOfLineSource, &EditedFiles), OutOfLineSourceOutputCheck);
EXPECT_NE(EditedFiles.begin(), EditedFiles.end())
<< "The header should be edited and receives the declaration of the new "
"function";
if (EditedFiles.begin() != EditedFiles.end()) {
EXPECT_EQ(EditedFiles.begin()->second, HeaderOutputCheck);
}
}
TEST_F(ExtractFunctionTest, StaticDifferentHeaderSourceTest) {
Header = R"cpp(
class SomeClass {
static void f();
};
)cpp";
std::string OutOfLineSource = R"cpp(
void SomeClass::f() {
[[int x;]]
}
)cpp";
std::string OutOfLineSourceOutputCheck = R"cpp(
void SomeClass::extracted() {
int x;
}
void SomeClass::f() {
extracted();
}
)cpp";
std::string HeaderOutputCheck = R"cpp(
class SomeClass {
static void extracted();
static void f();
};
)cpp";
llvm::StringMap<std::string> EditedFiles;
EXPECT_EQ(apply(OutOfLineSource, &EditedFiles), OutOfLineSourceOutputCheck);
EXPECT_NE(EditedFiles.begin(), EditedFiles.end())
<< "The header should be edited and receives the declaration of the new "
"function";
if (EditedFiles.begin() != EditedFiles.end()) {
EXPECT_EQ(EditedFiles.begin()->second, HeaderOutputCheck);
}
}
TEST_F(ExtractFunctionTest, DifferentContextHeaderSourceTest) {
Header = R"cpp(
namespace ns{
class A {
class C {
public:
class RType {};
};
class T {
class SomeClass {
static C::RType f();
};
};
};
} // ns
)cpp";
std::string OutOfLineSource = R"cpp(
ns::A::C::RType ns::A::T::SomeClass::f() {
[[A::C::RType x;
return x;]]
}
)cpp";
std::string OutOfLineSourceOutputCheck = R"cpp(
ns::A::C::RType ns::A::T::SomeClass::extracted() {
A::C::RType x;
return x;
}
ns::A::C::RType ns::A::T::SomeClass::f() {
return extracted();
}
)cpp";
std::string HeaderOutputCheck = R"cpp(
namespace ns{
class A {
class C {
public:
class RType {};
};
class T {
class SomeClass {
static ns::A::C::RType extracted();
static C::RType f();
};
};
};
} // ns
)cpp";
llvm::StringMap<std::string> EditedFiles;
EXPECT_EQ(apply(OutOfLineSource, &EditedFiles), OutOfLineSourceOutputCheck);
EXPECT_EQ(EditedFiles.begin()->second, HeaderOutputCheck);
}
TEST_F(ExtractFunctionTest, DifferentSyntacticContextNamespace) {
std::string OutOfLineSource = R"cpp(
namespace ns {
void f();
}
void ns::f() {
[[int x;]]
}
)cpp";
std::string OutOfLineSourceOutputCheck = R"cpp(
namespace ns {
void extracted();
void f();
}
void ns::extracted() {
int x;
}
void ns::f() {
extracted();
}
)cpp";
EXPECT_EQ(apply(OutOfLineSource), OutOfLineSourceOutputCheck);
}
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"));
}
TEST_F(ExtractFunctionTest, ExistingReturnStatement) {
Context = File;
const char *Before = R"cpp(
bool lucky(int N);
int getNum(bool Superstitious, int Min, int Max) {
if (Superstitious) [[{
for (int I = Min; I <= Max; ++I)
if (lucky(I))
return I;
return -1;
}]] else {
return (Min + Max) / 2;
}
}
)cpp";
// FIXME: min/max should be by value.
// FIXME: avoid emitting redundant braces
const char *After = R"cpp(
bool lucky(int N);
int extracted(int &Min, int &Max) {
{
for (int I = Min; I <= Max; ++I)
if (lucky(I))
return I;
return -1;
}
}
int getNum(bool Superstitious, int Min, int Max) {
if (Superstitious) return extracted(Min, Max); else {
return (Min + Max) / 2;
}
}
)cpp";
EXPECT_EQ(apply(Before), After);
}
TEST_F(ExtractFunctionTest, OverloadedOperators) {
Context = File;
std::string Before = R"cpp(struct A {
int operator+(int x) { return x; }
};
A &operator<<(A &, int);
A &operator|(A &, int);
A stream{};
void foo(int, int);
int main() {
[[foo(1, 2);
foo(3, 4);
stream << 42;
stream + 42;
stream | 42;
foo(1, 2);
foo(3, 4);]]
})cpp";
std::string After =
R"cpp(struct A {
int operator+(int x) { return x; }
};
A &operator<<(A &, int);
A &operator|(A &, int);
A stream{};
void foo(int, int);
void extracted() {
foo(1, 2);
foo(3, 4);
stream << 42;
stream + 42;
stream | 42;
foo(1, 2);
foo(3, 4);
}
int main() {
extracted();
})cpp";
EXPECT_EQ(apply(Before), After);
}
} // namespace
} // namespace clangd
} // namespace clang
|