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 | |
14 | #include "llvm/Remarks/RemarkParser.h" |
15 | #include "BitstreamRemarkParser.h" |
16 | #include "YAMLRemarkParser.h" |
17 | #include "llvm-c/Remarks.h" |
18 | #include "llvm/Support/CBindingWrapping.h" |
19 | #include <optional> |
20 | |
21 | using namespace llvm; |
22 | using namespace llvm::remarks; |
23 | |
24 | char EndOfFileError:: = 0; |
25 | |
26 | ParsedStringTable::(StringRef InBuffer) : Buffer(InBuffer) { |
27 | while (!InBuffer.empty()) { |
28 | // Strings are separated by '\0' bytes. |
29 | std::pair<StringRef, StringRef> Split = InBuffer.split(Separator: '\0'); |
30 | // We only store the offset from the beginning of the buffer. |
31 | Offsets.push_back(x: Split.first.data() - Buffer.data()); |
32 | InBuffer = Split.second; |
33 | } |
34 | } |
35 | |
36 | Expected<StringRef> ParsedStringTable::(size_t Index) const { |
37 | if (Index >= Offsets.size()) |
38 | return createStringError( |
39 | EC: std::make_error_code(e: std::errc::invalid_argument), |
40 | Fmt: "String with index %u is out of bounds (size = %u)." , Vals: Index, |
41 | Vals: Offsets.size()); |
42 | |
43 | size_t Offset = Offsets[Index]; |
44 | // If it's the last offset, we can't use the next offset to know the size of |
45 | // the string. |
46 | size_t NextOffset = |
47 | (Index == Offsets.size() - 1) ? Buffer.size() : Offsets[Index + 1]; |
48 | return StringRef(Buffer.data() + Offset, NextOffset - Offset - 1); |
49 | } |
50 | |
51 | Expected<std::unique_ptr<RemarkParser>> |
52 | llvm::remarks::(Format ParserFormat, StringRef Buf) { |
53 | switch (ParserFormat) { |
54 | case Format::YAML: |
55 | return std::make_unique<YAMLRemarkParser>(args&: Buf); |
56 | case Format::YAMLStrTab: |
57 | return createStringError( |
58 | EC: std::make_error_code(e: std::errc::invalid_argument), |
59 | S: "The YAML with string table format requires a parsed string table." ); |
60 | case Format::Bitstream: |
61 | return std::make_unique<BitstreamRemarkParser>(args&: Buf); |
62 | case Format::Unknown: |
63 | return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument), |
64 | S: "Unknown remark parser format." ); |
65 | } |
66 | llvm_unreachable("unhandled ParseFormat" ); |
67 | } |
68 | |
69 | Expected<std::unique_ptr<RemarkParser>> |
70 | llvm::remarks::(Format ParserFormat, StringRef Buf, |
71 | ParsedStringTable StrTab) { |
72 | switch (ParserFormat) { |
73 | case Format::YAML: |
74 | return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument), |
75 | S: "The YAML format can't be used with a string " |
76 | "table. Use yaml-strtab instead." ); |
77 | case Format::YAMLStrTab: |
78 | return std::make_unique<YAMLStrTabRemarkParser>(args&: Buf, args: std::move(StrTab)); |
79 | case Format::Bitstream: |
80 | return std::make_unique<BitstreamRemarkParser>(args&: Buf, args: std::move(StrTab)); |
81 | case Format::Unknown: |
82 | return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument), |
83 | S: "Unknown remark parser format." ); |
84 | } |
85 | llvm_unreachable("unhandled ParseFormat" ); |
86 | } |
87 | |
88 | Expected<std::unique_ptr<RemarkParser>> |
89 | llvm::remarks::( |
90 | Format ParserFormat, StringRef Buf, std::optional<ParsedStringTable> StrTab, |
91 | std::optional<StringRef> ExternalFilePrependPath) { |
92 | switch (ParserFormat) { |
93 | // Depending on the metadata, the format can be either yaml or yaml-strtab, |
94 | // regardless of the input argument. |
95 | case Format::YAML: |
96 | case Format::YAMLStrTab: |
97 | return createYAMLParserFromMeta(Buf, StrTab: std::move(StrTab), |
98 | ExternalFilePrependPath: std::move(ExternalFilePrependPath)); |
99 | case Format::Bitstream: |
100 | return createBitstreamParserFromMeta(Buf, StrTab: std::move(StrTab), |
101 | ExternalFilePrependPath: std::move(ExternalFilePrependPath)); |
102 | case Format::Unknown: |
103 | return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument), |
104 | S: "Unknown remark parser format." ); |
105 | } |
106 | llvm_unreachable("unhandled ParseFormat" ); |
107 | } |
108 | |
109 | namespace { |
110 | // Wrapper that holds the state needed to interact with the C API. |
111 | struct CParser { |
112 | std::unique_ptr<RemarkParser> TheParser; |
113 | std::optional<std::string> Err; |
114 | |
115 | (Format ParserFormat, StringRef Buf, |
116 | std::optional<ParsedStringTable> StrTab = std::nullopt) |
117 | : TheParser(cantFail( |
118 | ValOrErr: StrTab ? createRemarkParser(ParserFormat, Buf, StrTab: std::move(*StrTab)) |
119 | : createRemarkParser(ParserFormat, Buf))) {} |
120 | |
121 | void handleError(Error E) { Err.emplace(args: toString(E: std::move(E))); } |
122 | bool hasError() const { return Err.has_value(); } |
123 | const char *getMessage() const { return Err ? Err->c_str() : nullptr; }; |
124 | }; |
125 | } // namespace |
126 | |
127 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
128 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CParser, LLVMRemarkParserRef) |
129 | |
130 | extern "C" LLVMRemarkParserRef (const void *Buf, |
131 | uint64_t Size) { |
132 | return wrap(P: new CParser(Format::YAML, |
133 | StringRef(static_cast<const char *>(Buf), Size))); |
134 | } |
135 | |
136 | extern "C" LLVMRemarkParserRef (const void *Buf, |
137 | uint64_t Size) { |
138 | return wrap(P: new CParser(Format::Bitstream, |
139 | StringRef(static_cast<const char *>(Buf), Size))); |
140 | } |
141 | |
142 | extern "C" LLVMRemarkEntryRef |
143 | (LLVMRemarkParserRef Parser) { |
144 | CParser &TheCParser = *unwrap(P: Parser); |
145 | remarks::RemarkParser &TheParser = *TheCParser.TheParser; |
146 | |
147 | Expected<std::unique_ptr<Remark>> = TheParser.next(); |
148 | if (Error E = MaybeRemark.takeError()) { |
149 | if (E.isA<EndOfFileError>()) { |
150 | consumeError(Err: std::move(E)); |
151 | return nullptr; |
152 | } |
153 | |
154 | // Handle the error. Allow it to be checked through HasError and |
155 | // GetErrorMessage. |
156 | TheCParser.handleError(E: std::move(E)); |
157 | return nullptr; |
158 | } |
159 | |
160 | // Valid remark. |
161 | return wrap(P: MaybeRemark->release()); |
162 | } |
163 | |
164 | extern "C" LLVMBool (LLVMRemarkParserRef Parser) { |
165 | return unwrap(P: Parser)->hasError(); |
166 | } |
167 | |
168 | extern "C" const char * |
169 | (LLVMRemarkParserRef Parser) { |
170 | return unwrap(P: Parser)->getMessage(); |
171 | } |
172 | |
173 | extern "C" void (LLVMRemarkParserRef Parser) { |
174 | delete unwrap(P: Parser); |
175 | } |
176 | |