RemarkParser.h 3.3 KB

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