blob: 5444aebddd60450bf35f9bfc9fb1c2a755911a72 [file] [log] [blame]
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +00001/*===-- llvm-c/Remarks.h - Remarks Public C Interface -------------*- C -*-===*\
2|* *|
3|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4|* Exceptions. *|
5|* See https://llvm.org/LICENSE.txt for license information. *|
6|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
7|* *|
8|*===----------------------------------------------------------------------===*|
9|* *|
10|* This header provides a public interface to a remark diagnostics library. *|
11|* LLVM provides an implementation of this interface. *|
12|* *|
13\*===----------------------------------------------------------------------===*/
14
15#ifndef LLVM_C_REMARKS_H
16#define LLVM_C_REMARKS_H
17
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000018#include "llvm-c/Types.h"
19#ifdef __cplusplus
20#include <cstddef>
21extern "C" {
22#else
23#include <stddef.h>
24#endif /* !defined(__cplusplus) */
25
26/**
27 * @defgroup LLVMCREMARKS Remarks
28 * @ingroup LLVMC
29 *
30 * @{
31 */
32
Francis Visoiu Mistriha85d9ef2019-09-09 17:43:50 +000033// 0 -> 1: Bitstream remarks support.
34#define REMARKS_API_VERSION 1
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000035
36/**
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000037 * The type of the emitted remark.
38 */
39enum LLVMRemarkType {
40 LLVMRemarkTypeUnknown,
41 LLVMRemarkTypePassed,
42 LLVMRemarkTypeMissed,
43 LLVMRemarkTypeAnalysis,
44 LLVMRemarkTypeAnalysisFPCommute,
45 LLVMRemarkTypeAnalysisAliasing,
46 LLVMRemarkTypeFailure
47};
48
49/**
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000050 * String containing a buffer and a length. The buffer is not guaranteed to be
51 * zero-terminated.
52 *
53 * \since REMARKS_API_VERSION=0
54 */
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000055typedef struct LLVMRemarkOpaqueString *LLVMRemarkStringRef;
56
57/**
58 * Returns the buffer holding the string.
59 *
60 * \since REMARKS_API_VERSION=0
61 */
62extern const char *LLVMRemarkStringGetData(LLVMRemarkStringRef String);
63
64/**
65 * Returns the size of the string.
66 *
67 * \since REMARKS_API_VERSION=0
68 */
69extern uint32_t LLVMRemarkStringGetLen(LLVMRemarkStringRef String);
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000070
71/**
72 * DebugLoc containing File, Line and Column.
73 *
74 * \since REMARKS_API_VERSION=0
75 */
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000076typedef struct LLVMRemarkOpaqueDebugLoc *LLVMRemarkDebugLocRef;
77
78/**
79 * Return the path to the source file for a debug location.
80 *
81 * \since REMARKS_API_VERSION=0
82 */
83extern LLVMRemarkStringRef
84LLVMRemarkDebugLocGetSourceFilePath(LLVMRemarkDebugLocRef DL);
85
86/**
87 * Return the line in the source file for a debug location.
88 *
89 * \since REMARKS_API_VERSION=0
90 */
91extern uint32_t LLVMRemarkDebugLocGetSourceLine(LLVMRemarkDebugLocRef DL);
92
93/**
94 * Return the column in the source file for a debug location.
95 *
96 * \since REMARKS_API_VERSION=0
97 */
98extern uint32_t LLVMRemarkDebugLocGetSourceColumn(LLVMRemarkDebugLocRef DL);
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000099
100/**
101 * Element of the "Args" list. The key might give more information about what
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000102 * the semantics of the value are, e.g. "Callee" will tell you that the value
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000103 * is a symbol that names a function.
104 *
105 * \since REMARKS_API_VERSION=0
106 */
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000107typedef struct LLVMRemarkOpaqueArg *LLVMRemarkArgRef;
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000108
109/**
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000110 * Returns the key of an argument. The key defines what the value is, and the
111 * same key can appear multiple times in the list of arguments.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000112 *
113 * \since REMARKS_API_VERSION=0
114 */
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000115extern LLVMRemarkStringRef LLVMRemarkArgGetKey(LLVMRemarkArgRef Arg);
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000116
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000117/**
118 * Returns the value of an argument. This is a string that can contain newlines.
119 *
120 * \since REMARKS_API_VERSION=0
121 */
122extern LLVMRemarkStringRef LLVMRemarkArgGetValue(LLVMRemarkArgRef Arg);
123
124/**
125 * Returns the debug location that is attached to the value of this argument.
126 *
127 * If there is no debug location, the return value will be `NULL`.
128 *
129 * \since REMARKS_API_VERSION=0
130 */
131extern LLVMRemarkDebugLocRef LLVMRemarkArgGetDebugLoc(LLVMRemarkArgRef Arg);
132
133/**
134 * A remark emitted by the compiler.
135 *
136 * \since REMARKS_API_VERSION=0
137 */
138typedef struct LLVMRemarkOpaqueEntry *LLVMRemarkEntryRef;
139
140/**
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000141 * Free the resources used by the remark entry.
142 *
143 * \since REMARKS_API_VERSION=0
144 */
145extern void LLVMRemarkEntryDispose(LLVMRemarkEntryRef Remark);
146
147/**
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000148 * The type of the remark. For example, it can allow users to only keep the
149 * missed optimizations from the compiler.
150 *
151 * \since REMARKS_API_VERSION=0
152 */
153extern enum LLVMRemarkType LLVMRemarkEntryGetType(LLVMRemarkEntryRef Remark);
154
155/**
156 * Get the name of the pass that emitted this remark.
157 *
158 * \since REMARKS_API_VERSION=0
159 */
160extern LLVMRemarkStringRef
161LLVMRemarkEntryGetPassName(LLVMRemarkEntryRef Remark);
162
163/**
164 * Get an identifier of the remark.
165 *
166 * \since REMARKS_API_VERSION=0
167 */
168extern LLVMRemarkStringRef
169LLVMRemarkEntryGetRemarkName(LLVMRemarkEntryRef Remark);
170
171/**
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000172 * Get the name of the function being processed when the remark was emitted.
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000173 *
174 * \since REMARKS_API_VERSION=0
175 */
176extern LLVMRemarkStringRef
177LLVMRemarkEntryGetFunctionName(LLVMRemarkEntryRef Remark);
178
179/**
180 * Returns the debug location that is attached to this remark.
181 *
182 * If there is no debug location, the return value will be `NULL`.
183 *
184 * \since REMARKS_API_VERSION=0
185 */
186extern LLVMRemarkDebugLocRef
187LLVMRemarkEntryGetDebugLoc(LLVMRemarkEntryRef Remark);
188
189/**
190 * Return the hotness of the remark.
191 *
192 * A hotness of `0` means this value is not set.
193 *
194 * \since REMARKS_API_VERSION=0
195 */
196extern uint64_t LLVMRemarkEntryGetHotness(LLVMRemarkEntryRef Remark);
197
198/**
199 * The number of arguments the remark holds.
200 *
201 * \since REMARKS_API_VERSION=0
202 */
203extern uint32_t LLVMRemarkEntryGetNumArgs(LLVMRemarkEntryRef Remark);
204
205/**
206 * Get a new iterator to iterate over a remark's argument.
207 *
208 * If there are no arguments in \p Remark, the return value will be `NULL`.
209 *
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000210 * The lifetime of the returned value is bound to the lifetime of \p Remark.
211 *
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000212 * \since REMARKS_API_VERSION=0
213 */
214extern LLVMRemarkArgRef LLVMRemarkEntryGetFirstArg(LLVMRemarkEntryRef Remark);
215
216/**
217 * Get the next argument in \p Remark from the position of \p It.
218 *
219 * Returns `NULL` if there are no more arguments available.
220 *
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000221 * The lifetime of the returned value is bound to the lifetime of \p Remark.
222 *
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000223 * \since REMARKS_API_VERSION=0
224 */
225extern LLVMRemarkArgRef LLVMRemarkEntryGetNextArg(LLVMRemarkArgRef It,
226 LLVMRemarkEntryRef Remark);
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000227
228typedef struct LLVMRemarkOpaqueParser *LLVMRemarkParserRef;
229
230/**
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000231 * Creates a remark parser that can be used to parse the buffer located in \p
232 * Buf of size \p Size bytes.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000233 *
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000234 * \p Buf cannot be `NULL`.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000235 *
236 * This function should be paired with LLVMRemarkParserDispose() to avoid
237 * leaking resources.
238 *
239 * \since REMARKS_API_VERSION=0
240 */
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000241extern LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
242 uint64_t Size);
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000243
244/**
Francis Visoiu Mistriha85d9ef2019-09-09 17:43:50 +0000245 * Creates a remark parser that can be used to parse the buffer located in \p
246 * Buf of size \p Size bytes.
247 *
248 * \p Buf cannot be `NULL`.
249 *
250 * This function should be paired with LLVMRemarkParserDispose() to avoid
251 * leaking resources.
252 *
253 * \since REMARKS_API_VERSION=1
254 */
255extern LLVMRemarkParserRef LLVMRemarkParserCreateBitstream(const void *Buf,
256 uint64_t Size);
257
258/**
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000259 * Returns the next remark in the file.
260 *
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000261 * The value pointed to by the return value needs to be disposed using a call to
262 * LLVMRemarkEntryDispose().
263 *
264 * All the entries in the returned value that are of LLVMRemarkStringRef type
265 * will become invalidated once a call to LLVMRemarkParserDispose is made.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000266 *
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000267 * If the parser reaches the end of the buffer, the return value will be `NULL`.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000268 *
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000269 * In the case of an error, the return value will be `NULL`, and:
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000270 *
271 * 1) LLVMRemarkParserHasError() will return `1`.
272 *
273 * 2) LLVMRemarkParserGetErrorMessage() will return a descriptive error
274 * message.
275 *
276 * An error may occur if:
277 *
278 * 1) An argument is invalid.
279 *
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000280 * 2) There is a parsing error. This can occur on things like malformed YAML.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000281 *
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000282 * 3) There is a Remark semantic error. This can occur on well-formed files with
283 * missing or extra fields.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000284 *
285 * Here is a quick example of the usage:
286 *
287 * ```
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000288 * LLVMRemarkParserRef Parser = LLVMRemarkParserCreateYAML(Buf, Size);
289 * LLVMRemarkEntryRef Remark = NULL;
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000290 * while ((Remark = LLVMRemarkParserGetNext(Parser))) {
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000291 * // use Remark
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000292 * LLVMRemarkEntryDispose(Remark); // Release memory.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000293 * }
294 * bool HasError = LLVMRemarkParserHasError(Parser);
295 * LLVMRemarkParserDispose(Parser);
296 * ```
297 *
298 * \since REMARKS_API_VERSION=0
299 */
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000300extern LLVMRemarkEntryRef LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser);
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000301
302/**
303 * Returns `1` if the parser encountered an error while parsing the buffer.
304 *
305 * \since REMARKS_API_VERSION=0
306 */
307extern LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser);
308
309/**
310 * Returns a null-terminated string containing an error message.
311 *
312 * In case of no error, the result is `NULL`.
313 *
314 * The memory of the string is bound to the lifetime of \p Parser. If
315 * LLVMRemarkParserDispose() is called, the memory of the string will be
316 * released.
317 *
318 * \since REMARKS_API_VERSION=0
319 */
320extern const char *LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser);
321
322/**
323 * Releases all the resources used by \p Parser.
324 *
325 * \since REMARKS_API_VERSION=0
326 */
327extern void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser);
328
329/**
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000330 * Returns the version of the remarks library.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000331 *
332 * \since REMARKS_API_VERSION=0
333 */
334extern uint32_t LLVMRemarkVersion(void);
335
336/**
337 * @} // endgoup LLVMCREMARKS
338 */
339
340#ifdef __cplusplus
341}
342#endif /* !defined(__cplusplus) */
343
344#endif /* LLVM_C_REMARKS_H */