From caf366998b97b7cc29bc1f33c285feb2f5d33bff Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 22 Mar 2011 14:07:45 +1030 Subject: [PATCH] tools: fix up warnings with -Wwrite-strings. Be a little more careful with const. --- tools/ccanlint/ccanlint.c | 16 +++++++++++----- tools/ccanlint/ccanlint.h | 2 +- tools/ccanlint/tests/examples_compile.c | 25 +++++++++++++++---------- tools/ccanlint/tests/examples_exist.c | 4 +++- tools/ccanlint/tests/examples_run.c | 7 ++++--- tools/configurator/configurator.c | 9 +++++---- tools/depends.c | 2 +- tools/tools.c | 6 +++--- tools/tools.h | 2 +- 9 files changed, 44 insertions(+), 29 deletions(-) diff --git a/tools/ccanlint/ccanlint.c b/tools/ccanlint/ccanlint.c index 5180368b..291a1518 100644 --- a/tools/ccanlint/ccanlint.c +++ b/tools/ccanlint/ccanlint.c @@ -32,6 +32,7 @@ #include #include #include +#include int verbose = 0; static LIST_HEAD(compulsory_tests); @@ -43,8 +44,8 @@ static struct btree *info_exclude; static unsigned int timeout; /* These are overridden at runtime if we can find config.h */ -char *compiler = NULL; -char *cflags = NULL; +const char *compiler = NULL; +const char *cflags = NULL; const char *config_header; @@ -301,7 +302,7 @@ static void init_tests(void) } } -static int show_tmpdir(char *dir) +static int show_tmpdir(const char *dir) { printf("You can find ccanlint working files in '%s'\n", dir); return 0; @@ -576,6 +577,11 @@ static void read_config_header(void) compiler = CCAN_CFLAGS; } +static char *opt_set_const_charp(const char *arg, const char **p) +{ + return opt_set_charp(arg, cast_const2(char **, p)); +} + int main(int argc, char *argv[]) { bool summary = false; @@ -613,9 +619,9 @@ int main(int argc, char *argv[]) opt_register_arg("--target ", opt_set_charp, NULL, &target, "only run one test (and its prerequisites)"); - opt_register_arg("--compiler ", opt_set_charp, + opt_register_arg("--compiler ", opt_set_const_charp, NULL, &compiler, "set the compiler"); - opt_register_arg("--cflags ", opt_set_charp, + opt_register_arg("--cflags ", opt_set_const_charp, NULL, &cflags, "set the compiler flags"); opt_register_noarg("-?|-h|--help", opt_usage_and_exit, "\nA program for checking and guiding development" diff --git a/tools/ccanlint/ccanlint.h b/tools/ccanlint/ccanlint.h index 0c73716a..33db1257 100644 --- a/tools/ccanlint/ccanlint.h +++ b/tools/ccanlint/ccanlint.h @@ -222,7 +222,7 @@ extern bool safe_mode; extern const char *ccan_dir; /* Compiler and CFLAGS, from config.h if available. */ -extern char *compiler, *cflags; +extern const char *compiler, *cflags; /* Contents of config.h (or NULL if not found) */ extern const char *config_header; diff --git a/tools/ccanlint/tests/examples_compile.c b/tools/ccanlint/tests/examples_compile.c index 7d28aad6..4b6f4d62 100644 --- a/tools/ccanlint/tests/examples_compile.c +++ b/tools/ccanlint/tests/examples_compile.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -193,35 +194,38 @@ static bool looks_internal(char **lines, char **why) /* The winners. */ if (strstarts(line, "if") && len == 2) { - *why = "starts with if"; + *why = cast_const(char *, "starts with if"); return true; } if (strstarts(line, "for") && len == 3) { - *why = "starts with for"; + *why = cast_const(char *, "starts with for"); return true; } if (strstarts(line, "while") && len == 5) { - *why = "starts with while"; + *why = cast_const(char *, "starts with while"); return true; } if (strstarts(line, "do") && len == 2) { - *why = "starts with do"; + *why = cast_const(char *, "starts with do"); return true; } /* The losers. */ if (strstarts(line, "#include")) { - *why = "starts with #include"; + *why = cast_const(char *, "starts with #include"); return false; } if (last_ended && strchr(line, '(')) { if (strstarts(line, "static")) { - *why = "starts with static and contains ("; + *why = cast_const(char *, + "starts with static" + " and contains ("); return false; } if (strends(line, ")")) { - *why = "contains ( and ends with )"; + *why = cast_const(char *, + "contains ( and ends with )"); return false; } } @@ -229,7 +233,8 @@ static bool looks_internal(char **lines, char **why) /* Single identifier then operator == inside function. */ if (last_ended && len && cispunct(line[len+strspn(line+len, " ")])) { - *why = "starts with identifier then punctuation"; + *why = cast_const(char *, "starts with identifier" + " then punctuation"); return true; } @@ -239,7 +244,7 @@ static bool looks_internal(char **lines, char **why) } /* No idea... Say yes? */ - *why = "gave no clues"; + *why = cast_const(char *, "gave no clues"); return true; } @@ -521,7 +526,7 @@ static void build_examples(struct manifest *m, bool keep, bool res[3]; unsigned num, j; char **lines[3]; - char *error; + const char *error; score->total++; diff --git a/tools/ccanlint/tests/examples_exist.c b/tools/ccanlint/tests/examples_exist.c index 2091f67f..085f673a 100644 --- a/tools/ccanlint/tests/examples_exist.c +++ b/tools/ccanlint/tests/examples_exist.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -48,7 +49,8 @@ static char *add_example(struct manifest *m, struct ccan_file *source, != strlen(example->lines[i]) || write(fd, "\n", 1) != 1) { close(fd); - return "Failure writing to temporary file"; + return cast_const(char *, + "Failure writing to temporary file"); } } close(fd); diff --git a/tools/ccanlint/tests/examples_run.c b/tools/ccanlint/tests/examples_run.c index 23a3844a..5c06c263 100644 --- a/tools/ccanlint/tests/examples_run.c +++ b/tools/ccanlint/tests/examples_run.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -116,7 +117,7 @@ static char *find_expect(struct ccan_file *file, foreach_ptr(fmt, "outputs '%s'", "outputs \"%s\"") { if (scan_for(file, p, fmt, &expect)) { - *input = ""; + *input = cast_const(char *, ""); *exact = true; return expect; } @@ -173,7 +174,7 @@ static char *find_expect(struct ccan_file *file, "outputs \"%s\"", "outputs %s") { if (scan_for(file, p, fmt, &expect)) { - *input = ""; + *input = cast_const(char *, ""); *exact = true; return expect; } @@ -184,7 +185,7 @@ static char *find_expect(struct ccan_file *file, "output contains \"%s\"", "output contains %s") { if (scan_for(file, p, fmt, &expect)) { - *input = ""; + *input = cast_const(char *, ""); *exact = false; return expect; } diff --git a/tools/configurator/configurator.c b/tools/configurator/configurator.c index 56880fb1..e697d775 100644 --- a/tools/configurator/configurator.c +++ b/tools/configurator/configurator.c @@ -92,7 +92,7 @@ static struct test tests[] = { { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL, "#include \n" }, { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL, - "char **foo = (char *[]) { \"x\", \"y\", \"z\" };\n" + "int *foo = (int[]) { 1, 2, 3, 4 };\n" "return foo[0] ? 0 : 1;" }, { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL, "for (int i = 0; i < argc; i++) { return 0; };\n" @@ -208,7 +208,7 @@ static char *run(const char *cmd, int *exitstatus) return ret; } -static char *connect_args(char *argv[], const char *extra) +static char *connect_args(const char *argv[], const char *extra) { unsigned int i, len = strlen(extra) + 1; char *ret; @@ -325,11 +325,12 @@ static bool run_test(const char *cmd, struct test *test) return test->answer; } -int main(int argc, char *argv[]) +int main(int argc, const char *argv[]) { char *cmd; - char *default_args[] = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL }; unsigned int i; + const char *default_args[] + = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL }; if (argc > 1) { if (strcmp(argv[1], "--help") == 0) { diff --git a/tools/depends.c b/tools/depends.c index 82524da0..16c2a5b3 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -13,7 +13,7 @@ #include static char ** __attribute__((format(printf, 2, 3))) -lines_from_cmd(const void *ctx, char *format, ...) +lines_from_cmd(const void *ctx, const char *format, ...) { va_list ap; char *cmd, *buffer; diff --git a/tools/tools.c b/tools/tools.c index 77f77f0f..09cc3d3a 100644 --- a/tools/tools.c +++ b/tools/tools.c @@ -17,7 +17,7 @@ #include #include "tools.h" -static char *tmpdir = NULL; +static const char *tmpdir = NULL; bool tools_verbose = false; /* Ten minutes. */ @@ -176,7 +176,7 @@ bool run_command(const void *ctx, unsigned int *time_ms, char **output, return false; } -static int unlink_all(char *dir) +static int unlink_all(const char *dir) { char cmd[strlen(dir) + sizeof("rm -rf ")]; sprintf(cmd, "rm -rf %s", dir); @@ -187,7 +187,7 @@ static int unlink_all(char *dir) return 0; } -char *temp_dir(const void *ctx) +const char *temp_dir(const void *ctx) { /* For first call, create dir. */ while (!tmpdir) { diff --git a/tools/tools.h b/tools/tools.h index 0a6ef8cc..f08eb1dd 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -43,7 +43,7 @@ bool PRINTF_FMT(4,5) run_command(const void *ctx, const char *fmt, ...); char *run_with_timeout(const void *ctx, const char *cmd, bool *ok, unsigned *timeout_ms); -char *temp_dir(const void *ctx); +const char *temp_dir(const void *ctx); bool move_file(const char *oldname, const char *newname); /* From compile.c. -- 2.39.2