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