RemarkParser.h 3.1 KB

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