From 8cc15fa15f578d80eaded5c9ae52cf40256a998f Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Mon, 24 Aug 2026 13:37:30 +0100 Subject: [PATCH] Added warning for static functions used but never defined --- amd64.c | 4 ++++ cc.h | 3 ++- i386.c | 4 ++++ parse.c | 29 +++++++++++++++++++++++++++++ parse.h | 1 + 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/amd64.c b/amd64.c index 4308f83..d05ba32 100644 --- a/amd64.c +++ b/amd64.c @@ -40605,6 +40605,8 @@ static void parse_external_after_type (void) { if (find_global_symbol (name) >= 0) { + global_symbols[find_global_symbol (name)].is_static = (declaration_storage == STORAGE_STATIC); + set_global_symbol_size (name, declarator_is_pointer ? DATA_PTR : (parsed_type_is_void ? DATA_VOID : parsed_type_size)); set_global_symbol_pointer_info (name, declarator_effective_pointer_depth_now (), declarator_effective_pointed_size_now (parsed_type_size, object_fields, object_field_count)); @@ -40772,7 +40774,9 @@ void compile_translation_unit64 (void) { masm_flush_data_line (); } + warn_undefined_static_functions (get_filename ()); warn_unused_static_functions (get_filename ()); + emit_pending_extern_symbols (); if (vec_dllexports.length || vec_dllaliases.length) { diff --git a/cc.h b/cc.h index 3243bb5..9847d3b 100755 --- a/cc.h +++ b/cc.h @@ -21,8 +21,9 @@ #define WARN_FALLTHROUGH (1U << 8) #define WARN_OLD_STYLE (1U << 9) #define WARN_INITIALIZER (1U << 10) +#define WARN_STATIC_UNDEFINED (1U << 11) -#define WARN_DEFAULT (WARN_DUP_DEFINE) +#define WARN_DEFAULT (WARN_DUP_DEFINE | WARN_STATIC_UNDEFINED) #define WARN_ALL (WARN_DEFAULT | WARN_NO_RETURN | WARN_SIGNEDNESS | WARN_UNUSED | WARN_FALLTHROUGH | WARN_INITIALIZER) #define WARN_EXTRA (WARN_ALL | WARN_CONVERSION | WARN_OLD_STYLE) diff --git a/i386.c b/i386.c index 3f08288..ff467db 100644 --- a/i386.c +++ b/i386.c @@ -37902,6 +37902,8 @@ static void parse_external_after_type (void) { if (find_global_symbol (name) >= 0) { + global_symbols[find_global_symbol (name)].is_static = (declaration_storage == STORAGE_STATIC); + set_global_symbol_size (name, declarator_is_pointer ? DATA_PTR : (parsed_type_is_void ? DATA_VOID : parsed_type_size)); set_global_symbol_pointer_info (name, declarator_effective_pointer_depth_now (), declarator_effective_pointed_size_now (parsed_type_size, object_fields, object_field_count)); @@ -38072,7 +38074,9 @@ void compile_translation_unit32 (void) { masm_flush_data_line (); } + warn_undefined_static_functions (get_filename ()); warn_unused_static_functions (get_filename ()); + emit_pending_extern_symbols (); if (vec_dllexports.length || vec_dllaliases.length) { diff --git a/parse.c b/parse.c index 393b884..f86fb9d 100644 --- a/parse.c +++ b/parse.c @@ -2409,6 +2409,35 @@ void warn_unused_local_symbols (int start, const char *filename) { } +void warn_undefined_static_functions (const char *filename) { + + int i; + + if (!state || !(state->warn & WARN_STATIC_UNDEFINED)) { + return; + } + + for (i = 0; i < global_symbol_count; i++) { + + struct global_symbol_entry *sym = &global_symbols[i]; + + if (sym->kind != GLOBAL_SYMBOL_FUNCTION || !sym->is_static || !sym->used || !sym->is_extern) { + continue; + } + + if (sym->decl_start && sym->decl_line) { + + report_line_at (sym->decl_filename ? sym->decl_filename : (filename ? filename : get_filename ()), sym->decl_line, REPORT_WARNING, + sym->decl_start, sym->decl_start + sym->decl_caret_offset, "'%s' used but never defined", sym->name); + + } else { + report_at (filename ? filename : get_filename (), get_line_number (), REPORT_WARNING, "'%s' used but never defined", sym->name); + } + + } + +} + void warn_unused_static_functions (const char *filename) { int i; diff --git a/parse.h b/parse.h index 541a3a0..16e3d79 100644 --- a/parse.h +++ b/parse.h @@ -221,6 +221,7 @@ struct local_symbol *peek_local_symbol (const char *name); void warn_shadow_symbol (const char *name, int scope_start_count, int line, const char *start, const char *caret, int is_parameter); void warn_unused_local_symbols (int start, const char *filename); void warn_unused_static_functions (const char *filename); +void warn_undefined_static_functions (const char *filename); #define MAX_GLOBAL_SYMBOLS 4096 int find_global_symbol (const char *name); -- 2.34.1