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