Skip to content

Commit 63776b5

Browse files
committed
When converting some of the old Migrator automation to the new Migrator,
I had set up the driver to invoke a separate frontend invocation with the "update code" mode. We sort of did this last release, except we forked to the swift-update binary instead. This is causing problems with testing in Xcode. Instead, let's perform a single compile and add the remap file as an additional output during normal compiles. The driver, seeing -update-code, will add -emit-remap-file-path $PATH to the -c frontend invocation. rdar://problem/31857580
1 parent 62b6b3b commit 63776b5

20 files changed

+82
-82
lines changed

include/swift/Driver/Driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class OutputInfo {
9494

9595
/// Whether the compiler picked the current module name, rather than the user.
9696
bool ModuleNameIsFallback = false;
97-
97+
9898
/// The number of threads for multi-threaded compilation.
9999
unsigned numThreads = 0;
100100

include/swift/Frontend/FrontendOptions.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ class FrontendOptions {
172172
EmitIR, ///< Emit LLVM IR
173173
EmitBC, ///< Emit LLVM BC
174174
EmitObject, ///< Emit object file
175-
176-
UpdateCode, ///< Update Swift code
177175
};
178176

179177
/// Indicates the action the user requested that the frontend perform.

include/swift/Migrator/Migrator.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ namespace migrator {
2727

2828
/// Run the migrator on the compiler invocation's input file and emit a
2929
/// "replacement map" describing the requested changes to the source file.
30-
bool updateCodeAndEmitRemap(const CompilerInvocation &Invocation);
30+
bool updateCodeAndEmitRemap(CompilerInstance &Instance,
31+
const CompilerInvocation &Invocation);
3132

3233
class Migrator {
34+
CompilerInstance &StartInstance;
3335
const CompilerInvocation &StartInvocation;
3436
SourceManager SrcMgr;
3537
std::vector<RC<MigrationState>> States;
3638

3739
public:
38-
Migrator(const CompilerInvocation &StartInvocation);
40+
Migrator(CompilerInstance &StartInstance,
41+
const CompilerInvocation &StartInvocation);
3942

4043
/// The maximum number of times to run the compiler over the input to get
4144
/// fix-its. Nullability changes may expose other fix-its in subsequent

include/swift/Migrator/MigratorOptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ struct MigratorOptions {
3131
/// Whether to print each USR we query the api change data store about.
3232
bool DumpUsr = false;
3333

34+
/// If non-empty, print a replacement map describing changes to get from
35+
/// the first MigrationState's output text to the last MigrationState's
36+
/// output text.
37+
std::string EmitRemapFilePath = "";
38+
3439
/// If non-empty, print the last MigrationState's output text to the given
3540
/// file path.
3641
std::string EmitMigratedFilePath = "";
@@ -40,6 +45,11 @@ struct MigratorOptions {
4045

4146
/// If non-empty, use the api change data serialized to this path.
4247
std::string APIDigesterDataStorePath = "";
48+
49+
bool shouldRunMigrator() const {
50+
return !(EmitRemapFilePath.empty() && EmitMigratedFilePath.empty() &&
51+
DumpMigrationStatesDir.empty());
52+
}
4353
};
4454

4555
} // end namespace swift

include/swift/Option/Options.td

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,20 @@ def line_range : Separate<["-"], "line-range">, Group<code_formatting_Group>,
415415
"Can only be used with one input file.">, MetaVarName<"<n:n>">;
416416

417417
// Migrator Options
418+
419+
def update_code : Flag<["-"], "update-code">,
420+
HelpText<"Update Swift code">,
421+
Flags<[FrontendOption, HelpHidden, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
422+
418423
def disable_migrator_fixits: Flag<["-"], "disable-migrator-fixits">,
419424
Flags<[FrontendOption, NoInteractiveOption]>,
420425
HelpText<"Disable the Migrator phase which automatically applies fix-its">;
421426

427+
def emit_remap_file_path: Separate<["-"], "emit-remap-file-path">,
428+
Flags<[FrontendOption, NoDriverOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>,
429+
HelpText<"Emit the replacement map describing Swift Migrator changes to <path>">,
430+
MetaVarName<"<path>">;
431+
422432
def emit_migrated_file_path: Separate<["-"], "emit-migrated-file-path">,
423433
Flags<[FrontendOption, NoDriverOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>,
424434
HelpText<"Emit the migrated source file to <path>">,
@@ -508,10 +518,6 @@ def c : Flag<["-"], "c">, Alias<emit_object>,
508518
def S: Flag<["-"], "S">, Alias<emit_assembly>,
509519
Flags<[FrontendOption, NoInteractiveOption]>, ModeOpt;
510520

511-
def update_code : Flag<["-"], "update-code">,
512-
HelpText<"Update Swift code">, ModeOpt,
513-
Flags<[FrontendOption, HelpHidden, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
514-
515521
def fixit_all : Flag<["-"], "fixit-all">,
516522
HelpText<"Apply all fixits from diagnostics without any filtering">,
517523
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;

lib/Driver/Driver.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,6 @@ std::unique_ptr<Compilation> Driver::buildCompilation(
494494
ArgList->hasArg(options::OPT_driver_show_incremental);
495495
bool ShowJobLifecycle =
496496
ArgList->hasArg(options::OPT_driver_show_job_lifecycle);
497-
bool UpdateCode =
498-
ArgList->hasArg(options::OPT_update_code);
499497

500498
bool Incremental = ArgList->hasArg(options::OPT_incremental);
501499
if (ArgList->hasArg(options::OPT_whole_module_optimization)) {
@@ -671,10 +669,9 @@ std::unique_ptr<Compilation> Driver::buildCompilation(
671669

672670
buildJobs(Actions, OI, OFM.get(), *TC, *C);
673671

674-
// For updating code we need to go through all the files and pick up changes,
675-
// even if they have compiler errors. Also for getting bulk fixits, or for when
676-
// users explicitly request to continue building despite errors.
677-
if (UpdateCode || ContinueBuildingAfterErrors)
672+
// For getting bulk fixits, or for when users explicitly request to continue
673+
// building despite errors.
674+
if (ContinueBuildingAfterErrors)
678675
C->setContinueBuildingAfterErrors();
679676

680677
if (ShowIncrementalBuildDecisions || ShowJobLifecycle)
@@ -1126,11 +1123,6 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
11261123
OI.CompilerMode = OutputInfo::Mode::SingleCompile;
11271124
break;
11281125

1129-
case options::OPT_update_code:
1130-
OI.CompilerOutputType = types::TY_Remapping;
1131-
OI.LinkAction = LinkKind::None;
1132-
break;
1133-
11341126
case options::OPT_parse:
11351127
case options::OPT_typecheck:
11361128
case options::OPT_dump_parse:
@@ -2068,6 +2060,27 @@ Job *Driver::buildJobsForAction(Compilation &C, const JobAction *JA,
20682060
}
20692061
}
20702062

2063+
if (C.getArgs().hasArg(options::OPT_update_code) &&
2064+
isa<CompileJobAction>(JA)) {
2065+
StringRef OFMFixitsOutputPath;
2066+
if (OutputMap) {
2067+
auto iter = OutputMap->find(types::TY_Remapping);
2068+
if (iter != OutputMap->end())
2069+
OFMFixitsOutputPath = iter->second;
2070+
}
2071+
if (!OFMFixitsOutputPath.empty()) {
2072+
Output->setAdditionalOutputForType(types::ID::TY_Remapping,
2073+
OFMFixitsOutputPath);
2074+
} else {
2075+
llvm::SmallString<128> Path(Output->getPrimaryOutputFilenames()[0]);
2076+
bool isTempFile = C.isTemporaryFile(Path);
2077+
llvm::sys::path::replace_extension(Path, "remap");
2078+
Output->setAdditionalOutputForType(types::ID::TY_Remapping, Path);
2079+
if (isTempFile)
2080+
C.addTemporaryFile(Path);
2081+
}
2082+
}
2083+
20712084
if (isa<CompileJobAction>(JA) || isa<GeneratePCHJobAction>(JA)) {
20722085
// Choose the serialized diagnostics output path.
20732086
if (C.getArgs().hasArg(options::OPT_serialize_diagnostics)) {

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ ToolChain::constructInvocation(const CompileJobAction &job,
402402
const std::string &FixitsPath =
403403
context.Output.getAdditionalOutputForType(types::TY_Remapping);
404404
if (!FixitsPath.empty()) {
405-
Arguments.push_back("-emit-fixits-path");
405+
Arguments.push_back("-emit-remap-file-path");
406406
Arguments.push_back(FixitsPath.c_str());
407407
}
408408

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
323323
Action = FrontendOptions::REPL;
324324
} else if (Opt.matches(OPT_interpret)) {
325325
Action = FrontendOptions::Immediate;
326-
} else if (Opt.matches(OPT_update_code)) {
327-
Action = FrontendOptions::UpdateCode;
328326
} else {
329327
llvm_unreachable("Unhandled mode option");
330328
}
@@ -562,10 +560,6 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
562560
Suffix = "importedmodules";
563561
break;
564562

565-
case FrontendOptions::UpdateCode:
566-
Suffix = REMAP_EXTENSION;
567-
break;
568-
569563
case FrontendOptions::EmitTBD:
570564
if (Opts.OutputFilenames.empty())
571565
Opts.setSingleOutputFilename("-");
@@ -716,7 +710,6 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
716710
case FrontendOptions::DumpTypeRefinementContexts:
717711
case FrontendOptions::Immediate:
718712
case FrontendOptions::REPL:
719-
case FrontendOptions::UpdateCode:
720713
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_dependencies);
721714
return true;
722715
case FrontendOptions::Parse:
@@ -749,7 +742,6 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
749742
case FrontendOptions::DumpTypeRefinementContexts:
750743
case FrontendOptions::Immediate:
751744
case FrontendOptions::REPL:
752-
case FrontendOptions::UpdateCode:
753745
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_header);
754746
return true;
755747
case FrontendOptions::Parse:
@@ -781,7 +773,6 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
781773
case FrontendOptions::DumpTypeRefinementContexts:
782774
case FrontendOptions::Immediate:
783775
case FrontendOptions::REPL:
784-
case FrontendOptions::UpdateCode:
785776
Diags.diagnose(SourceLoc(),
786777
diag::error_mode_cannot_emit_loaded_module_trace);
787778
return true;
@@ -818,7 +809,6 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
818809
case FrontendOptions::EmitSILGen:
819810
case FrontendOptions::Immediate:
820811
case FrontendOptions::REPL:
821-
case FrontendOptions::UpdateCode:
822812
if (!Opts.ModuleOutputPath.empty())
823813
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module);
824814
else
@@ -1598,6 +1588,10 @@ bool ParseMigratorArgs(MigratorOptions &Opts, llvm::Triple &Triple,
15981588
Opts.EnableMigratorFixits = false;
15991589
}
16001590

1591+
if (auto RemapFilePath = Args.getLastArg(OPT_emit_remap_file_path)) {
1592+
Opts.EmitRemapFilePath = RemapFilePath->getValue();
1593+
}
1594+
16011595
if (auto MigratedFilePath = Args.getLastArg(OPT_emit_migrated_file_path)) {
16021596
Opts.EmitMigratedFilePath = MigratedFilePath->getValue();
16031597
}
@@ -1625,6 +1619,7 @@ bool ParseMigratorArgs(MigratorOptions &Opts, llvm::Triple &Triple,
16251619
if (Supported)
16261620
Opts.APIDigesterDataStorePath = dataPath.str();
16271621
}
1622+
16281623
return false;
16291624
}
16301625

lib/Frontend/FrontendOptions.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ bool FrontendOptions::actionHasOutput() const {
3434
case EmitSIBGen:
3535
case EmitSIB:
3636
case EmitModuleOnly:
37-
case UpdateCode:
3837
return true;
3938
case Immediate:
4039
case REPL:
@@ -67,7 +66,6 @@ bool FrontendOptions::actionIsImmediate() const {
6766
case EmitSIBGen:
6867
case EmitSIB:
6968
case EmitModuleOnly:
70-
case UpdateCode:
7169
return false;
7270
case Immediate:
7371
case REPL:

lib/FrontendTool/FrontendTool.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,16 +460,17 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
460460

461461
ASTContext &Context = Instance->getASTContext();
462462

463+
if (!Context.hadError() &&
464+
Invocation.getMigratorOptions().shouldRunMigrator()) {
465+
migrator::updateCodeAndEmitRemap(*Instance, Invocation);
466+
}
467+
463468
if (Action == FrontendOptions::REPL) {
464469
runREPL(*Instance, ProcessCmdLine(Args.begin(), Args.end()),
465470
Invocation.getParseStdlib());
466471
return Context.hadError();
467472
}
468473

469-
if (Action == FrontendOptions::UpdateCode) {
470-
return migrator::updateCodeAndEmitRemap(Invocation);
471-
}
472-
473474
SourceFile *PrimarySourceFile = Instance->getPrimarySourceFile();
474475

475476
// We've been told to dump the AST (either after parsing or type-checking,

0 commit comments

Comments
 (0)