1 //===-- YAMLRemarkParser.h - Parser for YAML remarks ------------*- C++/-*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file provides the impementation of the YAML remark parser.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_REMARKS_YAML_REMARK_PARSER_H
14 #define LLVM_REMARKS_YAML_REMARK_PARSER_H
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Remarks/Remark.h"
19 #include "llvm/Remarks/RemarkParser.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Support/YAMLParser.h"
23 #include "llvm/Support/YAMLTraits.h"
24 #include "llvm/Support/raw_ostream.h"
30 class YAMLParseError
: public ErrorInfo
<YAMLParseError
> {
34 YAMLParseError(StringRef Message
, SourceMgr
&SM
, yaml::Stream
&Stream
,
37 YAMLParseError(StringRef Message
) : Message(Message
) {}
39 void log(raw_ostream
&OS
) const override
{ OS
<< Message
; }
40 std::error_code
convertToErrorCode() const override
{
41 return inconvertibleErrorCode();
48 /// Regular YAML to Remark parser.
49 struct YAMLRemarkParser
: public Parser
{
50 /// The string table used for parsing strings.
51 Optional
<ParsedStringTable
> StrTab
;
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.
57 /// Stream for yaml parsing.
59 /// Iterator in the YAML stream.
60 yaml::document_iterator YAMLIt
;
62 YAMLRemarkParser(StringRef Buf
);
64 Expected
<std::unique_ptr
<Remark
>> next() override
;
66 static bool classof(const Parser
*P
) {
67 return P
->ParserFormat
== Format::YAML
;
71 YAMLRemarkParser(StringRef Buf
, Optional
<ParsedStringTable
> StrTab
);
72 /// Create a YAMLParseError error from an existing error generated by the YAML
74 /// If there is no error, this returns Success.
76 /// Create a YAMLParseError error referencing a specific node.
77 Error
error(StringRef Message
, yaml::Node
&Node
);
78 /// Parse a YAML remark to a remarks::Remark object.
79 Expected
<std::unique_ptr
<Remark
>> parseRemark(yaml::Document
&Remark
);
80 /// Parse the type of a remark to an enum type.
81 Expected
<Type
> parseType(yaml::MappingNode
&Node
);
82 /// Parse one key to a string.
83 Expected
<StringRef
> parseKey(yaml::KeyValueNode
&Node
);
84 /// Parse one value to a string.
85 virtual Expected
<StringRef
> parseStr(yaml::KeyValueNode
&Node
);
86 /// Parse one value to an unsigned.
87 Expected
<unsigned> parseUnsigned(yaml::KeyValueNode
&Node
);
88 /// Parse a debug location.
89 Expected
<RemarkLocation
> parseDebugLoc(yaml::KeyValueNode
&Node
);
90 /// Parse an argument.
91 Expected
<Argument
> parseArg(yaml::Node
&Node
);
94 /// YAML with a string table to Remark parser.
95 struct YAMLStrTabRemarkParser
: public YAMLRemarkParser
{
96 YAMLStrTabRemarkParser(StringRef Buf
, ParsedStringTable StrTab
)
97 : YAMLRemarkParser(Buf
, std::move(StrTab
)) {}
99 static bool classof(const Parser
*P
) {
100 return P
->ParserFormat
== Format::YAMLStrTab
;
104 /// Parse one value to a string.
105 Expected
<StringRef
> parseStr(yaml::KeyValueNode
&Node
) override
;
107 } // end namespace remarks
108 } // end namespace llvm
110 #endif /* LLVM_REMARKS_YAML_REMARK_PARSER_H */