Remark.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===- Remark.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. // Implementation of the Remark type and the C API.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Remarks/Remark.h"
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. using namespace llvm;
  16. using namespace llvm::remarks;
  17. std::string Remark::getArgsAsMsg() const {
  18. std::string Str;
  19. raw_string_ostream OS(Str);
  20. for (const Argument &Arg : Args)
  21. OS << Arg.Val;
  22. return OS.str();
  23. }
  24. // Create wrappers for C Binding types (see CBindingWrapping.h).
  25. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(StringRef, LLVMRemarkStringRef)
  26. extern "C" const char *LLVMRemarkStringGetData(LLVMRemarkStringRef String) {
  27. return unwrap(String)->data();
  28. }
  29. extern "C" uint32_t LLVMRemarkStringGetLen(LLVMRemarkStringRef String) {
  30. return unwrap(String)->size();
  31. }
  32. extern "C" LLVMRemarkStringRef
  33. LLVMRemarkDebugLocGetSourceFilePath(LLVMRemarkDebugLocRef DL) {
  34. return wrap(&unwrap(DL)->SourceFilePath);
  35. }
  36. extern "C" uint32_t LLVMRemarkDebugLocGetSourceLine(LLVMRemarkDebugLocRef DL) {
  37. return unwrap(DL)->SourceLine;
  38. }
  39. extern "C" uint32_t
  40. LLVMRemarkDebugLocGetSourceColumn(LLVMRemarkDebugLocRef DL) {
  41. return unwrap(DL)->SourceColumn;
  42. }
  43. extern "C" LLVMRemarkStringRef LLVMRemarkArgGetKey(LLVMRemarkArgRef Arg) {
  44. return wrap(&unwrap(Arg)->Key);
  45. }
  46. extern "C" LLVMRemarkStringRef LLVMRemarkArgGetValue(LLVMRemarkArgRef Arg) {
  47. return wrap(&unwrap(Arg)->Val);
  48. }
  49. extern "C" LLVMRemarkDebugLocRef
  50. LLVMRemarkArgGetDebugLoc(LLVMRemarkArgRef Arg) {
  51. if (const Optional<RemarkLocation> &Loc = unwrap(Arg)->Loc)
  52. return wrap(&*Loc);
  53. return nullptr;
  54. }
  55. extern "C" void LLVMRemarkEntryDispose(LLVMRemarkEntryRef Remark) {
  56. delete unwrap(Remark);
  57. }
  58. extern "C" LLVMRemarkType LLVMRemarkEntryGetType(LLVMRemarkEntryRef Remark) {
  59. // Assume here that the enums can be converted both ways.
  60. return static_cast<LLVMRemarkType>(unwrap(Remark)->RemarkType);
  61. }
  62. extern "C" LLVMRemarkStringRef
  63. LLVMRemarkEntryGetPassName(LLVMRemarkEntryRef Remark) {
  64. return wrap(&unwrap(Remark)->PassName);
  65. }
  66. extern "C" LLVMRemarkStringRef
  67. LLVMRemarkEntryGetRemarkName(LLVMRemarkEntryRef Remark) {
  68. return wrap(&unwrap(Remark)->RemarkName);
  69. }
  70. extern "C" LLVMRemarkStringRef
  71. LLVMRemarkEntryGetFunctionName(LLVMRemarkEntryRef Remark) {
  72. return wrap(&unwrap(Remark)->FunctionName);
  73. }
  74. extern "C" LLVMRemarkDebugLocRef
  75. LLVMRemarkEntryGetDebugLoc(LLVMRemarkEntryRef Remark) {
  76. if (const Optional<RemarkLocation> &Loc = unwrap(Remark)->Loc)
  77. return wrap(&*Loc);
  78. return nullptr;
  79. }
  80. extern "C" uint64_t LLVMRemarkEntryGetHotness(LLVMRemarkEntryRef Remark) {
  81. if (const Optional<uint64_t> &Hotness = unwrap(Remark)->Hotness)
  82. return *Hotness;
  83. return 0;
  84. }
  85. extern "C" uint32_t LLVMRemarkEntryGetNumArgs(LLVMRemarkEntryRef Remark) {
  86. return unwrap(Remark)->Args.size();
  87. }
  88. extern "C" LLVMRemarkArgRef
  89. LLVMRemarkEntryGetFirstArg(LLVMRemarkEntryRef Remark) {
  90. ArrayRef<Argument> Args = unwrap(Remark)->Args;
  91. // No arguments to iterate on.
  92. if (Args.empty())
  93. return nullptr;
  94. return reinterpret_cast<LLVMRemarkArgRef>(
  95. const_cast<Argument *>(Args.begin()));
  96. }
  97. extern "C" LLVMRemarkArgRef
  98. LLVMRemarkEntryGetNextArg(LLVMRemarkArgRef ArgIt, LLVMRemarkEntryRef Remark) {
  99. // No more arguments to iterate on.
  100. if (ArgIt == nullptr)
  101. return nullptr;
  102. auto It = (ArrayRef<Argument>::const_iterator)ArgIt;
  103. auto Next = std::next(It);
  104. if (Next == unwrap(Remark)->Args.end())
  105. return nullptr;
  106. return reinterpret_cast<LLVMRemarkArgRef>(const_cast<Argument *>(Next));
  107. }