Skip to content

Commit 676c396

Browse files
author
David Ungar
authored
Merge pull request #13397 from davidungar/PR5-setBuffer-rb2
Eliminate InputFile::setBuffer()
2 parents ec5c7ae + a13e114 commit 676c396

File tree

3 files changed

+81
-57
lines changed

3 files changed

+81
-57
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ class InputFile {
5858
return Filename;
5959
}
6060

61-
void setBuffer(llvm::MemoryBuffer *buffer) { Buffer = buffer; }
62-
6361
/// Return Swift-standard file name from a buffer name set by
6462
/// llvm::MemoryBuffer::getFileOrSTDIN, which uses "<stdin>" instead of "-".
6563
static StringRef convertBufferNameFromLLVM_getFileOrSTDIN_toSwiftConventions(
@@ -194,16 +192,12 @@ class FrontendInputs {
194192
addInput(InputFile(file, true, buffer));
195193
}
196194

197-
void setBuffer(llvm::MemoryBuffer *buffer, unsigned index) {
198-
AllFiles[index].setBuffer(buffer);
199-
}
200-
201195
void addInput(const InputFile &input) {
202196
if (!input.file().empty() && input.isPrimary())
203197
PrimaryInputs.insert(std::make_pair(input.file(), AllFiles.size()));
204198
AllFiles.push_back(input);
205199
}
206-
200+
207201
void clearInputs() {
208202
AllFiles.clear();
209203
PrimaryInputs.clear();

lib/Frontend/FrontendOptions.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ bool FrontendInputs::verifyInputs(DiagnosticEngine &diags, bool treatAsSIL,
9898

9999
bool FrontendInputs::areAllNonPrimariesSIB() const {
100100
for (const InputFile &input : getAllFiles()) {
101-
assert(!input.file().empty() && "all files have (perhaps pseudo) names");
102101
if (input.isPrimary())
103102
continue;
104103
if (!llvm::sys::path::extension(input.file()).endswith(SIB_EXTENSION)) {

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ struct InvocationOptions {
8888
}
8989

9090
void applyTo(CompilerInvocation &CompInvok) const;
91+
void applyToSubstitutingInputs(CompilerInvocation &CompInvok,
92+
FrontendInputs &&Inputs) const;
9193
void profile(llvm::FoldingSetNodeID &ID) const;
9294
void raw(std::vector<std::string> &Args, std::string &PrimaryFile) const;
9395

@@ -132,6 +134,11 @@ void SwiftInvocation::raw(std::vector<std::string> &Args,
132134
void InvocationOptions::applyTo(CompilerInvocation &CompInvok) const {
133135
CompInvok = this->Invok;
134136
}
137+
void InvocationOptions::applyToSubstitutingInputs(
138+
CompilerInvocation &CompInvok, FrontendInputs &&inputs) const {
139+
CompInvok = this->Invok;
140+
CompInvok.getFrontendOptions().Inputs = inputs;
141+
}
135142

136143
void InvocationOptions::raw(std::vector<std::string> &Args,
137144
std::string &PrimaryFile) const {
@@ -231,15 +238,20 @@ typedef uint64_t BufferStamp;
231238

232239
struct FileContent {
233240
ImmutableTextSnapshotRef Snapshot;
241+
std::string Filename;
234242
std::unique_ptr<llvm::MemoryBuffer> Buffer;
243+
bool IsPrimary;
235244
BufferStamp Stamp;
236245

237-
FileContent(ImmutableTextSnapshotRef Snapshot,
238-
std::unique_ptr<llvm::MemoryBuffer> Buffer,
246+
FileContent(ImmutableTextSnapshotRef Snapshot, std::string Filename,
247+
std::unique_ptr<llvm::MemoryBuffer> Buffer, bool IsPrimary,
239248
BufferStamp Stamp)
240-
: Snapshot(std::move(Snapshot)),
241-
Buffer(std::move(Buffer)),
242-
Stamp(Stamp) {}
249+
: Snapshot(std::move(Snapshot)), Filename(Filename),
250+
Buffer(std::move(Buffer)), IsPrimary(IsPrimary), Stamp(Stamp) {}
251+
252+
explicit operator InputFile() const {
253+
return InputFile(Filename, IsPrimary, Buffer.get());
254+
}
243255
};
244256

245257
class ASTProducer : public ThreadSafeRefCountedBase<ASTProducer> {
@@ -284,6 +296,11 @@ class ASTProducer : public ThreadSafeRefCountedBase<ASTProducer> {
284296
ASTUnitRef createASTUnit(SwiftASTManager::Implementation &MgrImpl,
285297
ArrayRef<ImmutableTextSnapshotRef> Snapshots,
286298
std::string &Error);
299+
300+
void findSnapshotAndOpenFiles(SwiftASTManager::Implementation &MgrImpl,
301+
ArrayRef<ImmutableTextSnapshotRef> Snapshots,
302+
SmallVectorImpl<FileContent> &Contents,
303+
std::string &Error) const;
287304
};
288305

289306
typedef IntrusiveRefCntPtr<ASTProducer> ASTProducerRef;
@@ -330,7 +347,8 @@ struct SwiftASTManager::Implementation {
330347
"sourcekit.swift.ASTBuilding" };
331348

332349
ASTProducerRef getASTProducer(SwiftInvocationRef InvokRef);
333-
FileContent getFileContent(StringRef FilePath, std::string &Error);
350+
FileContent getFileContent(StringRef FilePath, bool IsPrimary,
351+
std::string &Error);
334352
BufferStamp getBufferStamp(StringRef FilePath);
335353
std::unique_ptr<llvm::MemoryBuffer> getMemoryBuffer(StringRef Filename,
336354
std::string &Error);
@@ -391,6 +409,14 @@ static void sanitizeCompilerArgs(ArrayRef<const char *> Args,
391409
}
392410
}
393411

412+
static FrontendInputs
413+
convertFileContentsToInputs(const SmallVectorImpl<FileContent> &contents) {
414+
FrontendInputs inputs;
415+
for (const FileContent &content : contents)
416+
inputs.addInput(InputFile(content));
417+
return inputs;
418+
}
419+
394420
static FrontendInputs
395421
resolveSymbolicLinksInInputs(FrontendInputs &inputs,
396422
StringRef UnresolvedPrimaryFile,
@@ -547,23 +573,25 @@ SwiftASTManager::Implementation::getASTProducer(SwiftInvocationRef InvokRef) {
547573
}
548574

549575
static FileContent getFileContentFromSnap(ImmutableTextSnapshotRef Snap,
550-
StringRef FilePath) {
576+
bool IsPrimary, StringRef FilePath) {
551577
auto Buf = llvm::MemoryBuffer::getMemBufferCopy(
552578
Snap->getBuffer()->getText(), FilePath);
553-
return FileContent(Snap, std::move(Buf), Snap->getStamp());
579+
return FileContent(Snap, FilePath, std::move(Buf), IsPrimary,
580+
Snap->getStamp());
554581
}
555582

556-
FileContent
557-
SwiftASTManager::Implementation::getFileContent(StringRef UnresolvedPath,
558-
std::string &Error) {
583+
FileContent SwiftASTManager::Implementation::getFileContent(
584+
StringRef UnresolvedPath, bool IsPrimary, std::string &Error) {
559585
std::string FilePath = SwiftLangSupport::resolvePathSymlinks(UnresolvedPath);
560586
if (auto EditorDoc = EditorDocs.findByPath(FilePath))
561-
return getFileContentFromSnap(EditorDoc->getLatestSnapshot(), FilePath);
587+
return getFileContentFromSnap(EditorDoc->getLatestSnapshot(), IsPrimary,
588+
FilePath);
562589

563590
// FIXME: Is there a way to get timestamp and buffer for a file atomically ?
564591
auto Stamp = getBufferStamp(FilePath);
565592
auto Buffer = getMemoryBuffer(FilePath, Error);
566-
return FileContent(nullptr, std::move(Buffer), Stamp);
593+
return FileContent(nullptr, UnresolvedPath, std::move(Buffer), IsPrimary,
594+
Stamp);
567595
}
568596

569597
BufferStamp SwiftASTManager::Implementation::getBufferStamp(StringRef FilePath){
@@ -766,44 +794,17 @@ ASTUnitRef ASTProducer::createASTUnit(SwiftASTManager::Implementation &MgrImpl,
766794
Stamps.clear();
767795
DependencyStamps.clear();
768796

769-
const InvocationOptions &Opts = InvokRef->Impl.Opts;
770-
771797
SmallVector<FileContent, 8> Contents;
772-
for (const auto &input :
773-
Opts.Invok.getFrontendOptions().Inputs.getAllFiles()) {
774-
StringRef File = input.file();
775-
if (File.empty())
776-
continue;
777-
bool FoundSnapshot = false;
778-
for (auto &Snap : Snapshots) {
779-
if (Snap->getFilename() == File) {
780-
FoundSnapshot = true;
781-
Contents.push_back(getFileContentFromSnap(Snap, File));
782-
break;
783-
}
784-
}
785-
if (FoundSnapshot)
786-
break;
787-
788-
auto Content = MgrImpl.getFileContent(File, Error);
789-
if (!Content.Buffer) {
790-
LOG_WARN_FUNC("failed getting file contents for " << File << ": " << Error);
791-
// File may not exist, continue and recover as if it was empty.
792-
Content.Buffer = llvm::MemoryBuffer::getNewMemBuffer(0, File);
793-
}
794-
Contents.push_back(std::move(Content));
795-
}
796-
assert(Contents.size() ==
797-
Opts.Invok.getFrontendOptions().Inputs.inputCount());
798+
findSnapshotAndOpenFiles(MgrImpl, Snapshots, Contents, Error);
798799

799800
for (auto &Content : Contents)
800801
Stamps.push_back(Content.Stamp);
801802

802803
trace::SwiftInvocation TraceInfo;
803804

804805
if (trace::enabled()) {
805-
TraceInfo.Args.PrimaryFile = Opts.PrimaryFile;
806-
TraceInfo.Args.Args = Opts.Args;
806+
TraceInfo.Args.PrimaryFile = InvokRef->Impl.Opts.PrimaryFile;
807+
TraceInfo.Args.Args = InvokRef->Impl.Opts.Args;
807808
}
808809

809810
ASTUnitRef ASTRef = new ASTUnit(++ASTUnitGeneration, MgrImpl.Stats);
@@ -814,7 +815,6 @@ ASTUnitRef ASTProducer::createASTUnit(SwiftASTManager::Implementation &MgrImpl,
814815
if (trace::enabled()) {
815816
TraceInfo.addFile(Content.Buffer->getBufferIdentifier(),
816817
Content.Buffer->getBuffer());
817-
818818
}
819819
}
820820
auto &CompIns = ASTRef->Impl.CompInst;
@@ -824,12 +824,10 @@ ASTUnitRef ASTProducer::createASTUnit(SwiftASTManager::Implementation &MgrImpl,
824824
CompIns.addDiagnosticConsumer(&Consumer);
825825

826826
CompilerInvocation Invocation;
827-
Opts.applyTo(Invocation);
827+
InvokRef->Impl.Opts.applyToSubstitutingInputs(
828+
Invocation, convertFileContentsToInputs(Contents));
829+
828830
Invocation.getLangOptions().KeepSyntaxInfoInSourceFile = true;
829-
for (auto i : indices(Contents)) {
830-
Invocation.getFrontendOptions().Inputs.setBuffer(Contents[i].Buffer.get(),
831-
i);
832-
}
833831

834832
if (CompIns.setup(Invocation)) {
835833
// FIXME: Report the diagnostic.
@@ -891,3 +889,36 @@ ASTUnitRef ASTProducer::createASTUnit(SwiftASTManager::Implementation &MgrImpl,
891889

892890
return ASTRef;
893891
}
892+
893+
void ASTProducer::findSnapshotAndOpenFiles(
894+
SwiftASTManager::Implementation &MgrImpl,
895+
ArrayRef<ImmutableTextSnapshotRef> Snapshots,
896+
SmallVectorImpl<FileContent> &Contents, std::string &Error) const {
897+
const InvocationOptions &Opts = InvokRef->Impl.Opts;
898+
for (const auto &input :
899+
Opts.Invok.getFrontendOptions().Inputs.getAllFiles()) {
900+
StringRef File = input.file();
901+
bool IsPrimary = input.isPrimary();
902+
bool FoundSnapshot = false;
903+
for (auto &Snap : Snapshots) {
904+
if (Snap->getFilename() == File) {
905+
FoundSnapshot = true;
906+
Contents.push_back(getFileContentFromSnap(Snap, IsPrimary, File));
907+
break;
908+
}
909+
}
910+
if (FoundSnapshot)
911+
continue;
912+
913+
auto Content = MgrImpl.getFileContent(File, IsPrimary, Error);
914+
if (!Content.Buffer) {
915+
LOG_WARN_FUNC("failed getting file contents for " << File << ": "
916+
<< Error);
917+
// File may not exist, continue and recover as if it was empty.
918+
Content.Buffer = llvm::MemoryBuffer::getNewMemBuffer(0, File);
919+
}
920+
Contents.push_back(std::move(Content));
921+
}
922+
assert(Contents.size() ==
923+
Opts.Invok.getFrontendOptions().Inputs.inputCount());
924+
}

0 commit comments

Comments
 (0)