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/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
29namespace llvm {
30namespace remarks {
31
32class YAMLParseError : public ErrorInfo<YAMLParseError> {
33public:
34 static char ID;
35
36 YAMLParseError(StringRef Message, SourceMgr &SM, yaml::Stream &Stream,
37 yaml::Node &Node);
38
39 YAMLParseError(StringRef Message) : Message(std::string(Message)) {}
40
41 void log(raw_ostream &OS) const override { OS << Message; }
42 std::error_code convertToErrorCode() const override {
43 return inconvertibleErrorCode();
44 }
45
46private:
47 std::string Message;
48};
49
50/// Regular YAML to Remark parser.
51struct YAMLRemarkParser : 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 LastErrorMessage;
55 /// Source manager for better error messages.
56 SourceMgr SM;
57 /// Stream for yaml parsing.
58 yaml::Stream Stream;
59 /// Iterator in the YAML stream.
60 yaml::document_iterator YAMLIt;
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> SeparateBuf;
64 /// Storage for values that do not point into the input buffer: unescaped
65 /// scalars and block scalars. Remarks point into it.
66 BumpPtrAllocator Alloc;
67
68 YAMLRemarkParser(StringRef Buf);
69
70 Expected<std::unique_ptr<Remark>> next() override;
71
72 static bool classof(const RemarkParser *P) {
73 return P->ParserFormat == Format::YAML;
74 }
75
76protected:
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 error();
81 /// Create a YAMLParseError error referencing a specific node.
82 Error error(StringRef Message, yaml::Node &Node);
83 /// Parse a YAML remark to a remarks::Remark object.
84 Expected<std::unique_ptr<Remark>> parseRemark(yaml::Document &Remark);
85 /// Parse the type of a remark to an enum type.
86 Expected<Type> parseType(yaml::MappingNode &Node);
87 /// Decode a scalar, unescaping quoted forms.
88 Expected<StringRef> parseScalar(yaml::ScalarNode &Node);
89 /// Parse one key to a string.
90 Expected<StringRef> parseKey(yaml::KeyValueNode &Node);
91 /// Parse one value to a string.
92 virtual Expected<StringRef> parseStr(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> parseDebugLoc(yaml::KeyValueNode &Node);
98 /// Parse an argument.
99 Expected<Argument> parseArg(yaml::Node &Node);
100};
101
102template <typename T>
103Expected<T> YAMLRemarkParser::parseUnsigned(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
114Expected<std::unique_ptr<YAMLRemarkParser>> createYAMLParserFromMeta(
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