LLVM 24.0.0git
YAMLRemarkParser.h
Go to the documentation of this file.
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
17#include "llvm/Remarks/Remark.h"
21#include "llvm/Support/Error.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,
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 {
44 }
45
46private:
47 std::string Message;
48};
49
50/// Regular YAML to Remark parser.
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.
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.
67
69
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.
83 /// Parse a YAML remark to a remarks::Remark object.
85 /// Parse the type of a remark to an enum type.
87 /// Decode a scalar, unescaping quoted forms.
89 /// Parse one key to a string.
91 /// Parse one value to a string.
93 /// Parse one value to an unsigned integer of the requested width.
94 template <typename T = unsigned>
96 /// Parse a debug location.
98 /// Parse an argument.
100};
101
102template <typename T>
106 if (!Value)
107 return error("expected a value of scalar type.", Node);
108 T UnsignedValue = 0;
109 if (Value->getValue(Tmp).getAsInteger(10, UnsignedValue))
110 return error("expected a value of integer type.", *Value);
111 return UnsignedValue;
112}
113
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 */
This file defines the BumpPtrAllocator interface.
#define T
#define P(N)
This file defines the SmallVector class.
Base class for user error types.
Definition Error.h:354
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:34
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
void log(raw_ostream &OS) const override
Print an error message to an output stream.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
YAMLParseError(StringRef Message)
YAMLParseError(StringRef Message, SourceMgr &SM, yaml::Stream &Stream, yaml::Node &Node)
A YAML Stream is a sequence of Documents.
Definition YAMLParser.h:538
A key and value pair.
Definition YAMLParser.h:292
Represents a YAML map created from either a block map for a flow map.
Definition YAMLParser.h:421
Abstract base class for all Nodes.
Definition YAMLParser.h:121
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
Definition YAMLParser.h:214
This class represents a YAML stream potentially containing multiple documents.
Definition YAMLParser.h:88
Iterator abstraction for Documents over a Stream.
Definition YAMLParser.h:595
Expected< std::unique_ptr< YAMLRemarkParser > > createYAMLParserFromMeta(StringRef Buf, std::optional< StringRef > ExternalFilePrependPath=std::nullopt)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
RemarkParser(Format ParserFormat)
A remark type used for both emission and parsing.
Definition Remark.h:107
yaml::document_iterator YAMLIt
Iterator in the YAML stream.
Error error()
Create a YAMLParseError error from an existing error generated by the YAML parser.
yaml::Stream Stream
Stream for yaml parsing.
std::unique_ptr< MemoryBuffer > SeparateBuf
If we parse remark metadata in separate mode, we need to open a new file and parse that.
Expected< RemarkLocation > parseDebugLoc(yaml::KeyValueNode &Node)
Parse a debug location.
virtual Expected< StringRef > parseStr(yaml::KeyValueNode &Node)
Parse one value to a string.
Expected< StringRef > parseKey(yaml::KeyValueNode &Node)
Parse one key to a string.
BumpPtrAllocator Alloc
Storage for values that do not point into the input buffer: unescaped scalars and block scalars.
Expected< Argument > parseArg(yaml::Node &Node)
Parse an argument.
Expected< std::unique_ptr< Remark > > parseRemark(yaml::Document &Remark)
Parse a YAML remark to a remarks::Remark object.
static bool classof(const RemarkParser *P)
Expected< std::unique_ptr< Remark > > next() override
If no error occurs, this returns a valid Remark object.
SourceMgr SM
Source manager for better error messages.
Expected< StringRef > parseScalar(yaml::ScalarNode &Node)
Decode a scalar, unescaping quoted forms.
Expected< T > parseUnsigned(yaml::KeyValueNode &Node)
Parse one value to an unsigned integer of the requested width.
std::string LastErrorMessage
Last error message that can come from the YAML parser diagnostics.
Expected< Type > parseType(yaml::MappingNode &Node)
Parse the type of a remark to an enum type.