blob: 901acac76d90ff9fcbf3f6ddc2e3f46682ef82f8 [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
18#include "llvm-c/Core.h"
19#include "llvm-c/Types.h"
20#ifdef __cplusplus
21#include <cstddef>
22extern "C" {
23#else
24#include <stddef.h>
25#endif /* !defined(__cplusplus) */
26
27/**
28 * @defgroup LLVMCREMARKS Remarks
29 * @ingroup LLVMC
30 *
31 * @{
32 */
33
34#define REMARKS_API_VERSION 0
35
36/**
37 * String containing a buffer and a length. The buffer is not guaranteed to be
38 * zero-terminated.
39 *
40 * \since REMARKS_API_VERSION=0
41 */
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +000042typedef struct {
43 const char *Str;
44 uint32_t Len;
45} LLVMRemarkStringRef;
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000046
47/**
48 * DebugLoc containing File, Line and Column.
49 *
50 * \since REMARKS_API_VERSION=0
51 */
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +000052typedef struct {
53 // File:
54 LLVMRemarkStringRef SourceFile;
55 // Line:
56 uint32_t SourceLineNumber;
57 // Column:
58 uint32_t SourceColumnNumber;
59} LLVMRemarkDebugLoc;
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000060
61/**
62 * Element of the "Args" list. The key might give more information about what
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +000063 * are the semantics of the value, e.g. "Callee" will tell you that the value
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000064 * is a symbol that names a function.
65 *
66 * \since REMARKS_API_VERSION=0
67 */
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +000068typedef struct {
69 // e.g. "Callee"
70 LLVMRemarkStringRef Key;
71 // e.g. "malloc"
72 LLVMRemarkStringRef Value;
73
74 // "DebugLoc": Optional
75 LLVMRemarkDebugLoc DebugLoc;
76} LLVMRemarkArg;
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000077
78/**
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +000079 * One remark entry.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000080 *
81 * \since REMARKS_API_VERSION=0
82 */
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +000083typedef struct {
84 // e.g. !Missed, !Passed
85 LLVMRemarkStringRef RemarkType;
86 // "Pass": Required
87 LLVMRemarkStringRef PassName;
88 // "Name": Required
89 LLVMRemarkStringRef RemarkName;
90 // "Function": Required
91 LLVMRemarkStringRef FunctionName;
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000092
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +000093 // "DebugLoc": Optional
94 LLVMRemarkDebugLoc DebugLoc;
95 // "Hotness": Optional
96 uint32_t Hotness;
97 // "Args": Optional. It is an array of `num_args` elements.
98 uint32_t NumArgs;
99 LLVMRemarkArg *Args;
100} LLVMRemarkEntry;
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000101
102typedef struct LLVMRemarkOpaqueParser *LLVMRemarkParserRef;
103
104/**
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000105 * Creates a remark parser that can be used to read and parse the buffer located
106 * in \p Buf of size \p Size.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000107 *
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000108 * \p Buf cannot be NULL.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000109 *
110 * This function should be paired with LLVMRemarkParserDispose() to avoid
111 * leaking resources.
112 *
113 * \since REMARKS_API_VERSION=0
114 */
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000115extern LLVMRemarkParserRef LLVMRemarkParserCreate(const void *Buf,
116 uint64_t Size);
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000117
118/**
119 * Returns the next remark in the file.
120 *
121 * The value pointed to by the return value is invalidated by the next call to
122 * LLVMRemarkParserGetNext().
123 *
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000124 * If the parser reaches the end of the buffer, the return value will be NULL.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000125 *
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000126 * In the case of an error, the return value will be NULL, and:
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000127 *
128 * 1) LLVMRemarkParserHasError() will return `1`.
129 *
130 * 2) LLVMRemarkParserGetErrorMessage() will return a descriptive error
131 * message.
132 *
133 * An error may occur if:
134 *
135 * 1) An argument is invalid.
136 *
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000137 * 2) There is a YAML parsing error. This type of error aborts parsing
138 * immediately and returns `1`. It can occur on malformed YAML.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000139 *
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000140 * 3) Remark parsing error. If this type of error occurs, the parser won't call
141 * the handler and will continue to the next one. It can occur on malformed
142 * remarks, like missing or extra fields in the file.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000143 *
144 * Here is a quick example of the usage:
145 *
146 * ```
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000147 * LLVMRemarkParserRef Parser = LLVMRemarkParserCreate(Buf, Size);
148 * LLVMRemarkEntry *Remark = NULL;
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000149 * while ((Remark == LLVMRemarkParserGetNext(Parser))) {
150 * // use Remark
151 * }
152 * bool HasError = LLVMRemarkParserHasError(Parser);
153 * LLVMRemarkParserDispose(Parser);
154 * ```
155 *
156 * \since REMARKS_API_VERSION=0
157 */
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000158extern LLVMRemarkEntry *LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser);
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000159
160/**
161 * Returns `1` if the parser encountered an error while parsing the buffer.
162 *
163 * \since REMARKS_API_VERSION=0
164 */
165extern LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser);
166
167/**
168 * Returns a null-terminated string containing an error message.
169 *
170 * In case of no error, the result is `NULL`.
171 *
172 * The memory of the string is bound to the lifetime of \p Parser. If
173 * LLVMRemarkParserDispose() is called, the memory of the string will be
174 * released.
175 *
176 * \since REMARKS_API_VERSION=0
177 */
178extern const char *LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser);
179
180/**
181 * Releases all the resources used by \p Parser.
182 *
183 * \since REMARKS_API_VERSION=0
184 */
185extern void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser);
186
187/**
Francis Visoiu Mistrih064774f2019-03-19 18:21:43 +0000188 * Returns the version of the remarks dylib.
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000189 *
190 * \since REMARKS_API_VERSION=0
191 */
192extern uint32_t LLVMRemarkVersion(void);
193
194/**
195 * @} // endgoup LLVMCREMARKS
196 */
197
198#ifdef __cplusplus
199}
200#endif /* !defined(__cplusplus) */
201
202#endif /* LLVM_C_REMARKS_H */