LLVM  15.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"
19 
20 using namespace llvm;
21 using namespace llvm::remarks;
22 
23 char EndOfFileError::ID = 0;
24 
25 ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
26  while (!InBuffer.empty()) {
27  // Strings are separated by '\0' bytes.
28  std::pair<StringRef, StringRef> Split = InBuffer.split('\0');
29  // We only store the offset from the beginning of the buffer.
30  Offsets.push_back(Split.first.data() - Buffer.data());
31  InBuffer = Split.second;
32  }
33 }
34 
36  if (Index >= Offsets.size())
37  return createStringError(
38  std::make_error_code(std::errc::invalid_argument),
39  "String with index %u is out of bounds (size = %u).", Index,
40  Offsets.size());
41 
42  size_t Offset = Offsets[Index];
43  // If it's the last offset, we can't use the next offset to know the size of
44  // the string.
45  size_t NextOffset =
46  (Index == Offsets.size() - 1) ? Buffer.size() : Offsets[Index + 1];
47  return StringRef(Buffer.data() + Offset, NextOffset - Offset - 1);
48 }
49 
52  switch (ParserFormat) {
53  case Format::YAML:
54  return std::make_unique<YAMLRemarkParser>(Buf);
55  case Format::YAMLStrTab:
56  return createStringError(
57  std::make_error_code(std::errc::invalid_argument),
58  "The YAML with string table format requires a parsed string table.");
59  case Format::Bitstream:
60  return std::make_unique<BitstreamRemarkParser>(Buf);
61  case Format::Unknown:
62  return createStringError(std::make_error_code(std::errc::invalid_argument),
63  "Unknown remark parser format.");
64  }
65  llvm_unreachable("unhandled ParseFormat");
66 }
67 
70  ParsedStringTable StrTab) {
71  switch (ParserFormat) {
72  case Format::YAML:
73  return createStringError(std::make_error_code(std::errc::invalid_argument),
74  "The YAML format can't be used with a string "
75  "table. Use yaml-strtab instead.");
76  case Format::YAMLStrTab:
77  return std::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(StrTab));
78  case Format::Bitstream:
79  return std::make_unique<BitstreamRemarkParser>(Buf, std::move(StrTab));
80  case Format::Unknown:
81  return createStringError(std::make_error_code(std::errc::invalid_argument),
82  "Unknown remark parser format.");
83  }
84  llvm_unreachable("unhandled ParseFormat");
85 }
86 
89  Format ParserFormat, StringRef Buf, Optional<ParsedStringTable> StrTab,
90  Optional<StringRef> ExternalFilePrependPath) {
91  switch (ParserFormat) {
92  // Depending on the metadata, the format can be either yaml or yaml-strtab,
93  // regardless of the input argument.
94  case Format::YAML:
95  case Format::YAMLStrTab:
96  return createYAMLParserFromMeta(Buf, std::move(StrTab),
97  std::move(ExternalFilePrependPath));
98  case Format::Bitstream:
99  return createBitstreamParserFromMeta(Buf, std::move(StrTab),
100  std::move(ExternalFilePrependPath));
101  case Format::Unknown:
102  return createStringError(std::make_error_code(std::errc::invalid_argument),
103  "Unknown remark parser format.");
104  }
105  llvm_unreachable("unhandled ParseFormat");
106 }
107 
108 namespace {
109 // Wrapper that holds the state needed to interact with the C API.
110 struct CParser {
111  std::unique_ptr<RemarkParser> TheParser;
113 
114  CParser(Format ParserFormat, StringRef Buf,
115  Optional<ParsedStringTable> StrTab = None)
116  : TheParser(cantFail(
117  StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab))
118  : createRemarkParser(ParserFormat, Buf))) {}
119 
120  void handleError(Error E) { Err.emplace(toString(std::move(E))); }
121  bool hasError() const { return Err.hasValue(); }
122  const char *getMessage() const { return Err ? Err->c_str() : nullptr; };
123 };
124 } // namespace
125 
126 // Create wrappers for C Binding types (see CBindingWrapping.h).
128 
130  uint64_t Size) {
131  return wrap(new CParser(Format::YAML,
132  StringRef(static_cast<const char *>(Buf), Size)));
133 }
134 
136  uint64_t Size) {
137  return wrap(new CParser(Format::Bitstream,
138  StringRef(static_cast<const char *>(Buf), Size)));
139 }
140 
141 extern "C" LLVMRemarkEntryRef
143  CParser &TheCParser = *unwrap(Parser);
144  remarks::RemarkParser &TheParser = *TheCParser.TheParser;
145 
146  Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next();
147  if (Error E = MaybeRemark.takeError()) {
148  if (E.isA<EndOfFileError>()) {
150  return nullptr;
151  }
152 
153  // Handle the error. Allow it to be checked through HasError and
154  // GetErrorMessage.
155  TheCParser.handleError(std::move(E));
156  return nullptr;
157  }
158 
159  // Valid remark.
160  return wrap(MaybeRemark->release());
161 }
162 
164  return unwrap(Parser)->hasError();
165 }
166 
167 extern "C" const char *
169  return unwrap(Parser)->getMessage();
170 }
171 
173  delete unwrap(Parser);
174 }
llvm::remarks::Format::Unknown
@ Unknown
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::remarks::Format::Bitstream
@ Bitstream
llvm::remarks::ParsedStringTable::Offsets
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:64
LLVMRemarkParserDispose
void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser)
Releases all the resources used by Parser.
Definition: RemarkParser.cpp:172
LLVMRemarkParserGetNext
LLVMRemarkEntryRef LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser)
Returns the next remark in the file.
Definition: RemarkParser.cpp:142
RemarkParser.h
LLVMRemarkEntryRef
struct LLVMRemarkOpaqueEntry * LLVMRemarkEntryRef
A remark emitted by the compiler.
Definition: Remarks.h:140
Remarks.h
llvm::Optional
Definition: APInt.h:33
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:79
CBindingWrapping.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
DEFINE_SIMPLE_CONVERSION_FUNCTIONS
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
Definition: CBindingWrapping.h:19
LLVMRemarkParserCreateBitstream
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.
Definition: RemarkParser.cpp:135
llvm::remarks::ParsedStringTable::Buffer
StringRef Buffer
The buffer mapped from the section contents.
Definition: RemarkParser.h:61
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:749
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::remarks::RemarkParser::next
virtual Expected< std::unique_ptr< Remark > > next()=0
If no error occurs, this returns a valid Remark object.
llvm::unwrap
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:264
llvm::remarks::createRemarkParser
Expected< std::unique_ptr< RemarkParser > > createRemarkParser(Format ParserFormat, StringRef Buf)
Definition: RemarkParser.cpp:51
llvm::remarks
Definition: AsmPrinter.h:76
LLVMRemarkParserCreateYAML
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.
Definition: RemarkParser.cpp:129
llvm::remarks::ParsedStringTable::ParsedStringTable
ParsedStringTable(StringRef Buffer)
Definition: RemarkParser.cpp:25
llvm::remarks::EndOfFileError
Definition: RemarkParser.h:26
llvm::remarks::Format::YAML
@ YAML
llvm::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
Index
uint32_t Index
Definition: ELFObjHandler.cpp:82
uint64_t
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::remarks::Format::YAMLStrTab
@ YAMLStrTab
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1663
llvm::remarks::createYAMLParserFromMeta
Expected< std::unique_ptr< YAMLRemarkParser > > createYAMLParserFromMeta(StringRef Buf, Optional< ParsedStringTable > StrTab=None, Optional< StringRef > ExternalFilePrependPath=None)
Definition: YAMLRemarkParser.cpp:111
llvm::wrap
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:259
llvm::remarks::ParsedStringTable
In-memory representation of the string table parsed from a buffer (e.g.
Definition: RemarkParser.h:59
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:745
llvm::make_error_code
std::error_code make_error_code(BitcodeError E)
Definition: BitcodeReader.h:271
llvm::remarks::RemarkParser
Parser used to parse a raw buffer to remarks::Remark objects.
Definition: RemarkParser.h:39
llvm::StringRef::size
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1239
std
Definition: BitVector.h:851
LLVMBool
int LLVMBool
Definition: Types.h:28
YAMLRemarkParser.h
llvm::toString
const char * toString(DWARFSectionKind Kind)
Definition: DWARFUnitIndex.h:67
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::remarks::ParsedStringTable::operator[]
Expected< StringRef > operator[](size_t Index) const
Definition: RemarkParser.cpp:35
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::remarks::EndOfFileError::ID
static char ID
Definition: RemarkParser.h:28
llvm::remarks::createRemarkParserFromMeta
Expected< std::unique_ptr< RemarkParser > > createRemarkParserFromMeta(Format ParserFormat, StringRef Buf, Optional< ParsedStringTable > StrTab=None, Optional< StringRef > ExternalFilePrependPath=None)
Definition: RemarkParser.cpp:88
llvm::remarks::createBitstreamParserFromMeta
Expected< std::unique_ptr< BitstreamRemarkParser > > createBitstreamParserFromMeta(StringRef Buf, Optional< ParsedStringTable > StrTab=None, Optional< StringRef > ExternalFilePrependPath=None)
Definition: BitstreamRemarkParser.cpp:309
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:149
LLVMRemarkParserGetErrorMessage
const char * LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser)
Returns a null-terminated string containing an error message.
Definition: RemarkParser.cpp:168
LLVMRemarkParserRef
struct LLVMRemarkOpaqueParser * LLVMRemarkParserRef
Definition: Remarks.h:230
LLVMRemarkParserHasError
LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser)
Returns 1 if the parser encountered an error while parsing the buffer.
Definition: RemarkParser.cpp:163
llvm::remarks::Format
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25