YAMLRemarkParser.h 4.1 KB

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