RemarkParser.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "llvm/Remarks/RemarkParser.h"
  14. #include "BitstreamRemarkParser.h"
  15. #include "YAMLRemarkParser.h"
  16. #include "llvm-c/Remarks.h"
  17. #include "llvm/Support/CBindingWrapping.h"
  18. #include <optional>
  19. using namespace llvm;
  20. using namespace llvm::remarks;
  21. char EndOfFileError::ID = 0;
  22. ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
  23. while (!InBuffer.empty()) {
  24. // Strings are separated by '\0' bytes.
  25. std::pair<StringRef, StringRef> Split = InBuffer.split('\0');
  26. // We only store the offset from the beginning of the buffer.
  27. Offsets.push_back(Split.first.data() - Buffer.data());
  28. InBuffer = Split.second;
  29. }
  30. }
  31. Expected<StringRef> ParsedStringTable::operator[](size_t Index) const {
  32. if (Index >= Offsets.size())
  33. return createStringError(
  34. std::make_error_code(std::errc::invalid_argument),
  35. "String with index %u is out of bounds (size = %u).", Index,
  36. Offsets.size());
  37. size_t Offset = Offsets[Index];
  38. // If it's the last offset, we can't use the next offset to know the size of
  39. // the string.
  40. size_t NextOffset =
  41. (Index == Offsets.size() - 1) ? Buffer.size() : Offsets[Index + 1];
  42. return StringRef(Buffer.data() + Offset, NextOffset - Offset - 1);
  43. }
  44. Expected<std::unique_ptr<RemarkParser>>
  45. llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf) {
  46. switch (ParserFormat) {
  47. case Format::YAML:
  48. return std::make_unique<YAMLRemarkParser>(Buf);
  49. case Format::YAMLStrTab:
  50. return createStringError(
  51. std::make_error_code(std::errc::invalid_argument),
  52. "The YAML with string table format requires a parsed string table.");
  53. case Format::Bitstream:
  54. return std::make_unique<BitstreamRemarkParser>(Buf);
  55. case Format::Unknown:
  56. return createStringError(std::make_error_code(std::errc::invalid_argument),
  57. "Unknown remark parser format.");
  58. }
  59. llvm_unreachable("unhandled ParseFormat");
  60. }
  61. Expected<std::unique_ptr<RemarkParser>>
  62. llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf,
  63. ParsedStringTable StrTab) {
  64. switch (ParserFormat) {
  65. case Format::YAML:
  66. return createStringError(std::make_error_code(std::errc::invalid_argument),
  67. "The YAML format can't be used with a string "
  68. "table. Use yaml-strtab instead.");
  69. case Format::YAMLStrTab:
  70. return std::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(StrTab));
  71. case Format::Bitstream:
  72. return std::make_unique<BitstreamRemarkParser>(Buf, std::move(StrTab));
  73. case Format::Unknown:
  74. return createStringError(std::make_error_code(std::errc::invalid_argument),
  75. "Unknown remark parser format.");
  76. }
  77. llvm_unreachable("unhandled ParseFormat");
  78. }
  79. Expected<std::unique_ptr<RemarkParser>>
  80. llvm::remarks::createRemarkParserFromMeta(
  81. Format ParserFormat, StringRef Buf, std::optional<ParsedStringTable> StrTab,
  82. std::optional<StringRef> ExternalFilePrependPath) {
  83. switch (ParserFormat) {
  84. // Depending on the metadata, the format can be either yaml or yaml-strtab,
  85. // regardless of the input argument.
  86. case Format::YAML:
  87. case Format::YAMLStrTab:
  88. return createYAMLParserFromMeta(Buf, std::move(StrTab),
  89. std::move(ExternalFilePrependPath));
  90. case Format::Bitstream:
  91. return createBitstreamParserFromMeta(Buf, std::move(StrTab),
  92. std::move(ExternalFilePrependPath));
  93. case Format::Unknown:
  94. return createStringError(std::make_error_code(std::errc::invalid_argument),
  95. "Unknown remark parser format.");
  96. }
  97. llvm_unreachable("unhandled ParseFormat");
  98. }
  99. namespace {
  100. // Wrapper that holds the state needed to interact with the C API.
  101. struct CParser {
  102. std::unique_ptr<RemarkParser> TheParser;
  103. std::optional<std::string> Err;
  104. CParser(Format ParserFormat, StringRef Buf,
  105. std::optional<ParsedStringTable> StrTab = std::nullopt)
  106. : TheParser(cantFail(
  107. StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab))
  108. : createRemarkParser(ParserFormat, Buf))) {}
  109. void handleError(Error E) { Err.emplace(toString(std::move(E))); }
  110. bool hasError() const { return Err.has_value(); }
  111. const char *getMessage() const { return Err ? Err->c_str() : nullptr; };
  112. };
  113. } // namespace
  114. // Create wrappers for C Binding types (see CBindingWrapping.h).
  115. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CParser, LLVMRemarkParserRef)
  116. extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
  117. uint64_t Size) {
  118. return wrap(new CParser(Format::YAML,
  119. StringRef(static_cast<const char *>(Buf), Size)));
  120. }
  121. extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateBitstream(const void *Buf,
  122. uint64_t Size) {
  123. return wrap(new CParser(Format::Bitstream,
  124. StringRef(static_cast<const char *>(Buf), Size)));
  125. }
  126. extern "C" LLVMRemarkEntryRef
  127. LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) {
  128. CParser &TheCParser = *unwrap(Parser);
  129. remarks::RemarkParser &TheParser = *TheCParser.TheParser;
  130. Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next();
  131. if (Error E = MaybeRemark.takeError()) {
  132. if (E.isA<EndOfFileError>()) {
  133. consumeError(std::move(E));
  134. return nullptr;
  135. }
  136. // Handle the error. Allow it to be checked through HasError and
  137. // GetErrorMessage.
  138. TheCParser.handleError(std::move(E));
  139. return nullptr;
  140. }
  141. // Valid remark.
  142. return wrap(MaybeRemark->release());
  143. }
  144. extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) {
  145. return unwrap(Parser)->hasError();
  146. }
  147. extern "C" const char *
  148. LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) {
  149. return unwrap(Parser)->getMessage();
  150. }
  151. extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) {
  152. delete unwrap(Parser);
  153. }