Skip to content

Commit a082e9d

Browse files
committed
[Gardening] Use the CompilerInstance's DiagnosticEngine
This keeps us honest about the const-ness of the CompilerInstance itself: emitting diagnostics is a transitively mutating operation.
1 parent a4c883f commit a082e9d

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

lib/FrontendTool/FrontendTool.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,9 @@ static bool compileLLVMIR(CompilerInstance &Instance) {
791791
inputsAndOutputs.getFilenameOfFirstInput());
792792

793793
if (!FileBufOrErr) {
794-
Instance.getASTContext().Diags.diagnose(
795-
SourceLoc(), diag::error_open_input_file,
796-
inputsAndOutputs.getFilenameOfFirstInput(),
797-
FileBufOrErr.getError().message());
794+
Instance.getDiags().diagnose(SourceLoc(), diag::error_open_input_file,
795+
inputsAndOutputs.getFilenameOfFirstInput(),
796+
FileBufOrErr.getError().message());
798797
return true;
799798
}
800799
llvm::MemoryBuffer *MainFile = FileBufOrErr.get().get();
@@ -806,9 +805,9 @@ static bool compileLLVMIR(CompilerInstance &Instance) {
806805
if (!Module) {
807806
// TODO: Translate from the diagnostic info to the SourceManager location
808807
// if available.
809-
Instance.getASTContext().Diags.diagnose(
810-
SourceLoc(), diag::error_parse_input_file,
811-
inputsAndOutputs.getFilenameOfFirstInput(), Err.getMessage());
808+
Instance.getDiags().diagnose(SourceLoc(), diag::error_parse_input_file,
809+
inputsAndOutputs.getFilenameOfFirstInput(),
810+
Err.getMessage());
812811
return true;
813812
}
814813
return performLLVM(Invocation.getIRGenOptions(), Instance.getASTContext(),
@@ -927,7 +926,7 @@ static void emitReferenceDependenciesForAllPrimaryInputsIfNeeded(
927926
if (Invocation.getFrontendOptions()
928927
.InputsAndOutputs.hasReferenceDependenciesPath() &&
929928
Instance.getPrimarySourceFiles().empty()) {
930-
Instance.getASTContext().Diags.diagnose(
929+
Instance.getDiags().diagnose(
931930
SourceLoc(), diag::emit_reference_dependencies_without_primary_file);
932931
return;
933932
}
@@ -936,48 +935,47 @@ static void emitReferenceDependenciesForAllPrimaryInputsIfNeeded(
936935
Invocation.getReferenceDependenciesFilePathForPrimary(
937936
SF->getFilename());
938937
if (!referenceDependenciesFilePath.empty()) {
939-
auto LangOpts = Invocation.getLangOptions();
938+
const auto LangOpts = Invocation.getLangOptions();
940939
(void)fine_grained_dependencies::emitReferenceDependencies(
941-
Instance.getASTContext().Diags, SF,
942-
*Instance.getDependencyTracker(),
940+
Instance.getDiags(), SF, *Instance.getDependencyTracker(),
943941
referenceDependenciesFilePath,
944942
LangOpts.EmitFineGrainedDependencySourcefileDotFiles);
945943
}
946944
}
947945
}
948946
static void
949-
emitSwiftRangesForAllPrimaryInputsIfNeeded(const CompilerInstance &Instance) {
947+
emitSwiftRangesForAllPrimaryInputsIfNeeded(CompilerInstance &Instance) {
950948
const auto &Invocation = Instance.getInvocation();
951949
if (Invocation.getFrontendOptions().InputsAndOutputs.hasSwiftRangesPath() &&
952950
Instance.getPrimarySourceFiles().empty()) {
953-
Instance.getASTContext().Diags.diagnose(
954-
SourceLoc(), diag::emit_swift_ranges_without_primary_file);
951+
Instance.getDiags().diagnose(SourceLoc(),
952+
diag::emit_swift_ranges_without_primary_file);
955953
return;
956954
}
957955
for (auto *SF : Instance.getPrimarySourceFiles()) {
958956
const std::string &swiftRangesFilePath =
959957
Invocation.getSwiftRangesFilePathForPrimary(SF->getFilename());
960958
if (!swiftRangesFilePath.empty()) {
961-
(void)Instance.emitSwiftRanges(Instance.getASTContext().Diags, SF,
959+
(void)Instance.emitSwiftRanges(Instance.getDiags(), SF,
962960
swiftRangesFilePath);
963961
}
964962
}
965963
}
966964
static void emitCompiledSourceForAllPrimaryInputsIfNeeded(
967-
const CompilerInstance &Instance) {
965+
CompilerInstance &Instance) {
968966
const auto &Invocation = Instance.getInvocation();
969967
if (Invocation.getFrontendOptions()
970968
.InputsAndOutputs.hasCompiledSourcePath() &&
971969
Instance.getPrimarySourceFiles().empty()) {
972-
Instance.getASTContext().Diags.diagnose(
970+
Instance.getDiags().diagnose(
973971
SourceLoc(), diag::emit_compiled_source_without_primary_file);
974972
return;
975973
}
976974
for (auto *SF : Instance.getPrimarySourceFiles()) {
977975
const std::string &compiledSourceFilePath =
978976
Invocation.getCompiledSourceFilePathForPrimary(SF->getFilename());
979977
if (!compiledSourceFilePath.empty()) {
980-
(void)Instance.emitCompiledSource(Instance.getASTContext().Diags, SF,
978+
(void)Instance.emitCompiledSource(Instance.getDiags(), SF,
981979
compiledSourceFilePath);
982980
}
983981
}
@@ -1037,9 +1035,8 @@ static bool writeLdAddCFileIfNeeded(CompilerInstance &Instance) {
10371035
std::error_code EC;
10381036
llvm::raw_fd_ostream OS(Path, EC, llvm::sys::fs::F_None);
10391037
if (EC) {
1040-
module->getASTContext().Diags.diagnose(SourceLoc(),
1041-
diag::error_opening_output,
1042-
Path, EC.message());
1038+
Instance.getDiags().diagnose(SourceLoc(), diag::error_opening_output, Path,
1039+
EC.message());
10431040
return true;
10441041
}
10451042
OS << "// Automatically generated C source file from the Swift compiler \n"
@@ -1254,7 +1251,7 @@ static bool performCompile(CompilerInstance &Instance,
12541251
scanDependencies(Instance);
12551252
}
12561253

1257-
(void)emitMakeDependenciesIfNeeded(Context.Diags,
1254+
(void)emitMakeDependenciesIfNeeded(Instance.getDiags(),
12581255
Instance.getDependencyTracker(), opts);
12591256

12601257
if (Action == FrontendOptions::ActionType::ResolveImports ||

0 commit comments

Comments
 (0)