* 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.
This commit is contained in:
Paul Smith 2024-01-07 11:28:23 -05:00
parent 1161779ef8
commit 1ff728bff4
4 changed files with 9 additions and 12 deletions

View file

@ -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));
}

View file

@ -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;
}

View file

@ -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)

View file

@ -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;