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/MemoryBuffer.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/YAMLParser.h"
24 #include "llvm/Support/YAMLTraits.h"
25 #include "llvm/Support/raw_ostream.h"
31 class YAMLParseError
: public ErrorInfo
<YAMLParseError
> {
35 YAMLParseError(StringRef Message
, SourceMgr
&SM
, yaml::Stream
&Stream
,
38 YAMLParseError(StringRef Message
) : Message(Message
) {}
40 void log(raw_ostream
&OS
) const override
{ OS
<< Message
; }
41 std::error_code
convertToErrorCode() const override
{
42 return inconvertibleErrorCode();
49 /// Regular YAML to Remark parser.
50 struct YAMLRemarkParser
: public RemarkParser
{
51 /// The string table used for parsing strings.
52 Optional
<ParsedStringTable
> StrTab
;
53 /// Last error message that can come from the YAML parser diagnostics.
54 /// We need this for catching errors in the constructor.
55 std::string LastErrorMessage
;
56 /// Source manager for better error messages.
58 /// Stream for yaml parsing.
60 /// Iterator in the YAML stream.
61 yaml::document_iterator YAMLIt
;
62 /// If we parse remark metadata in separate mode, we need to open a new file
64 std::unique_ptr
<MemoryBuffer
> SeparateBuf
;
66 YAMLRemarkParser(StringRef Buf
);
68 Expected
<std::unique_ptr
<Remark
>> next() override
;
70 static bool classof(const RemarkParser
*P
) {
71 return P
->ParserFormat
== Format::YAML
;
75 YAMLRemarkParser(StringRef Buf
, Optional
<ParsedStringTable
> StrTab
);
76 /// Create a YAMLParseError error from an existing error generated by the YAML
78 /// If there is no error, this returns Success.
80 /// Create a YAMLParseError error referencing a specific node.
81 Error
error(StringRef Message
, yaml::Node
&Node
);
82 /// Parse a YAML remark to a remarks::Remark object.
83 Expected
<std::unique_ptr
<Remark
>> parseRemark(yaml::Document
&Remark
);
84 /// Parse the type of a remark to an enum type.
85 Expected
<Type
> parseType(yaml::MappingNode
&Node
);
86 /// Parse one key to a string.
87 Expected
<StringRef
> parseKey(yaml::KeyValueNode
&Node
);
88 /// Parse one value to a string.
89 virtual Expected
<StringRef
> parseStr(yaml::KeyValueNode
&Node
);
90 /// Parse one value to an unsigned.
91 Expected
<unsigned> parseUnsigned(yaml::KeyValueNode
&Node
);
92 /// Parse a debug location.
93 Expected
<RemarkLocation
> parseDebugLoc(yaml::KeyValueNode
&Node
);
94 /// Parse an argument.
95 Expected
<Argument
> parseArg(yaml::Node
&Node
);
98 /// YAML with a string table to Remark parser.
99 struct YAMLStrTabRemarkParser
: public YAMLRemarkParser
{
100 YAMLStrTabRemarkParser(StringRef Buf
, ParsedStringTable StrTab
)
101 : YAMLRemarkParser(Buf
, std::move(StrTab
)) {}
103 static bool classof(const RemarkParser
*P
) {
104 return P
->ParserFormat
== Format::YAMLStrTab
;
108 /// Parse one value to a string.
109 Expected
<StringRef
> parseStr(yaml::KeyValueNode
&Node
) override
;
112 Expected
<std::unique_ptr
<YAMLRemarkParser
>>
113 createYAMLParserFromMeta(StringRef Buf
,
114 Optional
<ParsedStringTable
> StrTab
= None
);
116 } // end namespace remarks
117 } // end namespace llvm
119 #endif /* LLVM_REMARKS_YAML_REMARK_PARSER_H */