forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswift-refactor.cpp
380 lines (334 loc) · 14.9 KB
/
swift-refactor.cpp
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
//===--- swift-refactor.cpp - Test driver for local refactoring --*- C++ -*-==//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/LLVMInitialize.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/IDE/Refactoring.h"
#include "swift/IDE/Utils.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include <regex>
using namespace swift;
using namespace swift::ide;
namespace options {
static llvm::cl::opt<RefactoringKind>
Action(llvm::cl::desc("kind:"), llvm::cl::init(RefactoringKind::None),
llvm::cl::values(
clEnumValN(RefactoringKind::LocalRename,
"rename", "Perform rename refactoring"),
clEnumValN(RefactoringKind::ExtractExpr,
"extract-expr", "Perform extract expression refactoring"),
clEnumValN(RefactoringKind::ExtractRepeatedExpr,
"extract-repeat", "Perform extract repeated expression refactoring"),
clEnumValN(RefactoringKind::FillProtocolStub,
"fill-stub", "Perform fill protocol stub refactoring"),
clEnumValN(RefactoringKind::ExpandDefault,
"expand-default", "Perform expand default statement refactoring"),
clEnumValN(RefactoringKind::ExpandSwitchCases,
"expand-switch-cases", "Perform switch cases expand refactoring"),
clEnumValN(RefactoringKind::LocalizeString,
"localize-string", "Perform string localization refactoring"),
clEnumValN(RefactoringKind::CollapseNestedIfStmt,
"collapse-nested-if", "Perform collapse nested if statements"),
clEnumValN(RefactoringKind::ConvertToDoCatch,
"convert-to-do-catch", "Perform force try to do try catch refactoring"),
clEnumValN(RefactoringKind::SimplifyNumberLiteral,
"simplify-long-number", "Perform simplify long number literal refactoring"),
clEnumValN(RefactoringKind::ConvertStringsConcatenationToInterpolation,
"strings-concatenation-to-interpolation", "Perform strings concatenation to interpolation refactoring"),
clEnumValN(RefactoringKind::ExpandTernaryExpr,
"expand-ternary-expr", "Perform expand ternary expression"),
clEnumValN(RefactoringKind::ConvertToTernaryExpr,
"convert-to-ternary-expr", "Perform convert to ternary expression"),
clEnumValN(RefactoringKind::ConvertIfLetExprToGuardExpr,
"convert-to-guard", "Perform convert to guard expression"),
clEnumValN(RefactoringKind::ConvertGuardExprToIfLetExpr,
"convert-to-iflet", "Perform convert to iflet expression"),
clEnumValN(RefactoringKind::ExtractFunction,
"extract-function", "Perform extract function refactoring"),
clEnumValN(RefactoringKind::MoveMembersToExtension,
"move-to-extension", "Move selected members to an extension"),
clEnumValN(RefactoringKind::GlobalRename,
"syntactic-rename", "Perform syntactic rename"),
clEnumValN(RefactoringKind::FindGlobalRenameRanges,
"find-rename-ranges", "Find detailed ranges for syntactic rename"),
clEnumValN(RefactoringKind::FindLocalRenameRanges,
"find-local-rename-ranges", "Find detailed ranges for local rename"),
clEnumValN(RefactoringKind::TrailingClosure,
"trailingclosure", "Perform trailing closure refactoring"),
clEnumValN(RefactoringKind::ReplaceBodiesWithFatalError,
"replace-bodies-with-fatalError", "Perform trailing closure refactoring"),
clEnumValN(RefactoringKind::MemberwiseInitLocalRefactoring, "memberwise-init", "Generate member wise initializer")));
static llvm::cl::opt<std::string>
ModuleName("module-name", llvm::cl::desc("The module name of the given test."),
llvm::cl::init("swift_refactor"));
static llvm::cl::opt<std::string>
SourceFilename("source-filename", llvm::cl::desc("Name of the source file"));
static llvm::cl::list<std::string>
InputFilenames(llvm::cl::Positional, llvm::cl::desc("[input files...]"),
llvm::cl::ZeroOrMore);
static llvm::cl::opt<std::string>
LineColumnPair("pos", llvm::cl::desc("Line:Column pair or /*label*/"));
static llvm::cl::opt<std::string>
EndLineColumnPair("end-pos", llvm::cl::desc("Line:Column pair or /*label*/"));
static llvm::cl::opt<std::string>
NewName("new-name", llvm::cl::desc("A given name to rename to"),
llvm::cl::init("new_name"));
static llvm::cl::opt<std::string>
OldName("old-name", llvm::cl::desc("The expected existing name"),
llvm::cl::init("old_name"));
static llvm::cl::opt<bool>
IsFunctionLike("is-function-like", llvm::cl::desc("The symbol being renamed is function-like"),
llvm::cl::ValueDisallowed);
static llvm::cl::opt<bool>
IsNonProtocolType("is-non-protocol-type",
llvm::cl::desc("The symbol being renamed is a type and not a protocol"));
static llvm::cl::opt<bool>
DumpInJason("dump-json",
llvm::cl::desc("Whether to dump refactoring edits in Json"),
llvm::cl::init(false));
static llvm::cl::opt<bool>
AvailableActions("actions",
llvm::cl::desc("Dump the available refactoring kind for a given range"),
llvm::cl::init(false));
}
bool doesFileExist(StringRef Path) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
llvm::MemoryBuffer::getFile(Path);
if(FileBufOrErr)
return true;
return false;
}
struct RefactorLoc {
unsigned Line;
unsigned Column;
NameUsage Usage;
};
NameUsage convertToNameUsage(StringRef RoleString) {
if (RoleString == "unknown")
return NameUsage::Unknown;
if (RoleString == "def")
return NameUsage::Definition;
if (RoleString == "ref")
return NameUsage::Reference;
if (RoleString == "call")
return NameUsage::Call;
llvm_unreachable("unhandled role string");
}
std::vector<RefactorLoc> getLocsByLabelOrPosition(StringRef LabelOrLineCol,
std::string &Buffer) {
std::vector<RefactorLoc> LocResults;
if (LabelOrLineCol.empty())
return LocResults;
if (LabelOrLineCol.contains(':')) {
auto LineCol = parseLineCol(LabelOrLineCol);
if (LineCol.hasValue()) {
LocResults.push_back({LineCol.getValue().first,LineCol.getValue().second,
NameUsage::Unknown});
} else {
llvm::errs() << "cannot parse position pair.";
}
return LocResults;
}
std::smatch Matches;
// Intended to match comments like below where the "+offset" and ":usage"
// are defaulted to 0 and ref respectively
// /*name+offset:usage*/
const std::regex LabelRegex("/\\*([^ *:+]+)(?:\\+(\\d+))?(?:\\:([^ *]+))?\\*/|\\n");
std::string::const_iterator SearchStart(Buffer.cbegin());
unsigned Line = 1;
unsigned Column = 1;
while (std::regex_search(SearchStart, Buffer.cend(), Matches, LabelRegex)) {
auto EndOffset = Matches.position(0) + Matches.length(0);
SWIFT_DEFER { SearchStart += EndOffset; };
if (!Matches[1].matched) {
++Line;
Column = 1;
continue;
}
Column += EndOffset;
if (LabelOrLineCol == Matches[1].str()) {
unsigned ColumnOffset = 0;
if (Matches[2].length() > 0 && !llvm::to_integer(Matches[2].str(), ColumnOffset))
continue; // bad column offset
auto Usage = NameUsage::Reference;
if (Matches[3].length() > 0)
Usage = convertToNameUsage(Matches[3].str());
LocResults.push_back({Line, Column + ColumnOffset, Usage});
}
}
return LocResults;
}
std::vector<RenameLoc> getRenameLocs(unsigned BufferID, SourceManager &SM,
ArrayRef<RefactorLoc> Locs,
StringRef OldName, StringRef NewName,
bool IsFunctionLike,
bool IsNonProtocolType) {
std::vector<RenameLoc> Renames;
std::transform(Locs.begin(), Locs.end(), std::back_inserter(Renames), [&](const RefactorLoc &Loc) -> RenameLoc {
return {Loc.Line, Loc.Column, Loc.Usage, OldName, NewName, IsFunctionLike,
IsNonProtocolType};
});
return Renames;
}
RangeConfig getRange(unsigned BufferID, SourceManager &SM,
RefactorLoc Start,
RefactorLoc End) {
RangeConfig Range;
SourceLoc EndLoc = SM.getLocForLineCol(BufferID, End.Line,
End.Column);
Range.BufferId = BufferID;
Range.Line = Start.Line;
Range.Column = Start.Column;
Range.Length = SM.getByteDistance(Range.getStart(SM), EndLoc);
assert(Range.getEnd(SM) == EndLoc);
return Range;
}
// This function isn't referenced outside its translation unit, but it
// can't use the "static" keyword because its address is used for
// getMainExecutable (since some platforms don't support taking the
// address of main, and some platforms can't implement getMainExecutable
// without being given the address of a function in the main executable).
void anchorForGetMainExecutable() {}
int main(int argc, char *argv[]) {
PROGRAM_START(argc, argv);
INITIALIZE_LLVM();
llvm::cl::ParseCommandLineOptions(argc, argv, "Swift refactor\n");
if (options::SourceFilename.empty()) {
llvm::errs() << "cannot find source filename\n";
return 1;
}
if (!doesFileExist(options::SourceFilename)) {
llvm::errs() << "cannot open source file.\n";
return 1;
}
CompilerInvocation Invocation;
Invocation.setMainExecutablePath(
llvm::sys::fs::getMainExecutable(argv[0],
reinterpret_cast<void *>(&anchorForGetMainExecutable)));
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(
options::SourceFilename);
Invocation.getLangOptions().AttachCommentsToDecls = true;
Invocation.getLangOptions().CollectParsedToken = true;
Invocation.getLangOptions().BuildSyntaxTree = true;
for (auto FileName : options::InputFilenames)
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(FileName);
Invocation.setModuleName(options::ModuleName);
CompilerInstance CI;
// Display diagnostics to stderr.
PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags);
if (CI.setup(Invocation))
return 1;
registerIDERequestFunctions(CI.getASTContext().evaluator);
switch (options::Action) {
case RefactoringKind::GlobalRename:
case RefactoringKind::FindGlobalRenameRanges:
CI.performParseOnly(/*EvaluateConditionals*/true);
break;
default:
CI.performSema();
}
SourceFile *SF = nullptr;
for (auto Unit : CI.getMainModule()->getFiles()) {
if (auto Current = dyn_cast<SourceFile>(Unit)) {
if (Current->getFilename() == options::SourceFilename)
SF = Current;
}
if (SF)
break;
}
assert(SF && "no source file?");
SourceManager &SM = SF->getASTContext().SourceMgr;
unsigned BufferID = SF->getBufferID().getValue();
std::string Buffer = SM.getRangeForBuffer(BufferID).str();
auto Start = getLocsByLabelOrPosition(options::LineColumnPair, Buffer);
if (Start.empty()) {
llvm::errs() << "cannot parse position pair.\n";
return 1;
}
RefactorLoc EndLoc = Start.front();
if (options::EndLineColumnPair.getNumOccurrences() == 1) {
auto End = getLocsByLabelOrPosition(options::EndLineColumnPair, Buffer);
if (End.size() > 1) {
llvm::errs() << "only a single start and end position may be specified.";
return 1;
}
EndLoc = End.front();
}
RefactorLoc &StartLoc = Start.front();
if (options::Action == RefactoringKind::FindLocalRenameRanges) {
RangeConfig Range = getRange(BufferID, SM, StartLoc, EndLoc);
FindRenameRangesAnnotatingConsumer Consumer(SM, BufferID, llvm::outs());
return findLocalRenameRanges(SF, Range, Consumer, PrintDiags);
}
if (options::Action == RefactoringKind::GlobalRename ||
options::Action == RefactoringKind::FindGlobalRenameRanges) {
if (!options::OldName.getNumOccurrences()) {
llvm::errs() << "old-name must be specified.";
return 1;
}
if (options::Action == RefactoringKind::GlobalRename && !options::NewName.getNumOccurrences()) {
llvm::errs() << "new-name must be specified.";
return 1;
}
std::string NewName = options::NewName;
if (!options::NewName.getNumOccurrences()) {
// Unlike other operations, we don't want to provide a default new_name,
// since we don't want to validate the new name.
NewName = "";
}
std::vector<RenameLoc>
RenameLocs = getRenameLocs(BufferID, SM, Start, options::OldName, NewName,
options::IsFunctionLike.getNumOccurrences(),
options::IsNonProtocolType.getNumOccurrences());
switch (options::Action) {
case RefactoringKind::GlobalRename: {
SourceEditOutputConsumer EditConsumer(SM, BufferID, llvm::outs());
return syntacticRename(SF, RenameLocs, EditConsumer, PrintDiags);
}
case RefactoringKind::FindGlobalRenameRanges: {
FindRenameRangesAnnotatingConsumer Consumer(SM, BufferID, llvm::outs());
return findSyntacticRenameRanges(SF, RenameLocs, Consumer, PrintDiags);
}
default:
llvm_unreachable("unexpected refactoring kind");
}
}
RangeConfig Range = getRange(BufferID, SM, StartLoc, EndLoc);
if (options::Action == RefactoringKind::None) {
std::vector<RefactoringKind> Scratch;
ArrayRef<RefactoringKind> AllKinds;
bool RangeStartMayNeedRename = false;
AllKinds = collectAvailableRefactorings(SF, Range,RangeStartMayNeedRename,
Scratch, {&PrintDiags});
llvm::outs() << "Action begins\n";
for (auto Kind : AllKinds) {
llvm::outs() << getDescriptiveRefactoringKindName(Kind) << "\n";
}
llvm::outs() << "Action ends\n";
return 0;
}
RefactoringOptions RefactoringConfig(options::Action);
RefactoringConfig.Range = Range;
RefactoringConfig.PreferredName = options::NewName;
std::string Error;
std::unique_ptr<SourceEditConsumer> pConsumer;
if (options::DumpInJason)
pConsumer.reset(new SourceEditJsonConsumer(llvm::outs()));
else
pConsumer.reset(new SourceEditOutputConsumer(SF->getASTContext().SourceMgr,
BufferID,
llvm::outs()));
return refactorSwiftModule(CI.getMainModule(), RefactoringConfig, *pConsumer,
PrintDiags);
}