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