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