1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <ccan/str_talloc/str_talloc.h>
6 #include <ccan/foreach/foreach.h>
18 #include "reduce_features.h"
20 static const char *uses_stringfuncs(struct manifest *m)
22 struct list_head *list;
24 foreach_ptr(list, &m->c_files, &m->h_files) {
28 list_for_each(list, i, list) {
29 if (strreg(m, get_ccan_file_contents(i),
30 "(isalnum|isalpha|isascii|isblank|iscntrl"
31 "|isdigit|isgraph|islower|isprint|ispunct"
32 "|isspace|isupper|isxdigit"
33 "|strstr|strchr|strrchr)", &match)) {
35 printf("Matched '%s' in %s\n",
41 return "No ctype.h or string functions found";
44 static void write_str(int fd, const char *str)
46 if (write(fd, str, strlen(str)) != strlen(str))
47 err(1, "Writing to temporary file");
50 static int start_file(const char *filename)
53 fd = open(filename, O_WRONLY|O_CREAT, 0600);
54 write_str(fd, "#define CCAN_STR_DEBUG 1\n#include <ccan/str/str.h>\n");
58 static void test_compile(struct score *score,
59 struct ccan_file *file,
65 char *output, *compiled;
67 compiled = temp_file(score, "", filename);
68 if (!compile_object(score, filename, ccan_dir, compiler, flags,
70 score_file_error(score, file, 0,
71 "Compiling object files:\n%s",
74 } else if (!streq(output, "")) {
75 score_file_error(score, file, 0,
76 "Compiling object files gave warnings:\n%s",
82 static struct ccan_file *get_main_header(struct manifest *m)
86 list_for_each(&m->h_files, f, list) {
87 if (strstarts(f->name, m->basename)
88 && strlen(f->name) == strlen(m->basename) + 2) {
92 /* Should not happen, since we passed main_header_exists! */
93 errx(1, "No main header?");
96 static void build_objects_with_stringchecks(struct manifest *m,
97 unsigned int *timeleft,
101 bool errors = false, warnings = false;
105 /* FIXME:: We need -I so local #includes work outside normal dir. */
106 flags = talloc_asprintf(score, "-I%s %s", m->dir, cflags);
108 /* Won't work into macros, but will get inline functions. */
109 if (list_empty(&m->c_files)) {
111 i = get_main_header(m);
112 tmp = temp_file(score, ".c", i->fullname);
113 tmpfd = start_file(tmp);
114 line = talloc_asprintf(score, "#include <ccan/%s/%s.h>\n",
115 m->modname, m->basename);
116 write_str(tmpfd, line);
118 test_compile(score, i, tmp, flags, &errors, &warnings);
120 list_for_each(&m->c_files, i, list) {
121 tmp = temp_file(score, ".c", i->fullname);
122 tmpfd = start_file(tmp);
123 write_str(tmpfd, get_ccan_file_contents(i));
125 test_compile(score, i, tmp, flags, &errors, &warnings);
129 /* We don't fail ccanlint for this. */
134 score->score = !warnings;
138 struct ccanlint objects_build_with_stringchecks = {
139 .key = "objects_build_with_stringchecks",
140 .name = "Module compiles with extra ctype.h and str function checks",
141 .check = build_objects_with_stringchecks,
142 .can_run = uses_stringfuncs,
143 .needs = "objects_build main_header_exists"
145 REGISTER_TEST(objects_build_with_stringchecks);