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