xref: /freebsd/contrib/llvm-project/llvm/lib/Remarks/RemarkParser.cpp (revision f2530c80db7b29b95368fce956b3a778f096b368)
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 "YAMLRemarkParser.h"
16 #include "llvm-c/Remarks.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/CBindingWrapping.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 
35 Expected<StringRef> ParsedStringTable::operator[](size_t Index) const {
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 
50 Expected<std::unique_ptr<Parser>>
51 llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf,
52                                   Optional<const ParsedStringTable *> StrTab) {
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;
66   Optional<std::string> Err;
67 
68   CParser(Format ParserFormat, StringRef Buf,
69           Optional<const ParsedStringTable *> StrTab = None)
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).
78 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CParser, LLVMRemarkParserRef)
79 
80 extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
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
87 LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) {
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 
108 extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) {
109   return unwrap(Parser)->hasError();
110 }
111 
112 extern "C" const char *
113 LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) {
114   return unwrap(Parser)->getMessage();
115 }
116 
117 extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) {
118   delete unwrap(Parser);
119 }
120