From 873d613dd597f550b1b64946c4577b012d61d1c9 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 18 Feb 2023 07:14:05 +0000 Subject: [PATCH] Fix missing statics and #includes on variables. After Ben fixed all the unwanted global functions by using gcc's -Wmissing-declarations to spot any that were not predeclared, I remembered that clang has -Wmissing-variable-declarations, which does the same job for global objects. Enabled it in -DSTRICT=ON, and made the code clean under it. Mostly this was just a matter of sticking 'static' on the front of things. One variable was outright removed ('verbose' in signpost.c) because after I made it static clang was then able to spot that it was also unused. The more interesting cases were the ones where declarations had to be _added_ to header files. In particular, in COMBINED builds, puzzles.h now arranges to have predeclared each 'game' structure defined by a puzzle backend. Also there's a new tiny header file gtk.h, containing the declarations of xpm_icons and n_xpm_icons which are exported by each puzzle's autogenerated icon source file and by no-icon.c. Happily even the real XPM icon files were generated by our own Perl script rather than being raw xpm output from ImageMagick, so there was no difficulty adding the corresponding #include in there. --- cmake/platforms/unix.cmake | 2 +- dominosa.c | 4 ++-- galaxies.c | 4 ++-- gtk.c | 10 +++------- gtk.h | 7 +++++++ icons/cicon.pl | 1 + latin.c | 2 +- lightup.c | 2 +- list.c | 4 ---- magnets.c | 6 +++--- map.c | 2 +- no-icon.c | 2 ++ pattern.c | 4 ++-- pearl.c | 2 +- penrose.c | 4 ++-- puzzles.h | 4 ++++ signpost.c | 3 +-- singles.c | 2 +- slant.c | 2 +- solo.c | 2 +- tents.c | 2 +- tree234.c | 8 ++++---- unequal.c | 4 ++-- unruly.c | 4 ++-- version.c | 1 + 25 files changed, 47 insertions(+), 41 deletions(-) create mode 100644 gtk.h diff --git a/cmake/platforms/unix.cmake b/cmake/platforms/unix.cmake index ca4be72..4a00b6c 100644 --- a/cmake/platforms/unix.cmake +++ b/cmake/platforms/unix.cmake @@ -63,7 +63,7 @@ if(STRICT AND (CMAKE_C_COMPILER_ID MATCHES "GNU")) endif() if(STRICT AND (CMAKE_C_COMPILER_ID MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-prototypes") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-prototypes -Wmissing-variable-declarations") endif() add_compile_definitions(HELP_DIR="${CMAKE_INSTALL_PREFIX}/share/sgt-puzzles/help") diff --git a/dominosa.c b/dominosa.c index 94e61b1..baf5054 100644 --- a/dominosa.c +++ b/dominosa.c @@ -259,9 +259,9 @@ static const char *validate_params(const game_params *params, bool full) #ifdef STANDALONE_SOLVER #define SOLVER_DIAGNOSTICS -bool solver_diagnostics = false; +static bool solver_diagnostics = false; #elif defined SOLVER_DIAGNOSTICS -const bool solver_diagnostics = true; +static const bool solver_diagnostics = true; #endif struct solver_domino; diff --git a/galaxies.c b/galaxies.c index d69ed84..a789349 100644 --- a/galaxies.c +++ b/galaxies.c @@ -1278,7 +1278,7 @@ static bool generate_try_block(game_state *state, random_state *rs, } #ifdef STANDALONE_SOLVER -int maxtries; +static int maxtries; #define MAXTRIES maxtries #else #define MAXTRIES 50 @@ -3869,7 +3869,7 @@ const struct game thegame = { #ifdef STANDALONE_SOLVER -const char *quis; +static const char *quis; #include diff --git a/gtk.c b/gtk.c index 0549f10..56b0e7f 100644 --- a/gtk.c +++ b/gtk.c @@ -30,6 +30,7 @@ #include #include "puzzles.h" +#include "gtk.h" #if GTK_CHECK_VERSION(2,0,0) # define USE_PANGO @@ -2391,8 +2392,8 @@ static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) midend_redraw(fe->me); } -GdkAtom compound_text_atom, utf8_string_atom; -bool paste_initialised = false; +static GdkAtom compound_text_atom, utf8_string_atom; +static bool paste_initialised = false; static void set_selection(frontend *fe, GdkAtom selection) { @@ -3076,9 +3077,6 @@ static void menu_about_event(GtkMenuItem *menuitem, gpointer data) "version", ver, \ "comments", "Part of Simon Tatham's Portable Puzzle Collection" - extern char *const *const xpm_icons[]; - extern const int n_xpm_icons; - if (n_xpm_icons) { GdkPixbuf *icon = gdk_pixbuf_new_from_xpm_data ((const gchar **)xpm_icons[0]); @@ -3193,8 +3191,6 @@ static frontend *new_window( GList *iconlist; int x, y, n; char errbuf[1024]; - extern char *const *const xpm_icons[]; - extern const int n_xpm_icons; struct preset_menu *preset_menu; fe = snew(frontend); diff --git a/gtk.h b/gtk.h new file mode 100644 index 0000000..a039f0d --- /dev/null +++ b/gtk.h @@ -0,0 +1,7 @@ +#ifndef PUZZLES_GTK_H +#define PUZZLES_GTK_H + +extern const char *const *const xpm_icons[]; +extern const int n_xpm_icons; + +#endif /* PUZZLES_GTK_H */ diff --git a/icons/cicon.pl b/icons/cicon.pl index a9f214e..4a5327a 100755 --- a/icons/cicon.pl +++ b/icons/cicon.pl @@ -21,6 +21,7 @@ foreach $f (@ARGV) { } # Now output. +print "#include \"gtk.h\"\n"; # for declarations of xpm_icons and n_xpm_icons foreach $line (@xpms) { print $line; } print "const char *const *const xpm_icons[] = {\n"; for ($i = 0; $i < $k; $i++) { print " xpm_icon_$i,\n"; } diff --git a/latin.c b/latin.c index 2b4848f..454e55c 100644 --- a/latin.c +++ b/latin.c @@ -1321,7 +1321,7 @@ bool latin_check(digit *sq, int order) #include #include -const char *quis; +static const char *quis; static void latin_print(digit *sq, int order) { diff --git a/lightup.c b/lightup.c index 3db47f7..181c502 100644 --- a/lightup.c +++ b/lightup.c @@ -59,7 +59,7 @@ */ #if defined STANDALONE_SOLVER #define SOLVER_DIAGNOSTICS -int verbose = 0; +static int verbose = 0; #undef debug #define debug(x) printf x #elif defined SOLVER_DIAGNOSTICS diff --git a/list.c b/list.c index 7e3318c..28cefca 100644 --- a/list.c +++ b/list.c @@ -8,10 +8,6 @@ #include "puzzles.h" -#define GAME(x) extern const game x; -#include "generated-games.h" -#undef GAME - #define GAME(x) &x, const game *gamelist[] = { #include "generated-games.h" diff --git a/magnets.c b/magnets.c index 1f8ec2c..7ac97c1 100644 --- a/magnets.c +++ b/magnets.c @@ -42,7 +42,7 @@ #include "puzzles.h" #ifdef STANDALONE_SOLVER -bool verbose = 0; +static bool verbose = false; #endif enum { @@ -2489,8 +2489,8 @@ const struct game thegame = { #include #include -const char *quis = NULL; -bool csv = false; +static const char *quis = NULL; +static bool csv = false; static void usage(FILE *out) { fprintf(out, "usage: %s [-v] [--print] |\n", quis); diff --git a/map.c b/map.c index 0d90f31..0478344 100644 --- a/map.c +++ b/map.c @@ -26,7 +26,7 @@ */ #if defined STANDALONE_SOLVER #define SOLVER_DIAGNOSTICS -bool verbose = false; +static bool verbose = false; #elif defined SOLVER_DIAGNOSTICS #define verbose true #endif diff --git a/no-icon.c b/no-icon.c index 114b2c5..5091dca 100644 --- a/no-icon.c +++ b/no-icon.c @@ -4,5 +4,7 @@ * `icons' subdirectory, when they're absent. */ +#include "gtk.h" + const char *const *const xpm_icons[] = { 0 }; const int n_xpm_icons = 0; diff --git a/pattern.c b/pattern.c index 1342cb6..cdd8fe6 100644 --- a/pattern.c +++ b/pattern.c @@ -363,7 +363,7 @@ static int compute_rowdata(int *ret, unsigned char *start, int len, int step) #define STILL_UNKNOWN 3 #ifdef STANDALONE_SOLVER -bool verbose = false; +static bool verbose = false; #endif static bool do_recurse(unsigned char *known, unsigned char *deduced, @@ -724,7 +724,7 @@ static unsigned char *generate_soluble(random_state *rs, int w, int h) #endif #ifdef STANDALONE_PICTURE_GENERATOR -unsigned char *picture; +static unsigned char *picture; #endif static char *new_game_desc(const game_params *params, random_state *rs, diff --git a/pearl.c b/pearl.c index 29dd9cd..ac9274a 100644 --- a/pearl.c +++ b/pearl.c @@ -2753,7 +2753,7 @@ const struct game thegame = { #include #include -const char *quis = NULL; +static const char *quis = NULL; static void usage(FILE *out) { fprintf(out, "usage: %s \n", quis); diff --git a/penrose.c b/penrose.c index b056ae5..1318109 100644 --- a/penrose.c +++ b/penrose.c @@ -509,8 +509,8 @@ void penrose_calculate_size(int which, int tilesize, int w, int h, #include #include -int show_recursion = 0; -int ntiles, nfinal; +static int show_recursion = 0; +static int ntiles, nfinal; static int test_cb(penrose_state *state, vector *vs, int n, int depth) { diff --git a/puzzles.h b/puzzles.h index 2f49613..ae48b2e 100644 --- a/puzzles.h +++ b/puzzles.h @@ -748,6 +748,10 @@ struct drawing_api { #ifdef COMBINED extern const game *gamelist[]; extern const int gamecount; +/* Also pre-declare every individual 'struct game' we expect */ +#define GAME(x) extern const game x; +#include "generated-games.h" +#undef GAME #else extern const game thegame; #endif diff --git a/signpost.c b/signpost.c index c74d9d4..cdc958e 100644 --- a/signpost.c +++ b/signpost.c @@ -2318,8 +2318,7 @@ const struct game thegame = { #include #include -const char *quis = NULL; -int verbose = 0; +static const char *quis = NULL; static void usage(FILE *out) { fprintf(out, "usage: %s [--stdin] [--soak] [--seed SEED] |\n", quis); diff --git a/singles.c b/singles.c index 51fb23a..fa39a61 100644 --- a/singles.c +++ b/singles.c @@ -64,7 +64,7 @@ #include "latin.h" #ifdef STANDALONE_SOLVER -bool verbose = false; +static bool verbose = false; #endif #define PREFERRED_TILE_SIZE 32 diff --git a/slant.c b/slant.c index fef73e0..8d0fddc 100644 --- a/slant.c +++ b/slant.c @@ -51,7 +51,7 @@ enum { */ #if defined STANDALONE_SOLVER #define SOLVER_DIAGNOSTICS -bool verbose = false; +static bool verbose = false; #elif defined SOLVER_DIAGNOSTICS #define verbose true #endif diff --git a/solo.c b/solo.c index 732ca2e..3155abb 100644 --- a/solo.c +++ b/solo.c @@ -91,7 +91,7 @@ #ifdef STANDALONE_SOLVER #include -int solver_show_working, solver_recurse_depth; +static int solver_show_working, solver_recurse_depth; #endif #include "puzzles.h" diff --git a/tents.c b/tents.c index f08a0e3..883b608 100644 --- a/tents.c +++ b/tents.c @@ -230,7 +230,7 @@ */ #if defined STANDALONE_SOLVER #define SOLVER_DIAGNOSTICS -bool verbose = false; +static bool verbose = false; #elif defined SOLVER_DIAGNOSTICS #define verbose true #endif diff --git a/tree234.c b/tree234.c index db33ed4..1d33be8 100644 --- a/tree234.c +++ b/tree234.c @@ -1501,12 +1501,12 @@ static void error(const char *fmt, ...) { } /* The array representation of the data. */ -void **array; -int arraylen, arraysize; -cmpfn234 cmp; +static void **array; +static int arraylen, arraysize; +static cmpfn234 cmp; /* The tree representation of the same data. */ -tree234 *tree; +static tree234 *tree; /* * Routines to provide a diagnostic printout of a tree. Currently diff --git a/unequal.c b/unequal.c index e0e08a1..b39683a 100644 --- a/unequal.c +++ b/unequal.c @@ -1075,7 +1075,7 @@ static int gg_best_clue(game_state *state, int *scratch, digit *latin) } #ifdef STANDALONE_SOLVER -int maxtries; +static int maxtries; #define MAXTRIES maxtries #else #define MAXTRIES 50 @@ -2191,7 +2191,7 @@ const struct game thegame = { #include #include -const char *quis = NULL; +static const char *quis = NULL; #if 0 /* currently unused */ diff --git a/unruly.c b/unruly.c index 63d25e2..e428e17 100644 --- a/unruly.c +++ b/unruly.c @@ -52,7 +52,7 @@ #include "puzzles.h" #ifdef STANDALONE_SOLVER -bool solver_verbose = false; +static bool solver_verbose = false; #endif enum { @@ -2067,7 +2067,7 @@ const struct game thegame = { /* Most of the standalone solver code was copied from unequal.c and singles.c */ -const char *quis; +static const char *quis; static void usage_exit(const char *msg) { diff --git a/version.c b/version.c index 1cef29f..73632f9 100644 --- a/version.c +++ b/version.c @@ -2,6 +2,7 @@ * Puzzles version numbering. */ +#include "puzzles.h" #include "version.h" char ver[] = VER;