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