]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.h
tools/ccanlint: compile cleanly with -Wextra.
[ccan] / tools / ccanlint / ccanlint.h
1 #ifndef CCAN_LINT_H
2 #define CCAN_LINT_H
3 #include "config.h"
4 #include <ccan/list/list.h>
5 #include <ccan/tal/tal.h>
6 #include <ccan/dgraph/dgraph.h>
7 #include <ccan/autodata/autodata.h>
8 #include <stdbool.h>
9 #include "../doc_extract.h"
10 #include "../manifest.h"
11 #include "../tools.h"
12 #include "licenses.h"
13
14 AUTODATA_TYPE(ccanlint_tests, struct ccanlint);
15 #define REGISTER_TEST(test) AUTODATA(ccanlint_tests, &test)
16
17 /* 0 == Describe failed tests.
18    1 == Describe results for partial failures.
19    2 == One line per test, plus details of failures.
20
21    Mainly for debugging ccanlint:
22    3 == Describe every object built.
23    4 == Describe every action. */
24 extern int verbose;
25
26 /* Error in a particular file: stored off score->per_file_errors. */
27 struct file_error {
28         struct list_node list;
29         struct ccan_file *file;
30         unsigned int line;
31 };
32
33 /* The score for an individual test. */
34 struct score {
35         /* Starts as false: if not set to true, ccanlint exits non-zero.
36          * Thus it is usually set for compilation or other serious failures. */
37         bool pass;
38         /* Starts at 0 and 1 respectively. */
39         unsigned int score, total;
40         /* The error message to print. */
41         char *error;
42         /* Per file errors, set by score_file_error() */
43         struct list_head per_file_errors;
44 };
45
46 struct ccanlint {
47         /* More concise unique name of test. */
48         const char *key;
49
50         /* Unique name of test */
51         const char *name;
52
53         /* Can we run this test?  Return string explaining why, if not. */
54         const char *(*can_run)(struct manifest *m);
55
56         /* Should we stop immediately if test fails? */
57         bool compulsory;
58
59         /* If timeleft is set to 0, means it timed out.
60          * score is the result, and a tal context freed after all our
61          * depends are done. */
62         void (*check)(struct manifest *m,
63                       unsigned int *timeleft, struct score *score);
64
65         /* Can we do something about it? (NULL if not) */
66         void (*handle)(struct manifest *m, struct score *score);
67
68         /* Options from _info. */
69         char **options;
70         /* If not set, we'll give an error if they try to set options. */
71         bool takes_options;
72
73         /* Space-separated list of dependency keys. */
74         const char *needs;
75
76         /* Internal use fields: */
77         /* We are a node in a dependency graph. */
78         struct dgraph_node node;
79         /* Did we skip a dependency?  If so, must skip this, too. */
80         const char *skip;
81         /* Have we already run this? */
82         bool done;
83 };
84
85 /* Ask the user a yes/no question: the answer is NO if there's an error. */
86 bool ask(const char *question);
87
88 enum line_info_type {
89         PREPROC_LINE, /* Line starts with # */
90         CODE_LINE, /* Code (ie. not pure comment). */
91         DOC_LINE, /* Line with kernel-doc-style comment. */
92         COMMENT_LINE, /* (pure) comment line */
93 };
94
95 /* So far, only do simple #ifdef/#ifndef/#if defined/#if !defined tests,
96  * and #if <SYMBOL>/#if !<SYMBOL> */
97 struct pp_conditions {
98         /* We're inside another ifdef? */
99         struct pp_conditions *parent;
100
101         enum {
102                 PP_COND_IF,
103                 PP_COND_IFDEF,
104                 PP_COND_UNKNOWN,
105         } type;
106
107         bool inverse;
108         const char *symbol;
109 };
110
111 /* Preprocessor information about each line. */
112 struct line_info {
113         enum line_info_type type;
114
115         /* Is this actually a continuation of line above? (which ends in \) */
116         bool continued;
117
118         /* Conditions for this line to be compiled. */
119         struct pp_conditions *cond;
120 };
121
122 /* Use this rather than accessing f->lines directly: loads on demand. */
123 struct line_info *get_ccan_line_info(struct ccan_file *f);
124
125 /* Use this rather than accessing f->simplified directly: loads on demand. */
126 const char *get_ccan_simplified(struct ccan_file *f);
127
128 enum line_compiled {
129         NOT_COMPILED,
130         COMPILED,
131         MAYBE_COMPILED,
132 };
133
134 /* Simple evaluator.  If symbols are set this way, is this condition true?
135  * NULL values mean undefined, NULL symbol terminates. */
136 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
137                                     const char *symbol,
138                                     const unsigned int *value, ...);
139
140 /* Get token if it's equal to token. */
141 bool get_token(const char **line, const char *token);
142 /* Tal copy of symbol token, or NULL.  Increment line. */
143 char *get_symbol_token(void *ctx, const char **line);
144
145 /* Similarly for ->doc_sections */
146 struct list_head *get_ccan_file_docs(struct ccan_file *f);
147
148 /* Get NULL-terminated array options for this file for this test */
149 char **per_file_options(const struct ccanlint *test, struct ccan_file *f);
150
151 /* Append message about this file (and line, if non-zero) to the score->error */
152 void score_file_error(struct score *, struct ccan_file *f, unsigned line,
153                       const char *errorfmt, ...);
154
155 /* Append message to the score->error */
156 void score_error(struct score *score, const char * source,
157                  const char *errorfmt, ...);
158
159 /* Start a command in the background. */
160 void run_command_async(const void *ctx, unsigned int time_ms,
161                        const char *fmt, ...);
162
163 /* Async version of compile_and_link. */
164 void compile_and_link_async(const void *ctx, unsigned int time_ms,
165                             const char *cfile, const char *ccandir,
166                             const char *objs, const char *compiler,
167                             const char *cflags,
168                             const char *libs, const char *outfile);
169
170 /* Get results of a command, returning ctx (and free it). */
171 void *collect_command(bool *ok, char **output);
172
173 /* Find manifest for this dir and return compiled _info filename. */
174 char *get_or_compile_info(const void *ctx, const char *dir);
175
176 /* Normal tests. */
177 extern struct ccanlint trailing_whitespace;
178
179 /* Dependencies */
180 struct dependent {
181         struct list_node node;
182         struct ccanlint *dependent;
183 };
184
185 /* Is this test excluded (cmdline or _info). */
186 bool is_excluded(const char *name);
187
188 /* Called to add options from _info, once it's located. */
189 void add_info_options(struct ccan_file *info);
190
191 /* Are we happy to compile stuff, or just non-intrusive tests? */
192 extern bool safe_mode;
193
194 /* Did the user want to keep all the results? */
195 extern bool keep_results;
196
197 /* Did we find non-ccan dependencies? */
198 extern bool non_ccan_deps;
199
200 /* Did we fail to build? */
201 extern bool build_failed;
202
203 /* Contents of config.h (or NULL if not found) */
204 extern const char *config_header;
205
206 /* Where is the ccan dir?   */
207 extern const char *ccan_dir;
208
209 #endif /* CCAN_LINT_H */