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