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