]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.h
ccanlint: print error information even if we pass.
[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 #include "generated-compulsory-tests"
9 #include "generated-normal-tests"
10 #undef REGISTER_TEST
11
12 #define REGISTER_TEST(name, ...) 
13
14 /* 0 == Describe failed tests.
15    1 == Describe results for partial failures.
16    2 == One line per test, plus details of failures.
17
18    Mainly for debugging ccanlint:
19    3 == Describe every object built.
20    4 == Describe every action. */
21 extern int verbose;
22
23 struct manifest {
24         char *dir;
25         /* The module name, ie. final element of dir name */
26         char *basename;
27         struct ccan_file *info_file;
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 dep_dirs;
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         /* Internal use fields: */
86         /* Who depends on us? */
87         struct list_head dependencies;
88         /* How many things do we (still) depend on? */
89         unsigned int num_depends;
90         /* Did we skip a dependency?  If so, must skip this, too. */
91         const char *skip;
92         /* Did we fail a dependency?  If so, skip and mark as fail. */
93         bool skip_fail;
94         /* Did the user want to keep these results? */
95         bool keep_results;
96 };
97
98 /* Ask the user a yes/no question: the answer is NO if there's an error. */
99 bool ask(const char *question);
100
101 enum line_info_type {
102         PREPROC_LINE, /* Line starts with # */
103         CODE_LINE, /* Code (ie. not pure comment). */
104         DOC_LINE, /* Line with kernel-doc-style comment. */
105         COMMENT_LINE, /* (pure) comment line */
106 };
107
108 /* So far, only do simple #ifdef/#ifndef/#if defined/#if !defined tests,
109  * and #if <SYMBOL>/#if !<SYMBOL> */
110 struct pp_conditions {
111         /* We're inside another ifdef? */
112         struct pp_conditions *parent;
113
114         enum {
115                 PP_COND_IF,
116                 PP_COND_IFDEF,
117                 PP_COND_UNKNOWN,
118         } type;
119
120         bool inverse;
121         const char *symbol;
122 };
123
124 /* Preprocessor information about each line. */
125 struct line_info {
126         enum line_info_type type;
127
128         /* Is this actually a continuation of line above? (which ends in \) */
129         bool continued;
130
131         /* Conditions for this line to be compiled. */
132         struct pp_conditions *cond;
133 };
134
135 struct ccan_file {
136         struct list_node list;
137
138         /* Name (usually, within m->dir). */
139         char *name;
140
141         /* Full path name. */
142         char *fullname;
143
144         /* Pristine version of the original file.
145          * Use get_ccan_file_contents to fill this. */
146         const char *contents;
147         size_t contents_size;
148
149         /* Use get_ccan_file_lines / get_ccan_line_info to fill these. */
150         unsigned int num_lines;
151         char **lines;
152         struct line_info *line_info;
153
154         struct list_head *doc_sections;
155
156         /* If this file gets compiled (eg. .C file to .o file), result here. */
157         char *compiled;
158
159         /* Compiled with coverage information. */
160         char *cov_compiled;
161 };
162
163 /* A new ccan_file, with the given name (talloc_steal onto returned value). */
164 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name);
165
166 /* Use this rather than accessing f->contents directly: loads on demand. */
167 const char *get_ccan_file_contents(struct ccan_file *f);
168
169 /* Use this rather than accessing f->lines directly: loads on demand. */
170 char **get_ccan_file_lines(struct ccan_file *f);
171
172 /* Use this rather than accessing f->lines directly: loads on demand. */
173 struct line_info *get_ccan_line_info(struct ccan_file *f);
174
175 enum line_compiled {
176         NOT_COMPILED,
177         COMPILED,
178         MAYBE_COMPILED,
179 };
180
181 /* Simple evaluator.  If symbols are set this way, is this condition true?
182  * NULL values mean undefined, NULL symbol terminates. */
183 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
184                                     const char *symbol,
185                                     const unsigned int *value, ...);
186
187 /* Get token if it's equal to token. */
188 bool get_token(const char **line, const char *token);
189 /* Talloc copy of symbol token, or NULL.  Increment line. */
190 char *get_symbol_token(void *ctx, const char **line);
191
192 /* Similarly for ->doc_sections */
193 struct list_head *get_ccan_file_docs(struct ccan_file *f);
194
195 /* Add an error about this file (and line, if non-zero) to the score struct */
196 void score_file_error(struct score *, struct ccan_file *f, unsigned line,
197                       const char *error);
198
199 /* Normal tests. */
200 extern struct ccanlint trailing_whitespace;
201
202 /* Dependencies */
203 struct dependent {
204         struct list_node node;
205         struct ccanlint *dependent;
206 };
207
208 /* Are we happy to compile stuff, or just non-intrusive tests? */
209 extern bool safe_mode;
210
211 /* Where is the ccan dir?  Available after first manifest. */
212 extern const char *ccan_dir;
213
214 #endif /* CCAN_LINT_H */