infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
error.c
Go to the documentation of this file.
1
42#include <infix/infix.h>
43#include <stdarg.h>
44#include <stdio.h> // For snprintf
45#include <string.h>
46
47// Use a portable mechanism for thread-local storage (TLS).
48// This series of #ifdefs selects the correct keyword for the current compiler.
49#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
50#define INFIX_TLS _Thread_local // C11 standard TLS
51#elif defined(__GNUC__) || defined(__clang__)
52#define INFIX_TLS __thread // GCC/Clang extension
53#elif defined(_MSC_VER)
54#define INFIX_TLS __declspec(thread) // MSVC specific
55#else
56// Fallback for compilers that do not support TLS. This is not thread-safe.
57#warning "Compiler does not support thread-local storage; error handling will not be thread-safe."
58#define INFIX_TLS
59#endif
60
61// A portable macro for safe string copying to prevent buffer overflows.
62#if defined(_MSC_VER)
63#define _INFIX_SAFE_STRNCPY(dest, src, count) strncpy_s(dest, sizeof(dest), src, count)
64#else
65#define _INFIX_SAFE_STRNCPY(dest, src, count) \
66 do { \
67 strncpy(dest, src, (count)); \
68 (dest)[(sizeof(dest)) - 1] = '\0'; \
69 } while (0)
70#endif
71
80
90
98 switch (code) {
100 return "Success";
102 return "An unknown error occurred";
104 return "Out of memory";
106 return "Failed to allocate executable memory";
108 return "Failed to change memory protection flags";
110 return "Unexpected token or character";
112 return "Unterminated aggregate (missing '}', '>', ']', or ')')'";
114 return "Invalid type keyword";
116 return "Function signature missing '->' or return type";
118 return "Integer overflow detected during layout calculation";
120 return "Type definition is too deeply nested";
122 return "Named type was declared with empty angle brackets";
124 return "The current platform ABI is not supported";
126 return "A data type was too large to be handled by the ABI";
128 return "Named type not found in registry or is an undefined forward declaration";
130 return "Aggregate contains an illegal member type (e.g., a struct with a void member)";
132 return "The requested dynamic library could not be found";
134 return "The requested symbol was not found in the library";
136 return "Loading the dynamic library failed";
137 default:
138 return "An unknown or unspecified error occurred";
139 }
140}
141
155void _infix_set_error(infix_error_category_t category, infix_error_code_t code, size_t position) {
156 g_infix_last_error.category = category;
158 g_infix_last_error.position = position;
160
161 // Check if we can generate a rich parser error message.
162 if (category == INFIX_CATEGORY_PARSER && g_infix_last_signature_context != nullptr) {
163 // Generate a rich, GCC-style error message for parser failures.
164 const char * signature = g_infix_last_signature_context;
165 size_t sig_len = strlen(signature);
166 const size_t radius = 20; // Number of characters to show around the error position.
167
168 // Calculate the start and end of the snippet to display.
169 size_t start = (position > radius) ? (position - radius) : 0;
170 size_t end = (position + radius < sig_len) ? (position + radius) : sig_len;
171
172 // Add indicators if the snippet is truncated.
173 const char * start_indicator = (start > 0) ? "... " : "";
174 const char * end_indicator = (end < sig_len) ? " ..." : "";
175 size_t start_indicator_len = (start > 0) ? 4 : 0;
176
177 // Create the code snippet line.
178 char snippet[128];
179 snprintf(snippet,
180 sizeof(snippet),
181 "%s%.*s%s",
182 start_indicator,
183 (int)(end - start),
184 signature + start,
185 end_indicator);
186
187 // Create the pointer line with a caret '^' under the error.
188 char pointer[128];
189 size_t caret_pos = position - start + start_indicator_len;
190 snprintf(pointer, sizeof(pointer), "%*s^", (int)caret_pos, "");
191
192 // Build the final multi-line message piece by piece to avoid buffer overflows.
193 char * p = g_infix_last_error.message;
194 size_t remaining = sizeof(g_infix_last_error.message);
195 int written;
196
197 // Write the snippet and pointer lines.
198 written = snprintf(p, remaining, "\n\n %s\n %s", snippet, pointer);
199 if (written < 0 || (size_t)written >= remaining) {
200 // Fallback to a simple message on snprintf failure or buffer overflow.
201 const char * msg = _get_error_message_for_code(code);
203 return;
204 }
205 p += written;
206 remaining -= written;
207
208 // Append the standard error description.
209 snprintf(p, remaining, "\n\nError: %s", _get_error_message_for_code(code));
210 }
211 else {
212 // For non-parser errors, just copy the standard message.
213 const char * msg = _get_error_message_for_code(code);
215 }
216}
217
234 long system_code,
235 const char * msg) {
236 g_infix_last_error.category = category;
240 if (msg) {
242 }
243 else {
244 const char * default_msg = _get_error_message_for_code(code);
246 }
247}
248
265
clock_t start
Definition 901_call_overhead.c:50
clock_t end
Definition 901_call_overhead.c:50
static const char * _get_error_message_for_code(infix_error_code_t code)
Definition error.c:97
void _infix_set_error(infix_error_category_t category, infix_error_code_t code, size_t position)
Sets the thread-local error state with detailed information.
Definition error.c:155
INFIX_TLS const char * g_infix_last_signature_context
A thread-local pointer to the full signature string being parsed.
Definition error.c:89
void _infix_set_system_error(infix_error_category_t category, infix_error_code_t code, long system_code, const char *msg)
Sets the thread-local error state for a system-level error.
Definition error.c:232
#define _INFIX_SAFE_STRNCPY(dest, src, count)
Definition error.c:65
void _infix_clear_error(void)
Clears the thread-local error state.
Definition error.c:257
static INFIX_TLS infix_error_details_t g_infix_last_error
The thread-local variable that stores the details of the last error.
Definition error.c:79
#define INFIX_TLS
Definition error.c:58
infix_error_code_t
Enumerates specific error codes.
Definition infix.h:1324
infix_error_details_t infix_get_last_error(void)
Retrieves detailed information about the last error that occurred on the current thread.
Definition error.c:270
infix_error_category_t
Enumerates the high-level categories of errors that can occur.
Definition infix.h:1313
@ INFIX_CODE_PROTECTION_FAILURE
Definition infix.h:1332
@ INFIX_CODE_SUCCESS
Definition infix.h:1326
@ INFIX_CODE_LIBRARY_NOT_FOUND
Definition infix.h:1351
@ INFIX_CODE_INVALID_MEMBER_TYPE
Definition infix.h:1347
@ INFIX_CODE_UNRESOLVED_NAMED_TYPE
Definition infix.h:1346
@ INFIX_CODE_INTEGER_OVERFLOW
Definition infix.h:1339
@ INFIX_CODE_TYPE_TOO_LARGE
Definition infix.h:1345
@ INFIX_CODE_UNEXPECTED_TOKEN
Definition infix.h:1335
@ INFIX_CODE_MISSING_RETURN_TYPE
Definition infix.h:1338
@ INFIX_CODE_EMPTY_MEMBER_NAME
Definition infix.h:1341
@ INFIX_CODE_EXECUTABLE_MEMORY_FAILURE
Definition infix.h:1331
@ INFIX_CODE_UNKNOWN
Definition infix.h:1327
@ INFIX_CODE_SYMBOL_NOT_FOUND
Definition infix.h:1352
@ INFIX_CODE_RECURSION_DEPTH_EXCEEDED
Definition infix.h:1340
@ INFIX_CODE_UNSUPPORTED_ABI
Definition infix.h:1344
@ INFIX_CODE_UNTERMINATED_AGGREGATE
Definition infix.h:1336
@ INFIX_CODE_LIBRARY_LOAD_FAILED
Definition infix.h:1353
@ INFIX_CODE_INVALID_KEYWORD
Definition infix.h:1337
@ INFIX_CODE_OUT_OF_MEMORY
Definition infix.h:1330
@ INFIX_CATEGORY_PARSER
Definition infix.h:1317
@ INFIX_CATEGORY_NONE
Definition infix.h:1314
size_t position
Definition infix.h:1363
infix_error_category_t category
Definition infix.h:1361
char message[256]
Definition infix.h:1365
long system_error_code
Definition infix.h:1364
infix_error_code_t code
Definition infix.h:1362
The public interface for the infix FFI library.
Internal data structures, function prototypes, and constants.
Provides detailed, thread-local information about the last error that occurred.
Definition infix.h:1360