Commit caf36699 authored by Rusty Russell's avatar Rusty Russell

tools: fix up warnings with -Wwrite-strings.

Be a little more careful with const.
parent e2fc21e5
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <ccan/opt/opt.h> #include <ccan/opt/opt.h>
#include <ccan/foreach/foreach.h> #include <ccan/foreach/foreach.h>
#include <ccan/grab_file/grab_file.h> #include <ccan/grab_file/grab_file.h>
#include <ccan/cast/cast.h>
int verbose = 0; int verbose = 0;
static LIST_HEAD(compulsory_tests); static LIST_HEAD(compulsory_tests);
...@@ -43,8 +44,8 @@ static struct btree *info_exclude; ...@@ -43,8 +44,8 @@ static struct btree *info_exclude;
static unsigned int timeout; static unsigned int timeout;
/* These are overridden at runtime if we can find config.h */ /* These are overridden at runtime if we can find config.h */
char *compiler = NULL; const char *compiler = NULL;
char *cflags = NULL; const char *cflags = NULL;
const char *config_header; const char *config_header;
...@@ -301,7 +302,7 @@ static void init_tests(void) ...@@ -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); printf("You can find ccanlint working files in '%s'\n", dir);
return 0; return 0;
...@@ -576,6 +577,11 @@ static void read_config_header(void) ...@@ -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[]) int main(int argc, char *argv[])
{ {
bool summary = false; bool summary = false;
...@@ -613,9 +619,9 @@ int main(int argc, char *argv[]) ...@@ -613,9 +619,9 @@ int main(int argc, char *argv[])
opt_register_arg("--target <testname>", opt_set_charp, opt_register_arg("--target <testname>", opt_set_charp,
NULL, &target, NULL, &target,
"only run one test (and its prerequisites)"); "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"); NULL, &cflags, "set the compiler flags");
opt_register_noarg("-?|-h|--help", opt_usage_and_exit, opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
"\nA program for checking and guiding development" "\nA program for checking and guiding development"
......
...@@ -222,7 +222,7 @@ extern bool safe_mode; ...@@ -222,7 +222,7 @@ extern bool safe_mode;
extern const char *ccan_dir; extern const char *ccan_dir;
/* Compiler and CFLAGS, from config.h if available. */ /* 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) */ /* Contents of config.h (or NULL if not found) */
extern const char *config_header; extern const char *config_header;
......
#include <tools/ccanlint/ccanlint.h> #include <tools/ccanlint/ccanlint.h>
#include <tools/tools.h> #include <tools/tools.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/cast/cast.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -193,35 +194,38 @@ static bool looks_internal(char **lines, char **why) ...@@ -193,35 +194,38 @@ static bool looks_internal(char **lines, char **why)
/* The winners. */ /* The winners. */
if (strstarts(line, "if") && len == 2) { if (strstarts(line, "if") && len == 2) {
*why = "starts with if"; *why = cast_const(char *, "starts with if");
return true; return true;
} }
if (strstarts(line, "for") && len == 3) { if (strstarts(line, "for") && len == 3) {
*why = "starts with for"; *why = cast_const(char *, "starts with for");
return true; return true;
} }
if (strstarts(line, "while") && len == 5) { if (strstarts(line, "while") && len == 5) {
*why = "starts with while"; *why = cast_const(char *, "starts with while");
return true; return true;
} }
if (strstarts(line, "do") && len == 2) { if (strstarts(line, "do") && len == 2) {
*why = "starts with do"; *why = cast_const(char *, "starts with do");
return true; return true;
} }
/* The losers. */ /* The losers. */
if (strstarts(line, "#include")) { if (strstarts(line, "#include")) {
*why = "starts with #include"; *why = cast_const(char *, "starts with #include");
return false; return false;
} }
if (last_ended && strchr(line, '(')) { if (last_ended && strchr(line, '(')) {
if (strstarts(line, "static")) { if (strstarts(line, "static")) {
*why = "starts with static and contains ("; *why = cast_const(char *,
"starts with static"
" and contains (");
return false; return false;
} }
if (strends(line, ")")) { 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) ...@@ -229,7 +233,8 @@ static bool looks_internal(char **lines, char **why)
/* Single identifier then operator == inside function. */ /* Single identifier then operator == inside function. */
if (last_ended && len if (last_ended && len
&& cispunct(line[len+strspn(line+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) ...@@ -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, ...@@ -521,7 +526,7 @@ static void build_examples(struct manifest *m, bool keep,
bool res[3]; bool res[3];
unsigned num, j; unsigned num, j;
char **lines[3]; char **lines[3];
char *error; const char *error;
score->total++; score->total++;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <tools/tools.h> #include <tools/tools.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/cast/cast.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -48,7 +49,8 @@ static char *add_example(struct manifest *m, struct ccan_file *source, ...@@ -48,7 +49,8 @@ static char *add_example(struct manifest *m, struct ccan_file *source,
!= strlen(example->lines[i]) != strlen(example->lines[i])
|| write(fd, "\n", 1) != 1) { || write(fd, "\n", 1) != 1) {
close(fd); close(fd);
return "Failure writing to temporary file"; return cast_const(char *,
"Failure writing to temporary file");
} }
} }
close(fd); close(fd);
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/foreach/foreach.h> #include <ccan/foreach/foreach.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/cast/cast.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -116,7 +117,7 @@ static char *find_expect(struct ccan_file *file, ...@@ -116,7 +117,7 @@ static char *find_expect(struct ccan_file *file,
foreach_ptr(fmt, "outputs '%s'", "outputs \"%s\"") { foreach_ptr(fmt, "outputs '%s'", "outputs \"%s\"") {
if (scan_for(file, p, fmt, &expect)) { if (scan_for(file, p, fmt, &expect)) {
*input = ""; *input = cast_const(char *, "");
*exact = true; *exact = true;
return expect; return expect;
} }
...@@ -173,7 +174,7 @@ static char *find_expect(struct ccan_file *file, ...@@ -173,7 +174,7 @@ static char *find_expect(struct ccan_file *file,
"outputs \"%s\"", "outputs \"%s\"",
"outputs %s") { "outputs %s") {
if (scan_for(file, p, fmt, &expect)) { if (scan_for(file, p, fmt, &expect)) {
*input = ""; *input = cast_const(char *, "");
*exact = true; *exact = true;
return expect; return expect;
} }
...@@ -184,7 +185,7 @@ static char *find_expect(struct ccan_file *file, ...@@ -184,7 +185,7 @@ static char *find_expect(struct ccan_file *file,
"output contains \"%s\"", "output contains \"%s\"",
"output contains %s") { "output contains %s") {
if (scan_for(file, p, fmt, &expect)) { if (scan_for(file, p, fmt, &expect)) {
*input = ""; *input = cast_const(char *, "");
*exact = false; *exact = false;
return expect; return expect;
} }
......
...@@ -92,7 +92,7 @@ static struct test tests[] = { ...@@ -92,7 +92,7 @@ static struct test tests[] = {
{ "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL, { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL,
"#include <byteswap.h>\n" }, "#include <byteswap.h>\n" },
{ "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL, { "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;" }, "return foo[0] ? 0 : 1;" },
{ "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL, { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL,
"for (int i = 0; i < argc; i++) { return 0; };\n" "for (int i = 0; i < argc; i++) { return 0; };\n"
...@@ -208,7 +208,7 @@ static char *run(const char *cmd, int *exitstatus) ...@@ -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; unsigned int i, len = strlen(extra) + 1;
char *ret; char *ret;
...@@ -325,11 +325,12 @@ static bool run_test(const char *cmd, struct test *test) ...@@ -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 (argc > 1) {
if (strcmp(argv[1], "--help") == 0) { if (strcmp(argv[1], "--help") == 0) {
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#include <errno.h> #include <errno.h>
static char ** __attribute__((format(printf, 2, 3))) 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; va_list ap;
char *cmd, *buffer; char *cmd, *buffer;
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include <assert.h> #include <assert.h>
#include "tools.h" #include "tools.h"
static char *tmpdir = NULL; static const char *tmpdir = NULL;
bool tools_verbose = false; bool tools_verbose = false;
/* Ten minutes. */ /* Ten minutes. */
...@@ -176,7 +176,7 @@ bool run_command(const void *ctx, unsigned int *time_ms, char **output, ...@@ -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 ")]; char cmd[strlen(dir) + sizeof("rm -rf ")];
sprintf(cmd, "rm -rf %s", dir); sprintf(cmd, "rm -rf %s", dir);
...@@ -187,7 +187,7 @@ static int unlink_all(char *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. */ /* For first call, create dir. */
while (!tmpdir) { while (!tmpdir) {
......
...@@ -43,7 +43,7 @@ bool PRINTF_FMT(4,5) run_command(const void *ctx, ...@@ -43,7 +43,7 @@ bool PRINTF_FMT(4,5) run_command(const void *ctx,
const char *fmt, ...); const char *fmt, ...);
char *run_with_timeout(const void *ctx, const char *cmd, char *run_with_timeout(const void *ctx, const char *cmd,
bool *ok, unsigned *timeout_ms); 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); bool move_file(const char *oldname, const char *newname);
/* From compile.c. /* From compile.c.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment