]> git.ozlabs.org Git - ccan/commitdiff
tools: fix up warnings with -Wwrite-strings.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 22 Mar 2011 03:37:45 +0000 (14:07 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 22 Mar 2011 03:37:45 +0000 (14:07 +1030)
Be a little more careful with const.

tools/ccanlint/ccanlint.c
tools/ccanlint/ccanlint.h
tools/ccanlint/tests/examples_compile.c
tools/ccanlint/tests/examples_exist.c
tools/ccanlint/tests/examples_run.c
tools/configurator/configurator.c
tools/depends.c
tools/tools.c
tools/tools.h

index 5180368b3dda56c080f4eb97ed83fb5875168b13..291a151842e0ce0d1126ae8ce6250ad68dc8dc48 100644 (file)
@@ -32,6 +32,7 @@
 #include <ccan/opt/opt.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/grab_file/grab_file.h>
 #include <ccan/opt/opt.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/grab_file/grab_file.h>
+#include <ccan/cast/cast.h>
 
 int verbose = 0;
 static LIST_HEAD(compulsory_tests);
 
 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 */
 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;
 
 
 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;
 {
        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;
 }
 
                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;
 int main(int argc, char *argv[])
 {
        bool summary = false;
@@ -613,9 +619,9 @@ int main(int argc, char *argv[])
        opt_register_arg("--target <testname>", opt_set_charp,
                         NULL, &target,
                         "only run one test (and its prerequisites)");
        opt_register_arg("--target <testname>", opt_set_charp,
                         NULL, &target,
                         "only run one test (and its prerequisites)");
-       opt_register_arg("--compiler <compiler>", opt_set_charp,
+       opt_register_arg("--compiler <compiler>", opt_set_const_charp,
                         NULL, &compiler, "set the compiler");
                         NULL, &compiler, "set the compiler");
-       opt_register_arg("--cflags <flags>", opt_set_charp,
+       opt_register_arg("--cflags <flags>", 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"
                         NULL, &cflags, "set the compiler flags");
        opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
                           "\nA program for checking and guiding development"
index 0c73716a714cfeafe80b0337490802dd610d73cc..33db1257eb126bbf9f59299175af05f13c5edaf6 100644 (file)
@@ -222,7 +222,7 @@ extern bool safe_mode;
 extern const char *ccan_dir;
 
 /* Compiler and CFLAGS, from config.h if available. */
 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;
 
 /* Contents of config.h (or NULL if not found) */
 extern const char *config_header;
index 7d28aad6ca63ec965f796e3d9f3f2e33ad393082..4b6f4d624be2ead86e1a66a09d8f678bad8cb627 100644 (file)
@@ -1,6 +1,7 @@
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
+#include <ccan/cast/cast.h>
 #include <ccan/str/str.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <ccan/str/str.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -193,35 +194,38 @@ static bool looks_internal(char **lines, char **why)
 
                /* The winners. */
                if (strstarts(line, "if") && len == 2) {
 
                /* 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) {
                        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) {
                        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) {
                        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")) {
                        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")) {
                        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, ")")) {
                                return false;
                        }
                        if (strends(line, ")")) {
-                               *why = "contains ( and ends with )";
+                               *why = cast_const(char *,
+                                                 "contains ( and ends with )");
                                return false;
                        }
                }
                                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, " ")])) {
                /* 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;
                }
 
                        return true;
                }
 
@@ -239,7 +244,7 @@ static bool looks_internal(char **lines, char **why)
        }
 
        /* No idea... Say yes? */
        }
 
        /* No idea... Say yes? */
-       *why = "gave no clues";
+       *why = cast_const(char *, "gave no clues");
        return true;
 }
 
        return true;
 }
 
@@ -521,7 +526,7 @@ static void build_examples(struct manifest *m, bool keep,
                bool res[3];
                unsigned num, j;
                char **lines[3];
                bool res[3];
                unsigned num, j;
                char **lines[3];
-               char *error;
+               const char *error;
 
                score->total++;
 
 
                score->total++;
 
index 2091f67f0f97c82218b57d77a380ee3528eedc4f..085f673a7e5ed506a6abe819911ef8e21af5a444 100644 (file)
@@ -2,6 +2,7 @@
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
 #include <ccan/str/str.h>
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
 #include <ccan/str/str.h>
+#include <ccan/cast/cast.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -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);
                    != 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);
                }
        }
        close(fd);
index 23a3844a5bb7d0d153bb2f55cb638107e77b3a79..5c06c263e5228ebc0fffaf9e19abdc9ce6f7503e 100644 (file)
@@ -3,6 +3,7 @@
 #include <ccan/talloc/talloc.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/str/str.h>
 #include <ccan/talloc/talloc.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/str/str.h>
+#include <ccan/cast/cast.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -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)) {
 
                foreach_ptr(fmt, "outputs '%s'", "outputs \"%s\"") {
                        if (scan_for(file, p, fmt, &expect)) {
-                               *input = "";
+                               *input = cast_const(char *, "");
                                *exact = true;
                                return expect;
                        }
                                *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)) {
                            "outputs \"%s\"",
                            "outputs %s") {
                        if (scan_for(file, p, fmt, &expect)) {
-                               *input = "";
+                               *input = cast_const(char *, "");
                                *exact = true;
                                return expect;
                        }
                                *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)) {
                            "output contains \"%s\"",
                            "output contains %s") {
                        if (scan_for(file, p, fmt, &expect)) {
-                               *input = "";
+                               *input = cast_const(char *, "");
                                *exact = false;
                                return expect;
                        }
                                *exact = false;
                                return expect;
                        }
index 56880fb1875b066a6eddc3b99954ca65b0158cec..e697d7759877793b19f8b57517b3a88153ee2671 100644 (file)
@@ -92,7 +92,7 @@ static struct test tests[] = {
        { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL,
          "#include <byteswap.h>\n" },
        { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL,
        { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL,
          "#include <byteswap.h>\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"
          "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;
 }
 
        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;
 {
        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;
 }
 
        return test->answer;
 }
 
-int main(int argc, char *argv[])
+int main(int argc, const char *argv[])
 {
        char *cmd;
 {
        char *cmd;
-       char *default_args[] = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
        unsigned int i;
        unsigned int i;
+       const char *default_args[]
+               = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
 
        if (argc > 1) {
                if (strcmp(argv[1], "--help") == 0) {
 
        if (argc > 1) {
                if (strcmp(argv[1], "--help") == 0) {
index 82524da0a256977ac61191f8ebf2b8d689e5eace..16c2a5b3de54bbfe32f963958693fe8c851dfbce 100644 (file)
@@ -13,7 +13,7 @@
 #include <errno.h>
 
 static char ** __attribute__((format(printf, 2, 3)))
 #include <errno.h>
 
 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;
 {
        va_list ap;
        char *cmd, *buffer;
index 77f77f0fe6741b09904f64d31ddd147871f7e40b..09cc3d3a17b362b324b99d5ef20fc922d885ff36 100644 (file)
@@ -17,7 +17,7 @@
 #include <assert.h>
 #include "tools.h"
 
 #include <assert.h>
 #include "tools.h"
 
-static char *tmpdir = NULL;
+static const char *tmpdir = NULL;
 bool tools_verbose = false;
 
 /* Ten minutes. */
 bool tools_verbose = false;
 
 /* Ten minutes. */
@@ -176,7 +176,7 @@ bool run_command(const void *ctx, unsigned int *time_ms, char **output,
        return false;
 }
 
        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);
 {
        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;
 }
 
        return 0;
 }
 
-char *temp_dir(const void *ctx)
+const char *temp_dir(const void *ctx)
 {
        /* For first call, create dir. */
        while (!tmpdir) {
 {
        /* For first call, create dir. */
        while (!tmpdir) {
index 0a6ef8ccc4ac2b9a54fba7bfe9223bb48c4da068..f08eb1ddb0fad0ecc4ac644926b323cc26682eb4 100644 (file)
@@ -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);
                                 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.
 bool move_file(const char *oldname, const char *newname);
 
 /* From compile.c.