Skip to content

Commit 2af598f

Browse files
committed
[Gardening] Const-qualify Many Usages of CompilerInstance
For those operations that do not need to emit diagnostics or manipulate modules, there's no reason to mutate the passed instance.
1 parent 8fe25f7 commit 2af598f

File tree

7 files changed

+43
-35
lines changed

7 files changed

+43
-35
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class CompilerInvocation {
328328
/// in generating a cached PCH file for the bridging header.
329329
std::string getPCHHash() const;
330330

331-
SourceFile::ImplicitModuleImportKind getImplicitModuleImportKind() {
331+
SourceFile::ImplicitModuleImportKind getImplicitModuleImportKind() const {
332332
if (getInputKind() == InputFileKind::SIL) {
333333
return SourceFile::ImplicitModuleImportKind::None;
334334
}
@@ -404,7 +404,7 @@ class CompilerInstance {
404404
/// Null if no tracker.
405405
std::unique_ptr<DependencyTracker> DepTracker;
406406

407-
ModuleDecl *MainModule = nullptr;
407+
mutable ModuleDecl *MainModule = nullptr;
408408
SerializedModuleLoader *SML = nullptr;
409409
MemoryBufferSerializedModuleLoader *MemoryBufferLoader = nullptr;
410410

@@ -453,14 +453,16 @@ class CompilerInstance {
453453
void operator=(CompilerInstance &&) = delete;
454454

455455
SourceManager &getSourceMgr() { return SourceMgr; }
456+
const SourceManager &getSourceMgr() const { return SourceMgr; }
456457

457458
DiagnosticEngine &getDiags() { return Diagnostics; }
459+
const DiagnosticEngine &getDiags() const { return Diagnostics; }
458460

459461
llvm::vfs::FileSystem &getFileSystem() { return *SourceMgr.getFileSystem(); }
460462

461-
ASTContext &getASTContext() {
462-
return *Context;
463-
}
463+
ASTContext &getASTContext() { return *Context; }
464+
const ASTContext &getASTContext() const { return *Context; }
465+
464466
bool hasASTContext() const { return Context != nullptr; }
465467

466468
SILOptions &getSILOptions() { return Invocation.getSILOptions(); }
@@ -483,6 +485,7 @@ class CompilerInstance {
483485
DepTracker = llvm::make_unique<DependencyTracker>(TrackSystemDeps);
484486
}
485487
DependencyTracker *getDependencyTracker() { return DepTracker.get(); }
488+
const DependencyTracker *getDependencyTracker() const { return DepTracker.get(); }
486489

487490
SILModule *getSILModule() {
488491
return TheSILModule.get();
@@ -494,7 +497,7 @@ class CompilerInstance {
494497
return static_cast<bool>(TheSILModule);
495498
}
496499

497-
ModuleDecl *getMainModule();
500+
ModuleDecl *getMainModule() const;
498501

499502
MemoryBufferSerializedModuleLoader *
500503
getMemoryBufferSerializedModuleLoader() const {
@@ -515,7 +518,7 @@ class CompilerInstance {
515518

516519
/// Gets the set of SourceFiles which are the primary inputs for this
517520
/// CompilerInstance.
518-
ArrayRef<SourceFile *> getPrimarySourceFiles() {
521+
ArrayRef<SourceFile *> getPrimarySourceFiles() const {
519522
return PrimarySourceFiles;
520523
}
521524

@@ -525,7 +528,7 @@ class CompilerInstance {
525528
///
526529
/// FIXME: This should be removed eventually, once there are no longer any
527530
/// codepaths that rely on a single primary file.
528-
SourceFile *getPrimarySourceFile() {
531+
SourceFile *getPrimarySourceFile() const {
529532
if (PrimarySourceFiles.empty()) {
530533
return nullptr;
531534
} else {

include/swift/Subsystems.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ namespace swift {
263263
/// Get the CPU, subtarget feature options, and triple to use when emitting code.
264264
std::tuple<llvm::TargetOptions, std::string, std::vector<std::string>,
265265
std::string>
266-
getIRTargetOptions(ASTContext &Ctx);
266+
getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx);
267267

268268
/// Turn the given Swift module into either LLVM IR or native code
269269
/// and return the generated LLVM IR module.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
111111
}
112112
}
113113

114-
static void setIRGenOutputOptsFromFrontendOptions(IRGenOptions &IRGenOpts,
115-
const FrontendOptions &FrontendOpts) {
114+
static void
115+
setIRGenOutputOptsFromFrontendOptions(IRGenOptions &IRGenOpts,
116+
const FrontendOptions &FrontendOpts) {
116117
// Set the OutputKind for the given Action.
117118
IRGenOpts.OutputKind = [](FrontendOptions::ActionType Action) {
118119
switch (Action) {
@@ -140,8 +141,12 @@ static void setIRGenOutputOptsFromFrontendOptions(IRGenOptions &IRGenOpts,
140141
}
141142
}
142143

143-
static void setBridgingHeaderFromFrontendOptions(ClangImporterOptions &ImporterOpts,
144-
const FrontendOptions &FrontendOpts) {
144+
static void
145+
setBridgingHeaderFromFrontendOptions(ClangImporterOptions &ImporterOpts,
146+
const FrontendOptions &FrontendOpts) {
147+
if (FrontendOpts.RequestedAction != FrontendOptions::ActionType::EmitPCH)
148+
return;
149+
145150
// If there aren't any inputs, there's nothing to do.
146151
if (!FrontendOpts.InputsAndOutputs.hasInputs())
147152
return;
@@ -1352,7 +1357,6 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
13521357
Opts.AutolinkRuntimeCompatibilityDynamicReplacementLibraryVersion =
13531358
getRuntimeCompatVersion();
13541359
}
1355-
13561360
return false;
13571361
}
13581362

lib/FrontendTool/FrontendTool.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ static bool writeSIL(SILModule &SM, ModuleDecl *M, bool EmitVerboseSIL,
509509
}
510510

511511
static bool writeSIL(SILModule &SM, const PrimarySpecificPaths &PSPs,
512-
CompilerInstance &Instance,
512+
const CompilerInstance &Instance,
513513
const CompilerInvocation &Invocation) {
514514
const FrontendOptions &opts = Invocation.getFrontendOptions();
515515
return writeSIL(SM, Instance.getMainModule(), opts.EmitVerboseSIL,
@@ -640,10 +640,10 @@ static void debugFailWithCrash() {
640640
/// \return true on error.
641641
static bool emitIndexDataIfNeeded(SourceFile *PrimarySourceFile,
642642
const CompilerInvocation &Invocation,
643-
CompilerInstance &Instance);
643+
const CompilerInstance &Instance);
644644

645645
static void countStatsOfSourceFile(UnifiedStatsReporter &Stats,
646-
CompilerInstance &Instance,
646+
const CompilerInstance &Instance,
647647
SourceFile *SF) {
648648
auto &C = Stats.getFrontendCounters();
649649
auto &SM = Instance.getSourceMgr();
@@ -724,7 +724,7 @@ createOptRecordFile(StringRef Filename, DiagnosticEngine &DE) {
724724
}
725725

726726
static bool precompileBridgingHeader(const CompilerInvocation &Invocation,
727-
CompilerInstance &Instance) {
727+
const CompilerInstance &Instance) {
728728
auto clangImporter = static_cast<ClangImporter *>(
729729
Instance.getASTContext().getClangModuleLoader());
730730
auto &ImporterOpts = Invocation.getClangImporterOptions();
@@ -743,7 +743,7 @@ static bool precompileBridgingHeader(const CompilerInvocation &Invocation,
743743
}
744744

745745
static bool precompileClangModule(const CompilerInvocation &Invocation,
746-
CompilerInstance &Instance) {
746+
const CompilerInstance &Instance) {
747747
auto clangImporter = static_cast<ClangImporter *>(
748748
Instance.getASTContext().getClangModuleLoader());
749749
return clangImporter->emitPrecompiledModule(
@@ -755,7 +755,7 @@ static bool precompileClangModule(const CompilerInvocation &Invocation,
755755
}
756756

757757
static bool dumpPrecompiledClangModule(const CompilerInvocation &Invocation,
758-
CompilerInstance &Instance) {
758+
const CompilerInstance &Instance) {
759759
auto clangImporter = static_cast<ClangImporter *>(
760760
Instance.getASTContext().getClangModuleLoader());
761761
return clangImporter->dumpPrecompiledModule(
@@ -835,7 +835,7 @@ static void verifyGenericSignaturesIfNeeded(const CompilerInvocation &Invocation
835835
}
836836

837837
static void dumpAndPrintScopeMap(const CompilerInvocation &Invocation,
838-
CompilerInstance &Instance, SourceFile *SF) {
838+
const CompilerInstance &Instance, SourceFile *SF) {
839839
// Not const because may require reexpansion
840840
ASTScope &scope = SF->getScope();
841841

@@ -854,7 +854,7 @@ static void dumpAndPrintScopeMap(const CompilerInvocation &Invocation,
854854
}
855855

856856
static SourceFile *getPrimaryOrMainSourceFile(const CompilerInvocation &Invocation,
857-
CompilerInstance &Instance) {
857+
const CompilerInstance &Instance) {
858858
SourceFile *SF = Instance.getPrimarySourceFile();
859859
if (!SF) {
860860
SourceFileKind Kind = Invocation.getSourceFileKind();
@@ -963,7 +963,7 @@ static void emitReferenceDependenciesForAllPrimaryInputsIfNeeded(
963963
}
964964
static void
965965
emitSwiftRangesForAllPrimaryInputsIfNeeded(const CompilerInvocation &Invocation,
966-
CompilerInstance &Instance) {
966+
const CompilerInstance &Instance) {
967967
if (Invocation.getFrontendOptions().InputsAndOutputs.hasSwiftRangesPath() &&
968968
Instance.getPrimarySourceFiles().empty()) {
969969
Instance.getASTContext().Diags.diagnose(
@@ -981,7 +981,7 @@ emitSwiftRangesForAllPrimaryInputsIfNeeded(const CompilerInvocation &Invocation,
981981
}
982982
static void
983983
emitCompiledSourceForAllPrimaryInputsIfNeeded(const CompilerInvocation &Invocation,
984-
CompilerInstance &Instance) {
984+
const CompilerInstance &Instance) {
985985
if (Invocation.getFrontendOptions()
986986
.InputsAndOutputs.hasCompiledSourcePath() &&
987987
Instance.getPrimarySourceFiles().empty()) {
@@ -1158,7 +1158,7 @@ performCompileStepsPostSema(const CompilerInvocation &Invocation,
11581158

11591159
/// Emits index data for all primary inputs, or the main module.
11601160
static bool
1161-
emitIndexData(const CompilerInvocation &Invocation, CompilerInstance &Instance) {
1161+
emitIndexData(const CompilerInvocation &Invocation, const CompilerInstance &Instance) {
11621162
bool hadEmitIndexDataError = false;
11631163
if (Instance.getPrimarySourceFiles().empty())
11641164
return emitIndexDataIfNeeded(nullptr, Invocation, Instance);
@@ -1344,7 +1344,7 @@ static bool performCompile(CompilerInstance &Instance,
13441344
}
13451345

13461346
static bool serializeSIB(SILModule *SM, const PrimarySpecificPaths &PSPs,
1347-
ASTContext &Context, ModuleOrSourceFile MSF) {
1347+
const ASTContext &Context, ModuleOrSourceFile MSF) {
13481348
const std::string &moduleOutputPath =
13491349
PSPs.SupplementaryOutputs.ModuleOutputPath;
13501350
assert(!moduleOutputPath.empty() && "must have an output path");
@@ -1399,7 +1399,8 @@ static bool processCommandLineAndRunImmediately(const CompilerInvocation &Invoca
13991399
: MSF.get<ModuleDecl *>()->getModuleFilename());
14001400

14011401
ReturnValue =
1402-
RunImmediately(Instance, CmdLine, IRGenOpts, Invocation.getSILOptions());
1402+
RunImmediately(Instance, CmdLine, IRGenOpts, Invocation.getSILOptions(),
1403+
std::move(SM));
14031404
return Instance.getASTContext().hadError();
14041405
}
14051406

@@ -1492,7 +1493,7 @@ static bool performCompileStepsPostSILGen(
14921493

14931494
FrontendOptions opts = Invocation.getFrontendOptions();
14941495
FrontendOptions::ActionType Action = opts.RequestedAction;
1495-
ASTContext &Context = Instance.getASTContext();
1496+
const ASTContext &Context = Instance.getASTContext();
14961497
const SILOptions &SILOpts = Invocation.getSILOptions();
14971498
const IRGenOptions &IRGenOpts = Invocation.getIRGenOptions();
14981499

@@ -1512,7 +1513,7 @@ static bool performCompileStepsPostSILGen(
15121513
}
15131514

15141515
if (Action == FrontendOptions::ActionType::EmitSIBGen) {
1515-
serializeSIB(SM.get(), PSPs, Instance.getASTContext(), MSF);
1516+
serializeSIB(SM.get(), PSPs, Context, MSF);
15161517
return Context.hadError();
15171518
}
15181519

@@ -1556,7 +1557,7 @@ static bool performCompileStepsPostSILGen(
15561557
moduleIsPublic);
15571558

15581559
if (Action == FrontendOptions::ActionType::EmitSIB)
1559-
return serializeSIB(SM.get(), PSPs, Instance.getASTContext(), MSF);
1560+
return serializeSIB(SM.get(), PSPs, Context, MSF);
15601561

15611562
{
15621563
if (PSPs.haveModuleOrModuleDocOutputPaths()) {
@@ -1643,7 +1644,7 @@ static bool performCompileStepsPostSILGen(
16431644

16441645
static bool emitIndexDataIfNeeded(SourceFile *PrimarySourceFile,
16451646
const CompilerInvocation &Invocation,
1646-
CompilerInstance &Instance) {
1647+
const CompilerInstance &Instance) {
16471648
const FrontendOptions &opts = Invocation.getFrontendOptions();
16481649

16491650
if (opts.IndexStorePath.empty())

lib/IRGen/IRGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
152152

153153
std::tuple<llvm::TargetOptions, std::string, std::vector<std::string>,
154154
std::string>
155-
swift::getIRTargetOptions(ASTContext &Ctx) {
155+
swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) {
156156
// Things that maybe we should collect from the command line:
157157
// - relocation model
158158
// - code model
@@ -657,7 +657,7 @@ swift::createTargetMachine(const IRGenOptions &Opts, ASTContext &Ctx) {
657657
std::string EffectiveClangTriple;
658658
std::vector<std::string> targetFeaturesArray;
659659
std::tie(TargetOpts, CPU, targetFeaturesArray, EffectiveClangTriple)
660-
= getIRTargetOptions(Ctx);
660+
= getIRTargetOptions(Opts, Ctx);
661661
const llvm::Triple &EffectiveTriple = llvm::Triple(EffectiveClangTriple);
662662
std::string targetFeatures;
663663
if (!targetFeaturesArray.empty()) {

lib/Immediate/Immediate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ int swift::RunImmediately(CompilerInstance &CI,
304304
std::string Triple;
305305
std::vector<std::string> Features;
306306
std::tie(TargetOpt, CPU, Features, Triple)
307-
= getIRTargetOptions(swiftModule->getASTContext());
307+
= getIRTargetOptions(IRGenOpts, swiftModule->getASTContext());
308308
builder.setRelocationModel(llvm::Reloc::PIC_);
309309
builder.setTargetOptions(TargetOpt);
310310
builder.setMCPU(CPU);

lib/Immediate/REPL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ class REPLEnvironment {
984984
std::string Triple;
985985
std::vector<std::string> Features;
986986
std::tie(TargetOpt, CPU, Features, Triple)
987-
= getIRTargetOptions(CI.getASTContext());
987+
= getIRTargetOptions(IRGenOpts, CI.getASTContext());
988988

989989
builder.setRelocationModel(llvm::Reloc::PIC_);
990990
builder.setTargetOptions(TargetOpt);

0 commit comments

Comments
 (0)