1 //===- Remark.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 // Implementation of the Remark type and the C API.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Remarks/Remark.h"
14 #include "llvm-c/Remarks.h"
15 #include "llvm/Support/CBindingWrapping.h"
16 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm::remarks
;
21 std::string
Remark::getArgsAsMsg() const {
23 raw_string_ostream
OS(Str
);
24 for (const Argument
&Arg
: Args
)
29 // Create wrappers for C Binding types (see CBindingWrapping.h).
30 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(StringRef
, LLVMRemarkStringRef
)
32 extern "C" const char *LLVMRemarkStringGetData(LLVMRemarkStringRef String
) {
33 return unwrap(String
)->data();
36 extern "C" uint32_t LLVMRemarkStringGetLen(LLVMRemarkStringRef String
) {
37 return unwrap(String
)->size();
40 extern "C" LLVMRemarkStringRef
41 LLVMRemarkDebugLocGetSourceFilePath(LLVMRemarkDebugLocRef DL
) {
42 return wrap(&unwrap(DL
)->SourceFilePath
);
45 extern "C" uint32_t LLVMRemarkDebugLocGetSourceLine(LLVMRemarkDebugLocRef DL
) {
46 return unwrap(DL
)->SourceLine
;
50 LLVMRemarkDebugLocGetSourceColumn(LLVMRemarkDebugLocRef DL
) {
51 return unwrap(DL
)->SourceColumn
;
54 extern "C" LLVMRemarkStringRef
LLVMRemarkArgGetKey(LLVMRemarkArgRef Arg
) {
55 return wrap(&unwrap(Arg
)->Key
);
58 extern "C" LLVMRemarkStringRef
LLVMRemarkArgGetValue(LLVMRemarkArgRef Arg
) {
59 return wrap(&unwrap(Arg
)->Val
);
62 extern "C" LLVMRemarkDebugLocRef
63 LLVMRemarkArgGetDebugLoc(LLVMRemarkArgRef Arg
) {
64 if (const Optional
<RemarkLocation
> &Loc
= unwrap(Arg
)->Loc
)
69 extern "C" void LLVMRemarkEntryDispose(LLVMRemarkEntryRef Remark
) {
70 delete unwrap(Remark
);
73 extern "C" LLVMRemarkType
LLVMRemarkEntryGetType(LLVMRemarkEntryRef Remark
) {
74 // Assume here that the enums can be converted both ways.
75 return static_cast<LLVMRemarkType
>(unwrap(Remark
)->RemarkType
);
78 extern "C" LLVMRemarkStringRef
79 LLVMRemarkEntryGetPassName(LLVMRemarkEntryRef Remark
) {
80 return wrap(&unwrap(Remark
)->PassName
);
83 extern "C" LLVMRemarkStringRef
84 LLVMRemarkEntryGetRemarkName(LLVMRemarkEntryRef Remark
) {
85 return wrap(&unwrap(Remark
)->RemarkName
);
88 extern "C" LLVMRemarkStringRef
89 LLVMRemarkEntryGetFunctionName(LLVMRemarkEntryRef Remark
) {
90 return wrap(&unwrap(Remark
)->FunctionName
);
93 extern "C" LLVMRemarkDebugLocRef
94 LLVMRemarkEntryGetDebugLoc(LLVMRemarkEntryRef Remark
) {
95 if (const Optional
<RemarkLocation
> &Loc
= unwrap(Remark
)->Loc
)
100 extern "C" uint64_t LLVMRemarkEntryGetHotness(LLVMRemarkEntryRef Remark
) {
101 if (const Optional
<uint64_t> &Hotness
= unwrap(Remark
)->Hotness
)
106 extern "C" uint32_t LLVMRemarkEntryGetNumArgs(LLVMRemarkEntryRef Remark
) {
107 return unwrap(Remark
)->Args
.size();
110 extern "C" LLVMRemarkArgRef
111 LLVMRemarkEntryGetFirstArg(LLVMRemarkEntryRef Remark
) {
112 ArrayRef
<Argument
> Args
= unwrap(Remark
)->Args
;
113 // No arguments to iterate on.
116 return reinterpret_cast<LLVMRemarkArgRef
>(
117 const_cast<Argument
*>(Args
.begin()));
120 extern "C" LLVMRemarkArgRef
121 LLVMRemarkEntryGetNextArg(LLVMRemarkArgRef ArgIt
, LLVMRemarkEntryRef Remark
) {
122 // No more arguments to iterate on.
126 auto It
= (ArrayRef
<Argument
>::const_iterator
)ArgIt
;
127 auto Next
= std::next(It
);
128 if (Next
== unwrap(Remark
)->Args
.end())
131 return reinterpret_cast<LLVMRemarkArgRef
>(const_cast<Argument
*>(Next
));