RemarkParser.cpp 6.4 KB

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