From 1ff728bff4285df8924cfcba024cea58c3350f77 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sun, 7 Jan 2024 11:28:23 -0500 Subject: [PATCH] * src/makeint.h: Add an ARRAYLEN macro to compute array sizes * src/main.c: Replace inline array length computation with ARRAYLEN. * src/function.c: Ditto. * src/read.h: Ditto. --- src/function.c | 6 ++---- src/main.c | 10 +++------- src/makeint.h | 3 +++ src/read.c | 2 +- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/function.c b/src/function.c index 00686f32..5e7285a5 100644 --- a/src/function.c +++ b/src/function.c @@ -2436,8 +2436,6 @@ static struct function_table_entry function_table_init[] = FT_ENTRY ("not", 0, 1, 1, func_not), #endif }; - -#define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry)) /* These must come after the definition of function_table. */ @@ -2736,9 +2734,9 @@ define_new_function (const floc *flocp, const char *name, void hash_init_function_table (void) { - hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2, + hash_init (&function_table, ARRAYLEN (function_table_init) * 2, function_table_entry_hash_1, function_table_entry_hash_2, function_table_entry_hash_cmp); hash_load (&function_table, function_table_init, - FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry)); + ARRAYLEN (function_table_init), sizeof (struct function_table_entry)); } diff --git a/src/main.c b/src/main.c index 0e7c53a2..c9c64b54 100644 --- a/src/main.c +++ b/src/main.c @@ -2898,10 +2898,8 @@ main (int argc, char **argv, char **envp) /* Parsing of arguments, decoding of switches. */ -static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3]; -static struct option long_options[(sizeof (switches) / sizeof (switches[0])) + - (sizeof (long_option_aliases) / - sizeof (long_option_aliases[0]))]; +static char options[1 + ARRAYLEN (switches) * 3]; +static struct option long_options[ARRAYLEN (switches) + ARRAYLEN (long_option_aliases)]; /* Fill in the string and vector for getopt. */ static void @@ -2956,9 +2954,7 @@ init_switches (void) } } *p = '\0'; - for (c = 0; c < (sizeof (long_option_aliases) / - sizeof (long_option_aliases[0])); - ++c) + for (c = 0; c < ARRAYLEN (long_option_aliases); ++c) long_options[i++] = long_option_aliases[c]; long_options[i].name = 0; } diff --git a/src/makeint.h b/src/makeint.h index 6907b2b3..00289b9c 100644 --- a/src/makeint.h +++ b/src/makeint.h @@ -506,6 +506,9 @@ extern struct rlimit stack_limit; #define NILF ((floc *)0) +/* Number of elements in an array. */ +#define ARRAYLEN(_a) (sizeof (_a) / sizeof ((_a)[0])) + /* Number of characters in a string constant. Does NOT include the \0 byte. */ #define CSTRLEN(_s) (sizeof (_s)-1) diff --git a/src/read.c b/src/read.c index 33020633..e97d3061 100644 --- a/src/read.c +++ b/src/read.c @@ -2964,7 +2964,7 @@ construct_include_path (const char **arg_dirs) int disable = 0; /* Compute the number of pointers we need in the table. */ - idx = sizeof (default_include_directories) / sizeof (const char *); + idx = ARRAYLEN (default_include_directories); if (arg_dirs) for (cpp = arg_dirs; *cpp != 0; ++cpp) ++idx;