void llvm::readNaClBitcodeRecordList( NaClBitcodeRecordList &RecordList, std::unique_ptr<MemoryBuffer> InputBuffer) { if (InputBuffer->getBufferSize() % 4 != 0) report_fatal_error( "Bitcode stream must be a multiple of 4 bytes in length"); const unsigned char *BufPtr = (const unsigned char *) InputBuffer->getBufferStart(); const unsigned char *EndBufPtr = BufPtr + InputBuffer->getBufferSize(); // Read header and verify it is good. NaClBitcodeHeader Header; if (Header.Read(BufPtr, EndBufPtr)) report_fatal_error("Invalid PNaCl bitcode header.\n"); if (!Header.IsSupported()) errs() << Header.Unsupported(); if (!Header.IsReadable()) report_fatal_error("Invalid PNaCl bitcode header.\n"); NaClBitstreamReader Reader(BufPtr, EndBufPtr, Header); NaClBitstreamCursor Cursor(Reader); // Parse the bitcode buffer. BitcodeParser Parser(Cursor, RecordList); while (!Cursor.AtEndOfStream()) { if (Parser.Parse()) report_fatal_error("Malformed records founds, unable to continue"); } }
Expected<std::unique_ptr<InstrProfReader>> InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) { // Sanity check the buffer. if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max()) return make_error<InstrProfError>(instrprof_error::too_large); if (Buffer->getBufferSize() == 0) return make_error<InstrProfError>(instrprof_error::empty_raw_profile); std::unique_ptr<InstrProfReader> Result; // Create the reader. if (IndexedInstrProfReader::hasFormat(*Buffer)) Result.reset(new IndexedInstrProfReader(std::move(Buffer))); else if (RawInstrProfReader64::hasFormat(*Buffer)) Result.reset(new RawInstrProfReader64(std::move(Buffer))); else if (RawInstrProfReader32::hasFormat(*Buffer)) Result.reset(new RawInstrProfReader32(std::move(Buffer))); else if (TextInstrProfReader::hasFormat(*Buffer)) Result.reset(new TextInstrProfReader(std::move(Buffer))); else return make_error<InstrProfError>(instrprof_error::unrecognized_format); // Initialize the reader and return the result. if (Error E = initializeReader(*Result)) return std::move(E); return std::move(Result); }
/// Canonicalize whitespaces in the input file. Line endings are replaced /// with UNIX-style '\n'. /// /// \param PreserveHorizontal Don't squash consecutive horizontal whitespace /// characters to a single space. static std::unique_ptr<MemoryBuffer> CanonicalizeInputFile(std::unique_ptr<MemoryBuffer> MB, bool PreserveHorizontal) { SmallString<128> NewFile; NewFile.reserve(MB->getBufferSize()); for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd(); Ptr != End; ++Ptr) { // Eliminate trailing dosish \r. if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') { continue; } // If current char is not a horizontal whitespace or if horizontal // whitespace canonicalization is disabled, dump it to output as is. if (PreserveHorizontal || (*Ptr != ' ' && *Ptr != '\t')) { NewFile.push_back(*Ptr); continue; } // Otherwise, add one space and advance over neighboring space. NewFile.push_back(' '); while (Ptr+1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t')) ++Ptr; } return std::unique_ptr<MemoryBuffer>( MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier())); }
ErrorOr<std::unique_ptr<InstrProfReader>> InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) { // Sanity check the buffer. if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max()) return instrprof_error::too_large; std::unique_ptr<InstrProfReader> Result; // Create the reader. if (IndexedInstrProfReader::hasFormat(*Buffer)) Result.reset(new IndexedInstrProfReader(std::move(Buffer))); else if (RawInstrProfReader64::hasFormat(*Buffer)) Result.reset(new RawInstrProfReader64(std::move(Buffer))); else if (RawInstrProfReader32::hasFormat(*Buffer)) Result.reset(new RawInstrProfReader32(std::move(Buffer))); else if (TextInstrProfReader::hasFormat(*Buffer)) Result.reset(new TextInstrProfReader(std::move(Buffer))); else return instrprof_error::unrecognized_format; // Initialize the reader and return the result. if (std::error_code EC = initializeReader(*Result)) return EC; return std::move(Result); }
void ModuleManager::addInMemoryBuffer(StringRef FileName, std::unique_ptr<llvm::MemoryBuffer> Buffer) { const FileEntry *Entry = FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); InMemoryBuffers[Entry] = std::move(Buffer); }
static error_code setupMemoryBuffer(std::string Path, std::unique_ptr<MemoryBuffer> &Buffer) { if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer)) return EC; // Sanity check the file. if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max()) return instrprof_error::too_large; return instrprof_error::success; }
static std::unique_ptr<LTOModule> getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer, const TargetOptions &Options, std::string &Error) { ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = MemoryBuffer::getFile(Path); if (std::error_code EC = BufferOrErr.getError()) { Error = EC.message(); return nullptr; } Buffer = std::move(BufferOrErr.get()); return std::unique_ptr<LTOModule>(LTOModule::createInLocalContext( Buffer->getBufferStart(), Buffer->getBufferSize(), Options, Error, Path)); }
static std::unique_ptr<LTOModule> getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer, const TargetOptions &Options) { ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = MemoryBuffer::getFile(Path); error(BufferOrErr, "error loading file '" + Path + "'"); Buffer = std::move(BufferOrErr.get()); CurrentActivity = ("loading file '" + Path + "'").str(); ErrorOr<std::unique_ptr<LTOModule>> Ret = LTOModule::createInLocalContext( Buffer->getBufferStart(), Buffer->getBufferSize(), Options, Path); CurrentActivity = ""; return std::move(*Ret); }
static std::unique_ptr<LTOModule> getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer, const TargetOptions &Options) { ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = MemoryBuffer::getFile(Path); error(BufferOrErr, "error loading file '" + Path + "'"); Buffer = std::move(BufferOrErr.get()); CurrentActivity = ("loading file '" + Path + "'").str(); std::unique_ptr<LLVMContext> Context = llvm::make_unique<LLVMContext>(); Context->setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); ErrorOr<std::unique_ptr<LTOModule>> Ret = LTOModule::createInLocalContext( std::move(Context), Buffer->getBufferStart(), Buffer->getBufferSize(), Options, Path); CurrentActivity = ""; return std::move(*Ret); }
ErrorOr<std::unique_ptr<IndexedInstrProfReader>> IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) { // Sanity check the buffer. if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max()) return instrprof_error::too_large; // Create the reader. if (!IndexedInstrProfReader::hasFormat(*Buffer)) return instrprof_error::bad_magic; auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer)); // Initialize the reader and return the result. if (std::error_code EC = initializeReader(*Result)) return EC; return std::move(Result); }
std::error_code Registry::parseFile(std::unique_ptr<MemoryBuffer> &mb, std::vector<std::unique_ptr<File>> &result) const { // Get file type. StringRef content(mb->getBufferStart(), mb->getBufferSize()); llvm::sys::fs::file_magic fileType = llvm::sys::fs::identify_magic(content); // Get file extension. StringRef extension = llvm::sys::path::extension(mb->getBufferIdentifier()); // Ask each registered reader if it can handle this file type or extension. for (const std::unique_ptr<Reader> &reader : _readers) if (reader->canParse(fileType, extension, *mb)) return reader->parseFile(mb, *this, result); // No Reader could parse this file. return make_error_code(llvm::errc::executable_format_error); }