RemarkParser.h 3.3 KB

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