| 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 |
| 15 | |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/Remarks/Remark.h" |
| 18 | #include "llvm/Remarks/RemarkParser.h" |
| 19 | #include "llvm/Support/Allocator.h" |
| 20 | #include "llvm/Support/Casting.h" |
| 21 | #include "llvm/Support/Error.h" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/Support/SourceMgr.h" |
| 24 | #include "llvm/Support/YAMLParser.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | #include <optional> |
| 27 | #include <string> |
| 28 | |
| 29 | namespace llvm { |
| 30 | namespace remarks { |
| 31 | |
| 32 | class : public ErrorInfo<YAMLParseError> { |
| 33 | public: |
| 34 | static char ; |
| 35 | |
| 36 | (StringRef Message, SourceMgr &SM, yaml::Stream &Stream, |
| 37 | yaml::Node &Node); |
| 38 | |
| 39 | (StringRef Message) : Message(std::string(Message)) {} |
| 40 | |
| 41 | void (raw_ostream &OS) const override { OS << Message; } |
| 42 | std::error_code () const override { |
| 43 | return inconvertibleErrorCode(); |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | std::string ; |
| 48 | }; |
| 49 | |
| 50 | /// Regular YAML to Remark parser. |
| 51 | struct : public RemarkParser { |
| 52 | /// Last error message that can come from the YAML parser diagnostics. |
| 53 | /// We need this for catching errors in the constructor. |
| 54 | std::string ; |
| 55 | /// Source manager for better error messages. |
| 56 | SourceMgr ; |
| 57 | /// Stream for yaml parsing. |
| 58 | yaml::Stream ; |
| 59 | /// Iterator in the YAML stream. |
| 60 | yaml::document_iterator ; |
| 61 | /// If we parse remark metadata in separate mode, we need to open a new file |
| 62 | /// and parse that. |
| 63 | std::unique_ptr<MemoryBuffer> ; |
| 64 | /// Storage for values that do not point into the input buffer: unescaped |
| 65 | /// scalars and block scalars. Remarks point into it. |
| 66 | BumpPtrAllocator ; |
| 67 | |
| 68 | (StringRef Buf); |
| 69 | |
| 70 | Expected<std::unique_ptr<Remark>> () override; |
| 71 | |
| 72 | static bool (const RemarkParser *P) { |
| 73 | return P->ParserFormat == Format::YAML; |
| 74 | } |
| 75 | |
| 76 | protected: |
| 77 | /// Create a YAMLParseError error from an existing error generated by the YAML |
| 78 | /// parser. |
| 79 | /// If there is no error, this returns Success. |
| 80 | Error (); |
| 81 | /// Create a YAMLParseError error referencing a specific node. |
| 82 | Error (StringRef Message, yaml::Node &Node); |
| 83 | /// Parse a YAML remark to a remarks::Remark object. |
| 84 | Expected<std::unique_ptr<Remark>> (yaml::Document &); |
| 85 | /// Parse the type of a remark to an enum type. |
| 86 | Expected<Type> (yaml::MappingNode &Node); |
| 87 | /// Decode a scalar, unescaping quoted forms. |
| 88 | Expected<StringRef> (yaml::ScalarNode &Node); |
| 89 | /// Parse one key to a string. |
| 90 | Expected<StringRef> (yaml::KeyValueNode &Node); |
| 91 | /// Parse one value to a string. |
| 92 | virtual Expected<StringRef> (yaml::KeyValueNode &Node); |
| 93 | /// Parse one value to an unsigned integer of the requested width. |
| 94 | template <typename T = unsigned> |
| 95 | Expected<T> parseUnsigned(yaml::KeyValueNode &Node); |
| 96 | /// Parse a debug location. |
| 97 | Expected<RemarkLocation> (yaml::KeyValueNode &Node); |
| 98 | /// Parse an argument. |
| 99 | Expected<Argument> (yaml::Node &Node); |
| 100 | }; |
| 101 | |
| 102 | template <typename T> |
| 103 | Expected<T> YAMLRemarkParser::(yaml::KeyValueNode &Node) { |
| 104 | SmallVector<char, 4> Tmp; |
| 105 | auto *Value = dyn_cast_if_present<yaml::ScalarNode>(Val: Node.getValue()); |
| 106 | if (!Value) |
| 107 | return error(Message: "expected a value of scalar type." , Node); |
| 108 | T UnsignedValue = 0; |
| 109 | if (Value->getValue(Storage&: Tmp).getAsInteger(10, UnsignedValue)) |
| 110 | return error(Message: "expected a value of integer type." , Node&: *Value); |
| 111 | return UnsignedValue; |
| 112 | } |
| 113 | |
| 114 | Expected<std::unique_ptr<YAMLRemarkParser>> ( |
| 115 | StringRef Buf, |
| 116 | std::optional<StringRef> ExternalFilePrependPath = std::nullopt); |
| 117 | |
| 118 | } // end namespace remarks |
| 119 | } // end namespace llvm |
| 120 | |
| 121 | #endif /* LLVM_REMARKS_YAML_REMARK_PARSER_H */ |
| 122 | |