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