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