fix access of stat.st_mtime
[m6w6/ext-psi] / src / data.c
1 /*******************************************************************************
2 Copyright (c) 2016, 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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31 #include "php_psi.h"
32 #include "data.h"
33
34 #include "php_globals.h"
35 #include "zend_types.h"
36
37 #include <dlfcn.h>
38 #include <ctype.h>
39
40 #if PSI_THREADED_PARSER
41 # include <pthread.h>
42
43 static pthread_mutex_t psi_string_mutex = PTHREAD_MUTEX_INITIALIZER;
44
45 zend_string *psi_string_init_interned(const char *buf, size_t len, int p)
46 {
47 zend_string *str;
48
49 pthread_mutex_lock(&psi_string_mutex);
50 str = zend_string_init_interned(buf, len, p);
51 pthread_mutex_unlock(&psi_string_mutex);
52
53 return str;
54 }
55
56 zend_string *psi_new_interned_string(zend_string *str)
57 {
58 zend_string *new_str;
59
60 pthread_mutex_lock(&psi_string_mutex);
61 new_str = zend_new_interned_string(str);
62 pthread_mutex_unlock(&psi_string_mutex);
63
64 return new_str;
65 }
66
67 #endif
68
69 static void psi_data_ctor_internal(struct psi_data *data,
70 psi_error_cb error, unsigned flags)
71 {
72 data->error = error;
73 data->flags = flags;
74
75 if (data->flags & PSI_DEBUG) {
76 int fd = psi_fdopen(getenv("PSI_DEBUG"));
77
78 if (fd > 0) {
79 data->debug_fd = fd;
80 } else {
81 data->debug_fd = STDERR_FILENO;
82 }
83 }
84 }
85
86 struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data,
87 psi_error_cb error, unsigned flags)
88 {
89 if (!data) {
90 data = pecalloc(1, sizeof(*data), 1);
91 }
92
93 psi_data_ctor_internal(data, error, flags);
94
95 if (!data->file.libnames) {
96 data->file.libnames = psi_plist_init((psi_plist_dtor) psi_names_free);
97 }
98 if (!data->file.dlopened) {
99 data->file.dlopened = psi_plist_init((psi_plist_dtor) psi_libs_free);
100 }
101
102 if (!data->consts) {
103 data->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
104 }
105 if (!data->types) {
106 data->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
107 }
108 if (!data->structs) {
109 data->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
110 }
111 if (!data->unions) {
112 data->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
113 }
114 if (!data->enums) {
115 data->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
116 }
117 if (!data->decls) {
118 data->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
119 }
120 if (!data->vars) {
121 data->vars = psi_plist_init((psi_plist_dtor) psi_decl_extvar_free);
122 }
123 if (!data->impls) {
124 data->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
125 }
126 return data;
127 }
128
129 struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error,
130 unsigned flags)
131 {
132 if (!data) {
133 data = pecalloc(1, sizeof(*data), 1);
134 }
135
136 psi_data_ctor_internal(data, error, flags);
137
138 if (!data->file.libnames) {
139 data->file.libnames = psi_plist_init(NULL);
140 }
141 if (!data->file.dlopened) {
142 data->file.dlopened = psi_plist_init(NULL);
143 }
144
145 if (!data->consts) {
146 data->consts = psi_plist_init(NULL);
147 }
148 if (!data->types) {
149 data->types = psi_plist_init(NULL);
150 }
151 if (!data->structs) {
152 data->structs = psi_plist_init(NULL);
153 }
154 if (!data->unions) {
155 data->unions = psi_plist_init(NULL);
156 }
157 if (!data->enums) {
158 data->enums = psi_plist_init(NULL);
159 }
160 if (!data->decls) {
161 data->decls = psi_plist_init(NULL);
162 }
163 if (!data->vars) {
164 data->vars = psi_plist_init(NULL);
165 }
166 if (!data->impls) {
167 data->impls = psi_plist_init(NULL);
168 }
169 return data;
170 }
171
172 struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src)
173 {
174 if (!dest) {
175 dest = pemalloc(sizeof(*dest), 1);
176 }
177 *dest = *src;
178 memset(src, 0, sizeof(*src));
179 return dest;
180 }
181
182 void psi_data_dtor(struct psi_data *data)
183 {
184 if (data->debug_fd) {
185 close(data->debug_fd);
186 }
187 if (data->consts) {
188 psi_plist_free(data->consts);
189 }
190 if (data->types) {
191 psi_plist_free(data->types);
192 }
193 if (data->structs) {
194 psi_plist_free(data->structs);
195 }
196 if (data->unions) {
197 psi_plist_free(data->unions);
198 }
199 if (data->enums) {
200 psi_plist_free(data->enums);
201 }
202 if (data->decls) {
203 psi_plist_free(data->decls);
204 }
205 if (data->vars) {
206 psi_plist_free(data->vars);
207 }
208 if (data->impls) {
209 psi_plist_free(data->impls);
210 }
211
212 psi_decl_file_dtor(&data->file);
213 }
214
215 void psi_data_dump(struct psi_dump *dump, struct psi_data *D)
216 {
217 size_t i = 0;
218 char *libname;
219
220 if (D->file.filename) {
221 PSI_DUMP(dump, "// filename=%s (%u errors)\n", D->file.filename->val, D->errors);
222 }
223 while (psi_plist_get(D->file.libnames, i++, &libname)) {
224 PSI_DUMP(dump, "lib \"%s\";\n", libname);
225 }
226 if (psi_plist_count(D->types)) {
227 size_t i = 0;
228 struct psi_decl_arg *def;
229
230 while (psi_plist_get(D->types, i++, &def)) {
231 PSI_DUMP(dump, "typedef ");
232 psi_decl_arg_dump(dump, def, 0);
233 PSI_DUMP(dump, ";\n");
234 }
235 PSI_DUMP(dump, "\n");
236 }
237 if (psi_plist_count(D->unions)) {
238 size_t i = 0;
239 struct psi_decl_union *unn;
240
241 while (psi_plist_get(D->unions, i++, &unn)) {
242 if (!psi_decl_type_is_anon(unn->name, "union")) {
243 psi_decl_union_dump(dump, unn);
244 PSI_DUMP(dump, "\n");
245 }
246 }
247 PSI_DUMP(dump, "\n");
248 }
249 if (psi_plist_count(D->structs)) {
250 size_t i = 0;
251 struct psi_decl_struct *strct;
252
253 while (psi_plist_get(D->structs, i++, &strct)) {
254 if (!psi_decl_type_is_anon(strct->name, "struct")) {
255 psi_decl_struct_dump(dump, strct);
256 PSI_DUMP(dump, "\n");
257 }
258 }
259 PSI_DUMP(dump, "\n");
260 }
261 if (psi_plist_count(D->enums)) {
262 size_t i = 0;
263 struct psi_decl_enum *enm;
264
265 while (psi_plist_get(D->enums, i++, &enm)) {
266 if (true || !psi_decl_type_is_anon(enm->name, "enum")) {
267 psi_decl_enum_dump(dump, enm, 0);
268 PSI_DUMP(dump, "\n");
269 }
270 }
271 PSI_DUMP(dump, "\n");
272 }
273 if (psi_plist_count(D->consts)) {
274 size_t i = 0;
275 struct psi_const *c;
276
277 while (psi_plist_get(D->consts, i++, &c)) {
278 psi_const_dump(dump, c);
279 PSI_DUMP(dump, "\n");
280 }
281 PSI_DUMP(dump, "\n");
282 }
283 if (psi_plist_count(D->decls)) {
284 size_t i = 0;
285 struct psi_decl *decl;
286
287 while (psi_plist_get(D->decls, i++, &decl)) {
288 if (decl->extvar) {
289 PSI_DUMP(dump, "/* extvar accessor \n");
290 }
291 psi_decl_dump(dump, decl);
292 if (decl->extvar) {
293 PSI_DUMP(dump, " */");
294 }
295 PSI_DUMP(dump, "\n");
296 }
297 PSI_DUMP(dump, "\n");
298 }
299 if (psi_plist_count(D->vars)) {
300 size_t i = 0;
301 struct psi_decl_extvar *evar;
302
303 while (psi_plist_get(D->vars, i++, &evar)) {
304 psi_decl_extvar_dump(dump, evar);
305 }
306 PSI_DUMP(dump, "\n");
307 }
308 if (psi_plist_count(D->impls)) {
309 size_t i = 0;
310 struct psi_impl *impl;
311
312 while (psi_plist_get(D->impls, i++, &impl)) {
313 psi_impl_dump(dump, impl);
314 PSI_DUMP(dump, "\n");
315 }
316 PSI_DUMP(dump, "\n");
317 }
318 }