| 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_REMARKPARSER_H |
| 14 | #define LLVM_REMARKS_REMARKPARSER_H |
| 15 | |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/Remarks/RemarkFormat.h" |
| 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include "llvm/Support/Error.h" |
| 20 | #include <memory> |
| 21 | #include <optional> |
| 22 | |
| 23 | namespace llvm { |
| 24 | namespace remarks { |
| 25 | |
| 26 | struct Remark; |
| 27 | |
| 28 | class EndOfFileError : public ErrorInfo<EndOfFileError> { |
| 29 | public: |
| 30 | LLVM_ABI static char ID; |
| 31 | |
| 32 | EndOfFileError() = default; |
| 33 | |
| 34 | void log(raw_ostream &OS) const override { OS << "End of file reached."; } |
| 35 | std::error_code convertToErrorCode() const override { |
| 36 | return inconvertibleErrorCode(); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | /// Parser used to parse a raw buffer to remarks::Remark objects. |
| 41 | struct RemarkParser { |
| 42 | /// The format of the parser. |
| 43 | Format ParserFormat; |
| 44 | /// Path to prepend when opening an external remark file. |
| 45 | std::string ExternalFilePrependPath; |
| 46 | |
| 47 | RemarkParser(Format ParserFormat) : ParserFormat(ParserFormat) {} |
| 48 | |
| 49 | /// If no error occurs, this returns a valid Remark object. |
| 50 | /// If an error of type EndOfFileError occurs, it is safe to recover from it |
| 51 | /// by stopping the parsing. |
| 52 | /// If any other error occurs, it should be propagated to the user. |
| 53 | /// The pointer should never be null. |
| 54 | virtual Expected<std::unique_ptr<Remark>> next() = 0; |
| 55 | |
| 56 | virtual ~RemarkParser() = default; |
| 57 | }; |
| 58 | |
| 59 | /// In-memory representation of the string table parsed from a buffer (e.g. the |
| 60 | /// remarks section). |
| 61 | struct ParsedStringTable { |
| 62 | /// The buffer mapped from the section contents. |
| 63 | StringRef Buffer; |
| 64 | /// This object has high changes to be std::move'd around, so don't use a |
| 65 | /// SmallVector for once. |
| 66 | std::vector<size_t> Offsets; |
| 67 | |
| 68 | LLVM_ABI ParsedStringTable(StringRef Buffer); |
| 69 | /// Disable copy. |
| 70 | ParsedStringTable(const ParsedStringTable &) = delete; |
| 71 | ParsedStringTable &operator=(const ParsedStringTable &) = delete; |
| 72 | /// Should be movable. |
| 73 | ParsedStringTable(ParsedStringTable &&) = default; |
| 74 | ParsedStringTable &operator=(ParsedStringTable &&) = default; |
| 75 | |
| 76 | size_t size() const { return Offsets.size(); } |
| 77 | LLVM_ABI Expected<StringRef> operator[](size_t Index) const; |
| 78 | }; |
| 79 | |
| 80 | LLVM_ABI Expected<std::unique_ptr<RemarkParser>> |
| 81 | createRemarkParser(Format ParserFormat, StringRef Buf); |
| 82 | |
| 83 | LLVM_ABI Expected<std::unique_ptr<RemarkParser>> createRemarkParserFromMeta( |
| 84 | Format ParserFormat, StringRef Buf, |
| 85 | std::optional<StringRef> ExternalFilePrependPath = std::nullopt); |
| 86 | |
| 87 | } // end namespace remarks |
| 88 | } // end namespace llvm |
| 89 | |
| 90 | #endif // LLVM_REMARKS_REMARKPARSER_H |
| 91 |