LLVM 22.0.0git
RemarkParser.cpp
Go to the documentation of this file.
1//===- RemarkParser.cpp --------------------------------------------------===//
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 utility methods used by clients that want to use the
10// parser for remark diagnostics in LLVM.
11//
12//===----------------------------------------------------------------------===//
13
15#include "BitstreamRemarkParser.h"
16#include "YAMLRemarkParser.h"
17#include "llvm-c/Remarks.h"
20#include <optional>
21
22using namespace llvm;
23using namespace llvm::remarks;
24
25char EndOfFileError::ID = 0;
26
27ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
28 while (!InBuffer.empty()) {
29 // Strings are separated by '\0' bytes.
30 std::pair<StringRef, StringRef> Split = InBuffer.split('\0');
31 // We only store the offset from the beginning of the buffer.
32 Offsets.push_back(Split.first.data() - Buffer.data());
33 InBuffer = Split.second;
34 }
35}
36
38 if (Index >= Offsets.size())
39 return createStringError(
40 std::make_error_code(std::errc::invalid_argument),
41 "String with index %u is out of bounds (size = %u).", Index,
42 Offsets.size());
43
44 size_t Offset = Offsets[Index];
45 // If it's the last offset, we can't use the next offset to know the size of
46 // the string.
47 size_t NextOffset =
48 (Index == Offsets.size() - 1) ? Buffer.size() : Offsets[Index + 1];
49 return StringRef(Buffer.data() + Offset, NextOffset - Offset - 1);
50}
51
54 auto DetectedFormat = detectFormat(ParserFormat, Buf);
55 if (!DetectedFormat)
56 return DetectedFormat.takeError();
57
58 switch (*DetectedFormat) {
59 case Format::YAML:
60 return std::make_unique<YAMLRemarkParser>(Buf);
62 return std::make_unique<BitstreamRemarkParser>(Buf);
63 case Format::Unknown:
64 case Format::Auto:
65 break;
66 }
67 llvm_unreachable("unhandled ParseFormat");
68}
69
72 Format ParserFormat, StringRef Buf,
73 std::optional<StringRef> ExternalFilePrependPath) {
74 auto DetectedFormat = detectFormat(ParserFormat, Buf);
75 if (!DetectedFormat)
76 return DetectedFormat.takeError();
77
78 switch (*DetectedFormat) {
79 case Format::YAML:
80 return createYAMLParserFromMeta(Buf, std::move(ExternalFilePrependPath));
83 std::move(ExternalFilePrependPath));
84 case Format::Unknown:
85 case Format::Auto:
86 break;
87 }
88 llvm_unreachable("unhandled ParseFormat");
89}
90
91namespace {
92// Wrapper that holds the state needed to interact with the C API.
93struct CParser {
94 std::unique_ptr<RemarkParser> TheParser;
95 std::optional<std::string> Err;
96
97 CParser(Format ParserFormat, StringRef Buf)
98 : TheParser(cantFail(createRemarkParser(ParserFormat, Buf))) {}
99
100 void handleError(Error E) { Err.emplace(toString(std::move(E))); }
101 bool hasError() const { return Err.has_value(); }
102 const char *getMessage() const { return Err ? Err->c_str() : nullptr; };
103};
104} // namespace
105
106// Create wrappers for C Binding types (see CBindingWrapping.h).
108
110 uint64_t Size) {
111 return wrap(new CParser(Format::YAML,
112 StringRef(static_cast<const char *>(Buf), Size)));
113}
114
116 uint64_t Size) {
117 return wrap(new CParser(Format::Bitstream,
118 StringRef(static_cast<const char *>(Buf), Size)));
119}
120
121extern "C" LLVMRemarkEntryRef
123 CParser &TheCParser = *unwrap(Parser);
124 remarks::RemarkParser &TheParser = *TheCParser.TheParser;
125
126 Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next();
127 if (Error E = MaybeRemark.takeError()) {
128 if (E.isA<EndOfFileError>()) {
129 consumeError(std::move(E));
130 return nullptr;
131 }
132
133 // Handle the error. Allow it to be checked through HasError and
134 // GetErrorMessage.
135 TheCParser.handleError(std::move(E));
136 return nullptr;
137 }
138
139 // Valid remark.
140 return wrap(MaybeRemark->release());
141}
142
144 return unwrap(Parser)->hasError();
145}
146
147extern "C" const char *
149 return unwrap(Parser)->getMessage();
150}
151
153 delete unwrap(Parser);
154}
aarch64 promote const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
uint64_t Size
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
bool isA() const
Check whether one error is a subclass of another.
Definition: Error.h:246
Tagged union holding either a T or a Error.
Definition: Error.h:485
Error takeError()
Take ownership of the stored error.
Definition: Error.h:612
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:710
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:154
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:148
static LLVM_ABI char ID
Definition: RemarkParser.h:30
LLVM_C_ABI LLVMRemarkEntryRef LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser)
Returns the next remark in the file.
struct LLVMRemarkOpaqueEntry * LLVMRemarkEntryRef
A remark emitted by the compiler.
Definition: Remarks.h:146
LLVM_C_ABI const char * LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser)
Returns a null-terminated string containing an error message.
LLVM_C_ABI void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser)
Releases all the resources used by Parser.
LLVM_C_ABI LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf, uint64_t Size)
Creates a remark parser that can be used to parse the buffer located in Buf of size Size bytes.
struct LLVMRemarkOpaqueParser * LLVMRemarkParserRef
Definition: Remarks.h:238
LLVM_C_ABI LLVMRemarkParserRef LLVMRemarkParserCreateBitstream(const void *Buf, uint64_t Size)
Creates a remark parser that can be used to parse the buffer located in Buf of size Size bytes.
LLVM_C_ABI LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser)
Returns 1 if the parser encountered an error while parsing the buffer.
int LLVMBool
Definition: Types.h:28
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:26
LLVM_ABI Expected< Format > detectFormat(Format Selected, StringRef Magic)
Detect format based on selected format and magic number.
Expected< std::unique_ptr< BitstreamRemarkParser > > createBitstreamParserFromMeta(StringRef Buf, std::optional< StringRef > ExternalFilePrependPath=std::nullopt)
LLVM_ABI Expected< std::unique_ptr< RemarkParser > > createRemarkParserFromMeta(Format ParserFormat, StringRef Buf, std::optional< StringRef > ExternalFilePrependPath=std::nullopt)
LLVM_ABI Expected< std::unique_ptr< RemarkParser > > createRemarkParser(Format ParserFormat, StringRef Buf)
Expected< std::unique_ptr< YAMLRemarkParser > > createYAMLParserFromMeta(StringRef Buf, std::optional< StringRef > ExternalFilePrependPath=std::nullopt)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1305
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:769
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:351
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:346
const char * toString(DWARFSectionKind Kind)
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1083
LLVM_ABI Expected< StringRef > operator[](size_t Index) const
std::vector< size_t > Offsets
This object has high changes to be std::move'd around, so don't use a SmallVector for once.
Definition: RemarkParser.h:66
StringRef Buffer
The buffer mapped from the section contents.
Definition: RemarkParser.h:63
LLVM_ABI ParsedStringTable(StringRef Buffer)
Parser used to parse a raw buffer to remarks::Remark objects.
Definition: RemarkParser.h:41
virtual Expected< std::unique_ptr< Remark > > next()=0
If no error occurs, this returns a valid Remark object.