From 7058c27785793506c21bbd324e92fc88ac6050be Mon Sep 17 00:00:00 2001 From: "Felipe A. Costa" Date: Sun, 8 Oct 2023 16:11:44 -0300 Subject: [PATCH] Improve error printing --- dusk/dusk.h | 5 +++++ dusk/dusk_compiler.c | 25 +++++++++++++++++++++++++ dusk/dusk_internal.h | 10 ++++++---- dusk/dusk_string_builder.c | 3 +-- duskc/duskc.c | 3 +++ 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/dusk/dusk.h b/dusk/dusk.h index 1d9aad9..cb3d878 100644 --- a/dusk/dusk.h +++ b/dusk/dusk.h @@ -13,6 +13,7 @@ typedef struct DuskCompiler DuskCompiler; DuskCompiler *duskCompilerCreate(void); void duskCompilerDestroy(DuskCompiler *compiler); +// Returns NULL if there was an error. uint8_t *duskCompile( DuskCompiler *compiler, const char *path, @@ -20,6 +21,10 @@ uint8_t *duskCompile( size_t text_length, size_t *spirv_byte_size); +// Builds a null-terminated string containing the error messages from the last +// compilation. +char *duskCompilerGetErrorsStringMalloc(DuskCompiler *compiler); + #ifdef __cplusplus } #endif diff --git a/dusk/dusk_compiler.c b/dusk/dusk_compiler.c index 3f53800..4ee8182 100644 --- a/dusk/dusk_compiler.c +++ b/dusk/dusk_compiler.c @@ -293,3 +293,28 @@ const char *duskGetBuiltinFunctionName(DuskBuiltinFunctionKind kind) if (kind >= DUSK_BUILTIN_FUNCTION_COUNT) return NULL; return DUSK_BUILTIN_FUNCTION_NAMES[kind]; } + +char *duskCompilerGetErrorsStringMalloc(DuskCompiler *compiler) +{ + DuskStringBuilder *sb = duskStringBuilderCreate(NULL, 8192); + +#define LINE_BUF_LEN 4096 + + char line_buf[LINE_BUF_LEN]; + for (size_t i = 0; i < duskArrayLength(compiler->errors_arr); ++i) { + DuskError err = compiler->errors_arr[i]; + int len = snprintf( + line_buf, + LINE_BUF_LEN, + "%s:%zu:%zu: %s\n", + err.location.file->path, + err.location.line, + err.location.col, + err.message); + duskStringBuilderAppendLen(sb, line_buf, (size_t)len); + } + + char *str = duskStringBuilderBuild(sb, NULL); + duskStringBuilderDestroy(sb); + return str; +} diff --git a/dusk/dusk_internal.h b/dusk/dusk_internal.h index 871a346..ce881bb 100644 --- a/dusk/dusk_internal.h +++ b/dusk/dusk_internal.h @@ -170,7 +170,7 @@ static inline uint64_t duskStringMapHash(const char *string) { uint64_t hash = 14695981039346656037ULL; while (*string) { - hash = ((hash)*1099511628211) ^ (*string); + hash = ((hash) * 1099511628211) ^ (*string); ++string; } return hash; @@ -209,8 +209,7 @@ void duskStringBuilderAppendLen( DUSK_PRINTF_FORMATTING(2, 3) void duskStringBuilderAppendFormat( DuskStringBuilder *sb, const char *format, ...); -const char * -duskStringBuilderBuild(DuskStringBuilder *sb, DuskAllocator *allocator); +char *duskStringBuilderBuild(DuskStringBuilder *sb, DuskAllocator *allocator); // }}} // Typedefs {{{ @@ -487,7 +486,10 @@ DuskType *duskTypeNewFunction( size_t param_type_count, DuskType **param_types); DuskType *duskTypeNewPointer( - DuskCompiler *compiler, DuskType *sub, DuskStorageClass storage_class, uint16_t alignment); + DuskCompiler *compiler, + DuskType *sub, + DuskStorageClass storage_class, + uint16_t alignment); DuskType *duskTypeNewImage( DuskCompiler *compiler, DuskType *sampled_type, diff --git a/dusk/dusk_string_builder.c b/dusk/dusk_string_builder.c index e9efa7b..dea3e4f 100644 --- a/dusk/dusk_string_builder.c +++ b/dusk/dusk_string_builder.c @@ -58,8 +58,7 @@ void duskStringBuilderAppendFormat( duskFree(sb->allocator, (void *)string); } -const char * -duskStringBuilderBuild(DuskStringBuilder *sb, DuskAllocator *allocator) +char *duskStringBuilderBuild(DuskStringBuilder *sb, DuskAllocator *allocator) { char *new_str = (char *)duskAllocate(allocator, duskArrayLength(sb->arr) + 1); diff --git a/duskc/duskc.c b/duskc/duskc.c index 9b8ae34..4d8f7d0 100644 --- a/duskc/duskc.c +++ b/duskc/duskc.c @@ -75,6 +75,9 @@ int main(int argc, char *argv[]) duskCompile(compiler, in_path, text, text_size, &spirv_size); if (!spirv) { + char *errors = duskCompilerGetErrorsStringMalloc(compiler); + fprintf(stderr, "Compilation finished with errors:\n%s", errors); + free(errors); exit(1); }