使用llvm::SourceMgr 读取文件
//代码如下
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
int main(int argc, char *argv[]) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file =
llvm::MemoryBuffer::getFile(argv[1]);
if (std::error_code bufferError = file.getError()) {
llvm::errs() << "error read" << bufferError.message() << "\n";
}
llvm::SourceMgr srcMgr;
srcMgr.AddNewSourceBuffer(std::move(*file), llvm::SMLoc());
llvm::StringRef buffer =
srcMgr.getMemoryBuffer(srcMgr.getMainFileID())->getBuffer();
llvm::outs() << "print buffer:" << '\n';
llvm::outs() << buffer << '\n';
llvm::StringRef::iterator ptr = buffer.begin();
return 0;
}
$./learn-llvm hello.txt
hello world