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