LLVM  10.0.0svn
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 "YAMLRemarkParser.h"
16 #include "llvm-c/Remarks.h"
17 #include "llvm/ADT/STLExtras.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 
53  switch (ParserFormat) {
54  case Format::YAML:
55  return llvm::make_unique<YAMLRemarkParser>(Buf, StrTab);
56  case Format::Unknown:
57  return createStringError(std::make_error_code(std::errc::invalid_argument),
58  "Unknown remark parser format.");
59  }
60  llvm_unreachable("unknown format");
61 }
62 
63 // Wrapper that holds the state needed to interact with the C API.
64 struct CParser {
65  std::unique_ptr<Parser> TheParser;
67 
68  CParser(Format ParserFormat, StringRef Buf,
70  : TheParser(cantFail(createRemarkParser(ParserFormat, Buf, StrTab))) {}
71 
72  void handleError(Error E) { Err.emplace(toString(std::move(E))); }
73  bool hasError() const { return Err.hasValue(); }
74  const char *getMessage() const { return Err ? Err->c_str() : nullptr; };
75 };
76 
77 // Create wrappers for C Binding types (see CBindingWrapping.h).
79 
81  uint64_t Size) {
82  return wrap(new CParser(Format::YAML,
83  StringRef(static_cast<const char *>(Buf), Size)));
84 }
85 
86 extern "C" LLVMRemarkEntryRef
88  CParser &TheCParser = *unwrap(Parser);
89  remarks::Parser &TheParser = *TheCParser.TheParser;
90 
91  Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next();
92  if (Error E = MaybeRemark.takeError()) {
93  if (E.isA<EndOfFileError>()) {
94  consumeError(std::move(E));
95  return nullptr;
96  }
97 
98  // Handle the error. Allow it to be checked through HasError and
99  // GetErrorMessage.
100  TheCParser.handleError(std::move(E));
101  return nullptr;
102  }
103 
104  // Valid remark.
105  return wrap(MaybeRemark->release());
106 }
107 
109  return unwrap(Parser)->hasError();
110 }
111 
112 extern "C" const char *
114  return unwrap(Parser)->getMessage();
115 }
116 
118  delete unwrap(Parser);
119 }
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:703
uint64_t CallInst * C
const char * getMessage() const
bool hasError() const
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void push_back(const T &Elt)
Definition: SmallVector.h:211
void emplace(ArgTypes &&... Args)
Create a new object by constructing it in place with the given arguments.
Definition: Optional.h:237
struct LLVMRemarkOpaqueParser * LLVMRemarkParserRef
Definition: Remarks.h:227
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25
CParser(Format ParserFormat, StringRef Buf, Optional< const ParsedStringTable *> StrTab=None)
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:966
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:203
const char * LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser)
Returns a null-terminated string containing an error message.
std::error_code make_error_code(BitcodeError E)
void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser)
Releases all the resources used by Parser.
Expected< std::unique_ptr< Parser > > createRemarkParser(Format ParserFormat, StringRef Buf, Optional< const ParsedStringTable *> StrTab=None)
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser)
Returns 1 if the parser encountered an error while parsing the buffer.
void handleError(Error E)
ParsedStringTable(StringRef Buffer)
StringRef Buffer
The buffer mapped from the section contents.
Definition: RemarkParser.h:62
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVMRemarkEntryRef LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser)
Returns the next remark in the file.
int LLVMBool
Definition: Types.h:29
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:981
size_t size() const
Definition: SmallVector.h:52
SmallVector< size_t, 8 > Offsets
Collection of offsets in the buffer for each string entry.
Definition: RemarkParser.h:64
Parser used to parse a raw buffer to remarks::Remark objects.
Definition: RemarkParser.h:42
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::unique_ptr< Parser > TheParser
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:696
virtual Expected< std::unique_ptr< Remark > > next()=0
If no error occurs, this returns a valid Remark object.
bool hasValue() const
Definition: Optional.h:259
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:198
uint32_t Size
Definition: Profile.cpp:46
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...
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:122
aarch64 promote const
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
struct LLVMRemarkOpaqueEntry * LLVMRemarkEntryRef
A remark emitted by the compiler.
Definition: Remarks.h:137
Expected< StringRef > operator[](size_t Index) const
Optional< std::string > Err
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1163