Skip to content

Commit 31444e8

Browse files
author
IgnatSergeev
committed
Add test support and location option tests
1 parent f4bcf55 commit 31444e8

File tree

6 files changed

+348
-31
lines changed

6 files changed

+348
-31
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: rm -f %t.cp.cpp
2+
// RUN: cp %s %t.cp.cpp
3+
// RUN: clang-refactor simplify-expr -location=%t.cp.cpp:5:18 -v %t.cp.cpp -- | FileCheck --check-prefix=CHECK1 %s
4+
5+
bool test = true && true;
6+
7+
// CHECK1: invoking action 'simplify-expr':
8+
// CHECK1-NEXT: -location={{.*}}.cp.cpp:5:18
9+
10+
// RUN: not clang-refactor simplify-expr -location=%s:5:18 -v %t.cp.cpp -- 2>&1 | FileCheck --check-prefix=CHECK-FILE-ERR %s
11+
// CHECK-FILE-ERR: given file is not in the target TU
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: clang-refactor simplify-expr -location=test:%s -v %s -- | FileCheck %s
2+
3+
/*loc =*/int test;
4+
5+
/*loc named=*/int test2;
6+
7+
/*loc = +1*/int test3;
8+
9+
/* loc = +100 */int test4;
10+
11+
/*loc named =+0*/int test5;
12+
13+
// CHECK: Test location group '':
14+
// CHECK-NEXT: 89
15+
// CHECK-NEXT: 139
16+
// CHECK-NEXT: 176
17+
// CHECK-NEXT: Test location group 'named':
18+
// CHECK-NEXT: 114
19+
// CHECK-NEXT: 195
20+
21+
// The following invocations are in the default group:
22+
23+
// CHECK: invoking action 'simplify-expr':
24+
// CHECK-NEXT: -location={{.*}}tool-test-support-location.c:3:10
25+
26+
// CHECK: invoking action 'simplify-expr':
27+
// CHECK-NEXT: -location={{.*}}tool-test-support-location.c:7:14
28+
29+
// CHECK: invoking action 'simplify-expr':
30+
// CHECK-NEXT: -location={{.*}}tool-test-support-location.c:9:27
31+
32+
// The following invocations are in the 'named' group, and they follow
33+
// the default invocation even if some of their locations occur prior to the
34+
// locations from the default group because the groups are tested one-by-one:
35+
36+
// CHECK: invoking action 'simplify-expr':
37+
// CHECK-NEXT: -location={{.*}}tool-test-support-location.c:5:15
38+
39+
// CHECK: invoking action 'simplify-expr':
40+
// CHECK-NEXT: -location={{.*}}tool-test-support-location.c:11:18

clang/test/Refactor/tool-test-support.c renamed to clang/test/Refactor/tool-test-support-range.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@
2424
// The following invocations are in the default group:
2525

2626
// CHECK: invoking action 'local-rename':
27-
// CHECK-NEXT: -selection={{.*}}tool-test-support.c:3:11
27+
// CHECK-NEXT: -selection={{.*}}tool-test-support-range.c:3:11
2828

2929
// CHECK: invoking action 'local-rename':
30-
// CHECK-NEXT: -selection={{.*}}tool-test-support.c:7:15
30+
// CHECK-NEXT: -selection={{.*}}tool-test-support-range.c:7:15
3131

3232
// CHECK: invoking action 'local-rename':
33-
// CHECK-NEXT: -selection={{.*}}tool-test-support.c:9:29
33+
// CHECK-NEXT: -selection={{.*}}tool-test-support-range.c:9:29
3434

3535
// CHECK: invoking action 'local-rename':
36-
// CHECK-NEXT: -selection={{.*}}tool-test-support.c:13:19 -> {{.*}}tool-test-support.c:13:22
36+
// CHECK-NEXT: -selection={{.*}}tool-test-support-range.c:13:19 -> {{.*}}tool-test-support-range.c:13:22
3737

3838
// The following invocations are in the 'named' group, and they follow
3939
// the default invocation even if some of their ranges occur prior to the
4040
// ranges from the default group because the groups are tested one-by-one:
4141

4242
// CHECK: invoking action 'local-rename':
43-
// CHECK-NEXT: -selection={{.*}}tool-test-support.c:5:17
43+
// CHECK-NEXT: -selection={{.*}}tool-test-support-range.c:5:17
4444

4545
// CHECK: invoking action 'local-rename':
46-
// CHECK-NEXT: -selection={{.*}}tool-test-support.c:11:20
46+
// CHECK-NEXT: -selection={{.*}}tool-test-support-range.c:11:20

clang/tools/clang-refactor/ClangRefactor.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@ class AbstractSourceLocationArgument {
200200
llvm::function_ref<void(SourceLocation L)> Callback) = 0;
201201
};
202202

203+
/// Stores the parsed -location=test:<filename> option.
204+
class TestSourceLocationArgument final : public AbstractSourceLocationArgument {
205+
public:
206+
TestSourceLocationArgument(TestLocationsInFile TestLocations)
207+
: TestLocations(std::move(TestLocations)) {}
208+
209+
void print(raw_ostream &OS) override { TestLocations.dump(OS); }
210+
211+
std::unique_ptr<ClangRefactorToolConsumerInterface>
212+
createCustomConsumer() override {
213+
return TestLocations.createConsumer();
214+
}
215+
216+
/// Testing support: invokes the location action for each source location in
217+
/// the test file.
218+
bool forAllLocations(
219+
const SourceManager &SM,
220+
llvm::function_ref<void(SourceLocation L)> Callback) override {
221+
return TestLocations.foreachLocation(SM, Callback);
222+
}
223+
224+
private:
225+
TestLocationsInFile TestLocations;
226+
};
227+
203228
/// Stores the parsed -location=filename:line:column option.
204229
class SourceLocationArgument final : public AbstractSourceLocationArgument {
205230
public:
@@ -235,6 +260,15 @@ class SourceLocationArgument final : public AbstractSourceLocationArgument {
235260

236261
std::unique_ptr<AbstractSourceLocationArgument>
237262
AbstractSourceLocationArgument::fromString(StringRef Value) {
263+
if (Value.starts_with("test:")) {
264+
StringRef Filename = Value.drop_front(strlen("test:"));
265+
std::optional<TestLocationsInFile> ParsedTestLocations =
266+
findTestLocations(Filename);
267+
if (!ParsedTestLocations)
268+
return nullptr; // A parsing error was already reported.
269+
return std::make_unique<TestSourceLocationArgument>(
270+
std::move(*ParsedTestLocations));
271+
}
238272
ParsedSourceLocation Location = ParsedSourceLocation::FromString(Value);
239273
if (Location.FileName != "")
240274
return std::make_unique<SourceLocationArgument>(std::move(Location));
@@ -509,8 +543,11 @@ class ClangRefactorTool {
509543
std::unique_ptr<ClangRefactorToolConsumerInterface> TestConsumer;
510544
bool HasSelection = MatchingRule->hasSelectionRequirement();
511545
bool HasLocation = MatchingRule->hasLocationRequirement();
512-
if (HasSelection)
546+
if (HasSelection) {
513547
TestConsumer = SelectedSubcommand->getSelection()->createCustomConsumer();
548+
} else if (HasLocation) {
549+
TestConsumer = SelectedSubcommand->getLocation()->createCustomConsumer();
550+
}
514551
ClangRefactorToolConsumerInterface *ActiveConsumer =
515552
TestConsumer ? TestConsumer.get() : Consumer.get();
516553
ActiveConsumer->beginTU(AST);

0 commit comments

Comments
 (0)