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"
33 struct psi_cpp_exp
*psi_cpp_exp_init(token_t type
, void *data
)
35 struct psi_cpp_exp
*exp
= calloc(1, sizeof(*exp
));
37 switch ((exp
->type
= type
)) {
45 case PSI_T_INCLUDE_NEXT
:
49 exp
->data
.decl
= data
;
67 void psi_cpp_exp_free(struct psi_cpp_exp
**exp_ptr
)
70 struct psi_cpp_exp
*exp
= *exp_ptr
;
85 case PSI_T_INCLUDE_NEXT
:
90 psi_cpp_macro_decl_free(&exp
->data
.decl
);
94 psi_num_exp_free(&exp
->data
.num
);
111 void psi_cpp_exp_dump(int fd
, struct psi_cpp_exp
*exp
)
113 dprintf(fd
, "#%s ", exp
->token
->text
);
117 if (!exp
->data
.tok
) {
126 case PSI_T_INCLUDE_NEXT
:
127 dprintf(fd
, "%s", exp
->data
.tok
->text
);
130 psi_cpp_macro_decl_dump(fd
, exp
->data
.decl
);
134 psi_num_exp_dump(fd
, exp
->data
.num
);
148 static inline bool psi_cpp_level_skipped(struct psi_cpp
*cpp
)
150 return cpp
->skip
== cpp
->level
;
153 static inline void psi_cpp_level_skip(struct psi_cpp
*cpp
)
156 cpp
->skip
= cpp
->level
;
159 static inline void psi_cpp_level_unskip(struct psi_cpp
*cpp
)
161 if (psi_cpp_level_skipped(cpp
)) {
166 static inline bool psi_cpp_level_masked(struct psi_cpp
*cpp
)
168 return cpp
->seen
& (1 << cpp
->level
);
171 static inline void psi_cpp_level_mask(struct psi_cpp
*cpp
)
173 assert(!psi_cpp_level_masked(cpp
));
174 cpp
->seen
|= (1 << cpp
->level
);
177 static inline void psi_cpp_level_unmask(struct psi_cpp
*cpp
)
179 cpp
->seen
&= ~(1 << cpp
->level
);
182 void psi_cpp_exp_exec(struct psi_cpp_exp
*exp
, struct psi_cpp
*cpp
, struct psi_data
*D
)
184 PSI_DEBUG_PRINT(D
, "PSI: CPP EVAL < %s (level=%u, skip=%u)\n",
185 exp
->token
->text
, cpp
->level
, cpp
->skip
);
188 psi_cpp_exp_dump(2, exp
);
194 D
->error(D
, exp
->token
, PSI_ERROR
, "%s",
195 exp
->data
.tok
? exp
->data
.tok
->text
: "");
200 D
->error(D
, exp
->token
, PSI_WARNING
, "%s",
201 exp
->data
.tok
? exp
->data
.tok
->text
: "");
206 psi_cpp_undef(cpp
, exp
->data
.tok
);
211 psi_cpp_define(cpp
, exp
->data
.decl
);
213 exp
->data
.decl
= NULL
;
219 if (psi_cpp_defined(cpp
, exp
->data
.tok
)) {
220 psi_cpp_level_mask(cpp
);
222 psi_cpp_level_skip(cpp
);
229 if (psi_cpp_defined(cpp
, exp
->data
.tok
)) {
230 psi_cpp_level_skip(cpp
);
232 psi_cpp_level_mask(cpp
);
239 if (psi_cpp_if(cpp
, exp
)) {
240 psi_cpp_level_mask(cpp
);
242 psi_cpp_level_skip(cpp
);
248 D
->error(D
, exp
->token
, PSI_WARNING
, "Ingoring lone #endif");
250 psi_cpp_level_unskip(cpp
);
251 psi_cpp_level_unmask(cpp
);
256 /* FIXME: catch "else" after "else" */
258 D
->error(D
, exp
->token
, PSI_WARNING
, "Ingoring lone #else");
259 } else if (psi_cpp_level_skipped(cpp
) && !psi_cpp_level_masked(cpp
)) {
261 * if skip is set on this level and the level has
262 * not been masked yet, then unskip and mask this level
264 psi_cpp_level_unskip(cpp
);
265 psi_cpp_level_mask(cpp
);
266 } else if (!cpp
->skip
&& psi_cpp_level_masked(cpp
)) {
268 * previous block masked this level
270 psi_cpp_level_skip(cpp
);
272 assert(cpp
->skip
<= cpp
->level
);
277 D
->error(D
, exp
->token
, PSI_WARNING
, "Ingoring lone #elif");
278 } else if (psi_cpp_level_skipped(cpp
) && !psi_cpp_level_masked(cpp
)) {
280 * if skip is set on this level and the level has
281 * not been masked yet, then unskip and mask this
282 * level, if the condition evals truthy
284 if (psi_cpp_if(cpp
, exp
)) {
285 psi_cpp_level_unskip(cpp
);
286 psi_cpp_level_mask(cpp
);
288 } else if (!cpp
->skip
&& psi_cpp_level_masked(cpp
)) {
290 * previous block masked this level
292 psi_cpp_level_skip(cpp
);
294 assert(cpp
->skip
<= cpp
->level
);
299 if (!psi_cpp_include(cpp
, exp
->data
.tok
->text
, PSI_CPP_INCLUDE
)) {
300 D
->error(D
, exp
->token
, PSI_WARNING
, "Failed to include %s", exp
->data
.tok
->text
);
304 case PSI_T_INCLUDE_NEXT
:
306 if (!psi_cpp_include(cpp
, exp
->data
.tok
->text
, PSI_CPP_INCLUDE_NEXT
)) {
307 D
->error(D
, exp
->token
, PSI_WARNING
, "Failed to include %s", exp
->data
.tok
->text
);
313 if (!psi_cpp_include(cpp
, exp
->data
.tok
->text
, PSI_CPP_INCLUDE_ONCE
)) {
314 D
->error(D
, exp
->token
, PSI_WARNING
, "Failed to include %s", exp
->data
.tok
->text
);
319 zend_hash_str_add_empty_element(&cpp
->once
, exp
->token
->file
, strlen(exp
->token
->file
));
327 PSI_DEBUG_PRINT(D
, "PSI: CPP EVAL > %s (level=%u, skip=%u)\n",
328 exp
->token
->text
, cpp
->level
, cpp
->skip
);