Browse Source

tools: fix up warnings with -Wwrite-strings.

Be a little more careful with const.
Rusty Russell 15 years ago
parent
commit
caf366998b

+ 11 - 5
tools/ccanlint/ccanlint.c

@@ -32,6 +32,7 @@
 #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);
@@ -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 <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");
-	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"

+ 1 - 1
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;

+ 15 - 10
tools/ccanlint/tests/examples_compile.c

@@ -1,6 +1,7 @@
 #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>
@@ -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++;
 

+ 3 - 1
tools/ccanlint/tests/examples_exist.c

@@ -2,6 +2,7 @@
 #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>
@@ -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);

+ 4 - 3
tools/ccanlint/tests/examples_run.c

@@ -3,6 +3,7 @@
 #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>
@@ -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;
 			}

+ 5 - 4
tools/configurator/configurator.c

@@ -92,7 +92,7 @@ static struct test tests[] = {
 	{ "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"
@@ -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) {

+ 1 - 1
tools/depends.c

@@ -13,7 +13,7 @@
 #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;

+ 3 - 3
tools/tools.c

@@ -17,7 +17,7 @@
 #include <assert.h>
 #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) {

+ 1 - 1
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.