/// Retrieve the memory buffer associated with the given file.
///
- /// \param Invalid If non-NULL, will be set \c true if an error
- /// occurs while retrieving the memory buffer.
- const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
- bool *Invalid = nullptr);
+ /// Returns None if the buffer is not valid.
+ llvm::Optional<llvm::MemoryBufferRef>
+ getMemoryBufferForFileOrNone(const FileEntry *File);
+
+ /// Retrieve the memory buffer associated with the given file.
+ ///
+ /// Returns a fake buffer if there isn't a real one.
+ llvm::MemoryBufferRef getMemoryBufferForFileOrFake(const FileEntry *File) {
+ if (auto B = getMemoryBufferForFileOrNone(File))
+ return *B;
+ return getFakeBufferForRecovery()->getMemBufferRef();
+ }
/// Override the contents of the given source file by providing an
/// already-allocated buffer.
namespace llvm {
class MemoryBuffer;
+class MemoryBufferRef;
namespace vfs {
class FileSystem;
}
static PreambleFileHash createForFile(off_t Size, time_t ModTime);
static PreambleFileHash
- createForMemoryBuffer(const llvm::MemoryBuffer *Buffer);
+ createForMemoryBuffer(const llvm::MemoryBufferRef &Buffer);
friend bool operator==(const PreambleFileHash &LHS,
const PreambleFileHash &RHS) {
return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1));
}
-const llvm::MemoryBuffer *
-SourceManager::getMemoryBufferForFile(const FileEntry *File, bool *Invalid) {
+llvm::Optional<llvm::MemoryBufferRef>
+SourceManager::getMemoryBufferForFileOrNone(const FileEntry *File) {
const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
assert(IR && "getOrCreateContentCache() cannot return NULL");
- auto *B = IR->getBufferPointer(Diag, getFileManager(), SourceLocation());
- if (Invalid)
- *Invalid = !B;
- return B ? B : getFakeBufferForRecovery();
+ return IR->getBufferOrNone(Diag, getFileManager(), SourceLocation());
}
void SourceManager::overrideFileContents(const FileEntry *SourceFile,
PrecompiledPreamble::PreambleFileHash::createForFile(File->getSize(),
ModTime);
} else {
- const llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File);
+ llvm::MemoryBufferRef Buffer =
+ SourceMgr.getMemoryBufferForFileOrFake(File);
FilesInPreamble[File->getName()] =
PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
}
llvm::StringMap<PreambleFileHash> OverridenFileBuffers;
for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
const PrecompiledPreamble::PreambleFileHash PreambleHash =
- PreambleFileHash::createForMemoryBuffer(RB.second);
+ PreambleFileHash::createForMemoryBuffer(RB.second->getMemBufferRef());
llvm::vfs::Status Status;
if (moveOnNoError(VFS->status(RB.first), Status))
OverriddenFiles[Status.getUniqueID()] = PreambleHash;
PrecompiledPreamble::PreambleFileHash
PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(
- const llvm::MemoryBuffer *Buffer) {
+ const llvm::MemoryBufferRef &Buffer) {
PreambleFileHash Result;
- Result.Size = Buffer->getBufferSize();
+ Result.Size = Buffer.getBufferSize();
Result.ModTime = 0;
llvm::MD5 MD5Ctx;
- MD5Ctx.update(Buffer->getBuffer().data());
+ MD5Ctx.update(Buffer.getBuffer().data());
MD5Ctx.final(Result.MD5);
return Result;
assert(CompleteLine && CompleteColumn && "Starts from 1:1");
assert(!CodeCompletionFile && "Already set");
- using llvm::MemoryBuffer;
-
// Load the actual file's contents.
- bool Invalid = false;
- const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
- if (Invalid)
+ Optional<llvm::MemoryBufferRef> Buffer =
+ SourceMgr.getMemoryBufferForFileOrNone(File);
+ if (!Buffer)
return true;
// Find the byte position of the truncation point.