forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigrator.cpp
438 lines (370 loc) · 14.2 KB
/
Migrator.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
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
//===--- Migrator.cpp -----------------------------------------------------===//
// 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 "Diff.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Migrator/ASTMigratorPass.h"
#include "swift/Migrator/EditorAdapter.h"
#include "swift/Migrator/FixitApplyDiagnosticConsumer.h"
#include "swift/Migrator/Migrator.h"
#include "swift/Migrator/RewriteBufferEditsReceiver.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Edit/EditedSource.h"
#include "clang/Rewrite/Core/RewriteBuffer.h"
#include "llvm/Support/FileSystem.h"
using namespace swift;
using namespace swift::migrator;
bool migrator::updateCodeAndEmitRemapIfNeeded(
CompilerInstance *Instance, const CompilerInvocation &Invocation) {
if (!Invocation.getMigratorOptions().shouldRunMigrator())
return false;
// Delete the remap file, in case someone is re-running the Migrator. If the
// file fails to compile and we don't get a chance to overwrite it, the old
// changes may get picked up.
llvm::sys::fs::remove(Invocation.getMigratorOptions().EmitRemapFilePath);
Migrator M { Instance, Invocation }; // Provide inputs and configuration
auto EffectiveVersion = Invocation.getLangOptions().EffectiveLanguageVersion;
auto CurrentVersion = version::Version::getCurrentLanguageVersion();
// Phase 1: Pre Fix-it passes
// These uses the initial frontend invocation to apply any obvious fix-its
// to see if we can get an error-free AST to get to Phase 2.
std::unique_ptr<swift::CompilerInstance> PreFixItInstance;
if (Instance->getASTContext().hadError()) {
PreFixItInstance = M.repeatFixitMigrations(2, EffectiveVersion);
// If we still couldn't fix all of the errors, give up.
if (PreFixItInstance == nullptr ||
!PreFixItInstance->hasASTContext() ||
PreFixItInstance->getASTContext().hadError()) {
return true;
}
M.StartInstance = PreFixItInstance.get();
}
// Phase 2: Syntactic Transformations
// Don't run these passes if we're already in newest Swift version.
if (EffectiveVersion != CurrentVersion) {
SyntacticPassOptions Opts;
// Type of optional try changes since Swift 5.
Opts.RunOptionalTryMigration = !EffectiveVersion.isVersionAtLeast(5);
auto FailedSyntacticPasses = M.performSyntacticPasses(Opts);
if (FailedSyntacticPasses) {
return true;
}
}
// Phase 3: Post Fix-it Passes
// Perform fix-it based migrations on the compiler, some number of times in
// order to give the compiler an opportunity to
// take its time reaching a fixed point.
// This is the end of the pipeline, so we throw away the compiler instance(s)
// we used in these fix-it runs.
if (M.getMigratorOptions().EnableMigratorFixits) {
M.repeatFixitMigrations(Migrator::MaxCompilerFixitPassIterations,
CurrentVersion);
}
// OK, we have a final resulting text. Now we compare against the input
// to calculate a replacement map describing the changes to the input
// necessary to get the output.
// TODO: Document replacement map format.
auto EmitRemapFailed = M.emitRemap();
auto EmitMigratedFailed = M.emitMigratedFile();
auto DumpMigrationStatesFailed = M.dumpStates();
return EmitRemapFailed || EmitMigratedFailed || DumpMigrationStatesFailed;
}
Migrator::Migrator(CompilerInstance *StartInstance,
const CompilerInvocation &StartInvocation)
: StartInstance(StartInstance), StartInvocation(StartInvocation) {
auto ErrorOrStartBuffer = llvm::MemoryBuffer::getFile(getInputFilename());
auto &StartBuffer = ErrorOrStartBuffer.get();
auto StartBufferID = SrcMgr.addNewSourceBuffer(std::move(StartBuffer));
States.push_back(MigrationState::start(SrcMgr, StartBufferID));
}
std::unique_ptr<swift::CompilerInstance>
Migrator::repeatFixitMigrations(const unsigned Iterations,
version::Version SwiftLanguageVersion) {
for (unsigned i = 0; i < Iterations; ++i) {
auto ThisInstance = performAFixItMigration(SwiftLanguageVersion);
if (ThisInstance == nullptr) {
break;
} else {
if (States.back()->noChangesOccurred()) {
return ThisInstance;
}
}
}
return nullptr;
}
std::unique_ptr<swift::CompilerInstance>
Migrator::performAFixItMigration(version::Version SwiftLanguageVersion) {
auto InputState = States.back();
auto InputText = InputState->getOutputText();
auto InputBuffer =
llvm::MemoryBuffer::getMemBufferCopy(InputText, getInputFilename());
CompilerInvocation Invocation { StartInvocation };
Invocation.getFrontendOptions().InputsAndOutputs.clearInputs();
Invocation.getLangOptions().EffectiveLanguageVersion = SwiftLanguageVersion;
auto &LLVMArgs = Invocation.getFrontendOptions().LLVMArgs;
auto aarch64_use_tbi = std::find(LLVMArgs.begin(), LLVMArgs.end(),
"-aarch64-use-tbi");
if (aarch64_use_tbi != LLVMArgs.end()) {
LLVMArgs.erase(aarch64_use_tbi);
}
const auto &OrigFrontendOpts = StartInvocation.getFrontendOptions();
assert(OrigFrontendOpts.InputsAndOutputs.hasPrimaryInputs() &&
"Migration must have a primary");
for (const auto &input : OrigFrontendOpts.InputsAndOutputs.getAllInputs()) {
Invocation.getFrontendOptions().InputsAndOutputs.addInput(
InputFile(input.file(), input.isPrimary(),
input.isPrimary() ? InputBuffer.get() : input.buffer()));
}
auto Instance = std::make_unique<swift::CompilerInstance>();
if (Instance->setup(Invocation)) {
return nullptr;
}
FixitApplyDiagnosticConsumer FixitApplyConsumer {
InputText,
getInputFilename(),
};
Instance->addDiagnosticConsumer(&FixitApplyConsumer);
Instance->performSema();
StringRef ResultText = InputText;
unsigned ResultBufferID = InputState->getOutputBufferID();
if (FixitApplyConsumer.getNumFixitsApplied() > 0) {
SmallString<4096> Scratch;
llvm::raw_svector_ostream OS(Scratch);
FixitApplyConsumer.printResult(OS);
auto ResultBuffer = llvm::MemoryBuffer::getMemBufferCopy(OS.str());
ResultText = ResultBuffer->getBuffer();
ResultBufferID = SrcMgr.addNewSourceBuffer(std::move(ResultBuffer));
}
States.push_back(MigrationState::make(MigrationKind::CompilerFixits,
SrcMgr, InputState->getOutputBufferID(),
ResultBufferID));
return Instance;
}
bool Migrator::performSyntacticPasses(SyntacticPassOptions Opts) {
clang::FileSystemOptions ClangFileSystemOptions;
clang::FileManager ClangFileManager { ClangFileSystemOptions };
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DummyClangDiagIDs {
new clang::DiagnosticIDs()
};
auto ClangDiags =
std::make_unique<clang::DiagnosticsEngine>(DummyClangDiagIDs,
new clang::DiagnosticOptions,
new clang::DiagnosticConsumer(),
/*ShouldOwnClient=*/true);
clang::SourceManager ClangSourceManager { *ClangDiags, ClangFileManager };
clang::LangOptions ClangLangOpts;
clang::edit::EditedSource Edits { ClangSourceManager, ClangLangOpts };
auto InputState = States.back();
auto InputText = InputState->getOutputText();
EditorAdapter Editor { StartInstance->getSourceMgr(), ClangSourceManager };
runAPIDiffMigratorPass(Editor, StartInstance->getPrimarySourceFile(),
getMigratorOptions());
if (Opts.RunOptionalTryMigration) {
runOptionalTryMigratorPass(Editor, StartInstance->getPrimarySourceFile(),
getMigratorOptions());
}
Edits.commit(Editor.getEdits());
RewriteBufferEditsReceiver Rewriter {
ClangSourceManager,
Editor.getClangFileIDForSwiftBufferID(
StartInstance->getPrimarySourceFile()->getBufferID().getValue()),
InputState->getOutputText()
};
Edits.applyRewrites(Rewriter);
SmallString<1024> Scratch;
llvm::raw_svector_ostream OS(Scratch);
Rewriter.printResult(OS);
auto ResultBuffer = this->SrcMgr.addMemBufferCopy(OS.str());
States.push_back(
MigrationState::make(MigrationKind::Syntactic,
this->SrcMgr,
States.back()->getInputBufferID(),
ResultBuffer));
return false;
}
namespace {
/// Print a replacement from a diff edit scriptto the given output stream.
///
/// \param Filename The filename of the original file
/// \param Rep The Replacement to print
/// \param OS The output stream
void printReplacement(const StringRef Filename,
const Replacement &Rep,
llvm::raw_ostream &OS) {
assert(!Filename.empty());
if (Rep.Remove == 0 && Rep.Text.empty()) {
return;
}
OS << " {\n";
OS << " \"file\": \"";
OS.write_escaped(Filename);
OS << "\",\n";
OS << " \"offset\": " << Rep.Offset;
if (Rep.Remove > 0) {
OS << ",\n";
OS << " \"remove\": " << Rep.Remove;
}
if (!Rep.Text.empty()) {
OS << ",\n";
OS << " \"text\": \"";
OS.write_escaped(Rep.Text);
OS << "\"\n";
} else {
OS << "\n";
}
OS << " }";
}
/// Print a remap file to the given output stream.
///
/// \param OriginalFilename The filename of the file that was edited
/// not the output file for printing here.
/// \param InputText The input text without any changes.
/// \param OutputText The result text after any changes.
/// \param OS The output stream.
void printRemap(const StringRef OriginalFilename,
const StringRef InputText,
const StringRef OutputText,
llvm::raw_ostream &OS) {
assert(!OriginalFilename.empty());
diff_match_patch<std::string> DMP;
const auto Diffs =
DMP.diff_main(InputText.str(), OutputText.str(), /*checkLines=*/false);
OS << "[";
size_t Offset = 0;
llvm::SmallVector<Replacement, 32> Replacements;
for (const auto &Diff : Diffs) {
size_t OffsetIncrement = 0;
switch (Diff.operation) {
case decltype(DMP)::EQUAL:
OffsetIncrement += Diff.text.size();
break;
case decltype(DMP)::INSERT:
Replacements.push_back({ Offset, 0, Diff.text });
break;
case decltype(DMP)::DELETE:
Replacements.push_back({ Offset, Diff.text.size(), "" });
OffsetIncrement = Diff.text.size();
break;
}
Offset += OffsetIncrement;
}
assert(Offset == InputText.size());
// Combine removal edits with previous edits that are consecutive.
for (unsigned i = 1; i < Replacements.size();) {
auto &Previous = Replacements[i-1];
auto &Current = Replacements[i];
assert(Current.Offset >= Previous.Offset + Previous.Remove);
unsigned Distance = Current.Offset-(Previous.Offset + Previous.Remove);
if (Distance > 0) {
++i;
continue;
}
if (!Current.Text.empty()) {
++i;
continue;
}
Previous.Remove += Current.Remove;
Replacements.erase(Replacements.begin() + i);
}
// Combine removal edits with next edits that are consecutive.
for (unsigned i = 0; i + 1 < Replacements.size();) {
auto &Current = Replacements[i];
auto &nextRep = Replacements[i + 1];
assert(nextRep.Offset >= Current.Offset + Current.Remove);
unsigned Distance = nextRep.Offset - (Current.Offset + Current.Remove);
if (Distance > 0) {
++i;
continue;
}
if (!Current.Text.empty()) {
++i;
continue;
}
nextRep.Offset -= Current.Remove;
nextRep.Remove += Current.Remove;
Replacements.erase(Replacements.begin() + i);
}
// For remaining removal diffs, include the byte adjacent to the range on the
// left. libclang applies the diffs as byte diffs, so it doesn't matter if the
// byte is part of a multi-byte UTF8 character.
for (unsigned i = 0; i < Replacements.size(); ++i) {
auto &Current = Replacements[i];
if (!Current.Text.empty())
continue;
if (Current.Offset == 0)
continue;
Current.Offset -= 1;
Current.Remove += 1;
Current.Text = InputText.substr(Current.Offset, 1).str();
}
for (auto Rep = Replacements.begin(); Rep != Replacements.end(); ++Rep) {
if (Rep != Replacements.begin()) {
OS << ",\n";
} else {
OS << "\n";
}
printReplacement(OriginalFilename, *Rep, OS);
}
OS << "\n]";
}
} // end anonymous namespace
bool Migrator::emitRemap() const {
const auto &RemapPath = getMigratorOptions().EmitRemapFilePath;
if (RemapPath.empty()) {
return false;
}
std::error_code Error;
llvm::raw_fd_ostream FileOS(RemapPath,
Error, llvm::sys::fs::F_Text);
if (FileOS.has_error()) {
return true;
}
auto InputText = States.front()->getOutputText();
auto OutputText = States.back()->getOutputText();
printRemap(getInputFilename(), InputText, OutputText, FileOS);
FileOS.flush();
return FileOS.has_error();
}
bool Migrator::emitMigratedFile() const {
const auto &OutFilename = getMigratorOptions().EmitMigratedFilePath;
if (OutFilename.empty()) {
return false;
}
std::error_code Error;
llvm::raw_fd_ostream FileOS(OutFilename,
Error, llvm::sys::fs::F_Text);
if (FileOS.has_error()) {
return true;
}
FileOS << States.back()->getOutputText();
FileOS.flush();
return FileOS.has_error();
}
bool Migrator::dumpStates() const {
const auto &OutDir = getMigratorOptions().DumpMigrationStatesDir;
if (OutDir.empty()) {
return false;
}
auto Failed = false;
for (size_t i = 0; i < States.size(); ++i) {
Failed |= States[i]->print(i, OutDir);
}
return Failed;
}
const MigratorOptions &Migrator::getMigratorOptions() const {
return StartInvocation.getMigratorOptions();
}
const StringRef Migrator::getInputFilename() const {
auto &PrimaryInput = StartInvocation.getFrontendOptions()
.InputsAndOutputs.getRequiredUniquePrimaryInput();
return PrimaryInput.file();
}