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