1 //===- RemarkParser.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file provides utility methods used by clients that want to use the
10 // parser for remark diagnostics in LLVM.
12 //===----------------------------------------------------------------------===//
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"
21 using namespace llvm::remarks
;
23 Parser::Parser(StringRef Buf
) : Impl(llvm::make_unique
<YAMLParserImpl
>(Buf
)) {}
25 Parser::Parser(StringRef Buf
, StringRef StrTabBuf
)
26 : Impl(llvm::make_unique
<YAMLParserImpl
>(Buf
, StrTabBuf
)) {}
28 Parser::~Parser() = default;
30 static Expected
<const Remark
*> getNextYAML(YAMLParserImpl
&Impl
) {
31 YAMLRemarkParser
&YAMLParser
= Impl
.YAMLParser
;
33 if (Impl
.YAMLIt
== Impl
.YAMLParser
.Stream
.end())
36 auto CurrentIt
= Impl
.YAMLIt
;
38 // Try to parse an entry.
39 if (Error E
= YAMLParser
.parseYAMLElement(*CurrentIt
)) {
40 // Set the iterator to the end, in case the user calls getNext again.
41 Impl
.YAMLIt
= Impl
.YAMLParser
.Stream
.end();
48 // Return the just-parsed remark.
49 if (const Optional
<YAMLRemarkParser::ParseState
> &State
= YAMLParser
.State
)
50 return &State
->TheRemark
;
52 return createStringError(std::make_error_code(std::errc::invalid_argument
),
53 "unexpected error while parsing.");
56 Expected
<const Remark
*> Parser::getNext() const {
57 if (auto *Impl
= dyn_cast
<YAMLParserImpl
>(this->Impl
.get()))
58 return getNextYAML(*Impl
);
59 llvm_unreachable("Get next called with an unknown parsing implementation.");
62 ParsedStringTable::ParsedStringTable(StringRef InBuffer
) : Buffer(InBuffer
) {
63 while (!InBuffer
.empty()) {
64 // Strings are separated by '\0' bytes.
65 std::pair
<StringRef
, StringRef
> Split
= InBuffer
.split('\0');
66 // We only store the offset from the beginning of the buffer.
67 Offsets
.push_back(Split
.first
.data() - Buffer
.data());
68 InBuffer
= Split
.second
;
72 Expected
<StringRef
> ParsedStringTable::operator[](size_t Index
) {
73 if (Index
>= Offsets
.size())
74 return createStringError(
75 std::make_error_code(std::errc::invalid_argument
),
76 "String with index %u is out of bounds (size = %u).", Index
,
79 size_t Offset
= Offsets
[Index
];
80 // If it's the last offset, we can't use the next offset to know the size of
83 (Index
== Offsets
.size() - 1) ? Buffer
.size() : Offsets
[Index
+ 1];
84 return StringRef(Buffer
.data() + Offset
, NextOffset
- Offset
- 1);
87 // Create wrappers for C Binding types (see CBindingWrapping.h).
88 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(remarks::Parser
, LLVMRemarkParserRef
)
90 extern "C" LLVMRemarkParserRef
LLVMRemarkParserCreateYAML(const void *Buf
,
93 new remarks::Parser(StringRef(static_cast<const char *>(Buf
), Size
)));
96 static void handleYAMLError(remarks::YAMLParserImpl
&Impl
, Error E
) {
99 [&](const YAMLParseError
&PE
) {
100 Impl
.YAMLParser
.Stream
.printError(&PE
.getNode(),
101 Twine(PE
.getMessage()) + Twine('\n'));
103 [&](const ErrorInfoBase
&EIB
) { EIB
.log(Impl
.YAMLParser
.ErrorStream
); });
104 Impl
.HasErrors
= true;
107 extern "C" LLVMRemarkEntryRef
108 LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser
) {
109 remarks::Parser
&TheParser
= *unwrap(Parser
);
111 Expected
<const remarks::Remark
*> RemarkOrErr
= TheParser
.getNext();
113 // Error during parsing.
114 if (auto *Impl
= dyn_cast
<remarks::YAMLParserImpl
>(TheParser
.Impl
.get()))
115 handleYAMLError(*Impl
, RemarkOrErr
.takeError());
117 llvm_unreachable("unkown parser implementation.");
121 if (*RemarkOrErr
== nullptr)
124 return wrap(*RemarkOrErr
);
127 extern "C" LLVMBool
LLVMRemarkParserHasError(LLVMRemarkParserRef Parser
) {
129 dyn_cast
<remarks::YAMLParserImpl
>(unwrap(Parser
)->Impl
.get()))
130 return Impl
->HasErrors
;
131 llvm_unreachable("unkown parser implementation.");
134 extern "C" const char *
135 LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser
) {
137 dyn_cast
<remarks::YAMLParserImpl
>(unwrap(Parser
)->Impl
.get()))
138 return Impl
->YAMLParser
.ErrorStream
.str().c_str();
139 llvm_unreachable("unkown parser implementation.");
142 extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser
) {
143 delete unwrap(Parser
);