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