0c64246098ae8ba42844bea3e52170a8365fa329
1 /*******************************************************************************
2 Copyright (c) 2017, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
31 #define PSI_CPP_SEARCH
32 #define PSI_CPP_PREDEF
33 #include "php_psi_cpp.h"
35 static void free_cpp_def(zval
*p
)
37 if (Z_TYPE_P(p
) == IS_PTR
) {
38 psi_cpp_macro_decl_free((void *) &Z_PTR_P(p
));
42 struct psi_cpp
*psi_cpp_init(struct psi_parser
*P
)
44 struct psi_cpp
*cpp
= calloc(1, sizeof(*cpp
));
47 ALLOC_HASHTABLE(cpp
->defs
);
48 zend_hash_init(cpp
->defs
, 0, NULL
, free_cpp_def
, 1);
53 bool psi_cpp_load_defaults(struct psi_cpp
*cpp
)
55 struct psi_parser_input
*predef
;
57 if ((predef
= psi_parser_open_string(cpp
->parser
, psi_cpp_predef
, sizeof(psi_cpp_predef
) - 1))) {
58 bool parsed
= psi_parser_parse(cpp
->parser
, predef
);
66 static int dump_def(zval
*p
)
68 struct psi_cpp_macro_decl
*decl
= Z_PTR_P(p
);
71 dprintf(2, "#define ");
72 psi_cpp_macro_decl_dump(2, decl
);
75 return ZEND_HASH_APPLY_KEEP
;
78 void psi_cpp_free(struct psi_cpp
**cpp_ptr
)
81 struct psi_cpp
*cpp
= *cpp_ptr
;
84 if (cpp
->parser
->flags
& PSI_DEBUG
) {
85 fprintf(stderr
, "PSI: CPP decls:\n");
86 zend_hash_apply(cpp
->defs
, dump_def
);
88 zend_hash_destroy(cpp
->defs
);
89 FREE_HASHTABLE(cpp
->defs
);
94 static bool psi_cpp_stage1(struct psi_cpp
*cpp
)
96 bool name
= false, define
= false, hash
= false, eol
= true, esc
= false, ws
= false;
98 psi_cpp_tokiter_reset(cpp
);
99 while (psi_cpp_tokiter_valid(cpp
)) {
100 struct psi_token
*token
= psi_cpp_tokiter_current(cpp
);
103 if (token
->type
== PSI_T_COMMENT
) {
104 psi_cpp_tokiter_del_cur(cpp
, true);
108 /* line continuations */
109 if (token
->type
== PSI_T_EOL
) {
111 psi_cpp_tokiter_del_range(cpp
, psi_cpp_tokiter_index(cpp
) - 1, 2, true);
112 psi_cpp_tokiter_prev(cpp
);
116 } else if (token
->type
== PSI_T_BSLASH
) {
122 /* this whole turf is needed to distinct between:
123 * #define foo (1,2,3)
127 if (token
->type
== PSI_T_WHITESPACE
) {
129 psi_cpp_tokiter_del_cur(cpp
, true);
133 switch (token
->type
) {
159 /* mask special token for parser */
160 struct psi_token
*no_ws
= psi_token_copy(token
);
162 no_ws
->type
= PSI_T_NO_WHITESPACE
;
163 no_ws
->text
[0] = '\xA0';
164 psi_cpp_tokiter_ins_cur(cpp
, no_ws
);
170 name
= define
= hash
= eol
= false;
175 psi_cpp_tokiter_next(cpp
);
181 static bool psi_cpp_stage2(struct psi_cpp
*cpp
)
183 struct psi_plist
*parser_tokens
= psi_plist_init((psi_plist_dtor
) psi_token_free
);
186 bool is_eol
= true, do_cpp
= false, do_expansion
= true, skip_paren
= false, skip_all
= false;
188 psi_cpp_tokiter_reset(cpp
);
190 while (psi_cpp_tokiter_valid(cpp
)) {
191 struct psi_token
*current
= psi_cpp_tokiter_current(cpp
);
193 if (current
->type
== PSI_T_HASH
) {
198 } else if (current
->type
== PSI_T_EOL
) {
200 fprintf(stderr
, "PSI: CPP do_expansion=true, PSI_T_EOL\n");
206 psi_cpp_tokiter_del_cur(cpp
, true);
213 switch (current
->type
) {
216 fprintf(stderr
, "PSI: CPP do_expansion=false, PSI_T_DEFINE, skip_all\n");
218 do_expansion
= false;
228 fprintf(stderr
, "PSI: CPP do_expansion=false, PSI_T_{IF{,N},UN}DEF\n");
230 do_expansion
= false;
240 fprintf(stderr
, "PSI: CPP do_expansion=true, PSI_T_LPAREN, !skip_all, !skip_paren\n");
248 do_expansion
= !skip_all
;
250 fprintf(stderr
, "PSI: CPP do_expansion=%s, <- !skip_all\n", do_expansion
?"true":"false");
257 /* FIXME: del_range */
260 fprintf(stderr
, "PSI: CPP skip ");
261 psi_token_dump(2, current
);
263 psi_cpp_tokiter_del_cur(cpp
, true);
268 if (do_expansion
&& current
->type
== PSI_T_NAME
&& psi_cpp_tokiter_defined(cpp
)) {
269 bool expanded
= false;
271 while (psi_cpp_tokiter_expand(cpp
)) {
280 parser_tokens
= psi_plist_add(parser_tokens
, ¤t
);
281 psi_cpp_tokiter_del_cur(cpp
, false);
284 size_t processed
= 0;
286 if (!psi_parser_process(cpp
->parser
, parser_tokens
, &processed
)) {
287 psi_plist_free(parser_tokens
);
290 psi_plist_clean(parser_tokens
);
294 #if PSI_CPP_DEBUG > 1
295 psi_cpp_tokiter_dump(2, cpp
);
301 psi_cpp_tokiter_next(cpp
);
303 } while (cpp
->expanded
);
305 psi_plist_free(parser_tokens
);
310 bool psi_cpp_process(struct psi_cpp
*cpp
, struct psi_plist
**tokens
)
312 struct psi_cpp temp
= *cpp
;
314 temp
.tokens
= *tokens
;
316 if (psi_cpp_stage1(&temp
) && psi_cpp_stage2(&temp
)) {
317 *tokens
= temp
.tokens
;
321 *tokens
= temp
.tokens
;
325 bool psi_cpp_defined(struct psi_cpp
*cpp
, struct psi_token
*tok
)
329 if (tok
->type
== PSI_T_NAME
) {
330 defined
= zend_hash_str_exists(cpp
->defs
, tok
->text
, tok
->size
);
336 fprintf(stderr
, "PSI: CPP defined -> %s ", defined
? "true" : "false");
337 psi_token_dump(2, tok
);
343 void psi_cpp_define(struct psi_cpp
*cpp
, struct psi_cpp_macro_decl
*decl
)
345 struct psi_cpp_macro_decl
*old
= zend_hash_str_find_ptr(cpp
->defs
, decl
->token
->text
, decl
->token
->size
);
347 if (old
&& !psi_cpp_macro_decl_equal(old
, decl
)) {
348 cpp
->parser
->error(PSI_DATA(cpp
->parser
), decl
->token
, PSI_WARNING
,
349 "'%s' redefined", decl
->token
->text
);
350 cpp
->parser
->error(PSI_DATA(cpp
->parser
), old
->token
, PSI_WARNING
,
351 "'%s' previously defined", old
->token
->text
);
353 zend_hash_str_update_ptr(cpp
->defs
, decl
->token
->text
, decl
->token
->size
, decl
);
356 bool psi_cpp_undef(struct psi_cpp
*cpp
, struct psi_token
*tok
)
358 return SUCCESS
== zend_hash_str_del(cpp
->defs
, tok
->text
, tok
->size
);
361 bool psi_cpp_if(struct psi_cpp
*cpp
, struct psi_cpp_exp
*exp
)
363 if (!psi_num_exp_validate(PSI_DATA(cpp
->parser
), exp
->data
.num
, NULL
, NULL
, NULL
, NULL
, NULL
)) {
366 if (!psi_long_num_exp(exp
->data
.num
, NULL
, cpp
->defs
)) {
372 static inline bool try_include(struct psi_cpp
*cpp
, const char *path
, bool *parsed
)
374 struct psi_parser_input
*include
;
376 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP include trying %s\n", path
);
378 include
= psi_parser_open_file(cpp
->parser
, path
, false);
380 struct psi_plist
*tokens
;
382 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP include scanning %s\n", path
);
384 tokens
= psi_parser_scan(cpp
->parser
, include
);
386 if ((*parsed
= psi_cpp_process(cpp
, &tokens
))) {
387 psi_cpp_tokiter_ins_range(cpp
, psi_cpp_tokiter_index(cpp
),
388 psi_plist_count(tokens
), psi_plist_eles(tokens
));
390 psi_plist_free(tokens
);
398 bool psi_cpp_include(struct psi_cpp
*cpp
, const char *file
, unsigned flags
)
402 int f_len
= strlen(file
) - 2;
404 if (file
[1] == '/' && PATH_MAX
> snprintf(path
, PATH_MAX
, "%.*s", f_len
, file
+ 1)) {
405 return try_include(cpp
, path
, &parsed
) && parsed
;
409 if ((flags
& PSI_CPP_INCLUDE_NEXT
) && cpp
->search
) {
410 if ((sep
= strchr(cpp
->search
, ':'))) {
411 cpp
->search
= sep
+ 1;
413 cpp
->search
+= strlen(cpp
->search
); /* point to end of string */
416 if (!(flags
& PSI_CPP_INCLUDE_NEXT
) || !cpp
->search
) {
417 cpp
->search
= &psi_cpp_search
[0];
423 sep
= strchr(cpp
->search
, ':');
424 d_len
= sep
? sep
- cpp
->search
: strlen(cpp
->search
);
426 if (PATH_MAX
> snprintf(path
, PATH_MAX
, "%.*s/%.*s", d_len
, cpp
->search
, f_len
, file
+ 1)) {
427 if (try_include(cpp
, path
, &parsed
)) {
431 cpp
->search
= sep
+ 1;