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