int LoadedID = 0, unsigned LoadedOffset = 0,
SourceLocation IncludeLoc = SourceLocation());
- enum UnownedTag { Unowned };
-
/// Create a new FileID that represents the specified memory buffer.
///
/// This does not take ownership of the MemoryBuffer. The memory buffer must
/// outlive the SourceManager.
- FileID createFileID(UnownedTag, const llvm::MemoryBuffer *Buffer,
+ FileID createFileID(const llvm::MemoryBufferRef &Buffer,
SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
int LoadedID = 0, unsigned LoadedOffset = 0,
SourceLocation IncludeLoc = SourceLocation());
return getFakeBufferForRecovery()->getMemBufferRef();
}
- /// Return the buffer for the specified FileID.
- ///
- /// If there is an error opening this buffer the first time, this
- /// manufactures a temporary buffer and returns a non-empty error string.
- ///
- /// TODO: Update users of Invalid to call getBufferOrNone and change return
- /// type to MemoryBufferRef.
- const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
- bool *Invalid = nullptr) const {
- bool MyInvalid = false;
- const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
- if (MyInvalid || !Entry.isFile()) {
- if (Invalid)
- *Invalid = true;
-
- return getFakeBufferForRecovery();
- }
-
- auto *B = Entry.getFile().getContentCache()->getBufferPointer(
- Diag, getFileManager(), Loc);
- if (Invalid)
- *Invalid = !B;
- return B ? B : getFakeBufferForRecovery();
- }
-
- const llvm::MemoryBuffer *getBuffer(FileID FID,
- bool *Invalid = nullptr) const {
- return getBuffer(FID, SourceLocation(), Invalid);
- }
-
/// Returns the FileEntry record for the provided FileID.
const FileEntry *getFileEntryForID(FileID FID) const {
bool MyInvalid = false;
/// Create a new ContentCache for the specified memory buffer.
const SrcMgr::ContentCache *
- createMemBufferContentCache(const llvm::MemoryBuffer *Buf, bool DoNotFree);
+ createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
FileID getFileIDSlow(unsigned SLocOffset) const;
FileID getFileIDLocal(unsigned SLocOffset) const;
assert(!CurrentInput.isEmpty() && "No current file!");
return CurrentInput.isFile()
? CurrentInput.getFile()
- : CurrentInput.getBuffer()->getBufferIdentifier();
+ : CurrentInput.getBuffer().getBufferIdentifier();
}
InputKind getCurrentFileKind() const {
/// The input, if it comes from a buffer rather than a file. This object
/// does not own the buffer, and the caller is responsible for ensuring
/// that it outlives any users.
- const llvm::MemoryBuffer *Buffer = nullptr;
+ llvm::Optional<llvm::MemoryBufferRef> Buffer;
/// The kind of input, e.g., C source, AST file, LLVM IR.
InputKind Kind;
FrontendInputFile() = default;
FrontendInputFile(StringRef File, InputKind Kind, bool IsSystem = false)
: File(File.str()), Kind(Kind), IsSystem(IsSystem) {}
- FrontendInputFile(const llvm::MemoryBuffer *Buffer, InputKind Kind,
+ FrontendInputFile(llvm::MemoryBufferRef Buffer, InputKind Kind,
bool IsSystem = false)
: Buffer(Buffer), Kind(Kind), IsSystem(IsSystem) {}
InputKind getKind() const { return Kind; }
bool isSystem() const { return IsSystem; }
- bool isEmpty() const { return File.empty() && Buffer == nullptr; }
+ bool isEmpty() const { return File.empty() && Buffer == None; }
bool isFile() const { return !isBuffer(); }
- bool isBuffer() const { return Buffer != nullptr; }
+ bool isBuffer() const { return Buffer != None; }
bool isPreprocessed() const { return Kind.isPreprocessed(); }
StringRef getFile() const {
return File;
}
- const llvm::MemoryBuffer *getBuffer() const {
+ llvm::MemoryBufferRef getBuffer() const {
assert(isBuffer());
- return Buffer;
+ return *Buffer;
}
};
/// Create a new ContentCache for the specified memory buffer.
/// This does no caching.
-const ContentCache *
-SourceManager::createMemBufferContentCache(const llvm::MemoryBuffer *Buffer,
- bool DoNotFree) {
+const ContentCache *SourceManager::createMemBufferContentCache(
+ std::unique_ptr<llvm::MemoryBuffer> Buffer) {
// Add a new ContentCache to the MemBufferInfos list and return it.
ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>();
new (Entry) ContentCache();
MemBufferInfos.push_back(Entry);
- Entry->replaceBuffer(Buffer, DoNotFree);
+ Entry->replaceBuffer(Buffer.release(), /*DoNotFree=*/false);
return Entry;
}
int LoadedID, unsigned LoadedOffset,
SourceLocation IncludeLoc) {
StringRef Name = Buffer->getBufferIdentifier();
- return createFileID(
- createMemBufferContentCache(Buffer.release(), /*DoNotFree*/ false),
- Name, IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
+ return createFileID(createMemBufferContentCache(std::move(Buffer)), Name,
+ IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
}
/// Create a new FileID that represents the specified memory buffer.
///
/// This does not take ownership of the MemoryBuffer. The memory buffer must
/// outlive the SourceManager.
-FileID SourceManager::createFileID(UnownedTag, const llvm::MemoryBuffer *Buffer,
+FileID SourceManager::createFileID(const llvm::MemoryBufferRef &Buffer,
SrcMgr::CharacteristicKind FileCharacter,
int LoadedID, unsigned LoadedOffset,
SourceLocation IncludeLoc) {
- return createFileID(createMemBufferContentCache(Buffer, /*DoNotFree*/ true),
- Buffer->getBufferIdentifier(), IncludeLoc,
- FileCharacter, LoadedID, LoadedOffset);
+ return createFileID(llvm::MemoryBuffer::getMemBuffer(Buffer), FileCharacter,
+ LoadedID, LoadedOffset, IncludeLoc);
}
/// Get the FileID for \p SourceFile if it exists. Otherwise, create a
void MacroExpander::parseDefinition(const std::string &Macro) {
Buffers.push_back(
llvm::MemoryBuffer::getMemBufferCopy(Macro, "<scratch space>"));
- clang::FileID FID =
- SourceMgr.createFileID(SourceManager::Unowned, Buffers.back().get());
+ clang::FileID FID = SourceMgr.createFileID(Buffers.back()->getMemBufferRef());
FormatTokenLexer Lex(SourceMgr, FID, 0, Style, encoding::Encoding_UTF8,
Allocator, IdentTable);
const auto Tokens = Lex.lex();
if (Input.isFile())
return Input.getFile();
else
- return Input.getBuffer()->getBufferIdentifier();
+ return Input.getBuffer().getBufferIdentifier();
}
if (SourceMgr) {
: Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User;
if (Input.isBuffer()) {
- SourceMgr.setMainFileID(SourceMgr.createFileID(SourceManager::Unowned,
- Input.getBuffer(), Kind));
+ SourceMgr.setMainFileID(SourceMgr.createFileID(Input.getBuffer(), Kind));
assert(SourceMgr.getMainFileID().isValid() &&
"Couldn't establish MainFileID!");
return true;
if (auto *File = OldSM.getFileEntryForID(ID))
Input = FrontendInputFile(File->getName(), Kind);
else
- Input = FrontendInputFile(OldSM.getBuffer(ID), Kind);
+ Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind);
}
setCurrentInput(Input, std::move(AST));
}
if (FIF.getKind().getFormat() != InputKind::Source || !FIF.isFile()) {
CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
<< (FIF.isFile() ? FIF.getFile()
- : FIF.getBuffer()->getBufferIdentifier());
+ : FIF.getBuffer().getBufferIdentifier());
return true;
}
// Set that buffer up as our "real" input.
Inputs.clear();
- Inputs.push_back(FrontendInputFile(Buffer.get(), Kind, /*IsSystem*/false));
+ Inputs.push_back(
+ FrontendInputFile(Buffer->getMemBufferRef(), Kind, /*IsSystem*/ false));
return GenerateModuleAction::PrepareToExecuteAction(CI);
}
TokenList lex(llvm::StringRef Code) {
Buffers.push_back(
llvm::MemoryBuffer::getMemBufferCopy(Code, "<scratch space>"));
- clang::FileID FID = SourceMgr.get().createFileID(SourceManager::Unowned,
- Buffers.back().get());
+ clang::FileID FID =
+ SourceMgr.get().createFileID(Buffers.back()->getMemBufferRef());
FormatTokenLexer Lex(SourceMgr.get(), FID, 0, Style, Encoding, Allocator,
IdentTable);
auto Result = Lex.lex();