1 //===-- llvm/Remarks/Remark.h - The remark type -----------------*- C++/-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file provides an interface for parsing remarks in LLVM. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_REMARKS_REMARK_PARSER_H 14 #define LLVM_REMARKS_REMARK_PARSER_H 15 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Remarks/Remark.h" 19 #include "llvm/Remarks/RemarkFormat.h" 20 #include "llvm/Support/Error.h" 21 #include <memory> 22 23 namespace llvm { 24 namespace remarks { 25 26 struct ParserImpl; 27 struct ParsedStringTable; 28 29 class EndOfFileError : public ErrorInfo<EndOfFileError> { 30 public: 31 static char ID; 32 33 EndOfFileError() {} 34 35 void log(raw_ostream &OS) const override { OS << "End of file reached."; } 36 std::error_code convertToErrorCode() const override { 37 return inconvertibleErrorCode(); 38 } 39 }; 40 41 /// Parser used to parse a raw buffer to remarks::Remark objects. 42 struct Parser { 43 /// The format of the parser. 44 Format ParserFormat; 45 46 Parser(Format ParserFormat) : ParserFormat(ParserFormat) {} 47 48 /// If no error occurs, this returns a valid Remark object. 49 /// If an error of type EndOfFileError occurs, it is safe to recover from it 50 /// by stopping the parsing. 51 /// If any other error occurs, it should be propagated to the user. 52 /// The pointer should never be null. 53 virtual Expected<std::unique_ptr<Remark>> next() = 0; 54 55 virtual ~Parser() = default; 56 }; 57 58 /// In-memory representation of the string table parsed from a buffer (e.g. the 59 /// remarks section). 60 struct ParsedStringTable { 61 /// The buffer mapped from the section contents. 62 StringRef Buffer; 63 /// Collection of offsets in the buffer for each string entry. 64 SmallVector<size_t, 8> Offsets; 65 66 Expected<StringRef> operator[](size_t Index) const; 67 ParsedStringTable(StringRef Buffer); 68 }; 69 70 Expected<std::unique_ptr<Parser>> 71 createRemarkParser(Format ParserFormat, StringRef Buf, 72 Optional<const ParsedStringTable *> StrTab = None); 73 74 } // end namespace remarks 75 } // end namespace llvm 76 77 #endif /* LLVM_REMARKS_REMARK_PARSER_H */ 78