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