From: Rusty Russell Date: Tue, 19 Apr 2011 10:56:13 +0000 (+0930) Subject: ccanlint: test for C++ reserved words in headers. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=076877c266706ba432987195422a1e8653bd0c3b;hp=4db6a6709bb3426466317c04c618bcb8034bb5b5;ds=sidebyside ccanlint: test for C++ reserved words in headers. Don't check the whole source, but it's nice for headers to be C++-clean. --- diff --git a/tools/ccanlint/tests/avoids_cpp_reserved.c b/tools/ccanlint/tests/avoids_cpp_reserved.c new file mode 100644 index 00000000..a1663ad5 --- /dev/null +++ b/tools/ccanlint/tests/avoids_cpp_reserved.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char *can_build(struct manifest *m) +{ + if (safe_mode) + return "Safe mode enabled"; + return NULL; +} + +static struct ccan_file *main_header(struct manifest *m) +{ + struct ccan_file *f; + + list_for_each(&m->h_files, f, list) { + if (strstarts(f->name, m->basename) + && strlen(f->name) == strlen(m->basename) + 2) + return f; + } + /* Should not happen: we depend on main_header_compiles */ + abort(); +} + +static void check_headers_no_cpp(struct manifest *m, + bool keep, + unsigned int *timeleft, struct score *score) +{ + char *contents; + char *tmpsrc, *tmpobj, *cmdout; + int fd; + struct ccan_file *mainh = main_header(m); + + tmpsrc = maybe_temp_file(m, "-included.c", keep, mainh->fullname); + tmpobj = maybe_temp_file(m, ".o", keep, tmpsrc); + + /* We don't fail you for this. */ + score->pass = true; + fd = open(tmpsrc, O_WRONLY | O_CREAT | O_EXCL, 0600); + if (fd < 0) + err(1, "Creating temporary file %s", tmpsrc); + + contents = talloc_asprintf(tmpsrc, + "#define class #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n" + "#define private #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n" + "#define public #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n" + "#define protected #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n" + "#define template #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n" + "#define this #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n" + "#define new #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n" + "#define delete #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n" + "#define friend #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n" + "#include \n", + m->basename, m->basename); + if (write(fd, contents, strlen(contents)) != strlen(contents)) + err(1, "writing to temporary file %s", tmpsrc); + close(fd); + + if (compile_object(score, tmpsrc, ccan_dir, compiler, cflags, + tmpobj, &cmdout)) { + score->score = score->total; + } else { + score->error = talloc_asprintf(score, + "Main header file with C++ names:\n%s", + cmdout); + } +} + +struct ccanlint avoids_cpp_reserved = { + .key = "avoids_cpp_reserved", + .name = "Modules main header compiles without C++ reserved words", + .check = check_headers_no_cpp, + .can_run = can_build, + .needs = "main_header_compiles" +}; + +REGISTER_TEST(avoids_cpp_reserved);