Skip to content

Commit 2494b14

Browse files
graydonDavid Ungar
authored andcommitted
[BatchMode] Sink a bunch of FrontendTool uses of getPrimarySourceFile()
1 parent b0e5726 commit 2494b14

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,16 @@ class CompilerInstance {
437437
return PrimarySourceFiles;
438438
}
439439

440+
/// Gets the Primary Source File if one exists, otherwise the main
441+
/// module. If multiple Primary Source Files exist, fails with an
442+
/// assertion.
443+
ModuleOrSourceFile getPrimarySourceFileOrMainModule() {
444+
if (PrimarySourceFiles.empty())
445+
return getMainModule();
446+
else
447+
return getPrimarySourceFile();
448+
}
449+
440450
/// Gets the SourceFile which is the primary input for this CompilerInstance.
441451
/// \returns the primary SourceFile, or nullptr if there is no primary input;
442452
/// if there are _multiple_ primary inputs, fails with an assertion.

lib/FrontendTool/FrontendTool.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,9 @@ static void countStatsPostSema(UnifiedStatsReporter &Stats,
453453
C.NumReferencedMemberNames = R->getUsedMembers().size();
454454
}
455455

456-
if (auto *SF = Instance.getPrimarySourceFile()) {
457-
countStatsOfSourceFile(Stats, Instance, SF);
456+
if (!Instance.getPrimarySourceFiles().empty()) {
457+
for (auto SF : Instance.getPrimarySourceFiles())
458+
countStatsOfSourceFile(Stats, Instance, SF);
458459
} else if (auto *M = Instance.getMainModule()) {
459460
// No primary source file, but a main module; this is WMO-mode
460461
for (auto *F : M->getFiles()) {
@@ -630,8 +631,6 @@ static bool performCompile(CompilerInstance &Instance,
630631
return Context.hadError();
631632
}
632633

633-
SourceFile *PrimarySourceFile = Instance.getPrimarySourceFile();
634-
635634
// We've been told to dump the AST (either after parsing or type-checking,
636635
// which is already differentiated in CompilerInstance::performSema()),
637636
// so dump or print the main source file and return.
@@ -642,7 +641,7 @@ static bool performCompile(CompilerInstance &Instance,
642641
Action == FrontendOptions::ActionType::DumpScopeMaps ||
643642
Action == FrontendOptions::ActionType::DumpTypeRefinementContexts ||
644643
Action == FrontendOptions::ActionType::DumpInterfaceHash) {
645-
SourceFile *SF = PrimarySourceFile;
644+
SourceFile *SF = Instance.getPrimarySourceFile();
646645
if (!SF) {
647646
SourceFileKind Kind = Invocation.getSourceFileKind();
648647
SF = &Instance.getMainModule()->getMainSourceFile(Kind);
@@ -717,9 +716,12 @@ static bool performCompile(CompilerInstance &Instance,
717716
(void)emitMakeDependencies(Context.Diags, *Instance.getDependencyTracker(),
718717
opts);
719718

720-
if (shouldTrackReferences)
721-
emitReferenceDependencies(Context.Diags, Instance.getPrimarySourceFile(),
722-
*Instance.getDependencyTracker(), opts);
719+
if (shouldTrackReferences) {
720+
for (auto *SF : Instance.getPrimarySourceFiles()) {
721+
emitReferenceDependencies(Context.Diags, SF,
722+
*Instance.getDependencyTracker(), opts);
723+
}
724+
}
723725

724726
if (!opts.LoadedModuleTracePath.empty())
725727
(void)emitLoadedModuleTrace(Context, *Instance.getDependencyTracker(),
@@ -730,7 +732,8 @@ static bool performCompile(CompilerInstance &Instance,
730732
if (Context.hadError()) {
731733
if (shouldIndex) {
732734
// Emit the index store data even if there were compiler errors.
733-
if (emitIndexData(PrimarySourceFile, Invocation, Instance))
735+
if (emitIndexData(Instance.getPrimarySourceFile(),
736+
Invocation, Instance))
734737
return true;
735738
}
736739
return true;
@@ -749,7 +752,8 @@ static bool performCompile(CompilerInstance &Instance,
749752
return printAsObjC(opts.ObjCHeaderOutputPath, Instance.getMainModule(),
750753
opts.ImplicitObjCHeaderPath, moduleIsPublic);
751754
if (shouldIndex) {
752-
if (emitIndexData(PrimarySourceFile, Invocation, Instance))
755+
if (emitIndexData(Instance.getPrimarySourceFile(),
756+
Invocation, Instance))
753757
return true;
754758
}
755759
return Context.hadError();
@@ -780,7 +784,7 @@ static bool performCompile(CompilerInstance &Instance,
780784
return SASTF && SASTF->isSIB();
781785
};
782786
if (opts.Inputs.hasPrimaryInputs()) {
783-
FileUnit *PrimaryFile = PrimarySourceFile;
787+
FileUnit *PrimaryFile = Instance.getPrimarySourceFile();
784788
if (!PrimaryFile) {
785789
for (FileUnit *fileUnit : Instance.getMainModule()->getFiles()) {
786790
if (auto SASTF = dyn_cast<SerializedASTFile>(fileUnit)) {
@@ -827,8 +831,7 @@ static bool performCompile(CompilerInstance &Instance,
827831
if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll)
828832
performSILLinking(SM.get(), true);
829833

830-
auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) :
831-
Instance.getMainModule();
834+
auto DC = Instance.getPrimarySourceFileOrMainModule();
832835
if (!opts.ModuleOutputPath.empty()) {
833836
SerializationOptions serializationOpts;
834837
serializationOpts.OutputPath = opts.ModuleOutputPath.c_str();
@@ -884,8 +887,7 @@ static bool performCompile(CompilerInstance &Instance,
884887

885888
auto SerializeSILModuleAction = [&]() {
886889
if (!opts.ModuleOutputPath.empty() || !opts.ModuleDocOutputPath.empty()) {
887-
auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile)
888-
: Instance.getMainModule();
890+
auto DC = Instance.getPrimarySourceFileOrMainModule();
889891
if (!opts.ModuleOutputPath.empty()) {
890892
SerializationOptions serializationOpts;
891893
serializationOpts.OutputPath = opts.ModuleOutputPath.c_str();
@@ -956,8 +958,9 @@ static bool performCompile(CompilerInstance &Instance,
956958

957959
// Get the main source file's private discriminator and attach it to
958960
// the compile unit's flags.
959-
if (PrimarySourceFile) {
960-
Identifier PD = PrimarySourceFile->getPrivateDiscriminator();
961+
if (IRGenOpts.DebugInfoKind != IRGenDebugInfoKind::None &&
962+
Instance.getPrimarySourceFile()) {
963+
Identifier PD = Instance.getPrimarySourceFile()->getPrivateDiscriminator();
961964
if (!PD.empty())
962965
IRGenOpts.DWARFDebugFlags += (" -private-discriminator "+PD.str()).str();
963966
}
@@ -968,8 +971,7 @@ static bool performCompile(CompilerInstance &Instance,
968971
}
969972

970973
if (Action == FrontendOptions::ActionType::EmitSIB) {
971-
auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) :
972-
Instance.getMainModule();
974+
auto DC = Instance.getPrimarySourceFileOrMainModule();
973975
if (!opts.ModuleOutputPath.empty()) {
974976
SerializationOptions serializationOpts;
975977
serializationOpts.OutputPath = opts.ModuleOutputPath.c_str();
@@ -988,7 +990,8 @@ static bool performCompile(CompilerInstance &Instance,
988990
if (Action == FrontendOptions::ActionType::MergeModules ||
989991
Action == FrontendOptions::ActionType::EmitModuleOnly) {
990992
if (shouldIndex) {
991-
if (emitIndexData(PrimarySourceFile, Invocation, Instance))
993+
if (emitIndexData(Instance.getPrimarySourceFile(),
994+
Invocation, Instance))
992995
return true;
993996
}
994997
return Context.hadError();
@@ -1019,7 +1022,8 @@ static bool performCompile(CompilerInstance &Instance,
10191022
// TODO: remove once the frontend understands what action it should perform
10201023
IRGenOpts.OutputKind = getOutputKind(Action);
10211024
if (Action == FrontendOptions::ActionType::Immediate) {
1022-
assert(!PrimarySourceFile && "-i doesn't work in -primary-file mode");
1025+
assert(Instance.getPrimarySourceFiles().empty() &&
1026+
"-i doesn't work in -primary-file mode");
10231027
IRGenOpts.UseJIT = true;
10241028
IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::Normal;
10251029
const ProcessCmdLine &CmdLine = ProcessCmdLine(opts.ImmediateArgv.begin(),
@@ -1040,8 +1044,10 @@ static bool performCompile(CompilerInstance &Instance,
10401044
auto &LLVMContext = getGlobalLLVMContext();
10411045
std::unique_ptr<llvm::Module> IRModule;
10421046
llvm::GlobalVariable *HashGlobal;
1043-
if (PrimarySourceFile) {
1044-
IRModule = performIRGeneration(IRGenOpts, *PrimarySourceFile, std::move(SM),
1047+
if (!Instance.getPrimarySourceFiles().empty()) {
1048+
IRModule = performIRGeneration(IRGenOpts,
1049+
*Instance.getPrimarySourceFile(),
1050+
std::move(SM),
10451051
opts.getSingleOutputFilename(), LLVMContext,
10461052
0, &HashGlobal);
10471053
} else {
@@ -1054,7 +1060,7 @@ static bool performCompile(CompilerInstance &Instance,
10541060
// Walk the AST for indexing after IR generation. Walking it before seems
10551061
// to cause miscompilation issues.
10561062
if (shouldIndex) {
1057-
if (emitIndexData(PrimarySourceFile, Invocation, Instance))
1063+
if (emitIndexData(Instance.getPrimarySourceFile(), Invocation, Instance))
10581064
return true;
10591065
}
10601066

@@ -1083,8 +1089,9 @@ static bool performCompile(CompilerInstance &Instance,
10831089
const auto &SILOpts = Invocation.getSILOptions();
10841090
const auto hasMultipleIGMs = SILOpts.hasMultipleIGMs();
10851091
bool error;
1086-
if (PrimarySourceFile)
1087-
error = validateTBD(PrimarySourceFile, *IRModule, hasMultipleIGMs,
1092+
if (!Instance.getPrimarySourceFiles().empty())
1093+
error = validateTBD(Instance.getPrimarySourceFile(),
1094+
*IRModule, hasMultipleIGMs,
10881095
allSymbols);
10891096
else
10901097
error = validateTBD(Instance.getMainModule(), *IRModule, hasMultipleIGMs,

0 commit comments

Comments
 (0)