From 8912c65029a77838ce791e31553744969022eabf Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 26 Apr 2022 19:57:43 +0900 Subject: [PATCH] [qfcc] Indicate type width in type strings Makes for much more informative error messages for type mismatches (confusing when both sides look the same). --- tools/qfcc/include/type.h | 1 + tools/qfcc/source/def.c | 3 ++- tools/qfcc/source/expr_binary.c | 9 +++++---- tools/qfcc/source/type.c | 23 ++++++++++++++++++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/tools/qfcc/include/type.h b/tools/qfcc/include/type.h index 63fb194e3..156189d33 100644 --- a/tools/qfcc/include/type.h +++ b/tools/qfcc/include/type.h @@ -166,6 +166,7 @@ type_t *alias_type (type_t *type, type_t *alias_chain, const char *name); const type_t *unalias_type (const type_t *type) __attribute__((pure)); const type_t *dereference_type (const type_t *type) __attribute__((pure)); void print_type_str (struct dstring_s *str, const type_t *type); +const char *get_type_string (const type_t *type); void print_type (const type_t *type); void dump_dot_type (void *t, const char *filename); const char *encode_params (const type_t *type); diff --git a/tools/qfcc/source/def.c b/tools/qfcc/source/def.c index 4303eed60..b5d63177a 100644 --- a/tools/qfcc/source/def.c +++ b/tools/qfcc/source/def.c @@ -596,7 +596,8 @@ initialize_def (symbol_t *sym, expr_t *init, defspace_t *space, } init_type = get_type (init); if (!type_assignable (sym->type, init_type)) { - error (init, "type mismatch in initializer"); + error (init, "type mismatch in initializer: %s = %s", + get_type_string (sym->type), get_type_string (init_type)); return; } if (storage == sc_local && local_expr) { diff --git a/tools/qfcc/source/expr_binary.c b/tools/qfcc/source/expr_binary.c index 6c792a1de..b2ac9eece 100644 --- a/tools/qfcc/source/expr_binary.c +++ b/tools/qfcc/source/expr_binary.c @@ -828,11 +828,12 @@ entity_compare (int op, expr_t *e1, expr_t *e2) static expr_t * invalid_binary_expr (int op, expr_t *e1, expr_t *e2) { - etype_t t1, t2; - t1 = extract_type (e1); - t2 = extract_type (e2); + type_t *t1, *t2; + t1 = get_type (e1); + t2 = get_type (e2); return error (e1, "invalid binary expression: %s %s %s", - pr_type_name[t1], get_op_string (op), pr_type_name[t2]); + get_type_string (t1), get_op_string (op), + get_type_string (t2)); } static expr_t * diff --git a/tools/qfcc/source/type.c b/tools/qfcc/source/type.c index a695e9f50..0387f862b 100644 --- a/tools/qfcc/source/type.c +++ b/tools/qfcc/source/type.c @@ -711,7 +711,9 @@ print_type_str (dstring_t *str, const type_t *type) case ev_short: case ev_ushort: case ev_double: - dasprintf (str, " %s", pr_type_name[type->type]); + dasprintf (str, " %s%s", pr_type_name[type->type], + type->width > 1 ? va (0, "{%d}", type->width) + : ""); return; case ev_invalid: case ev_type_count: @@ -722,6 +724,25 @@ print_type_str (dstring_t *str, const type_t *type) internal_error (0, "bad type meta:type %d:%d", type->meta, type->type); } +const char * +get_type_string (const type_t *type) +{ + static dstring_t *type_str[8]; + static int str_index; + + if (!type_str[str_index]) { + type_str[str_index] = dstring_newstr (); + } + dstring_clearstr (type_str[str_index]); + print_type_str (type_str[str_index], type); + const char *str = type_str[str_index++]->str; + str_index %= sizeof (type_str) / sizeof (type_str[0]); + while (*str == ' ') { + str++; + } + return str; +} + void print_type (const type_t *type) {