]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/objects_build_with_stringchecks.c
ccanlint: objects_build_with_stringchecks
[ccan] / tools / ccanlint / tests / objects_build_with_stringchecks.c
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>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <limits.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <err.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include "reduce_features.h"
19
20 static const char *uses_stringfuncs(struct manifest *m)
21 {
22         struct list_head *list;
23
24         foreach_ptr(list, &m->c_files, &m->h_files) {
25                 struct ccan_file *i;
26                 char *match;
27
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)) {
34                                 if (verbose > 2)
35                                         printf("Matched '%s' in %s\n",
36                                                match, i->fullname);
37                                 return NULL;
38                         }
39                 }
40         }
41         return "No ctype.h or string functions found";
42 }
43
44 static void write_str(int fd, const char *str)
45 {
46         if (write(fd, str, strlen(str)) != strlen(str))
47                 err(1, "Writing to temporary file");
48 }
49
50 static int start_file(const char *filename)
51 {
52         int fd;
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");
55         return fd;
56 }
57
58 static void test_compile(struct score *score,
59                          struct ccan_file *file,
60                          const char *filename,
61                          bool keep,
62                          const char *flags,
63                          bool *errors,
64                          bool *warnings)
65 {
66         char *output, *compiled;
67
68         compiled = maybe_temp_file(score, "", keep, filename);
69         if (!compile_object(score, filename, ccan_dir, compiler, flags,
70                             compiled, &output)) {
71                 score_file_error(score, file, 0,
72                                  "Compiling object files:\n%s",
73                                  output);
74                 *errors = true;
75         } else if (!streq(output, "")) {
76                 score_file_error(score, file, 0,
77                                  "Compiling object files gave warnings:\n%s",
78                                  output);
79                 *warnings = true;
80         }
81 }
82
83 static struct ccan_file *get_main_header(struct manifest *m)
84 {
85         struct ccan_file *f;
86
87         list_for_each(&m->h_files, f, list) {
88                 if (strstarts(f->name, m->basename)
89                     && strlen(f->name) == strlen(m->basename) + 2) {
90                         return f;
91                 }
92         }
93         /* Should not happen, since we passed main_header_exists! */
94         errx(1, "No main header?");
95 }
96
97 static void build_objects_with_stringchecks(struct manifest *m,
98                                             bool keep, unsigned int *timeleft,
99                                             struct score *score)
100 {
101         struct ccan_file *i;
102         bool errors = false, warnings = false;
103         char *tmp, *flags;
104         int tmpfd;
105
106         /* FIXME:: We need -I so local #includes work outside normal dir. */
107         flags = talloc_asprintf(score, "-I%s %s", m->dir, cflags);
108
109         /* Won't work into macros, but will get inline functions. */
110         if (list_empty(&m->c_files)) {
111                 char *line;
112                 i = get_main_header(m);
113                 tmp = maybe_temp_file(score, ".c", keep, i->fullname);
114                 tmpfd = start_file(tmp);
115                 line = talloc_asprintf(score, "#include <ccan/%s/%s.h>\n",
116                                        m->basename, m->basename);
117                 write_str(tmpfd, line);
118                 close(tmpfd);
119                 test_compile(score, i, tmp, keep, flags, &errors, &warnings);
120         } else {
121                 list_for_each(&m->c_files, i, list) {
122                         tmp = maybe_temp_file(score, ".c", keep, i->fullname);
123                         tmpfd = start_file(tmp);
124                         write_str(tmpfd, get_ccan_file_contents(i));
125                         close(tmpfd);
126                         test_compile(score, i, tmp, keep, flags,
127                                      &errors, &warnings);
128                 }
129         }
130
131         score->total = 1;
132         if (!errors) {
133                 score->pass = true;
134                 score->score = !warnings;
135         }
136 }
137
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"
144 };
145 REGISTER_TEST(objects_build_with_stringchecks);