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