From f800f8bae35e26475956c715efcad24f64b969d9 Mon Sep 17 00:00:00 2001 From: Dan D Date: Mon, 2 Sep 2024 09:54:17 -0400 Subject: [PATCH] [SV 65777] Add more const Add more const to static arrays. On some systems this allows more data to be placed in RO segments and shared between multiple instances of the make executable. Anyway more const is good hygiene. * src/default.c: Add const to arrays. * src/function.c: Ditto. * src/hash.c: Ditto. * src/hash.h: Ditto. * src/job.c: Ditto. * src/read.c: Ditto. * src/remake.c: Ditto. * src/rule.c: Ditto. * src/rule.h: Ditto. --- src/default.c | 18 +++++++++--------- src/function.c | 2 +- src/hash.c | 6 +++--- src/hash.h | 4 ++-- src/job.c | 46 +++++++++++++++++++++++----------------------- src/read.c | 17 +++++++++-------- src/remake.c | 4 ++-- src/rule.c | 2 +- src/rule.h | 2 +- 9 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/default.c b/src/default.c index 89939c15..c731817e 100644 --- a/src/default.c +++ b/src/default.c @@ -36,7 +36,7 @@ this program. If not, see . */ '.s' must come last, so that a '.o' file will be made from a '.c' or '.p' or ... file rather than from a .s file. */ -static char default_suffixes[] +static const char default_suffixes[] #if MK_OS_VMS /* VMS should include all UNIX/POSIX + some VMS extensions */ = ".out .exe .a .olb .hlb .tlb .mlb .ln .o .obj .c .cxx .cc .cpp .pas .p \ @@ -53,7 +53,7 @@ static char default_suffixes[] .w .ch .web .sh .elc .el"; #endif -static struct pspec default_pattern_rules[] = +static const struct pspec default_pattern_rules[] = { #if MK_OS_VMS { "(%)", "%", @@ -93,7 +93,7 @@ static struct pspec default_pattern_rules[] = { 0, 0, 0 } }; -static struct pspec default_terminal_rules[] = +static const struct pspec default_terminal_rules[] = { #if MK_OS_VMS @@ -128,7 +128,7 @@ static struct pspec default_terminal_rules[] = { 0, 0, 0 } }; -static const char *default_suffix_rules[] = +static const char *const default_suffix_rules[] = { #if MK_OS_VMS ".o", @@ -398,7 +398,7 @@ static const char *default_suffix_rules[] = 0, 0, }; -static const char *default_variables[] = +static const char *const default_variables[] = { #if MK_OS_VMS #ifdef __ALPHA @@ -707,7 +707,7 @@ set_default_suffixes (void) void install_default_suffix_rules () { - const char **s; + const char *const *s; if (no_builtin_rules_flag) return; @@ -734,7 +734,7 @@ install_default_suffix_rules () void install_default_implicit_rules (void) { - struct pspec *p; + const struct pspec *p; if (no_builtin_rules_flag) return; @@ -749,7 +749,7 @@ install_default_implicit_rules (void) void define_default_variables (void) { - const char **s; + const char *const *s; if (no_builtin_variables_flag) return; @@ -761,7 +761,7 @@ define_default_variables (void) void undefine_default_variables (void) { - const char **s; + const char *const *s; for (s = default_variables; *s != 0; s += 2) undefine_variable_global (NILF, s[0], strlen (s[0]), o_default); diff --git a/src/function.c b/src/function.c index 82a9fe26..b88ea184 100644 --- a/src/function.c +++ b/src/function.c @@ -2392,7 +2392,7 @@ static char *func_call (char *o, char **argv, const char *funcname); #define FT_ENTRY(_name, _min, _max, _exp, _func) \ { { (_func) }, STRING_SIZE_TUPLE(_name), (_min), (_max), (_exp), 0, 0 } -static struct function_table_entry function_table_init[] = +static const struct function_table_entry function_table_init[] = { /* Name MIN MAX EXP? Function */ FT_ENTRY ("abspath", 0, 1, 1, func_abspath), diff --git a/src/hash.c b/src/hash.c index d1652f84..41e16895 100644 --- a/src/hash.c +++ b/src/hash.c @@ -33,7 +33,7 @@ static unsigned long round_up_2 __P((unsigned long rough)); potentially hit every slot in the table during collision resolution. */ -void *hash_deleted_item = &hash_deleted_item; +const void *hash_deleted_item = &hash_deleted_item; /* Force the table size to be a power of two, possibly rounding up the given size. */ @@ -65,10 +65,10 @@ hash_init (struct hash_table *ht, unsigned long size, /* Load an array of items into 'ht'. */ void -hash_load (struct hash_table *ht, void *item_table, +hash_load (struct hash_table *ht, const void *item_table, unsigned long cardinality, unsigned long size) { - char *items = (char *) item_table; + const char *items = (const char *) item_table; while (cardinality--) { hash_insert (ht, items); diff --git a/src/hash.h b/src/hash.h index 56f9a90b..1bbda0f7 100644 --- a/src/hash.h +++ b/src/hash.h @@ -57,7 +57,7 @@ typedef int (*qsort_cmp_t) __P((void const *, void const *)); void hash_init __P((struct hash_table *ht, unsigned long size, hash_func_t hash_1, hash_func_t hash_2, hash_cmp_func_t hash_cmp)); -void hash_load __P((struct hash_table *ht, void *item_table, +void hash_load __P((struct hash_table *ht, const void *item_table, unsigned long cardinality, unsigned long size)); void **hash_find_slot __P((struct hash_table *ht, void const *key)); void *hash_find_item __P((struct hash_table *ht, void const *key)); @@ -76,7 +76,7 @@ void **hash_dump __P((struct hash_table *ht, void **vector_0, qsort_cmp_t compar extern unsigned jhash(unsigned char const *key, int n); extern unsigned jhash_string(unsigned char const *key); -extern void *hash_deleted_item; +extern const void *hash_deleted_item; #define HASH_VACANT(item) ((item) == 0 || (void *) (item) == hash_deleted_item) diff --git a/src/job.c b/src/job.c index db741fc0..27fc7640 100644 --- a/src/job.c +++ b/src/job.c @@ -2727,50 +2727,50 @@ construct_command_argv_internal (char *line, char **restp, const char *shell, DOS_CHARS also include characters special to 4DOS/NDOS, so we won't have to tell one from another and have one more set of commands and special characters. */ - static const char *sh_chars_dos = "*?[];|<>%^&()"; - static const char *sh_cmds_dos[] = + static const char *const sh_chars_dos = "*?[];|<>%^&()"; + static const char *const sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date", "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md", "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren", "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":", 0 }; - static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^"; - static const char *sh_cmds_sh[] = + static const char *const sh_chars_sh = "#;\"*?[]&|<>(){}$`^"; + static const char *const sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login", "logout", "set", "umask", "wait", "while", "for", "case", "if", ":", ".", "break", "continue", "export", "read", "readonly", "shift", "times", "trap", "switch", "unset", "ulimit", "command", 0 }; const char *sh_chars; - const char **sh_cmds; + const char *const *sh_cmds; #elif MK_OS_OS2 - static const char *sh_chars_dos = "*?[];|<>%^&()"; - static const char *sh_cmds_dos[] = + static const char *const sh_chars_dos = "*?[];|<>%^&()"; + static const char *const sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date", "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md", "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren", "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":", 0 }; - static const char *sh_chars_os2 = "*?[];|<>%^()\"'&"; - static const char *sh_cmds_os2[] = + static const char *const sh_chars_os2 = "*?[];|<>%^()\"'&"; + static const char *const sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy", "date", "del", "detach", "dir", "echo", "endlocal", "erase", "exit", "for", "goto", "if", "keys", "md", "mkdir", "move", "path", "pause", "prompt", "rd", "rem", "ren", "rename", "rmdir", "set", "setlocal", "shift", "start", "time", "type", "ver", "verify", "vol", ":", 0 }; - static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~'"; - static const char *sh_cmds_sh[] = + static const char *const sh_chars_sh = "#;\"*?[]&|<>(){}$`^~'"; + static const char *const sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login", "logout", "set", "umask", "wait", "while", "for", "case", "if", ":", ".", "break", "continue", "export", "read", "readonly", "shift", "times", "trap", "switch", "unset", "command", 0 }; const char *sh_chars; - const char **sh_cmds; + const char *const *sh_cmds; #elif MK_OS_W32 /* We used to have a double quote (") in sh_chars_dos[] below, but @@ -2780,17 +2780,17 @@ construct_command_argv_internal (char *line, char **restp, const char *shell, can handle quoted file names just fine, removing the quote lifts the limit from a very frequent use case, because using quoted file names is commonplace on MS-Windows. */ - static const char *sh_chars_dos = "|&<>"; - static const char *sh_cmds_dos[] = + static const char *const sh_chars_dos = "|&<>"; + static const char *const sh_cmds_dos[] = { "assoc", "break", "call", "cd", "chcp", "chdir", "cls", "color", "copy", "ctty", "date", "del", "dir", "echo", "echo.", "endlocal", "erase", - "exit", "for", "ftype", "goto", "if", "if", "md", "mkdir", "move", + "exit", "for", "ftype", "goto", "if", "md", "mkdir", "move", "path", "pause", "prompt", "rd", "rem", "ren", "rename", "rmdir", "set", "setlocal", "shift", "time", "title", "type", "ver", "verify", "vol", ":", 0 }; - static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^"; - static const char *sh_cmds_sh[] = + static const char *const sh_chars_sh = "#;\"*?[]&|<>(){}$`^"; + static const char *const sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login", "logout", "set", "umask", "wait", "while", "for", "case", "if", ":", ".", "break", "continue", "export", "read", "readonly", "shift", "times", "trap", "switch", "test", "command", @@ -2800,13 +2800,13 @@ construct_command_argv_internal (char *line, char **restp, const char *shell, 0 }; const char *sh_chars; - const char **sh_cmds; + const char *const *sh_cmds; #elif defined(__riscos__) - static const char *sh_chars = ""; - static const char *sh_cmds[] = { 0 }; + static const char *const sh_chars = ""; + static const char *const sh_cmds[] = { 0 }; #else /* must be UNIX-ish */ - static const char *sh_chars = "#;\"*?[]&|<>(){}$`^~!"; - static const char *sh_cmds[] = + static const char *const sh_chars = "#;\"*?[]&|<>(){}$`^~!"; + static const char *const sh_cmds[] = { ".", ":", "alias", "bg", "break", "case", "cd", "command", "continue", "eval", "exec", "exit", "export", "fc", "fg", "for", "getopts", "hash", "if", "jobs", "login", "logout", "read", "readonly", "return", "set", @@ -2818,7 +2818,7 @@ construct_command_argv_internal (char *line, char **restp, const char *shell, MK_OS_W32) are compiled with HAVE_DOS_PATHS defined, which uses sh_chars_sh directly (see below). The value must be identical to that of sh_chars immediately above. */ - static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!"; + static const char *const sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!"; # endif /* HAVE_DOS_PATHS */ #endif size_t i; diff --git a/src/read.c b/src/read.c index 8a487a94..f9b0dd75 100644 --- a/src/read.c +++ b/src/read.c @@ -96,7 +96,7 @@ static struct conditionals *conditionals = &toplevel_conditionals; /* Default directories to search for include files in */ -static const char *default_include_directories[] = +static const char *const default_include_directories[] = { #if MK_OS_W32 && !defined(INCLUDEDIR) /* This completely up to the user when they install MSVC or other packages. @@ -222,7 +222,7 @@ read_all_makefiles (const char **makefiles) if (num_makefiles == 0) { - static const char *default_makefiles[] = + static const char *const default_makefiles[] = #if MK_OS_VMS /* all lower case since readdir() (the vms version) 'lowercasifies' */ /* TODO: Above is not always true, this needs more work */ @@ -234,7 +234,7 @@ read_all_makefiles (const char **makefiles) { "GNUmakefile", "makefile", "Makefile", 0 }; #endif /* !MK_OS_VMS && !MK_OS_W32 */ #endif /* MK_OS_VMS */ - const char **p = default_makefiles; + const char *const *p = default_makefiles; while (*p != 0 && !file_exists_p (*p)) ++p; @@ -2925,6 +2925,7 @@ construct_include_path (const char **arg_dirs) /* Now add the standard default dirs at the end. */ if (!disable) { + const char *const *ccpp; #if MK_OS_DOS /* The environment variable $DJDIR holds the root of the DJGPP directory tree; add ${DJDIR}/include. */ @@ -2942,20 +2943,20 @@ construct_include_path (const char **arg_dirs) max_incl_len = len; } #endif - for (cpp = default_include_directories; *cpp != 0; ++cpp) + for (ccpp = default_include_directories; *ccpp != 0; ++ccpp) { int e; - EINTRLOOP (e, stat (*cpp, &stbuf)); + EINTRLOOP (e, stat (*ccpp, &stbuf)); if (e == 0 && S_ISDIR (stbuf.st_mode)) { - size_t len = strlen (*cpp); + size_t len = strlen (*ccpp); /* If dir name is written with trailing slashes, discard them. */ - while (len > 1 && (*cpp)[len - 1] == '/') + while (len > 1 && (*ccpp)[len - 1] == '/') --len; if (len > max_incl_len) max_incl_len = len; - dirs[idx++] = strcache_add_len (*cpp, len); + dirs[idx++] = strcache_add_len (*ccpp, len); } } } diff --git a/src/remake.c b/src/remake.c index 50d8f7ec..9d7ae8fd 100644 --- a/src/remake.c +++ b/src/remake.c @@ -1751,7 +1751,7 @@ name_mtime (const char *name) static const char * library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr) { - static const char *dirs[] = + static const char *const dirs[] = { "/lib", "/usr/lib", @@ -1779,7 +1779,7 @@ library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr) /* Information about the earliest (in the vpath sequence) match. */ unsigned int best_vpath = 0, best_path = 0; - const char **dp; + const char *const *dp; libpatterns = allocated_expand_variable (STRING_SIZE_TUPLE (".LIBPATTERNS")); diff --git a/src/rule.c b/src/rule.c index 8da27fcc..d6bad472 100644 --- a/src/rule.c +++ b/src/rule.c @@ -472,7 +472,7 @@ new_pattern_rule (struct rule *rule, int override) TERMINAL specifies what the 'terminal' field of the rule should be. */ void -install_pattern_rule (struct pspec *p, int terminal) +install_pattern_rule (const struct pspec *p, int terminal) { struct rule *r; const char *ptr; diff --git a/src/rule.h b/src/rule.h index 8c29f89c..973f8749 100644 --- a/src/rule.h +++ b/src/rule.h @@ -51,7 +51,7 @@ extern struct file *suffix_file; void snap_implicit_rules (void); void convert_to_pattern (void); -void install_pattern_rule (struct pspec *p, int terminal); +void install_pattern_rule (const struct pspec *p, int terminal); void create_pattern_rule (const char **targets, const char **target_percents, unsigned short num, int terminal, struct dep *deps, struct commands *commands, int override); -- 2.47.3