Skip to content

Commit a8766cc

Browse files
committed
[NFC] Refactor InputFile's Accessors
1 parent 0a5a6f1 commit a8766cc

File tree

9 files changed

+69
-38
lines changed

9 files changed

+69
-38
lines changed

include/swift/Frontend/InputFile.h

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
#ifndef SWIFT_FRONTEND_INPUTFILE_H
1414
#define SWIFT_FRONTEND_INPUTFILE_H
1515

16+
#include "swift/Basic/FileTypes.h"
1617
#include "swift/Basic/PrimarySpecificPaths.h"
1718
#include "swift/Basic/SupplementaryOutputPaths.h"
19+
#include "llvm/ADT/PointerIntPair.h"
1820
#include "llvm/Support/MemoryBuffer.h"
21+
#include "llvm/Support/Path.h"
1922
#include <string>
2023

2124
namespace swift {
@@ -67,9 +70,23 @@ class InputFile final {
6770
assert(!name.empty());
6871
}
6972

70-
bool isPrimary() const { return IsPrimary; }
71-
llvm::MemoryBuffer *buffer() const { return Buffer; }
72-
const std::string &file() const {
73+
public:
74+
/// Retrieves the type of this input file.
75+
file_types::ID getType() const { return FileID; };
76+
77+
/// Retrieves whether this input file was passed as a primary to the frontend.
78+
bool isPrimary() const { return BufferAndIsPrimary.getInt(); }
79+
80+
/// Retrieves the backing buffer for this input file, if any.
81+
llvm::MemoryBuffer *getBuffer() const {
82+
return BufferAndIsPrimary.getPointer();
83+
}
84+
85+
/// The name of this \c InputFile, or `-` if this input corresponds to the
86+
/// standard input stream.
87+
///
88+
/// The returned file name is guaranteed not to be the empty string.
89+
const std::string &getFileName() const {
7390
assert(!Filename.empty());
7491
return Filename;
7592
}
@@ -81,12 +98,22 @@ class InputFile final {
8198
return filename.equals("<stdin>") ? "-" : filename;
8299
}
83100

101+
/// Retrieves the name of the output file corresponding to this input.
102+
///
103+
/// If there is no such corresponding file, the result is the empty string.
104+
/// If there the resulting output should be directed to the standard output
105+
/// stream, the result is "-".
84106
std::string outputFilename() const { return PSPs.OutputFilename; }
85107

108+
/// If there are explicit primary inputs (i.e. designated with -primary-input
109+
/// or -primary-filelist), the paths specific to those inputs (other than the
110+
/// input file path itself) are kept here. If there are no explicit primary
111+
/// inputs (for instance for whole module optimization), the corresponding
112+
/// paths are kept in the first input file.
86113
const PrimarySpecificPaths &getPrimarySpecificPaths() const { return PSPs; }
87114

88-
void setPrimarySpecificPaths(const PrimarySpecificPaths &PSPs) {
89-
this->PSPs = PSPs;
115+
void setPrimarySpecificPaths(PrimarySpecificPaths &&PSPs) {
116+
this->PSPs = std::move(PSPs);
90117
}
91118

92119
// The next set of functions provides access to those primary-specific paths

lib/Frontend/ArgsToFrontendOutputsConverter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ OutputFilesComputer::computeOutputFile(StringRef outputArg,
183183

184184
Optional<std::string>
185185
OutputFilesComputer::deriveOutputFileFromInput(const InputFile &input) const {
186-
if (input.file() == "-" || HasTextualOutput)
186+
if (input.getFileName() == "-" || HasTextualOutput)
187187
return std::string("-");
188188

189189
std::string baseName = determineBaseNameOfOutput(input);
@@ -210,7 +210,7 @@ std::string
210210
OutputFilesComputer::determineBaseNameOfOutput(const InputFile &input) const {
211211
std::string nameToStem =
212212
input.isPrimary()
213-
? input.file()
213+
? input.getFileName()
214214
: ModuleNameArg ? ModuleNameArg->getValue() : FirstInput;
215215
return llvm::sys::path::stem(nameToStem).str();
216216
}
@@ -474,8 +474,8 @@ StringRef SupplementaryOutputPathsComputer::
474474
if (!outputFilename.empty() && outputFilename != "-")
475475
return outputFilename;
476476

477-
if (input.isPrimary() && input.file() != "-")
478-
return llvm::sys::path::filename(input.file());
477+
if (input.isPrimary() && input.getFileName() != "-")
478+
return llvm::sys::path::filename(input.getFileName());
479479

480480
return ModuleName;
481481
}
@@ -600,12 +600,12 @@ SupplementaryOutputPathsComputer::readSupplementaryOutputFileMap() const {
600600
InputsAndOutputs.forEachInputProducingSupplementaryOutput(
601601
[&](const InputFile &input) -> bool {
602602
const TypeToPathMap *mapForInput =
603-
OFM->getOutputMapForInput(input.file());
603+
OFM->getOutputMapForInput(input.getFileName());
604604
if (!mapForInput) {
605605
Diags.diagnose(
606606
SourceLoc(),
607607
diag::error_missing_entry_in_supplementary_output_file_map,
608-
supplementaryFileMapPath, input.file());
608+
supplementaryFileMapPath, input.getFileName());
609609
hadError = true;
610610
}
611611
outputPaths.push_back(createFromTypeToPathMap(mapForInput));

lib/Frontend/Frontend.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,9 @@ bool CompilerInstance::setUpInputs() {
633633

634634
Optional<unsigned> CompilerInstance::getRecordedBufferID(const InputFile &input,
635635
bool &failed) {
636-
if (!input.buffer()) {
636+
if (!input.getBuffer()) {
637637
if (Optional<unsigned> existingBufferID =
638-
SourceMgr.getIDForBufferIdentifier(input.file())) {
638+
SourceMgr.getIDForBufferIdentifier(input.getFileName())) {
639639
return existingBufferID;
640640
}
641641
}
@@ -663,17 +663,18 @@ Optional<unsigned> CompilerInstance::getRecordedBufferID(const InputFile &input,
663663

664664
Optional<ModuleBuffers> CompilerInstance::getInputBuffersIfPresent(
665665
const InputFile &input) {
666-
if (auto b = input.buffer()) {
666+
if (auto b = input.getBuffer()) {
667667
return ModuleBuffers(llvm::MemoryBuffer::getMemBufferCopy(b->getBuffer(),
668668
b->getBufferIdentifier()));
669669
}
670670
// FIXME: Working with filenames is fragile, maybe use the real path
671671
// or have some kind of FileManager.
672672
using FileOrError = llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>;
673673
FileOrError inputFileOrErr = swift::vfs::getFileOrSTDIN(getFileSystem(),
674-
input.file());
674+
input.getFileName());
675675
if (!inputFileOrErr) {
676-
Diagnostics.diagnose(SourceLoc(), diag::error_open_input_file, input.file(),
676+
Diagnostics.diagnose(SourceLoc(), diag::error_open_input_file,
677+
input.getFileName(),
677678
inputFileOrErr.getError().message());
678679
return None;
679680
}
@@ -689,7 +690,7 @@ Optional<ModuleBuffers> CompilerInstance::getInputBuffersIfPresent(
689690

690691
Optional<std::unique_ptr<llvm::MemoryBuffer>>
691692
CompilerInstance::openModuleSourceInfo(const InputFile &input) {
692-
llvm::SmallString<128> pathWithoutProjectDir(input.file());
693+
llvm::SmallString<128> pathWithoutProjectDir(input.getFileName());
693694
llvm::sys::path::replace_extension(pathWithoutProjectDir,
694695
file_types::getExtension(file_types::TY_SwiftSourceInfoFile));
695696
llvm::SmallString<128> pathWithProjectDir = pathWithoutProjectDir.str();
@@ -708,7 +709,7 @@ CompilerInstance::openModuleSourceInfo(const InputFile &input) {
708709

709710
Optional<std::unique_ptr<llvm::MemoryBuffer>>
710711
CompilerInstance::openModuleDoc(const InputFile &input) {
711-
llvm::SmallString<128> moduleDocFilePath(input.file());
712+
llvm::SmallString<128> moduleDocFilePath(input.getFileName());
712713
llvm::sys::path::replace_extension(
713714
moduleDocFilePath,
714715
file_types::getExtension(file_types::TY_SwiftModuleDocFile));
@@ -773,9 +774,12 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
773774

774775
bool CompilerInstance::createFilesForMainModule(
775776
ModuleDecl *mod, SmallVectorImpl<FileUnit *> &files) const {
776-
// Make sure the main file is the first file in the module.
777-
if (MainBufferID != NO_SUCH_BUFFER) {
778-
files.push_back(mainFile);
777+
// Try to pull out the main source file, if any. This ensures that it
778+
// is at the start of the list of files.
779+
Optional<unsigned> MainBufferID = None;
780+
if (SourceFile *mainSourceFile = computeMainSourceFileForModule(mod)) {
781+
MainBufferID = mainSourceFile->getBufferID();
782+
files.push_back(mainSourceFile);
779783
}
780784

781785
// If we have partial modules to load, do so now, bailing if any failed to

lib/Frontend/FrontendInputsAndOutputs.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ operator=(const FrontendInputsAndOutputs &other) {
5454
std::vector<std::string> FrontendInputsAndOutputs::getInputFilenames() const {
5555
std::vector<std::string> filenames;
5656
for (auto &input : AllInputs) {
57-
filenames.push_back(input.file());
57+
filenames.push_back(input.getFileName());
5858
}
5959
return filenames;
6060
}
@@ -66,7 +66,7 @@ bool FrontendInputsAndOutputs::isReadingFromStdin() const {
6666
const std::string &FrontendInputsAndOutputs::getFilenameOfFirstInput() const {
6767
assert(hasInputs());
6868
const InputFile &inp = AllInputs[0];
69-
const std::string &f = inp.file();
69+
const std::string &f = inp.getFileName();
7070
assert(!f.empty());
7171
return f;
7272
}
@@ -135,7 +135,7 @@ FrontendInputsAndOutputs::getRequiredUniquePrimaryInput() const {
135135
std::string FrontendInputsAndOutputs::getStatsFileMangledInputName() const {
136136
// Use the first primary, even if there are multiple primaries.
137137
// That's enough to keep the file names unique.
138-
return isWholeModule() ? "all" : firstPrimaryInput().file();
138+
return isWholeModule() ? "all" : firstPrimaryInput().getFileName();
139139
}
140140

141141
bool FrontendInputsAndOutputs::isInputPrimary(StringRef file) const {
@@ -146,7 +146,7 @@ unsigned FrontendInputsAndOutputs::numberOfPrimaryInputsEndingWith(
146146
StringRef extension) const {
147147
unsigned n = 0;
148148
(void)forEachPrimaryInput([&](const InputFile &input) -> bool {
149-
if (llvm::sys::path::extension(input.file()).endswith(extension))
149+
if (llvm::sys::path::extension(input.getFileName()).endswith(extension))
150150
++n;
151151
return false;
152152
});
@@ -216,7 +216,7 @@ bool FrontendInputsAndOutputs::areAllNonPrimariesSIB() const {
216216
for (const InputFile &input : AllInputs) {
217217
if (input.isPrimary())
218218
continue;
219-
StringRef extension = llvm::sys::path::extension(input.file());
219+
StringRef extension = llvm::sys::path::extension(input.getFileName());
220220
if (file_types::lookupTypeForExtension(extension) != file_types::TY_SIB) {
221221
return false;
222222
}
@@ -269,7 +269,7 @@ void FrontendInputsAndOutputs::addInput(const InputFile &input) {
269269
AllInputs.push_back(input);
270270
if (input.isPrimary()) {
271271
PrimaryInputsInOrder.push_back(index);
272-
PrimaryInputsByName.insert(std::make_pair(AllInputs.back().file(), index));
272+
PrimaryInputsByName.insert({AllInputs.back().getFileName(), index});
273273
}
274274
}
275275

@@ -330,7 +330,7 @@ void FrontendInputsAndOutputs::setMainAndSupplementaryOutputs(
330330
for (auto &input : AllInputs) {
331331
if (input.isPrimary()) {
332332
input.setPrimarySpecificPaths(PrimarySpecificPaths(
333-
outputFiles[i], input.file(), supplementaryOutputs[i]));
333+
outputFiles[i], input.getFileName(), supplementaryOutputs[i]));
334334
++i;
335335
}
336336
}
@@ -340,7 +340,7 @@ void FrontendInputsAndOutputs::setMainAndSupplementaryOutputs(
340340
"WMO only ever produces one set of supplementary outputs");
341341
if (outputFiles.size() == 1) {
342342
AllInputs.front().setPrimarySpecificPaths(PrimarySpecificPaths(
343-
outputFiles.front(), firstInputProducingOutput().file(),
343+
outputFiles.front(), firstInputProducingOutput().getFileName(),
344344
supplementaryOutputs.front()));
345345
return;
346346
}

lib/Frontend/ModuleInterfaceBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
179179
bool isTypeChecking =
180180
(FEOpts.RequestedAction == FrontendOptions::ActionType::Typecheck);
181181
const auto &InputInfo = FEOpts.InputsAndOutputs.firstInput();
182-
StringRef InPath = InputInfo.file();
182+
StringRef InPath = InputInfo.getFileName();
183183
const auto &OutputInfo =
184184
InputInfo.getPrimarySpecificPaths().SupplementaryOutputs;
185185
StringRef OutPath = OutputInfo.ModuleOutputPath;

lib/FrontendTool/FrontendTool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ createDispatchingDiagnosticConsumerIfNeeded(
24142414
inputsAndOutputs.forEachInputProducingSupplementaryOutput(
24152415
[&](const InputFile &input) -> bool {
24162416
if (auto consumer = maybeCreateConsumerForDiagnosticsFrom(input))
2417-
subconsumers.emplace_back(input.file(), std::move(consumer));
2417+
subconsumers.emplace_back(input.getFileName(), std::move(consumer));
24182418
return false;
24192419
});
24202420
// For batch mode, the compiler must sometimes swallow diagnostics pertaining
@@ -2431,7 +2431,7 @@ createDispatchingDiagnosticConsumerIfNeeded(
24312431
if (!subconsumers.empty() && inputsAndOutputs.hasMultiplePrimaryInputs()) {
24322432
inputsAndOutputs.forEachNonPrimaryInput(
24332433
[&](const InputFile &input) -> bool {
2434-
subconsumers.emplace_back(input.file(), nullptr);
2434+
subconsumers.emplace_back(input.getFileName(), nullptr);
24352435
return false;
24362436
});
24372437
}

lib/IDE/Utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ static FrontendInputsAndOutputs resolveSymbolicLinksInInputs(
206206
FrontendInputsAndOutputs replacementInputsAndOutputs;
207207
for (const InputFile &input : inputsAndOutputs.getAllInputs()) {
208208
llvm::SmallString<128> newFilename;
209-
if (auto err = FileSystem->getRealPath(input.file(), newFilename))
210-
newFilename = input.file();
209+
if (auto err = FileSystem->getRealPath(input.getFileName(), newFilename))
210+
newFilename = input.getFileName();
211211
llvm::sys::path::native(newFilename);
212212
bool newIsPrimary = input.isPrimary() ||
213213
(!PrimaryFile.empty() && PrimaryFile == newFilename);
@@ -216,7 +216,7 @@ static FrontendInputsAndOutputs resolveSymbolicLinksInInputs(
216216
}
217217
assert(primaryCount < 2 && "cannot handle multiple primaries");
218218
replacementInputsAndOutputs.addInput(
219-
InputFile(newFilename.str(), newIsPrimary, input.buffer()));
219+
InputFile(newFilename.str(), newIsPrimary, input.getBuffer()));
220220
}
221221

222222
if (PrimaryFile.empty() || primaryCount == 1) {

lib/Migrator/Migrator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,5 +435,5 @@ const MigratorOptions &Migrator::getMigratorOptions() const {
435435
const StringRef Migrator::getInputFilename() const {
436436
auto &PrimaryInput = StartInvocation.getFrontendOptions()
437437
.InputsAndOutputs.getRequiredUniquePrimaryInput();
438-
return PrimaryInput.file();
438+
return PrimaryInput.getFileName();
439439
}

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ bool ASTProducer::shouldRebuild(
711711
Invok.Opts.Invok.getFrontendOptions().InputsAndOutputs.inputCount());
712712
for (const auto &input :
713713
Invok.Opts.Invok.getFrontendOptions().InputsAndOutputs.getAllInputs()) {
714-
const std::string &File = input.file();
714+
const std::string &File = input.getFileName();
715715
bool FoundSnapshot = false;
716716
for (auto &Snap : Snapshots) {
717717
if (Snap->getFilename() == File) {
@@ -906,7 +906,7 @@ void ASTProducer::findSnapshotAndOpenFiles(
906906
const InvocationOptions &Opts = InvokRef->Impl.Opts;
907907
for (const auto &input :
908908
Opts.Invok.getFrontendOptions().InputsAndOutputs.getAllInputs()) {
909-
const std::string &File = input.file();
909+
const std::string &File = input.getFileName();
910910
bool IsPrimary = input.isPrimary();
911911
bool FoundSnapshot = false;
912912
for (auto &Snap : Snapshots) {

0 commit comments

Comments
 (0)