]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.h
ccanlint: allow per-file restrictions, so we don't have to remove all tests.
[ccan] / tools / ccanlint / ccanlint.h
1 #ifndef CCAN_LINT_H
2 #define CCAN_LINT_H
3 #include "config.h"
4 #include <ccan/list/list.h>
5 #include <stdbool.h>
6 #include "../doc_extract.h"
7 #include "licenses.h"
8
9 #define REGISTER_TEST(name, ...) extern struct ccanlint name
10
11 /* 0 == Describe failed tests.
12    1 == Describe results for partial failures.
13    2 == One line per test, plus details of failures.
14
15    Mainly for debugging ccanlint:
16    3 == Describe every object built.
17    4 == Describe every action. */
18 extern int verbose;
19
20 struct manifest {
21         char *dir;
22         /* The module name, ie. final element of dir name */
23         char *basename;
24         struct ccan_file *info_file;
25
26         /* Linked off deps. */
27         struct list_node list;
28         /* Where our final compiled output is */
29         char *compiled;
30
31         struct list_head c_files;
32         struct list_head h_files;
33
34         struct list_head run_tests;
35         struct list_head api_tests;
36         struct list_head compile_ok_tests;
37         struct list_head compile_fail_tests;
38         struct list_head other_test_c_files;
39         struct list_head other_test_files;
40
41         struct list_head other_files;
42         struct list_head examples;
43         struct list_head mangled_examples;
44
45         /* From tests/check_depends_exist.c */
46         struct list_head deps;
47
48         /* From tests/license_exists.c */
49         enum license license;
50 };
51
52 /* Get the manifest for a given directory. */
53 struct manifest *get_manifest(const void *ctx, const char *dir);
54
55 /* Error in a particular file: stored off score->per_file_errors. */
56 struct file_error {
57         struct list_node list;
58         struct ccan_file *file;
59         unsigned int line;
60 };
61
62 /* The score for an individual test. */
63 struct score {
64         /* Starts as false: if not set to true, ccanlint exits non-zero.
65          * Thus it is usually set for compilation or other serious failures. */
66         bool pass;
67         /* Starts at 0 and 1 respectively. */
68         unsigned int score, total;
69         /* The error message to print. */
70         char *error;
71         /* Per file errors, set by score_file_error() */
72         struct list_head per_file_errors;
73 };
74
75 struct ccanlint {
76         struct list_node list;
77
78         /* More concise unique name of test. */
79         const char *key;
80
81         /* Unique name of test */
82         const char *name;
83
84         /* Can we run this test?  Return string explaining why, if not. */
85         const char *(*can_run)(struct manifest *m);
86
87         /* keep is set if you should keep the results.
88          * If timeleft is set to 0, means it timed out.
89          * score is the result, and a talloc context freed after all our
90          * depends are done. */
91         void (*check)(struct manifest *m,
92                       bool keep, unsigned int *timeleft, struct score *score);
93
94         /* Can we do something about it? (NULL if not) */
95         void (*handle)(struct manifest *m, struct score *score);
96
97         /* Options from _info. */
98         char **options;
99         /* If not set, we'll give an error if they try to set options. */
100         bool takes_options;
101
102         /* Space-separated list of dependency keys. */
103         const char *needs;
104
105         /* Internal use fields: */
106         /* Who depends on us? */
107         struct list_head dependencies;
108         /* How many things do we (still) depend on? */
109         unsigned int num_depends;
110         /* Did we skip a dependency?  If so, must skip this, too. */
111         const char *skip;
112         /* Did we fail a dependency?  If so, skip and mark as fail. */
113         bool skip_fail;
114         /* Did the user want to keep these results? */
115         bool keep_results;
116 };
117
118 /* Ask the user a yes/no question: the answer is NO if there's an error. */
119 bool ask(const char *question);
120
121 enum line_info_type {
122         PREPROC_LINE, /* Line starts with # */
123         CODE_LINE, /* Code (ie. not pure comment). */
124         DOC_LINE, /* Line with kernel-doc-style comment. */
125         COMMENT_LINE, /* (pure) comment line */
126 };
127
128 /* So far, only do simple #ifdef/#ifndef/#if defined/#if !defined tests,
129  * and #if <SYMBOL>/#if !<SYMBOL> */
130 struct pp_conditions {
131         /* We're inside another ifdef? */
132         struct pp_conditions *parent;
133
134         enum {
135                 PP_COND_IF,
136                 PP_COND_IFDEF,
137                 PP_COND_UNKNOWN,
138         } type;
139
140         bool inverse;
141         const char *symbol;
142 };
143
144 /* Preprocessor information about each line. */
145 struct line_info {
146         enum line_info_type type;
147
148         /* Is this actually a continuation of line above? (which ends in \) */
149         bool continued;
150
151         /* Conditions for this line to be compiled. */
152         struct pp_conditions *cond;
153 };
154
155 struct ccan_file {
156         struct list_node list;
157
158         /* Name (usually, within m->dir). */
159         char *name;
160
161         /* Full path name. */
162         char *fullname;
163
164         /* Pristine version of the original file.
165          * Use get_ccan_file_contents to fill this. */
166         const char *contents;
167         size_t contents_size;
168
169         /* Use get_ccan_file_lines / get_ccan_line_info to fill these. */
170         unsigned int num_lines;
171         char **lines;
172         struct line_info *line_info;
173
174         struct list_head *doc_sections;
175
176         /* If this file gets compiled (eg. .C file to .o file), result here. */
177         char *compiled;
178
179         /* Compiled with coverage information. */
180         char *cov_compiled;
181
182         /* Leak output from valgrind. */
183         char *leak_info;
184
185         /* Simplified stream (lowercase letters and single spaces) */
186         char *simplified;
187 };
188
189 /* A new ccan_file, with the given name (talloc_steal onto returned value). */
190 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name);
191
192 /* Use this rather than accessing f->contents directly: loads on demand. */
193 const char *get_ccan_file_contents(struct ccan_file *f);
194
195 /* Use this rather than accessing f->lines directly: loads on demand. */
196 char **get_ccan_file_lines(struct ccan_file *f);
197
198 /* Use this rather than accessing f->lines directly: loads on demand. */
199 struct line_info *get_ccan_line_info(struct ccan_file *f);
200
201 /* Use this rather than accessing f->simplified directly: loads on demand. */
202 const char *get_ccan_simplified(struct ccan_file *f);
203
204 enum line_compiled {
205         NOT_COMPILED,
206         COMPILED,
207         MAYBE_COMPILED,
208 };
209
210 /* Simple evaluator.  If symbols are set this way, is this condition true?
211  * NULL values mean undefined, NULL symbol terminates. */
212 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
213                                     const char *symbol,
214                                     const unsigned int *value, ...);
215
216 /* Get token if it's equal to token. */
217 bool get_token(const char **line, const char *token);
218 /* Talloc copy of symbol token, or NULL.  Increment line. */
219 char *get_symbol_token(void *ctx, const char **line);
220
221 /* Similarly for ->doc_sections */
222 struct list_head *get_ccan_file_docs(struct ccan_file *f);
223
224 /* Get NULL-terminated array options for this file for this test */
225 char **per_file_options(const struct ccanlint *test, struct ccan_file *f);
226
227 /* Append message about this file (and line, if non-zero) to the score->error */
228 void score_file_error(struct score *, struct ccan_file *f, unsigned line,
229                       const char *errorfmt, ...);
230
231 /* Normal tests. */
232 extern struct ccanlint trailing_whitespace;
233
234 /* Dependencies */
235 struct dependent {
236         struct list_node node;
237         struct ccanlint *dependent;
238 };
239
240 /* Are we happy to compile stuff, or just non-intrusive tests? */
241 extern bool safe_mode;
242
243 /* Where is the ccan dir?  Available after first manifest. */
244 extern const char *ccan_dir;
245
246 /* Compiler and CFLAGS, from config.h if available. */
247 extern const char *compiler, *cflags;
248
249 /* Contents of config.h (or NULL if not found) */
250 extern const char *config_header;
251
252 #endif /* CCAN_LINT_H */