10b57cec5SDimitry Andric //===-- llvm/Remarks/Remark.h - The remark type -----------------*- C++/-*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file provides an interface for parsing remarks in LLVM. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 13fe6060f1SDimitry Andric #ifndef LLVM_REMARKS_REMARKPARSER_H 14fe6060f1SDimitry Andric #define LLVM_REMARKS_REMARKPARSER_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 170b57cec5SDimitry Andric #include "llvm/Remarks/RemarkFormat.h" 180b57cec5SDimitry Andric #include "llvm/Support/Error.h" 190b57cec5SDimitry Andric #include <memory> 20*bdd1243dSDimitry Andric #include <optional> 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric namespace llvm { 230b57cec5SDimitry Andric namespace remarks { 240b57cec5SDimitry Andric 251fd87a68SDimitry Andric struct Remark; 261fd87a68SDimitry Andric 270b57cec5SDimitry Andric class EndOfFileError : public ErrorInfo<EndOfFileError> { 280b57cec5SDimitry Andric public: 290b57cec5SDimitry Andric static char ID; 300b57cec5SDimitry Andric 311fd87a68SDimitry Andric EndOfFileError() = default; 320b57cec5SDimitry Andric log(raw_ostream & OS)330b57cec5SDimitry Andric void log(raw_ostream &OS) const override { OS << "End of file reached."; } convertToErrorCode()340b57cec5SDimitry Andric std::error_code convertToErrorCode() const override { 350b57cec5SDimitry Andric return inconvertibleErrorCode(); 360b57cec5SDimitry Andric } 370b57cec5SDimitry Andric }; 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric /// Parser used to parse a raw buffer to remarks::Remark objects. 408bcb0991SDimitry Andric struct RemarkParser { 410b57cec5SDimitry Andric /// The format of the parser. 420b57cec5SDimitry Andric Format ParserFormat; 438bcb0991SDimitry Andric /// Path to prepend when opening an external remark file. 448bcb0991SDimitry Andric std::string ExternalFilePrependPath; 450b57cec5SDimitry Andric RemarkParserRemarkParser468bcb0991SDimitry Andric RemarkParser(Format ParserFormat) : ParserFormat(ParserFormat) {} 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric /// If no error occurs, this returns a valid Remark object. 490b57cec5SDimitry Andric /// If an error of type EndOfFileError occurs, it is safe to recover from it 500b57cec5SDimitry Andric /// by stopping the parsing. 510b57cec5SDimitry Andric /// If any other error occurs, it should be propagated to the user. 520b57cec5SDimitry Andric /// The pointer should never be null. 530b57cec5SDimitry Andric virtual Expected<std::unique_ptr<Remark>> next() = 0; 540b57cec5SDimitry Andric 558bcb0991SDimitry Andric virtual ~RemarkParser() = default; 560b57cec5SDimitry Andric }; 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric /// In-memory representation of the string table parsed from a buffer (e.g. the 590b57cec5SDimitry Andric /// remarks section). 600b57cec5SDimitry Andric struct ParsedStringTable { 610b57cec5SDimitry Andric /// The buffer mapped from the section contents. 620b57cec5SDimitry Andric StringRef Buffer; 638bcb0991SDimitry Andric /// This object has high changes to be std::move'd around, so don't use a 648bcb0991SDimitry Andric /// SmallVector for once. 658bcb0991SDimitry Andric std::vector<size_t> Offsets; 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric ParsedStringTable(StringRef Buffer); 688bcb0991SDimitry Andric /// Disable copy. 698bcb0991SDimitry Andric ParsedStringTable(const ParsedStringTable &) = delete; 708bcb0991SDimitry Andric ParsedStringTable &operator=(const ParsedStringTable &) = delete; 718bcb0991SDimitry Andric /// Should be movable. 728bcb0991SDimitry Andric ParsedStringTable(ParsedStringTable &&) = default; 738bcb0991SDimitry Andric ParsedStringTable &operator=(ParsedStringTable &&) = default; 748bcb0991SDimitry Andric sizeParsedStringTable758bcb0991SDimitry Andric size_t size() const { return Offsets.size(); } 768bcb0991SDimitry Andric Expected<StringRef> operator[](size_t Index) const; 770b57cec5SDimitry Andric }; 780b57cec5SDimitry Andric 798bcb0991SDimitry Andric Expected<std::unique_ptr<RemarkParser>> createRemarkParser(Format ParserFormat, 808bcb0991SDimitry Andric StringRef Buf); 818bcb0991SDimitry Andric 828bcb0991SDimitry Andric Expected<std::unique_ptr<RemarkParser>> 830b57cec5SDimitry Andric createRemarkParser(Format ParserFormat, StringRef Buf, 848bcb0991SDimitry Andric ParsedStringTable StrTab); 858bcb0991SDimitry Andric 86*bdd1243dSDimitry Andric Expected<std::unique_ptr<RemarkParser>> createRemarkParserFromMeta( 87*bdd1243dSDimitry Andric Format ParserFormat, StringRef Buf, 88*bdd1243dSDimitry Andric std::optional<ParsedStringTable> StrTab = std::nullopt, 89*bdd1243dSDimitry Andric std::optional<StringRef> ExternalFilePrependPath = std::nullopt); 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric } // end namespace remarks 920b57cec5SDimitry Andric } // end namespace llvm 930b57cec5SDimitry Andric 94fe6060f1SDimitry Andric #endif // LLVM_REMARKS_REMARKPARSER_H 95