From 261ea0c4de42f36fee8ab514346d135217cc8b42 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 2 Oct 2023 14:21:40 +0900 Subject: [PATCH] [qfcc] Be more const-correct with expressions Diagnostics that return an expression now return const, and fixes error not returning an error expression. --- tools/qfcc/include/diagnostic.h | 22 ++++++++++++---------- tools/qfcc/include/expr.h | 10 +++++----- tools/qfcc/source/diagnostic.c | 14 +++++++------- tools/qfcc/source/expr.c | 10 +++++----- tools/qfcc/source/method.c | 2 +- tools/qfcc/source/stub.c | 4 ++-- 6 files changed, 32 insertions(+), 30 deletions(-) diff --git a/tools/qfcc/include/diagnostic.h b/tools/qfcc/include/diagnostic.h index 4e72b6da0..529f7db99 100644 --- a/tools/qfcc/include/diagnostic.h +++ b/tools/qfcc/include/diagnostic.h @@ -31,6 +31,8 @@ #ifndef __diagnostic_h #define __diagnostic_h +typedef struct expr_s expr_t; + /** \defgroup qfcc_diagnostic Diagnostic Messages \ingroup qfcc */ @@ -42,37 +44,37 @@ extern diagnostic_hook error_hook; extern diagnostic_hook warning_hook; extern diagnostic_hook notice_hook; -struct expr_s *_error (const struct expr_s *e, const char *file, - int line, const char *func, const char *fmt, ...) +const expr_t *_error (const expr_t *e, const char *file, + int line, const char *func, const char *fmt, ...) __attribute__ ((format (PRINTF, 5, 6))); #define error(e, fmt...) _error(e, __FILE__, __LINE__, __FUNCTION__, fmt) -void _internal_error (const struct expr_s *e, const char *file, int line, +void _internal_error (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) __attribute__ ((format (PRINTF, 5, 6), noreturn)); #define internal_error(e, fmt...) _internal_error(e, __FILE__, __LINE__, __FUNCTION__, fmt) -struct expr_s *_warning (const struct expr_s *e, const char *file, - int line, const char *func, const char *fmt, ...) +const expr_t *_warning (const expr_t *e, const char *file, + int line, const char *func, const char *fmt, ...) __attribute__ ((format (PRINTF, 5, 6))); #define warning(e, fmt...) _warning(e, __FILE__, __LINE__, __FUNCTION__, fmt) -struct expr_s *_notice (const struct expr_s *e, const char *file, - int line, const char *func, const char *fmt, ...) +const expr_t *_notice (const expr_t *e, const char *file, + int line, const char *func, const char *fmt, ...) __attribute__ ((format (PRINTF, 5, 6))); #define notice(e, fmt...) _notice(e, __FILE__, __LINE__, __FUNCTION__, fmt) -void _debug (const struct expr_s *e, const char *file, int line, +void _debug (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) __attribute__ ((format (PRINTF, 5, 6))); #define debug(e, fmt...) _debug(e, __FILE__, __LINE__, __FUNCTION__, fmt) -void _bug (const struct expr_s *e, const char *file, int line, +void _bug (const expr_t *e, const char *file, int line, const char * func, const char *fmt, ...) __attribute__ ((format (PRINTF, 5, 6))); #define bug(e, fmt...) _bug(e, __FILE__, __LINE__, __FUNCTION__, fmt) -void print_srcline (int rep, const struct expr_s *e); +void print_srcline (int rep, const expr_t *e); ///@} diff --git a/tools/qfcc/include/expr.h b/tools/qfcc/include/expr.h index 8a5b042ba..4fb859faf 100644 --- a/tools/qfcc/include/expr.h +++ b/tools/qfcc/include/expr.h @@ -360,11 +360,11 @@ extern const char *expr_names[]; \param op The opcode of the expression. \return \a e1 with its type set to ex_error. */ -expr_t *type_mismatch (const expr_t *e1, const expr_t *e2, int op); +const expr_t *type_mismatch (const expr_t *e1, const expr_t *e2, int op); -expr_t *param_mismatch (const expr_t *e, int param, const char *fn, - type_t *t1, type_t *t2); -expr_t *test_error (const expr_t *e, type_t *t); +const expr_t *param_mismatch (const expr_t *e, int param, const char *fn, + struct type_s *t1, struct type_s *t2); +const expr_t *test_error (const expr_t *e, struct type_s *t); extern expr_t *local_expr; @@ -540,7 +540,7 @@ expr_t *new_unary_expr (int op, const expr_t *e1); */ expr_t *new_horizontal_expr (int op, const expr_t *vec, type_t *type); -expr_t *new_swizzle_expr (const expr_t *src, const char *swizzle); +const expr_t *new_swizzle_expr (const expr_t *src, const char *swizzle); expr_t *new_extend_expr (const expr_t *src, type_t *type, int ext, bool rev); diff --git a/tools/qfcc/source/diagnostic.c b/tools/qfcc/source/diagnostic.c index c6b447467..a6ba01d5e 100644 --- a/tools/qfcc/source/diagnostic.c +++ b/tools/qfcc/source/diagnostic.c @@ -208,14 +208,14 @@ _bug (const expr_t *e, const char *file, int line, const char *func, va_end (args); } -expr_t * +const expr_t * _notice (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) { va_list args; if (options.notices.silent) - return (expr_t *) e; + return e; va_start (args, fmt); if (options.notices.promote) { @@ -237,10 +237,10 @@ _notice (const expr_t *e, const char *file, int line, const char *func, dstring_delete (message); } va_end (args); - return (expr_t *) e; + return e; } -expr_t * +const expr_t * _warning (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) { @@ -249,7 +249,7 @@ _warning (const expr_t *e, const char *file, int line, const char *func, va_start (args, fmt); __warning (e, file, line, func, fmt, args); va_end (args); - return (expr_t *) e; + return e; } void @@ -263,7 +263,7 @@ _internal_error (const expr_t *e, const char *file, int line, va_end (args); } -expr_t * +const expr_t * _error (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) { @@ -292,5 +292,5 @@ _error (const expr_t *e, const char *file, int line, const char *func, expr_t *err = new_expr (); err->type = ex_error; - return (expr_t *) e; + return err; } diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index d24c65cbb..7a1e3ca80 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -214,7 +214,7 @@ extract_type (const expr_t *e) return ev_type_count; } -expr_t * +const expr_t * type_mismatch (const expr_t *e1, const expr_t *e2, int op) { if (options.verbosity >= 2) { @@ -226,7 +226,7 @@ type_mismatch (const expr_t *e1, const expr_t *e2, int op) get_type_string (get_type (e2))); } -expr_t * +const expr_t * param_mismatch (const expr_t *e, int param, const char *fn, type_t *t1, type_t *t2) { return error (e, "type mismatch for parameter %d of %s: " @@ -234,7 +234,7 @@ param_mismatch (const expr_t *e, int param, const char *fn, type_t *t1, type_t * get_type_string (t2)); } -expr_t * +const expr_t * test_error (const expr_t *e, type_t *t) { dstring_t *s = dstring_newstr (); @@ -591,7 +591,7 @@ new_horizontal_expr (int op, const expr_t *vec, type_t *type) return e; } -expr_t * +const expr_t * new_swizzle_expr (const expr_t *src, const char *swizzle) { src = convert_name (src); @@ -2076,7 +2076,7 @@ build_function_call (const expr_t *fexpr, const type_t *ftype, const expr_t *par { int param_count = 0; expr_t *call; - expr_t *err = 0; + const expr_t *err = 0; int arg_count = params ? list_count (¶ms->list) :0; const expr_t *arguments[arg_count]; diff --git a/tools/qfcc/source/method.c b/tools/qfcc/source/method.c index 7604e1f51..167ec1e30 100644 --- a/tools/qfcc/source/method.c +++ b/tools/qfcc/source/method.c @@ -704,7 +704,7 @@ const expr_t * method_check_params (method_t *method, const expr_t *args) { int i, param_count; - expr_t *err = 0; + const expr_t *err = 0; type_t *mtype = method->type; if (mtype->t.func.num_params == -1) diff --git a/tools/qfcc/source/stub.c b/tools/qfcc/source/stub.c index d944bc7b0..7dd5dcbff 100644 --- a/tools/qfcc/source/stub.c +++ b/tools/qfcc/source/stub.c @@ -55,8 +55,8 @@ void codespace_addcode (codespace_t *codespace, struct dstatement_s *code, int s __attribute__((const)) int function_parms (function_t *f, byte *parm_size) {return 0;} void def_to_ddef (def_t *def, ddef_t *ddef, int aux) {} __attribute__((noreturn)) void _internal_error (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) {abort();} -__attribute__((const)) expr_t *_warning (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) {return 0;} -__attribute__((const)) expr_t *_error (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) {return 0;} +__attribute__((const)) const expr_t *_warning (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) {return 0;} +__attribute__((const)) const expr_t *_error (const expr_t *e, const char *file, int line, const char *func, const char *fmt, ...) {return 0;} __attribute__((const)) symbol_t *make_structure (const char *name, int su, struct_def_t *defs, type_t *type) {return 0;} __attribute__((const)) symbol_t *symtab_addsymbol (symtab_t *symtab, symbol_t *symbol) {return 0;} __attribute__((const)) symbol_t *new_symbol_type (const char *name, type_t *type) {return 0;}