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