-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser-cpp.c
293 lines (253 loc) · 6.85 KB
/
parser-cpp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "parser-cpp.h"
#include "config.h"
#include <string.h>
#include "libks/arena.h"
#include "libks/string.h"
#include "clang.h"
#include "doc.h"
#include "expr.h"
#include "lexer.h"
#include "parser-expr.h"
#include "parser-priv.h"
#include "parser-type.h"
#include "ruler.h"
#include "token.h"
static int iscdefs(const char *, size_t);
static int
is_list_entry(const struct token *tk)
{
return clang_token_type(tk) == CLANG_TOKEN_LIST_ENTRY;
}
int
parser_cpp_peek_type(struct parser *pr, struct token **rparen)
{
struct lexer_state s;
struct lexer *lx = pr->pr_lx;
struct token *ident;
int peek = 0;
/* Detect usage of types hidden behind cpp such as STACK_OF(X509). */
lexer_peek_enter(lx, &s);
if (lexer_if(lx, TOKEN_IDENT, &ident) &&
lexer_if(lx, TOKEN_LPAREN, NULL) &&
lexer_if(lx, TOKEN_IDENT, NULL) &&
lexer_if(lx, TOKEN_RPAREN, rparen)) {
struct token *nx;
if (lexer_peek_if(lx, TOKEN_IDENT, &nx) &&
token_cmp(ident, nx) == 0)
peek = 1;
else if (lexer_peek_if(lx, TOKEN_STAR, NULL))
peek = 1;
}
lexer_peek_leave(lx, &s);
if (peek)
return 1;
/* Detect LIST_ENTRY(list, struct s) from libks:list(3). */
lexer_peek_enter(lx, &s);
if (lexer_if(lx, TOKEN_IDENT, &ident) && is_list_entry(ident) &&
lexer_if(lx, TOKEN_LPAREN, NULL) &&
lexer_if(lx, TOKEN_IDENT, NULL) &&
lexer_if(lx, TOKEN_COMMA, NULL) &&
lexer_if(lx, TOKEN_IDENT, NULL) &&
lexer_if(lx, TOKEN_RPAREN, rparen) &&
lexer_if(lx, TOKEN_SEMI, NULL))
peek = 1;
lexer_peek_leave(lx, &s);
return peek;
}
/*
* Detect usage of preprocessor directives such as the ones provided by
* queue(3).
*/
int
parser_cpp_peek_decl(struct parser *pr, struct parser_type *type,
unsigned int flags)
{
struct lexer_state s;
struct lexer *lx = pr->pr_lx;
struct token *macro, *tk;
int peek = 0;
if (!lexer_peek(lx, &type->beg))
return 0;
lexer_peek_enter(lx, &s);
while (lexer_if_flags(lx, TOKEN_FLAG_QUALIFIER | TOKEN_FLAG_STORAGE,
NULL))
continue;
if (lexer_if(lx, TOKEN_IDENT, ¯o) &&
lexer_if_pair(lx, TOKEN_LPAREN, TOKEN_RPAREN, NULL, &type->end)) {
struct token *ident;
for (;;) {
if (!lexer_if(lx, TOKEN_STAR, &tk))
break;
type->end = tk;
}
if (lexer_if(lx, TOKEN_EQUAL, NULL)) {
peek = 1;
} else if ((flags & PARSER_CPP_DECL_ROOT) &&
lexer_if(lx, TOKEN_SEMI, NULL)) {
peek = 1;
} else if (lexer_if(lx, TOKEN_IDENT, &ident)) {
while (lexer_if(lx, TOKEN_IDENT, &ident))
continue;
if (token_cmp(macro, ident) == 0 ||
lexer_if(lx, TOKEN_SEMI, NULL))
peek = 1;
}
}
lexer_peek_leave(lx, &s);
return peek;
}
/*
* Detect usage of X macro. That is, something that looks like a function call
* but is not followed by a semicolon nor comma if being part of an initializer.
* One example are the macros provided by RBT_PROTOTYPE(9).
*/
int
parser_cpp_peek_x(struct parser *pr, struct token **tk)
{
struct lexer_state s;
struct lexer *lx = pr->pr_lx;
struct token *pv = NULL;
struct token *ident, *rparen;
int peek = 0;
(void)lexer_back(lx, &pv);
lexer_peek_enter(lx, &s);
while (lexer_if_flags(lx, TOKEN_FLAG_STORAGE, NULL))
continue;
if (lexer_if(lx, TOKEN_IDENT, &ident) &&
lexer_if_pair(lx, TOKEN_LPAREN, TOKEN_RPAREN, NULL, &rparen)) {
const struct token *nx;
/*
* The previous token must not reside on the same line as the
* identifier. The next token must reside on the next line and
* have the same or less indentation, assuming the identifier is
* not positioned at the start of the line. This is of
* importance in order to not confuse loop constructs hidden
* behind cpp.
*/
nx = token_next(rparen);
if ((pv == NULL || token_cmp(pv, ident) < 0) &&
(nx == NULL || (token_cmp(nx, rparen) > 0 &&
(ident->tk_cno == 1 || nx->tk_cno <= ident->tk_cno))))
peek = 1;
}
lexer_peek_leave(lx, &s);
if (peek && tk != NULL)
*tk = rparen;
return peek;
}
int
parser_cpp_x(struct parser *pr, struct doc *dc)
{
struct doc *concat;
struct lexer *lx = pr->pr_lx;
struct token *rparen, *tk;
int error;
if (!parser_cpp_peek_x(pr, &rparen))
return parser_none(pr);
if (pr->pr_cpp.ruler == NULL) {
pr->pr_cpp.ruler = arena_malloc(pr->pr_arena.scratch_scope,
sizeof(*pr->pr_cpp.ruler));
ruler_init(pr->pr_cpp.ruler, 0, RULER_ALIGN_SENSE);
}
concat = doc_alloc(DOC_CONCAT, doc_alloc(DOC_GROUP, dc));
while (lexer_if_flags(lx, TOKEN_FLAG_STORAGE, &tk)) {
parser_doc_token(pr, tk, concat);
doc_alloc(DOC_LINE, concat);
}
error = parser_expr(pr, NULL, &(struct parser_expr_arg){
.dc = concat,
.rl = pr->pr_cpp.ruler,
.stop = token_next(rparen),
.flags = EXPR_EXEC_ALIGN,
});
if (error & HALT)
return parser_fail(pr);
/* Compensate for the expression parser only honoring one new line. */
if (token_has_line(rparen, 2))
doc_alloc(DOC_HARDLINE, concat);
return parser_good(pr);
}
/*
* Parse usage of macros from cdefs.h, such as __BEGIN_HIDDEN_DECLS.
*/
int
parser_cpp_cdefs(struct parser *pr, struct doc *dc)
{
struct lexer_state s;
struct lexer *lx = pr->pr_lx;
struct token *ident, *nx;
int peek = 0;
lexer_peek_enter(lx, &s);
if (lexer_if(lx, TOKEN_IDENT, &ident) &&
lexer_pop(lx, &nx) && token_cmp(nx, ident) > 0 &&
iscdefs(ident->tk_str, ident->tk_len))
peek = 1;
lexer_peek_leave(lx, &s);
if (!peek)
return parser_none(pr);
if (lexer_expect(lx, TOKEN_IDENT, &ident))
parser_doc_token(pr, ident, dc);
return parser_good(pr);
}
int
parser_cpp_decl_root(struct parser *pr, struct doc *dc)
{
struct lexer_state s;
struct lexer *lx = pr->pr_lx;
struct token *ident, *semi;
int peek = 0;
lexer_peek_enter(lx, &s);
if (lexer_if(lx, TOKEN_IDENT, NULL) &&
lexer_if(lx, TOKEN_SEMI, NULL))
peek = 1;
lexer_peek_leave(lx, &s);
if (!peek)
return parser_none(pr);
if (lexer_expect(lx, TOKEN_IDENT, &ident))
parser_doc_token(pr, ident, dc);
if (lexer_expect(lx, TOKEN_SEMI, &semi))
parser_doc_token(pr, semi, dc);
return parser_good(pr);
}
void *
parser_cpp_decl_enter(struct parser *pr)
{
struct ruler *cookie = pr->pr_cpp.ruler;
/* Cope with nested declarations, use a dedicated ruler per scope. */
pr->pr_cpp.ruler = NULL;
return cookie;
}
void
parser_cpp_decl_leave(struct parser *pr, void *cookie)
{
struct ruler *rl = pr->pr_cpp.ruler;
pr->pr_cpp.ruler = cookie;
if (rl == NULL)
return;
ruler_exec(rl);
ruler_free(rl);
}
static int
iscdefs(const char *str, size_t len)
{
struct suffix {
const char *str;
size_t len;
} suffixes[] = {
#define S(s) { s, sizeof(s) - 1 }
S("_BEGIN_DECLS"),
S("_END_DECLS"),
#undef S
};
size_t nsuffixes = sizeof(suffixes) / sizeof(suffixes[0]);
size_t i;
for (i = 0; i < nsuffixes; i++) {
const struct suffix *s = &suffixes[i];
if (len >= s->len &&
strncmp(&str[len - s->len], s->str, s->len) == 0)
return 1;
}
if (len < 2 || strncmp(str, "__", 2) != 0)
return 0;
return KS_str_match(&str[2], len - 2, "AZ09__") == len - 2;
}