]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.h
fc92cd783c7dcd26a4717af2bbcca40d37f02351
[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         /* If timeleft is set to 0, means it timed out.
97          * score is the result, and a talloc context freed after all our
98          * depends are done. */
99         void (*check)(struct manifest *m,
100                       unsigned int *timeleft, struct score *score);
101
102         /* Can we do something about it? (NULL if not) */
103         void (*handle)(struct manifest *m, struct score *score);
104
105         /* Options from _info. */
106         char **options;
107         /* If not set, we'll give an error if they try to set options. */
108         bool takes_options;
109
110         /* Space-separated list of dependency keys. */
111         const char *needs;
112
113         /* Internal use fields: */
114         /* We are a node in a dependency graph. */
115         struct dgraph_node node;
116         /* Did we skip a dependency?  If so, must skip this, too. */
117         const char *skip;
118         /* Have we already run this? */
119         bool done;
120 };
121
122 /* Ask the user a yes/no question: the answer is NO if there's an error. */
123 bool ask(const char *question);
124
125 enum line_info_type {
126         PREPROC_LINE, /* Line starts with # */
127         CODE_LINE, /* Code (ie. not pure comment). */
128         DOC_LINE, /* Line with kernel-doc-style comment. */
129         COMMENT_LINE, /* (pure) comment line */
130 };
131
132 /* So far, only do simple #ifdef/#ifndef/#if defined/#if !defined tests,
133  * and #if <SYMBOL>/#if !<SYMBOL> */
134 struct pp_conditions {
135         /* We're inside another ifdef? */
136         struct pp_conditions *parent;
137
138         enum {
139                 PP_COND_IF,
140                 PP_COND_IFDEF,
141                 PP_COND_UNKNOWN,
142         } type;
143
144         bool inverse;
145         const char *symbol;
146 };
147
148 /* Preprocessor information about each line. */
149 struct line_info {
150         enum line_info_type type;
151
152         /* Is this actually a continuation of line above? (which ends in \) */
153         bool continued;
154
155         /* Conditions for this line to be compiled. */
156         struct pp_conditions *cond;
157 };
158
159 struct ccan_file {
160         struct list_node list;
161
162         /* Name (usually, within m->dir). */
163         char *name;
164
165         /* Full path name. */
166         char *fullname;
167
168         /* Pristine version of the original file.
169          * Use get_ccan_file_contents to fill this. */
170         const char *contents;
171         size_t contents_size;
172
173         /* Use get_ccan_file_lines / get_ccan_line_info to fill these. */
174         unsigned int num_lines;
175         char **lines;
176         struct line_info *line_info;
177
178         struct list_head *doc_sections;
179
180         /* If this file gets compiled (eg. .C file to .o file), result here. */
181         char *compiled[COMPILE_TYPES];
182
183         /* Filename containing output from valgrind. */
184         char *valgrind_log;
185
186         /* Leak output from valgrind. */
187         char *leak_info;
188
189         /* Simplified stream (lowercase letters and single spaces) */
190         char *simplified;
191 };
192
193 /* A new ccan_file, with the given name (talloc_steal onto returned value). */
194 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name);
195
196 /* Use this rather than accessing f->contents directly: loads on demand. */
197 const char *get_ccan_file_contents(struct ccan_file *f);
198
199 /* Use this rather than accessing f->lines directly: loads on demand. */
200 char **get_ccan_file_lines(struct ccan_file *f);
201
202 /* Use this rather than accessing f->lines directly: loads on demand. */
203 struct line_info *get_ccan_line_info(struct ccan_file *f);
204
205 /* Use this rather than accessing f->simplified directly: loads on demand. */
206 const char *get_ccan_simplified(struct ccan_file *f);
207
208 enum line_compiled {
209         NOT_COMPILED,
210         COMPILED,
211         MAYBE_COMPILED,
212 };
213
214 /* Simple evaluator.  If symbols are set this way, is this condition true?
215  * NULL values mean undefined, NULL symbol terminates. */
216 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
217                                     const char *symbol,
218                                     const unsigned int *value, ...);
219
220 /* Get token if it's equal to token. */
221 bool get_token(const char **line, const char *token);
222 /* Talloc copy of symbol token, or NULL.  Increment line. */
223 char *get_symbol_token(void *ctx, const char **line);
224
225 /* Similarly for ->doc_sections */
226 struct list_head *get_ccan_file_docs(struct ccan_file *f);
227
228 /* Get NULL-terminated array options for this file for this test */
229 char **per_file_options(const struct ccanlint *test, struct ccan_file *f);
230
231 /* Append message about this file (and line, if non-zero) to the score->error */
232 void score_file_error(struct score *, struct ccan_file *f, unsigned line,
233                       const char *errorfmt, ...);
234
235 /* Start a command in the background. */
236 void run_command_async(const void *ctx, unsigned int time_ms,
237                        const char *fmt, ...);
238
239 /* Async version of compile_and_link. */
240 void compile_and_link_async(const void *ctx, unsigned int time_ms,
241                             const char *cfile, const char *ccandir,
242                             const char *objs, const char *compiler,
243                             const char *cflags,
244                             const char *libs, const char *outfile);
245
246 /* Get results of a command, returning ctx (and free it). */
247 void *collect_command(bool *ok, char **output);
248
249 /* Normal tests. */
250 extern struct ccanlint trailing_whitespace;
251
252 /* Dependencies */
253 struct dependent {
254         struct list_node node;
255         struct ccanlint *dependent;
256 };
257
258 /* Is this test excluded (cmdline or _info). */
259 bool is_excluded(const char *name);
260
261 /* Called to add options from _info, once it's located. */
262 void add_info_options(struct ccan_file *info);
263
264 /* Are we happy to compile stuff, or just non-intrusive tests? */
265 extern bool safe_mode;
266
267 /* Did the user want to keep all the results? */
268 extern bool keep_results;
269
270 /* Where is the ccan dir?  Available after first manifest. */
271 extern const char *ccan_dir;
272
273 /* Compiler and CFLAGS, from config.h if available. */
274 extern const char *compiler, *cflags;
275
276 /* Contents of config.h (or NULL if not found) */
277 extern const char *config_header;
278
279 #endif /* CCAN_LINT_H */