From: Wolfgang (Blub) Bumiller Date: Thu, 23 Aug 2012 18:32:33 +0000 (+0200) Subject: Warn about unused globals too, not about functions or constants though X-Git-Tag: 0.1-rc1~63 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=8adbf0755530ff165854538ba8bee5fc5e190dc6;p=xonotic%2Fgmqcc.git Warn about unused globals too, not about functions or constants though --- diff --git a/parser.c b/parser.c index 33eba59..5b84ac2 100644 --- a/parser.c +++ b/parser.c @@ -52,7 +52,7 @@ static ast_expression* parser_parse_statement_or_block(parser_t *parser); static ast_expression* parser_expression_leave(parser_t *parser, bool stopatcomma); static ast_expression* parser_expression(parser_t *parser, bool stopatcomma); -void parseerror(parser_t *parser, const char *fmt, ...) +static void parseerror(parser_t *parser, const char *fmt, ...) { va_list ap; @@ -64,7 +64,7 @@ void parseerror(parser_t *parser, const char *fmt, ...) } /* returns true if it counts as an error */ -bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...) +static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...) { va_list ap; int lvl = LVL_WARNING; @@ -84,6 +84,24 @@ bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, .. return opts_werror; } +static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...) +{ + va_list ap; + int lvl = LVL_WARNING; + + if (!OPTS_WARN(warntype)) + return false; + + if (opts_werror) + lvl = LVL_ERROR; + + va_start(ap, fmt); + vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap); + va_end(ap); + + return opts_werror; +} + /********************************************************************** * some maths used for constant folding */ @@ -2812,6 +2830,7 @@ bool parser_finish(const char *output) { size_t i; ir_builder *ir; + bool retval = true; if (!parser->errors) { @@ -2848,9 +2867,15 @@ bool parser_finish(const char *output) } } for (i = 0; i < parser->globals_count; ++i) { + ast_value *asvalue; if (!ast_istype(parser->globals[i].var, ast_value)) continue; - if (!ast_global_codegen((ast_value*)(parser->globals[i].var), ir)) { + asvalue = (ast_value*)(parser->globals[i].var); + if (!asvalue->uses && !asvalue->isconst && asvalue->expression.vtype != TYPE_FUNCTION) { + retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE, + "unused global: `%s`", asvalue->name); + } + if (!ast_global_codegen(asvalue, ir)) { printf("failed to generate global %s\n", parser->globals[i].name); ir_builder_delete(ir); return false; @@ -2890,17 +2915,19 @@ bool parser_finish(const char *output) } } - if (opts_dump) - ir_builder_dump(ir, printf); + if (retval) { + if (opts_dump) + ir_builder_dump(ir, printf); - if (!ir_builder_generate(ir, output)) { - printf("*** failed to generate output file\n"); - ir_builder_delete(ir); - return false; + if (!ir_builder_generate(ir, output)) { + printf("*** failed to generate output file\n"); + ir_builder_delete(ir); + return false; + } } ir_builder_delete(ir); - return true; + return retval; } printf("*** there were compile errors\n");