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