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