68fec6ec48d3138a21ee131650580e6b1854a67c
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 *******************************************************************************/
29 # include "php_config.h"
38 struct psi_cpp_exp
*psi_cpp_exp_init(token_t type
, void *data
)
40 struct psi_cpp_exp
*exp
= pecalloc(1, sizeof(*exp
), 1);
42 switch ((exp
->type
= type
)) {
50 case PSI_T_INCLUDE_NEXT
:
55 exp
->data
.decl
= data
;
72 void psi_cpp_exp_free(struct psi_cpp_exp
**exp_ptr
)
75 struct psi_cpp_exp
*exp
= *exp_ptr
;
86 case PSI_T_INCLUDE_NEXT
:
87 psi_token_free(&exp
->data
.tok
);
91 psi_cpp_macro_decl_free(&exp
->data
.decl
);
95 psi_num_exp_free(&exp
->data
.num
);
104 psi_token_free(&exp
->token
);
109 void psi_cpp_exp_dump(struct psi_dump
*dump
, struct psi_cpp_exp
*exp
)
111 PSI_DUMP(dump
, "#%s ", exp
->token
->text
->val
);
115 if (!exp
->data
.tok
) {
122 PSI_DUMP(dump
, "%s", exp
->data
.tok
->text
->val
);
126 case PSI_T_INCLUDE_NEXT
:
127 if (exp
->data
.tok
->type
== PSI_T_CPP_HEADER
) {
128 PSI_DUMP(dump
, "<%s>", exp
->data
.tok
->text
->val
);
130 PSI_DUMP(dump
, "\"%s\"", exp
->data
.tok
->text
->val
);
135 psi_cpp_macro_decl_dump(dump
, exp
->data
.decl
);
139 psi_num_exp_dump(dump
, exp
->data
.num
);
148 PSI_DUMP(dump
, "\n");
152 static inline bool psi_cpp_level_skipped(struct psi_cpp
*cpp
)
154 return cpp
->skip
== cpp
->level
;
157 static inline void psi_cpp_level_skip(struct psi_cpp
*cpp
)
160 cpp
->skip
= cpp
->level
;
163 static inline void psi_cpp_level_unskip(struct psi_cpp
*cpp
)
165 if (psi_cpp_level_skipped(cpp
)) {
170 static inline bool psi_cpp_level_masked(struct psi_cpp
*cpp
)
172 return cpp
->seen
& (1 << cpp
->level
);
175 static inline void psi_cpp_level_mask(struct psi_cpp
*cpp
)
177 assert(!psi_cpp_level_masked(cpp
));
178 cpp
->seen
|= (1 << cpp
->level
);
181 static inline void psi_cpp_level_unmask(struct psi_cpp
*cpp
)
183 cpp
->seen
&= ~(1 << cpp
->level
);
186 void psi_cpp_exp_exec(struct psi_cpp_exp
*exp
, struct psi_cpp
*cpp
, struct psi_data
*D
)
188 PSI_DEBUG_PRINT(D
, "PSI: CPP EVAL < %s (level=%u, skip=%u)\n",
189 exp
->token
->text
->val
, cpp
->level
, cpp
->skip
);
192 PSI_DEBUG_PRINT(cpp
->parser
, "PSI: CPP exec -> ");
193 PSI_DEBUG_DUMP(cpp
->parser
, psi_cpp_exp_dump
, exp
);
199 D
->error(D
, exp
->token
, PSI_ERROR
, "%s",
200 exp
->data
.tok
? exp
->data
.tok
->text
->val
: "");
205 D
->error(D
, exp
->token
, PSI_WARNING
, "%s",
206 exp
->data
.tok
? exp
->data
.tok
->text
->val
: "");
211 psi_cpp_undef(cpp
, exp
->data
.tok
);
216 psi_cpp_define(cpp
, exp
->data
.decl
);
218 exp
->data
.decl
= NULL
;
223 psi_cpp_pragma(cpp
, exp
->data
.decl
);
229 if (psi_cpp_defined(cpp
, exp
->data
.tok
)) {
230 psi_cpp_level_mask(cpp
);
232 psi_cpp_level_skip(cpp
);
239 if (psi_cpp_defined(cpp
, exp
->data
.tok
)) {
240 psi_cpp_level_skip(cpp
);
242 psi_cpp_level_mask(cpp
);
249 if (psi_cpp_if(cpp
, exp
)) {
250 psi_cpp_level_mask(cpp
);
252 psi_cpp_level_skip(cpp
);
258 D
->error(D
, exp
->token
, PSI_WARNING
, "Ingoring lone #endif");
260 psi_cpp_level_unskip(cpp
);
261 psi_cpp_level_unmask(cpp
);
266 /* FIXME: catch "else" after "else" */
268 D
->error(D
, exp
->token
, PSI_WARNING
, "Ingoring lone #else");
269 } else if (psi_cpp_level_skipped(cpp
) && !psi_cpp_level_masked(cpp
)) {
271 * if skip is set on this level and the level has
272 * not been masked yet, then unskip and mask this level
274 psi_cpp_level_unskip(cpp
);
275 psi_cpp_level_mask(cpp
);
276 } else if (!cpp
->skip
&& psi_cpp_level_masked(cpp
)) {
278 * previous block masked this level
280 psi_cpp_level_skip(cpp
);
282 assert(cpp
->skip
<= cpp
->level
);
287 D
->error(D
, exp
->token
, PSI_WARNING
, "Ingoring lone #elif");
288 } else if (psi_cpp_level_skipped(cpp
) && !psi_cpp_level_masked(cpp
)) {
290 * if skip is set on this level and the level has
291 * not been masked yet, then unskip and mask this
292 * level, if the condition evals truthy
294 if (psi_cpp_if(cpp
, exp
)) {
295 psi_cpp_level_unskip(cpp
);
296 psi_cpp_level_mask(cpp
);
298 } else if (!cpp
->skip
&& psi_cpp_level_masked(cpp
)) {
300 * previous block masked this level
302 psi_cpp_level_skip(cpp
);
304 assert(cpp
->skip
<= cpp
->level
);
309 if (!psi_cpp_include(cpp
, exp
->data
.tok
, PSI_CPP_INCLUDE
)) {
310 D
->error(D
, exp
->token
, PSI_WARNING
, "Failed to include %s: %s",
311 exp
->data
.tok
->text
->val
, strerror(errno
));
315 case PSI_T_INCLUDE_NEXT
:
317 if (!psi_cpp_include(cpp
, exp
->data
.tok
, PSI_CPP_INCLUDE_NEXT
)) {
318 D
->error(D
, exp
->token
, PSI_WARNING
, "Failed to include next %s: %s",
319 exp
->data
.tok
->text
->val
, strerror(errno
));
325 if (!psi_cpp_include(cpp
, exp
->data
.tok
, PSI_CPP_INCLUDE_ONCE
)) {
326 D
->error(D
, exp
->token
, PSI_WARNING
, "Failed to include once %s: %s",
327 exp
->data
.tok
->text
->val
, strerror(errno
));
336 PSI_DEBUG_PRINT(D
, "PSI: CPP EVAL > %s (level=%u, skip=%u)\n",
337 exp
->token
->text
->val
, cpp
->level
, cpp
->skip
);