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