cpp: fix relative includes
[m6w6/ext-psi] / src / types / cpp_exp.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 <assert.h>
29
30 #include "data.h"
31 #include "cpp.h"
32
33 struct psi_cpp_exp *psi_cpp_exp_init(token_t type, void *data)
34 {
35 struct psi_cpp_exp *exp = calloc(1, sizeof(*exp));
36
37 switch ((exp->type = type)) {
38 case PSI_T_WARNING:
39 case PSI_T_ERROR:
40 case PSI_T_UNDEF:
41 case PSI_T_IFDEF:
42 case PSI_T_IFNDEF:
43 case PSI_T_IMPORT:
44 case PSI_T_INCLUDE:
45 case PSI_T_INCLUDE_NEXT:
46 exp->data.tok = data;
47 break;
48 case PSI_T_DEFINE:
49 exp->data.decl = data;
50 break;
51 case PSI_T_IF:
52 case PSI_T_ELIF:
53 exp->data.num = data;
54 break;
55 case PSI_T_ENDIF:
56 case PSI_T_ELSE:
57 case PSI_T_PRAGMA_ONCE:
58 break;
59 default:
60 assert(0);
61 break;
62 }
63
64 return exp;
65 }
66
67 void psi_cpp_exp_free(struct psi_cpp_exp **exp_ptr)
68 {
69 if (*exp_ptr) {
70 struct psi_cpp_exp *exp = *exp_ptr;
71
72 *exp_ptr = NULL;
73 switch (exp->type) {
74 case PSI_T_WARNING:
75 case PSI_T_ERROR:
76 if (!exp->data.tok) {
77 break;
78 }
79 /* no break */
80 case PSI_T_UNDEF:
81 case PSI_T_IFDEF:
82 case PSI_T_IFNDEF:
83 case PSI_T_IMPORT:
84 case PSI_T_INCLUDE:
85 case PSI_T_INCLUDE_NEXT:
86 free(exp->data.tok);
87 exp->data.tok = NULL;
88 break;
89 case PSI_T_DEFINE:
90 psi_cpp_macro_decl_free(&exp->data.decl);
91 break;
92 case PSI_T_IF:
93 case PSI_T_ELIF:
94 psi_num_exp_free(&exp->data.num);
95 break;
96 case PSI_T_ENDIF:
97 case PSI_T_ELSE:
98 case PSI_T_PRAGMA_ONCE:
99 break;
100 default:
101 assert(0);
102 break;
103 }
104 if (exp->token) {
105 free(exp->token);
106 }
107 free(exp);
108 }
109 }
110
111 void psi_cpp_exp_dump(int fd, struct psi_cpp_exp *exp)
112 {
113 dprintf(fd, "#%s ", exp->token->text);
114 switch (exp->type) {
115 case PSI_T_WARNING:
116 case PSI_T_ERROR:
117 if (!exp->data.tok) {
118 break;
119 }
120 /* no break */
121 case PSI_T_UNDEF:
122 case PSI_T_IFDEF:
123 case PSI_T_IFNDEF:
124 dprintf(fd, "%s", exp->data.tok->text);
125 break;
126 case PSI_T_IMPORT:
127 case PSI_T_INCLUDE:
128 case PSI_T_INCLUDE_NEXT:
129 if (exp->data.tok->type == PSI_T_CPP_HEADER) {
130 dprintf(fd, "<%s>", exp->data.tok->text);
131 } else {
132 dprintf(fd, "\"%s\"", exp->data.tok->text);
133 }
134 break;
135 case PSI_T_DEFINE:
136 psi_cpp_macro_decl_dump(fd, exp->data.decl);
137 break;
138 case PSI_T_IF:
139 case PSI_T_ELIF:
140 psi_num_exp_dump(fd, exp->data.num);
141 break;
142 case PSI_T_ENDIF:
143 case PSI_T_ELSE:
144 case PSI_T_PRAGMA_ONCE:
145 break;
146 default:
147 assert(0);
148 break;
149 }
150 dprintf(fd, "\n");
151 }
152
153
154 static inline bool psi_cpp_level_skipped(struct psi_cpp *cpp)
155 {
156 return cpp->skip == cpp->level;
157 }
158
159 static inline void psi_cpp_level_skip(struct psi_cpp *cpp)
160 {
161 assert(!cpp->skip);
162 cpp->skip = cpp->level;
163 }
164
165 static inline void psi_cpp_level_unskip(struct psi_cpp *cpp)
166 {
167 if (psi_cpp_level_skipped(cpp)) {
168 cpp->skip = 0;
169 }
170 }
171
172 static inline bool psi_cpp_level_masked(struct psi_cpp *cpp)
173 {
174 return cpp->seen & (1 << cpp->level);
175 }
176
177 static inline void psi_cpp_level_mask(struct psi_cpp *cpp)
178 {
179 assert(!psi_cpp_level_masked(cpp));
180 cpp->seen |= (1 << cpp->level);
181 }
182
183 static inline void psi_cpp_level_unmask(struct psi_cpp *cpp)
184 {
185 cpp->seen &= ~(1 << cpp->level);
186 }
187
188 void psi_cpp_exp_exec(struct psi_cpp_exp *exp, struct psi_cpp *cpp, struct psi_data *D)
189 {
190 PSI_DEBUG_PRINT(D, "PSI: CPP EVAL < %s (level=%u, skip=%u)\n",
191 exp->token->text, cpp->level, cpp->skip);
192
193 #if PSI_CPP_DEBUG
194 psi_cpp_exp_dump(2, exp);
195 #endif
196
197 switch (exp->type) {
198 case PSI_T_ERROR:
199 if (!cpp->skip) {
200 D->error(D, exp->token, PSI_ERROR, "%s",
201 exp->data.tok ? exp->data.tok->text : "");
202 }
203 break;
204 case PSI_T_WARNING:
205 if (!cpp->skip) {
206 D->error(D, exp->token, PSI_WARNING, "%s",
207 exp->data.tok ? exp->data.tok->text : "");
208 }
209 break;
210 case PSI_T_UNDEF:
211 if (!cpp->skip) {
212 psi_cpp_undef(cpp, exp->data.tok);
213 }
214 break;
215 case PSI_T_DEFINE:
216 if (!cpp->skip) {
217 psi_cpp_define(cpp, exp->data.decl);
218 /* FIXME: copy */
219 exp->data.decl = NULL;
220 }
221 break;
222 case PSI_T_IFDEF:
223 ++cpp->level;
224 if (!cpp->skip) {
225 if (psi_cpp_defined(cpp, exp->data.tok)) {
226 psi_cpp_level_mask(cpp);
227 } else {
228 psi_cpp_level_skip(cpp);
229 }
230 }
231 break;
232 case PSI_T_IFNDEF:
233 ++cpp->level;
234 if (!cpp->skip) {
235 if (psi_cpp_defined(cpp, exp->data.tok)) {
236 psi_cpp_level_skip(cpp);
237 } else {
238 psi_cpp_level_mask(cpp);
239 }
240 }
241 break;
242 case PSI_T_IF:
243 ++cpp->level;
244 if (!cpp->skip) {
245 if (psi_cpp_if(cpp, exp)) {
246 psi_cpp_level_mask(cpp);
247 } else {
248 psi_cpp_level_skip(cpp);
249 }
250 }
251 break;
252 case PSI_T_ENDIF:
253 if (!cpp->level) {
254 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #endif");
255 } else {
256 psi_cpp_level_unskip(cpp);
257 psi_cpp_level_unmask(cpp);
258 --cpp->level;
259 }
260 break;
261 case PSI_T_ELSE:
262 /* FIXME: catch "else" after "else" */
263 if (!cpp->level) {
264 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #else");
265 } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
266 /*
267 * if skip is set on this level and the level has
268 * not been masked yet, then unskip and mask this level
269 */
270 psi_cpp_level_unskip(cpp);
271 psi_cpp_level_mask(cpp);
272 } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
273 /*
274 * previous block masked this level
275 */
276 psi_cpp_level_skip(cpp);
277 } else {
278 assert(cpp->skip <= cpp->level);
279 }
280 break;
281 case PSI_T_ELIF:
282 if (!cpp->level) {
283 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #elif");
284 } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
285 /*
286 * if skip is set on this level and the level has
287 * not been masked yet, then unskip and mask this
288 * level, if the condition evals truthy
289 */
290 if (psi_cpp_if(cpp, exp)) {
291 psi_cpp_level_unskip(cpp);
292 psi_cpp_level_mask(cpp);
293 }
294 } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
295 /*
296 * previous block masked this level
297 */
298 psi_cpp_level_skip(cpp);
299 } else {
300 assert(cpp->skip <= cpp->level);
301 }
302 break;
303 case PSI_T_INCLUDE:
304 if (!cpp->skip) {
305 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE)) {
306 D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text);
307 }
308 }
309 break;
310 case PSI_T_INCLUDE_NEXT:
311 if (!cpp->skip) {
312 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_NEXT)) {
313 D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text);
314 }
315 }
316 break;
317 case PSI_T_IMPORT:
318 if (!cpp->skip) {
319 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_ONCE)) {
320 D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text);
321 }
322 }
323 break;
324 case PSI_T_PRAGMA_ONCE:
325 if (!cpp->skip) {
326 zend_hash_str_add_empty_element(&cpp->once, exp->token->file, strlen(exp->token->file));
327 }
328 break;
329 default:
330 assert(0);
331 break;
332 }
333
334 PSI_DEBUG_PRINT(D, "PSI: CPP EVAL > %s (level=%u, skip=%u)\n",
335 exp->token->text, cpp->level, cpp->skip);
336 }