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