]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/idempotent.c
ccanlint: print error information even if we pass.
[ccan] / tools / ccanlint / tests / 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, j;
28
29         for (i = j = 0; name[i]; i++) {
30                 if (isalnum(name[i]) || name[i] == '_')
31                         name[j++] = toupper(name[i]);
32         }
33         name[j] = '\0';
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 = talloc_asprintf(score, "CCAN_%s_H", m->basename);
49                 else
50                         name = talloc_asprintf(score, "CCAN_%s_%s_H",
51                                                m->basename, e->file->name);
52                 fix_name(name);
53
54                 q = talloc_asprintf(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 = maybe_temp_file(score, ".h", false, 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; i < e->file->num_lines; 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 bool 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 (f->num_lines < 3)
90                 /* FIXME: We assume small headers probably uninteresting. */
91                 return true;
92
93         for (i = 0; i < f->num_lines; 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 false;
102                 } else if (line_info[i].type == PREPROC_LINE)
103                         break;
104         }
105
106         /* No code at all?  Don't complain. */
107         if (i == f->num_lines)
108                 return true;
109
110         first_preproc_line = i;
111         for (i = first_preproc_line+1; i < f->num_lines; 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 false;
120                 } else if (line_info[i].type == PREPROC_LINE)
121                         break;
122         }
123
124         /* No code at all?  Weird. */
125         if (i == f->num_lines)
126                 return true;
127
128         /* We expect a condition on this line. */
129         if (!line_info[i].cond) {
130                 score_file_error(score, f, i+1, "Expected #ifndef");
131                 return false;
132         }
133
134         line = f->lines[i];
135
136         /* We expect the condition to be ! IFDEF <symbol>. */
137         if (line_info[i].cond->type != PP_COND_IFDEF
138             || !line_info[i].cond->inverse) {
139                 score_file_error(score, f, i+1, "Expected #ifndef");
140                 return false;
141         }
142
143         /* And this to be #define <symbol> */
144         if (!get_token(&line, "#"))
145                 abort();
146         if (!get_token(&line, "define")) {
147                 char *str = talloc_asprintf(score,
148                                             "expected '#define %s'",
149                                             line_info[i].cond->symbol);
150                 score_file_error(score, f, i+1, str);
151                 return false;
152         }
153         sym = get_symbol_token(f, &line);
154         if (!sym || !streq(sym, line_info[i].cond->symbol)) {
155                 char *str = talloc_asprintf(score,
156                                             "expected '#define %s'",
157                                             line_info[i].cond->symbol);
158                 score_file_error(score, f, i+1, str);
159                 return false;
160         }
161
162         /* Rest of code should all be covered by that conditional. */
163         for (i++; i < f->num_lines; i++) {
164                 unsigned int val = 0;
165                 if (line_info[i].type == DOC_LINE
166                     || line_info[i].type == COMMENT_LINE)
167                         continue;
168                 if (get_ccan_line_pp(line_info[i].cond, sym, &val, NULL)
169                     != NOT_COMPILED) {
170                         score_file_error(score, f, i+1, "code outside"
171                                          " idempotent region");
172                         return false;
173                 }
174         }
175
176         return true;
177 }
178
179 static void check_idempotent(struct manifest *m,
180                              bool keep,
181                              unsigned int *timeleft, struct score *score)
182 {
183         struct ccan_file *f;
184
185         list_for_each(&m->h_files, f, list) {
186                 if (!check_idem(f, score)) {
187                         score->error = "Headers are not idempotent";
188                         return;
189                 }
190         }
191         score->pass = true;
192         score->score = score->total;
193 }
194
195 struct ccanlint idempotent = {
196         .key = "idempotent",
197         .name = "Module headers are #ifndef/#define wrapped",
198         .check = check_idempotent,
199         .handle = handle_idem,
200 };
201
202 REGISTER_TEST(idempotent, NULL);