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