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