YAMLRemarkParser.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===-- YAMLRemarkParser.h - Parser for YAML remarks ------------*- 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 the impementation of the YAML remark parser.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_REMARKS_YAML_REMARK_PARSER_H
  13. #define LLVM_REMARKS_YAML_REMARK_PARSER_H
  14. #include "llvm/ADT/Optional.h"
  15. #include "llvm/Remarks/Remark.h"
  16. #include "llvm/Remarks/RemarkParser.h"
  17. #include "llvm/Support/Error.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include "llvm/Support/SourceMgr.h"
  20. #include "llvm/Support/YAMLParser.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <string>
  23. namespace llvm {
  24. namespace remarks {
  25. class YAMLParseError : public ErrorInfo<YAMLParseError> {
  26. public:
  27. static char ID;
  28. YAMLParseError(StringRef Message, SourceMgr &SM, yaml::Stream &Stream,
  29. yaml::Node &Node);
  30. YAMLParseError(StringRef Message) : Message(std::string(Message)) {}
  31. void log(raw_ostream &OS) const override { OS << Message; }
  32. std::error_code convertToErrorCode() const override {
  33. return inconvertibleErrorCode();
  34. }
  35. private:
  36. std::string Message;
  37. };
  38. /// Regular YAML to Remark parser.
  39. struct YAMLRemarkParser : public RemarkParser {
  40. /// The string table used for parsing strings.
  41. Optional<ParsedStringTable> StrTab;
  42. /// Last error message that can come from the YAML parser diagnostics.
  43. /// We need this for catching errors in the constructor.
  44. std::string LastErrorMessage;
  45. /// Source manager for better error messages.
  46. SourceMgr SM;
  47. /// Stream for yaml parsing.
  48. yaml::Stream Stream;
  49. /// Iterator in the YAML stream.
  50. yaml::document_iterator YAMLIt;
  51. /// If we parse remark metadata in separate mode, we need to open a new file
  52. /// and parse that.
  53. std::unique_ptr<MemoryBuffer> SeparateBuf;
  54. YAMLRemarkParser(StringRef Buf);
  55. Expected<std::unique_ptr<Remark>> next() override;
  56. static bool classof(const RemarkParser *P) {
  57. return P->ParserFormat == Format::YAML;
  58. }
  59. protected:
  60. YAMLRemarkParser(StringRef Buf, Optional<ParsedStringTable> StrTab);
  61. /// Create a YAMLParseError error from an existing error generated by the YAML
  62. /// parser.
  63. /// If there is no error, this returns Success.
  64. Error error();
  65. /// Create a YAMLParseError error referencing a specific node.
  66. Error error(StringRef Message, yaml::Node &Node);
  67. /// Parse a YAML remark to a remarks::Remark object.
  68. Expected<std::unique_ptr<Remark>> parseRemark(yaml::Document &Remark);
  69. /// Parse the type of a remark to an enum type.
  70. Expected<Type> parseType(yaml::MappingNode &Node);
  71. /// Parse one key to a string.
  72. Expected<StringRef> parseKey(yaml::KeyValueNode &Node);
  73. /// Parse one value to a string.
  74. virtual Expected<StringRef> parseStr(yaml::KeyValueNode &Node);
  75. /// Parse one value to an unsigned.
  76. Expected<unsigned> parseUnsigned(yaml::KeyValueNode &Node);
  77. /// Parse a debug location.
  78. Expected<RemarkLocation> parseDebugLoc(yaml::KeyValueNode &Node);
  79. /// Parse an argument.
  80. Expected<Argument> parseArg(yaml::Node &Node);
  81. };
  82. /// YAML with a string table to Remark parser.
  83. struct YAMLStrTabRemarkParser : public YAMLRemarkParser {
  84. YAMLStrTabRemarkParser(StringRef Buf, ParsedStringTable StrTab)
  85. : YAMLRemarkParser(Buf, std::move(StrTab)) {}
  86. static bool classof(const RemarkParser *P) {
  87. return P->ParserFormat == Format::YAMLStrTab;
  88. }
  89. protected:
  90. /// Parse one value to a string.
  91. Expected<StringRef> parseStr(yaml::KeyValueNode &Node) override;
  92. };
  93. Expected<std::unique_ptr<YAMLRemarkParser>>
  94. createYAMLParserFromMeta(StringRef Buf,
  95. Optional<ParsedStringTable> StrTab = None,
  96. Optional<StringRef> ExternalFilePrependPath = None);
  97. } // end namespace remarks
  98. } // end namespace llvm
  99. #endif /* LLVM_REMARKS_YAML_REMARK_PARSER_H */