[Frontend] When setting up the buffers for a CompilerInstance, allow a memory buffer to

provide the contents of a filename, based on its buffer identifier.

Swift SVN r10042
This commit is contained in:
Argyrios Kyrtzidis
2013-11-08 18:29:38 +00:00
parent e7b15e3a7f
commit 6fb1dce5c1
3 changed files with 41 additions and 13 deletions

View File

@@ -94,7 +94,24 @@ bool swift::CompilerInstance::setup(const CompilerInvocation &Invok) {
bool MainMode = (Invocation.getInputKind() == SourceFile::Main);
bool SILMode = (Invocation.getInputKind() == SourceFile::SIL);
// Add the memory buffers first, these will be associated with a filename
// and they can replace the contents of an input filename.
for (auto Buf : Invocation.getInputBuffers()) {
if (SILMode)
MainBufferIndex = BufferIDs.size();
// CompilerInvocation doesn't own the buffers, copy to a new buffer.
BufferIDs.push_back(SourceMgr.addNewSourceBuffer(
llvm::MemoryBuffer::getMemBufferCopy(Buf->getBuffer(),
Buf->getBufferIdentifier())));
}
for (auto &File : Invocation.getInputFilenames()) {
// FIXME: Working with filenames is fragile, maybe use the real path
// or have some kind of FileManager.
if (SourceMgr.getIDForBufferIdentifier(File).hasValue())
continue; // replaced by a memory buffer.
// Open the input file.
llvm::OwningPtr<llvm::MemoryBuffer> InputFile;
if (llvm::error_code Err =
@@ -112,16 +129,6 @@ bool swift::CompilerInstance::setup(const CompilerInvocation &Invok) {
BufferIDs.push_back(SourceMgr.addNewSourceBuffer(InputFile.take()));
}
for (auto Buf : Invocation.getInputBuffers()) {
if (SILMode)
MainBufferIndex = BufferIDs.size();
// CompilerInvocation doesn't own the buffers, copy to a new buffer.
BufferIDs.push_back(SourceMgr.addNewSourceBuffer(
llvm::MemoryBuffer::getMemBufferCopy(Buf->getBuffer(),
Buf->getBufferIdentifier())));
}
if (MainMode && MainBufferIndex == NO_SUCH_BUFFER && BufferIDs.size() == 1)
MainBufferIndex = 0;