]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/headers_idempotent.c
tools/ccanlint: detect more unmentioned dependencies.
[ccan] / tools / ccanlint / tests / headers_idempotent.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <err.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 static const char explain[] 
17 = "Headers usually start with the C preprocessor lines to prevent multiple\n"
18   "inclusions.  These look like the following:\n"
19   "#ifndef CCAN_<MODNAME>_H\n"
20   "#define CCAN_<MODNAME>_H\n"
21   "...\n"
22   "#endif /* CCAN_<MODNAME>_H */\n";
23
24 static void fix_name(char *name)
25 {
26         unsigned int i;
27
28         for (i = 0; name[i]; i++) {
29                 if (cisalnum(name[i]))
30                         name[i] = toupper(name[i]);
31                 else
32                         name[i] = '_';
33         }
34 }
35
36 static void handle_idem(struct manifest *m, struct score *score)
37 {
38         struct file_error *e;
39
40         list_for_each(&score->per_file_errors, e, list) {
41                 char *name, *q, *tmpname;
42                 FILE *out;
43                 unsigned int i;
44
45                 /* Main header gets CCAN_FOO_H, others CCAN_FOO_XXX_H */
46                 if (strstarts(e->file->name, m->basename)
47                     || strlen(e->file->name) == strlen(m->basename) + 2)
48                         name = tal_fmt(score, "CCAN_%s_H", m->modname);
49                 else
50                         name = tal_fmt(score, "CCAN_%s_%s",
51                                        m->modname, e->file->name);
52                 fix_name(name);
53
54                 q = tal_fmt(score,
55                             "Should I wrap %s in #ifndef/#define %s for you?",
56                             e->file->name, name);
57                 if (!ask(q))
58                         continue;
59
60                 tmpname = temp_file(score, ".h", e->file->name);
61                 out = fopen(tmpname, "w");
62                 if (!out)
63                         err(1, "Opening %s", tmpname);
64                 if (fprintf(out, "#ifndef %s\n#define %s\n", name, name) < 0)
65                         err(1, "Writing %s", tmpname);
66
67                 for (i = 0; e->file->lines[i]; i++)
68                         if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
69                                 err(1, "Writing %s", tmpname);
70
71                 if (fprintf(out, "#endif /* %s */\n", name) < 0)
72                         err(1, "Writing %s", tmpname);
73                 
74                 if (fclose(out) != 0)
75                         err(1, "Closing %s", tmpname);
76
77                 if (!move_file(tmpname, e->file->fullname))
78                         err(1, "Moving %s to %s", tmpname, e->file->fullname);
79         }
80 }
81
82 static void check_idem(struct ccan_file *f, struct score *score)
83 {
84         struct line_info *line_info;
85         unsigned int i, first_preproc_line;
86         const char *line, *sym;
87
88         line_info = get_ccan_line_info(f);
89         if (tal_count(f->lines) < 4)
90                 /* FIXME: We assume small headers probably uninteresting. */
91                 return;
92
93         for (i = 0; f->lines[i]; i++) {
94                 if (line_info[i].type == DOC_LINE
95                     || line_info[i].type == COMMENT_LINE)
96                         continue;
97                 if (line_info[i].type == CODE_LINE) {
98                         score_file_error(score, f, i+1,
99                                          "Expect first non-comment line to be"
100                                          " #ifndef.");
101                         return;
102                 } else if (line_info[i].type == PREPROC_LINE)
103                         break;
104         }
105
106         /* No code at all?  Don't complain. */
107         if (!f->lines[i])
108                 return;
109
110         first_preproc_line = i;
111         for (i = first_preproc_line+1; f->lines[i]; i++) {
112                 if (line_info[i].type == DOC_LINE
113                     || line_info[i].type == COMMENT_LINE)
114                         continue;
115                 if (line_info[i].type == CODE_LINE) {
116                         score_file_error(score, f, i+1,
117                                          "Expect second non-comment line to be"
118                                          " #define.");
119                         return;
120                 } else if (line_info[i].type == PREPROC_LINE)
121                         break;
122         }
123
124         /* No code at all?  Weird. */
125         if (!f->lines[i])
126                 return;
127
128         /* We expect a condition around this line. */
129         if (!line_info[i].cond) {
130                 score_file_error(score, f, first_preproc_line+1,
131                                  "Expected #ifndef");
132                 return;
133         }
134
135         line = f->lines[i];
136
137         /* We expect the condition to be ! IFDEF <symbol>. */
138         if (line_info[i].cond->type != PP_COND_IFDEF
139             || !line_info[i].cond->inverse) {
140                 score_file_error(score, f, first_preproc_line+1,
141                                  "Expected #ifndef");
142                 return;
143         }
144
145         /* And this to be #define <symbol> */
146         if (!get_token(&line, "#"))
147                 abort();
148         if (!get_token(&line, "define")) {
149                 score_file_error(score, f, i+1,
150                                  "expected '#define %s'",
151                                  line_info[i].cond->symbol);
152                 return;
153         }
154         sym = get_symbol_token(f, &line);
155         if (!sym || !streq(sym, line_info[i].cond->symbol)) {
156                 score_file_error(score, f, i+1,
157                                  "expected '#define %s'",
158                                  line_info[i].cond->symbol);
159                 return;
160         }
161
162         /* Record this for use in depends_accurate */
163         f->idempotent_cond = line_info[i].cond;
164
165         /* Rest of code should all be covered by that conditional. */
166         for (i++; f->lines[i]; i++) {
167                 unsigned int val = 0;
168                 if (line_info[i].type == DOC_LINE
169                     || line_info[i].type == COMMENT_LINE)
170                         continue;
171                 if (get_ccan_line_pp(line_info[i].cond, sym, &val, NULL)
172                     != NOT_COMPILED) {
173                         score_file_error(score, f, i+1, "code outside"
174                                          " idempotent region");
175                         return;
176                 }
177         }
178 }
179
180 static void check_idempotent(struct manifest *m,
181                              unsigned int *timeleft, struct score *score)
182 {
183         struct ccan_file *f;
184
185         /* We don't fail ccanlint for this. */
186         score->pass = true;
187
188         list_for_each(&m->h_files, f, list) {
189                 check_idem(f, score);
190         }
191         if (!score->error) {
192                 score->score = score->total;
193         }
194 }
195
196 struct ccanlint headers_idempotent = {
197         .key = "headers_idempotent",
198         .name = "Module headers are #ifndef/#define wrapped",
199         .check = check_idempotent,
200         .handle = handle_idem,
201         .needs = "info_exists main_header_exists"
202 };
203
204 REGISTER_TEST(headers_idempotent);