]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.h
ccanlint: keep separate array of compiled versions.
[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_TYPES
24 };
25
26 struct manifest {
27         char *dir;
28         /* The module name, ie. final element of dir name */
29         char *basename;
30         struct ccan_file *info_file;
31
32         /* Linked off deps. */
33         struct list_node list;
34         /* Where our final compiled output is */
35         char *compiled[COMPILE_TYPES];
36
37         struct list_head c_files;
38         struct list_head h_files;
39
40         struct list_head run_tests;
41         struct list_head api_tests;
42         struct list_head compile_ok_tests;
43         struct list_head compile_fail_tests;
44         struct list_head other_test_c_files;
45         struct list_head other_test_files;
46
47         struct list_head other_files;
48         struct list_head examples;
49         struct list_head mangled_examples;
50
51         /* From tests/check_depends_exist.c */
52         struct list_head deps;
53
54         /* From tests/license_exists.c */
55         enum license license;
56 };
57
58 /* Get the manifest for a given directory. */
59 struct manifest *get_manifest(const void *ctx, const char *dir);
60
61 /* Error in a particular file: stored off score->per_file_errors. */
62 struct file_error {
63         struct list_node list;
64         struct ccan_file *file;
65         unsigned int line;
66 };
67
68 /* The score for an individual test. */
69 struct score {
70         /* Starts as false: if not set to true, ccanlint exits non-zero.
71          * Thus it is usually set for compilation or other serious failures. */
72         bool pass;
73         /* Starts at 0 and 1 respectively. */
74         unsigned int score, total;
75         /* The error message to print. */
76         char *error;
77         /* Per file errors, set by score_file_error() */
78         struct list_head per_file_errors;
79 };
80
81 struct ccanlint {
82         struct list_node list;
83
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         /* keep is set if you should keep the results.
94          * If timeleft is set to 0, means it timed out.
95          * score is the result, and a talloc context freed after all our
96          * depends are done. */
97         void (*check)(struct manifest *m,
98                       bool keep, unsigned int *timeleft, struct score *score);
99
100         /* Can we do something about it? (NULL if not) */
101         void (*handle)(struct manifest *m, struct score *score);
102
103         /* Options from _info. */
104         char **options;
105         /* If not set, we'll give an error if they try to set options. */
106         bool takes_options;
107
108         /* Space-separated list of dependency keys. */
109         const char *needs;
110
111         /* Internal use fields: */
112         /* Who depends on us? */
113         struct list_head dependencies;
114         /* How many things do we (still) depend on? */
115         unsigned int num_depends;
116         /* Did we skip a dependency?  If so, must skip this, too. */
117         const char *skip;
118         /* Did we fail a dependency?  If so, skip and mark as fail. */
119         bool skip_fail;
120         /* Did the user want to keep these results? */
121         bool keep_results;
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         /* Compiled with coverage information. */
186         char *cov_compiled;
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 /* Normal tests. */
241 extern struct ccanlint trailing_whitespace;
242
243 /* Dependencies */
244 struct dependent {
245         struct list_node node;
246         struct ccanlint *dependent;
247 };
248
249 /* Is this test excluded (cmdline or _info). */
250 bool is_excluded(const char *name);
251
252 /* Are we happy to compile stuff, or just non-intrusive tests? */
253 extern bool safe_mode;
254
255 /* Where is the ccan dir?  Available after first manifest. */
256 extern const char *ccan_dir;
257
258 /* Compiler and CFLAGS, from config.h if available. */
259 extern const char *compiler, *cflags;
260
261 /* Contents of config.h (or NULL if not found) */
262 extern const char *config_header;
263
264 #endif /* CCAN_LINT_H */