LLVM  13.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"
18 #include "llvm/ADT/STLExtras.h"
20 
21 using namespace llvm;
22 using namespace llvm::remarks;
23 
24 char EndOfFileError::ID = 0;
25 
26 ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
27  while (!InBuffer.empty()) {
28  // Strings are separated by '\0' bytes.
29  std::pair<StringRef, StringRef> Split = InBuffer.split('\0');
30  // We only store the offset from the beginning of the buffer.
31  Offsets.push_back(Split.first.data() - Buffer.data());
32  InBuffer = Split.second;
33  }
34 }
35 
37  if (Index >= Offsets.size())
38  return createStringError(
39  std::make_error_code(std::errc::invalid_argument),
40  "String with index %u is out of bounds (size = %u).", Index,
41  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 
53  switch (ParserFormat) {
54  case Format::YAML:
55  return std::make_unique<YAMLRemarkParser>(Buf);
56  case Format::YAMLStrTab:
57  return createStringError(
58  std::make_error_code(std::errc::invalid_argument),
59  "The YAML with string table format requires a parsed string table.");
60  case Format::Bitstream:
61  return std::make_unique<BitstreamRemarkParser>(Buf);
62  case Format::Unknown:
63  return createStringError(std::make_error_code(std::errc::invalid_argument),
64  "Unknown remark parser format.");
65  }
66  llvm_unreachable("unhandled ParseFormat");
67 }
68 
71  ParsedStringTable StrTab) {
72  switch (ParserFormat) {
73  case Format::YAML:
74  return createStringError(std::make_error_code(std::errc::invalid_argument),
75  "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>(Buf, std::move(StrTab));
79  case Format::Bitstream:
80  return std::make_unique<BitstreamRemarkParser>(Buf, std::move(StrTab));
81  case Format::Unknown:
82  return createStringError(std::make_error_code(std::errc::invalid_argument),
83  "Unknown remark parser format.");
84  }
85  llvm_unreachable("unhandled ParseFormat");
86 }
87 
90  Format ParserFormat, StringRef Buf, Optional<ParsedStringTable> StrTab,
91  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, std::move(StrTab),
98  std::move(ExternalFilePrependPath));
99  case Format::Bitstream:
100  return createBitstreamParserFromMeta(Buf, std::move(StrTab),
101  std::move(ExternalFilePrependPath));
102  case Format::Unknown:
103  return createStringError(std::make_error_code(std::errc::invalid_argument),
104  "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;
114 
115  CParser(Format ParserFormat, StringRef Buf,
116  Optional<ParsedStringTable> StrTab = None)
117  : TheParser(cantFail(
118  StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab))
119  : createRemarkParser(ParserFormat, Buf))) {}
120 
121  void handleError(Error E) { Err.emplace(toString(std::move(E))); }
122  bool hasError() const { return Err.hasValue(); }
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).
129 
131  uint64_t Size) {
132  return wrap(new CParser(Format::YAML,
133  StringRef(static_cast<const char *>(Buf), Size)));
134 }
135 
137  uint64_t Size) {
138  return wrap(new CParser(Format::Bitstream,
139  StringRef(static_cast<const char *>(Buf), Size)));
140 }
141 
142 extern "C" LLVMRemarkEntryRef
144  CParser &TheCParser = *unwrap(Parser);
145  remarks::RemarkParser &TheParser = *TheCParser.TheParser;
146 
147  Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next();
148  if (Error E = MaybeRemark.takeError()) {
149  if (E.isA<EndOfFileError>()) {
151  return nullptr;
152  }
153 
154  // Handle the error. Allow it to be checked through HasError and
155  // GetErrorMessage.
156  TheCParser.handleError(std::move(E));
157  return nullptr;
158  }
159 
160  // Valid remark.
161  return wrap(MaybeRemark->release());
162 }
163 
165  return unwrap(Parser)->hasError();
166 }
167 
168 extern "C" const char *
170  return unwrap(Parser)->getMessage();
171 }
172 
174  delete unwrap(Parser);
175 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::remarks::Format::Unknown
@ Unknown
llvm
Definition: AllocatorList.h:23
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
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:173
LLVMRemarkParserGetNext
LLVMRemarkEntryRef LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser)
Returns the next remark in the file.
Definition: RemarkParser.cpp:143
llvm::toString
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:991
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:81
CBindingWrapping.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
STLExtras.h
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1006
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:136
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:727
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:244
llvm::remarks::createRemarkParser
Expected< std::unique_ptr< RemarkParser > > createRemarkParser(Format ParserFormat, StringRef Buf)
Definition: RemarkParser.cpp:52
llvm::remarks
Definition: AsmPrinter.h:77
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:130
llvm::remarks::ParsedStringTable::ParsedStringTable
ParsedStringTable(StringRef Buffer)
Definition: RemarkParser.cpp:26
llvm::remarks::EndOfFileError
Definition: RemarkParser.h:26
llvm::remarks::Format::YAML
@ YAML
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
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:1540
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:239
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:136
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:708
llvm::make_error_code
std::error_code make_error_code(BitcodeError E)
Definition: BitcodeReader.h:270
llvm::remarks::RemarkParser
Parser used to parse a raw buffer to remarks::Remark objects.
Definition: RemarkParser.h:39
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1202
std
Definition: BitVector.h:838
LLVMBool
int LLVMBool
Definition: Types.h:28
YAMLRemarkParser.h
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::remarks::ParsedStringTable::operator[]
Expected< StringRef > operator[](size_t Index) const
Definition: RemarkParser.cpp:36
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
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:89
llvm::remarks::createBitstreamParserFromMeta
Expected< std::unique_ptr< BitstreamRemarkParser > > createBitstreamParserFromMeta(StringRef Buf, Optional< ParsedStringTable > StrTab=None, Optional< StringRef > ExternalFilePrependPath=None)
Definition: BitstreamRemarkParser.cpp:308
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:169
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
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:164
llvm::remarks::Format
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25