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