validate: fix type stack and leaks
[m6w6/ext-psi] / src / cpp_tokiter.c
1 /*******************************************************************************
2 Copyright (c) 2017, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27
28 #include "cpp.h"
29 #include "parser.h"
30
31 #if PSI_CPP_DEBUG > 1
32 void psi_cpp_tokiter_dump(int fd, struct psi_cpp *cpp)
33 {
34 size_t i = cpp->index;
35 struct psi_token *T;
36
37 if (i > 20) {
38 i -= 20;
39 } else {
40 i = 0;
41 }
42 while (psi_plist_get(cpp->tokens, i, &T)) {
43 dprintf(fd, "PSI: CPP tokens %5zu %c ", i, cpp->index == i ? '*' : ' ');
44 psi_token_dump(fd, T);
45 if (i >= cpp->index + 10) {
46 dprintf(fd, "PSI: CPP tokens .....\n");
47 break;
48 }
49 ++i;
50 }
51 }
52 #endif
53
54 void psi_cpp_tokiter_reset(struct psi_cpp *cpp)
55 {
56 #if PSI_CPP_DEBUG
57 fprintf(stderr, "PSI: CPP reset (%zu tokens)\n", psi_plist_count(cpp->tokens));
58 # if PSI_CPP_DEBUG > 1
59 psi_cpp_tokiter_dump(2, cpp);
60 # endif
61 #endif
62 cpp->index = 0;
63 cpp->expanded = 0;
64 }
65
66 bool psi_cpp_tokiter_seek(struct psi_cpp *cpp, size_t index)
67 {
68 if (index < psi_plist_count(cpp->tokens)) {
69 cpp->index = index;
70 return true;
71 }
72 return false;
73 }
74
75 struct psi_token *psi_cpp_tokiter_current(struct psi_cpp *cpp)
76 {
77 struct psi_token *current = NULL;
78 bool found = psi_plist_get(cpp->tokens, cpp->index, &current);
79
80 assert(found);
81
82 return current;
83 }
84
85 size_t psi_cpp_tokiter_index(struct psi_cpp *cpp)
86 {
87 return cpp->index;
88 }
89
90 void psi_cpp_tokiter_next(struct psi_cpp *cpp)
91 {
92 #if 0 && PSI_CPP_DEBUG
93 fprintf(stderr, "PSI: CPP next -> index=%zu -> index=%zu\n", cpp->index, cpp->index+1);
94 #endif
95 ++cpp->index;
96 }
97
98 void psi_cpp_tokiter_prev(struct psi_cpp *cpp)
99 {
100 #if 0 && PSI_CPP_DEBUG
101 fprintf(stderr, "PSI: CPP prev -> index=%zu -> index=%zu\n", cpp->index, cpp->index-1);
102 #endif
103 if (cpp->index) {
104 --cpp->index;
105 }
106 }
107
108 bool psi_cpp_tokiter_valid(struct psi_cpp *cpp)
109 {
110 #if 0 && PSI_CPP_DEBUG
111 fprintf(stderr, "PSI: CPP valid -> index=%zu -> %d\n", cpp->index, cpp->index < psi_plist_count(cpp->tokens));
112 #endif
113 return cpp->index < psi_plist_count(cpp->tokens);
114 }
115
116 bool psi_cpp_tokiter_del_cur(struct psi_cpp *cpp, bool free_token)
117 {
118 struct psi_token *cur = NULL;
119 bool deleted = psi_plist_del(cpp->tokens, cpp->index, &cur);
120 size_t count;
121
122 #if PSI_CPP_DEBUG
123 fprintf(stderr, "PSI: CPP del_cur -> index=%zu, del=%d, free=%d, count=%zu ",
124 cpp->index, (int) deleted, (int) free_token, psi_plist_count(cpp->tokens));
125 if (cur) {
126 psi_token_dump(2, cur);
127 } else {
128 fprintf(stderr, "NULL\n");
129 }
130 #endif
131 if (cur && free_token) {
132 free(cur);
133 }
134 count = psi_plist_count(cpp->tokens);
135 if (deleted && cpp->index >= count) {
136 if (count > 0) {
137 cpp->index = count - 1;
138 } else {
139 cpp->index = 0;
140 }
141 }
142 return deleted;
143 }
144
145 bool psi_cpp_tokiter_del_range(struct psi_cpp *cpp, size_t offset, size_t num_eles, bool free_tokens)
146 {
147 struct psi_token **ptr;
148 bool deleted;
149
150 if (free_tokens) {
151 ptr = calloc(num_eles, sizeof(*ptr));
152 } else {
153 ptr = NULL;
154 }
155
156 #if PSI_CPP_DEBUG
157 fprintf(stderr, "PSI: CPP del_range -> index=%zu, offset=%zu, num_eles=%zu, count=%zu\n",
158 cpp->index, offset, num_eles, psi_plist_count(cpp->tokens));
159 #endif
160
161 deleted = psi_plist_del_r(cpp->tokens, offset, num_eles, (void *) ptr);
162
163 if (deleted) {
164 size_t count = psi_plist_count(cpp->tokens);
165
166 if (cpp->index >= count) {
167 if (count > 0) {
168 cpp->index = count - 1;
169 } else {
170 cpp->index = 0;
171 }
172 }
173
174 if (free_tokens) {
175 while (num_eles--) {
176 if (ptr[num_eles]) {
177 free(ptr[num_eles]);
178 }
179 }
180 free(ptr);
181 }
182 }
183 return deleted;
184 }
185
186 bool psi_cpp_tokiter_ins_cur(struct psi_cpp *cpp, struct psi_token *tok)
187 {
188 struct psi_plist *tokens = psi_plist_ins(cpp->tokens, cpp->index, &tok);
189
190 #if PSI_CPP_DEBUG
191 fprintf(stderr, "PSI: CPP ins_cur -> index=%zu ", cpp->index);
192 psi_token_dump(2, tok);
193 #endif
194
195 if (!tokens) {
196 return false;
197 }
198 cpp->tokens = tokens;
199 return true;
200 }
201
202 bool psi_cpp_tokiter_ins_range(struct psi_cpp *cpp, size_t offset,
203 size_t num_eles, void **eles)
204 {
205 struct psi_plist *tokens;
206
207 if (!num_eles) {
208 return true;
209 }
210
211 tokens = psi_plist_ins_r(cpp->tokens, offset, num_eles, eles);
212
213 #if PSI_CPP_DEBUG
214 fprintf(stderr, "PSI: CPP ins_range -> index=%zu, offset=%zu, num_eles=%zu, count=%zu\n",
215 cpp->index, offset, num_eles, psi_plist_count(tokens));
216 #endif
217
218 if (!tokens) {
219 return false;
220 }
221 cpp->tokens = tokens;
222 return true;
223 }
224
225 bool psi_cpp_tokiter_defined(struct psi_cpp *cpp)
226 {
227 if (psi_cpp_tokiter_valid(cpp)) {
228 struct psi_token *current = psi_cpp_tokiter_current(cpp);
229
230 return psi_cpp_defined(cpp, current);
231 }
232
233 return false;
234 }
235
236 static size_t psi_cpp_tokiter_expand_tokens(struct psi_cpp *cpp,
237 struct psi_token *target, struct psi_plist *tokens)
238 {
239 if (tokens && psi_plist_count(tokens)) {
240 size_t i = 0, n = 0;
241 bool stringify = false, paste = false;
242 struct psi_token *tok, **exp_tokens = calloc(psi_plist_count(tokens), sizeof(*exp_tokens));
243
244 while (psi_plist_get(tokens, i++, &tok)) {
245 struct psi_token *new_tok;
246
247 if (tok->type == PSI_T_EOL) {
248 continue;
249 }
250 if (tok->type == PSI_T_HASH) {
251 stringify = true;
252 continue;
253 }
254 if (tok->type == PSI_T_CPP_PASTE) {
255 paste = true;
256 continue;
257 }
258
259 if (paste && n > 0 && exp_tokens[n - 1]) {
260 struct psi_token *tmp_tok, *old_tok = exp_tokens[n - 1];
261
262 tmp_tok = psi_token_init(old_tok->type, "", 0,
263 target->col, target->line, target->file?:"");
264
265 new_tok = psi_token_cat(NULL, 3, tmp_tok, old_tok, tok);
266 free(old_tok);
267 free(tmp_tok);
268
269 exp_tokens[n - 1] = new_tok;
270 } else {
271 new_tok = psi_token_init(stringify ? PSI_T_QUOTED_STRING : tok->type,
272 tok->text, tok->size, target->col, target->line, target->file?:"");
273
274 exp_tokens[n++] = new_tok;
275 }
276
277 #if PSI_CPP_DEBUG
278 fprintf(stderr, "PSI: CPP expand > ");
279 psi_token_dump(2, tok);
280 #endif
281
282 paste = false;
283 stringify = false;
284 }
285 psi_cpp_tokiter_ins_range(cpp, psi_cpp_tokiter_index(cpp), n, (void *) exp_tokens);
286 free(exp_tokens);
287
288 return n;
289 } else {
290 return 0;
291 }
292 }
293
294 static void psi_cpp_tokiter_free_call_tokens(struct psi_plist **arg_tokens_list, size_t arg_count, bool free_tokens)
295 {
296 size_t i;
297
298 for (i = 0; i < arg_count; ++i) {
299 if (arg_tokens_list[i]) {
300 if (free_tokens) {
301 struct psi_token *tok;
302
303 while (psi_plist_pop(arg_tokens_list[i], &tok)) {
304 free(tok);
305 }
306 }
307 psi_plist_free(arg_tokens_list[i]);
308 }
309 }
310 free(arg_tokens_list);
311 }
312
313 static struct psi_plist **psi_cpp_tokiter_read_call_tokens(
314 struct psi_cpp *cpp, size_t arg_count)
315 {
316 size_t arg_index = 0, lparens = 1, rparens = 0;
317 struct psi_plist **arg_tokens = calloc(arg_count, sizeof(*arg_tokens));
318 struct psi_plist *free_tokens = psi_plist_init((psi_plist_dtor) psi_token_free);
319 struct psi_token *tok;
320
321 arg_tokens[0] = psi_plist_init(NULL);
322
323 /* next token must be a LPAREN for a macro call */
324 psi_cpp_tokiter_next(cpp);
325 tok = psi_cpp_tokiter_current(cpp);
326 if (!psi_cpp_tokiter_valid(cpp) || tok->type != PSI_T_LPAREN) {
327 goto fail;
328 }
329
330 /* free LPAREN on success */
331 free_tokens = psi_plist_add(free_tokens, &tok);
332
333 while (lparens > rparens) {
334 psi_cpp_tokiter_next(cpp);
335 if (!psi_cpp_tokiter_valid(cpp)) {
336 goto fail;
337 }
338 tok = psi_cpp_tokiter_current(cpp);
339
340 switch (tok->type) {
341 case PSI_T_LPAREN:
342 ++lparens;
343 arg_tokens[arg_index] = psi_plist_add(arg_tokens[arg_index], &tok);
344 break;
345 case PSI_T_RPAREN:
346 if (++rparens == lparens) {
347 /* closing RPAREN */
348 if (arg_index + 1 < arg_count) {
349 goto fail;
350 }
351 free_tokens = psi_plist_add(free_tokens, &tok);
352 } else {
353 arg_tokens[arg_index] = psi_plist_add(arg_tokens[arg_index], &tok);
354 }
355 break;
356 case PSI_T_COMMA:
357 if (1 == (lparens - rparens)) {
358 /* too many commas? */
359 if (++arg_index >= arg_count) {
360 goto fail;
361 }
362 free_tokens = psi_plist_add(free_tokens, &tok);
363 /* next arg */
364 arg_tokens[arg_index] = psi_plist_init(NULL);
365 } else {
366 arg_tokens[arg_index] = psi_plist_add(arg_tokens[arg_index], &tok);
367 }
368 break;
369 default:
370 arg_tokens[arg_index] = psi_plist_add(arg_tokens[arg_index], &tok);
371 }
372 }
373
374 psi_plist_free(free_tokens);
375 return arg_tokens;
376
377 fail:
378 psi_cpp_tokiter_free_call_tokens(arg_tokens, arg_count, false);
379 return NULL;
380 }
381
382 static void psi_cpp_tokiter_expand_call_tokens(struct psi_cpp *cpp,
383 struct psi_token *target, struct psi_cpp_macro_decl *macro,
384 struct psi_plist **arg_tokens_list)
385 {
386 size_t i;
387 struct psi_token *tok;
388 struct psi_plist *tokens = psi_plist_init(NULL);
389
390 for (i = 0; psi_plist_get(macro->tokens, i, &tok); ++i) {
391 struct psi_plist *arg_tokens = NULL;
392
393 if (tok->type == PSI_T_NAME) {
394 size_t s;
395 struct psi_token *arg_name;
396
397 for (s = 0; psi_plist_get(macro->sig, s, &arg_name); ++s) {
398 if (arg_name->size == tok->size) {
399 if (!memcmp(arg_name->text, tok->text, tok->size)) {
400 arg_tokens = arg_tokens_list[s];
401 break;
402 }
403 }
404 }
405 }
406
407 if (arg_tokens) {
408 tokens = psi_plist_add_r(tokens, psi_plist_count(arg_tokens), psi_plist_eles(arg_tokens));
409 } else {
410 tokens = psi_plist_add(tokens, &tok);
411 }
412 }
413
414 psi_cpp_tokiter_expand_tokens(cpp, target, tokens);
415 psi_plist_free(tokens);
416 }
417
418 static bool psi_cpp_tokiter_expand_call(struct psi_cpp *cpp,
419 struct psi_token *target, struct psi_cpp_macro_decl *macro)
420 {
421 /* function-like macro
422 * #define FOO(a,b) a>b // macro->sig == {a, b}, macro->tokens = {a, >, b}
423 * # if FOO(1,2) // expands to if 1 > 2
424 */
425 size_t start = psi_cpp_tokiter_index(cpp);
426 struct psi_plist **arg_tokens_list;
427
428 /* read in tokens, until we have balanced parens */
429 arg_tokens_list = psi_cpp_tokiter_read_call_tokens(cpp, psi_plist_count(macro->sig));
430 if (!arg_tokens_list) {
431 psi_cpp_tokiter_seek(cpp, start);
432 return false;
433 }
434
435 /* ditch arg tokens */
436 psi_cpp_tokiter_del_range(cpp, start, psi_cpp_tokiter_index(cpp) - start + 1, false);
437 psi_cpp_tokiter_seek(cpp, start);
438
439 /* insert and expand macro tokens */
440 psi_cpp_tokiter_expand_call_tokens(cpp, target, macro, arg_tokens_list);
441 psi_cpp_tokiter_free_call_tokens(arg_tokens_list, psi_plist_count(macro->sig), true);
442
443 /* back to where we took off */
444 psi_cpp_tokiter_seek(cpp, start);
445
446 free(target);
447 ++cpp->expanded;
448 return true;
449 }
450
451 static bool psi_cpp_tokiter_expand_def(struct psi_cpp *cpp,
452 struct psi_token *target, struct psi_cpp_macro_decl *macro)
453 {
454 size_t index = psi_cpp_tokiter_index(cpp);
455
456 /* delete current token from stream */
457 psi_cpp_tokiter_del_cur(cpp, false);
458
459 if (index != psi_cpp_tokiter_index(cpp)) {
460 /* might have been last token */
461 psi_cpp_tokiter_next(cpp);
462 }
463 /* replace with tokens from macro */
464 psi_cpp_tokiter_expand_tokens(cpp, target, macro->tokens);
465
466 free(target);
467 ++cpp->expanded;
468 return true;
469 }
470
471 static inline int psi_cpp_tokiter_expand_cmp(struct psi_token *t,
472 struct psi_cpp_macro_decl *m)
473 {
474 if (psi_plist_count(m->tokens) == 1) {
475 struct psi_token *r;
476
477 psi_plist_get(m->tokens, 0, &r);
478
479 return strcmp(r->text, t->text);
480 }
481 return -1;
482 }
483
484 bool psi_cpp_tokiter_expand(struct psi_cpp *cpp)
485 {
486 if (psi_cpp_tokiter_valid(cpp)) {
487 struct psi_token *current = psi_cpp_tokiter_current(cpp);
488
489 if (current) {
490 struct psi_cpp_macro_decl *macro = zend_hash_str_find_ptr(
491 &cpp->defs, current->text, current->size);
492
493 /* don't expand itself */
494 if (macro && macro->token != current) {
495 #if PSI_CPP_DEBUG
496 fprintf(stderr, "PSI: CPP expand < ");
497 psi_token_dump(2, current);
498 #endif
499 if (macro->sig) {
500 return psi_cpp_tokiter_expand_call(cpp, current, macro);
501 } else if (psi_cpp_tokiter_expand_cmp(current, macro)) {
502 return psi_cpp_tokiter_expand_def(cpp, current, macro);
503 }
504 }
505 }
506 }
507 return false;
508 }