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