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"
35 typedef bool (*psi_validate_list_entry
)(struct psi_validate_scope
*scope
,
36 struct psi_data
*dst
, void *ptr
);
38 struct psi_validate_list
{
39 struct psi_plist
*src
;
40 struct psi_plist
**dst
;
41 struct psi_plist
*cur
;
42 struct psi_plist
*chk
;
45 static inline void psi_validate_list(struct psi_validate_scope
*scope
,
46 struct psi_data
*dst
, struct psi_validate_list
*list
,
47 psi_validate_list_entry validate
)
50 list
->cur
= list
->src
;
53 if (psi_plist_count(list
->cur
)) {
57 list
->chk
= psi_plist_init(NULL
);
59 while (psi_plist_get(list
->cur
, i
++, &ptr
)) {
62 PSI_DEBUG_PRINT(dst
, "PSI: validate %s ", "»");
63 if (validate(scope
, dst
, ptr
)) {
64 PSI_DEBUG_PRINT(dst
, " %s\n", "✔");
65 *list
->dst
= psi_plist_add(*list
->dst
, &ptr
);
67 PSI_DEBUG_PRINT(dst
, " %s (%s)\n", "✘", dst
->last_error
);
68 list
->chk
= psi_plist_add(list
->chk
, &ptr
);
72 if (list
->cur
!= list
->src
) {
73 psi_plist_free(list
->cur
);
75 list
->cur
= list
->chk
;
79 static bool psi_validate_type(struct psi_validate_scope
*scope
,
80 struct psi_data
*dst
, void *ptr
)
82 struct psi_decl_arg
*def
= ptr
;
84 PSI_DEBUG_PRINT(dst
, "typedef %s", def
->var
->name
->val
);
85 return psi_decl_arg_validate_typedef(dst
, def
, scope
);
88 static bool psi_validate_struct(struct psi_validate_scope
*scope
,
89 struct psi_data
*dst
, void *ptr
)
91 struct psi_decl_struct
*str
= ptr
;
93 PSI_DEBUG_PRINT(dst
, "struct %s", str
->name
->val
);
94 if (psi_decl_struct_validate(dst
, str
, scope
)) {
95 PSI_DEBUG_PRINT(dst
, "::(%zu, %zu)", str
->align
, str
->size
);
101 static bool psi_validate_union(struct psi_validate_scope
*scope
,
102 struct psi_data
*dst
, void *ptr
)
104 struct psi_decl_union
*unn
= ptr
;
106 PSI_DEBUG_PRINT(dst
, "union %s", unn
->name
->val
);
107 if (psi_decl_union_validate(dst
, unn
, scope
)) {
108 PSI_DEBUG_PRINT(dst
, "::(%zu, %zu)", unn
->align
, unn
->size
);
114 static bool psi_validate_enum(struct psi_validate_scope
*scope
,
115 struct psi_data
*dst
, void *ptr
)
117 struct psi_decl_enum
*enm
= ptr
;
119 PSI_DEBUG_PRINT(dst
, "enum %s", enm
->name
->val
);
120 return psi_decl_enum_validate(dst
, enm
);
123 static bool psi_validate_extvar(struct psi_validate_scope
*scope
,
124 struct psi_data
*dst
, void *ptr
)
126 struct psi_decl_extvar
*evar
= ptr
;
128 PSI_DEBUG_PRINT(dst
, "extvar %s", evar
->arg
->var
->name
->val
);
129 if (psi_decl_extvar_validate(dst
, evar
, scope
)) {
130 dst
->decls
= psi_plist_add(dst
->decls
, &evar
->getter
);
131 dst
->decls
= psi_plist_add(dst
->decls
, &evar
->setter
);
137 static bool psi_validate_decl(struct psi_validate_scope
*scope
,
138 struct psi_data
*dst
, void *ptr
)
140 struct psi_decl
*decl
= ptr
;
142 PSI_DEBUG_PRINT(dst
, "decl %s", decl
->func
->var
->name
->val
);
143 return psi_decl_validate(dst
, decl
, scope
);
146 static bool psi_validate_const(struct psi_validate_scope
*scope
,
147 struct psi_data
*dst
, void *ptr
)
149 struct psi_const
*cnst
= ptr
;
151 PSI_DEBUG_PRINT(dst
, "constant %s", cnst
->name
->val
);
152 return psi_const_validate(dst
, cnst
, scope
);
155 static bool psi_validate_impl(struct psi_validate_scope
*scope
,
156 struct psi_data
*dst
, void *ptr
)
158 struct psi_impl
*impl
= ptr
;
160 PSI_DEBUG_PRINT(dst
, "impl %s", impl
->func
->name
->val
);
161 return psi_impl_validate(dst
, impl
, scope
);
164 bool psi_validate(struct psi_validate_scope
*scope
,
165 struct psi_data
*dst
, struct psi_data
*src
)
167 struct psi_validate_list types
= {src
->types
, &dst
->types
, NULL
, NULL
};
168 struct psi_validate_list structs
= {src
->structs
, &dst
->structs
, NULL
, NULL
};
169 struct psi_validate_list unions
= {src
->unions
, &dst
->unions
, NULL
, NULL
};
170 struct psi_validate_list enums
= {src
->enums
, &dst
->enums
, NULL
, NULL
};
171 struct psi_validate_list vars
= {src
->vars
, &dst
->vars
, NULL
, NULL
};
172 struct psi_validate_list decls
= {src
->decls
, &dst
->decls
, NULL
, NULL
};
173 struct psi_validate_list consts
= {src
->consts
, &dst
->consts
, NULL
, NULL
};
174 struct psi_validate_list impls
= {src
->impls
, &dst
->impls
, NULL
, NULL
};
175 unsigned flags
= dst
->flags
;
176 size_t check_count
= ~0;
178 /* fail early if libraries are not found */
179 if (!psi_decl_file_validate(dst
, src
)) {
184 dst
->flags
|= PSI_SILENT
;
187 size_t count_all
= psi_plist_count(types
.cur
?: types
.src
)
188 + psi_plist_count(structs
.cur
?: structs
.src
)
189 + psi_plist_count(unions
.cur
?: unions
.src
)
190 + psi_plist_count(enums
.cur
?: enums
.src
)
191 + psi_plist_count(vars
.cur
?: vars
.src
)
192 + psi_plist_count(decls
.cur
?: decls
.src
);
194 if (check_count
!= count_all
) {
195 check_count
= count_all
;
198 "PSI: validate data(%p) %zu type checks remaining\n",
201 psi_validate_list(scope
, dst
, &types
, psi_validate_type
);
202 psi_validate_list(scope
, dst
, &structs
, psi_validate_struct
);
203 psi_validate_list(scope
, dst
, &unions
, psi_validate_union
);
204 psi_validate_list(scope
, dst
, &enums
, psi_validate_enum
);
205 psi_validate_list(scope
, dst
, &vars
, psi_validate_extvar
);
206 psi_validate_list(scope
, dst
, &decls
, psi_validate_decl
);
210 /* nothing changed; bail out */
212 src
->errors
+= count_all
;
214 if ((dst
->flags
& PSI_SILENT
) && !(flags
& PSI_SILENT
)) {
215 /* one last error-spitting round, if not explicitly suppressed */
216 dst
->flags
^= PSI_SILENT
;
219 PSI_DEBUG_PRINT(dst
, "PSI: validation bail out with %zu"
220 " type checks remaining, errors follow\n", count_all
);
228 /* reset original flags */
231 psi_validate_list(scope
, dst
, &consts
, psi_validate_const
);
232 psi_validate_list(scope
, dst
, &impls
, psi_validate_impl
);
234 if (types
.cur
!= types
.src
) {
235 psi_plist_free(types
.cur
);
237 if (structs
.cur
!= structs
.src
) {
238 psi_plist_free(structs
.cur
);
240 if (unions
.cur
!= unions
.src
) {
241 psi_plist_free(unions
.cur
);
243 if (enums
.cur
!= enums
.src
) {
244 psi_plist_free(enums
.cur
);
246 if (vars
.cur
!= vars
.src
) {
247 psi_plist_free(vars
.cur
);
249 if (decls
.cur
!= decls
.src
) {
250 psi_plist_free(decls
.cur
);
252 if (consts
.cur
!= consts
.src
) {
253 psi_plist_free(consts
.cur
);
255 if (impls
.cur
!= impls
.src
) {
256 psi_plist_free(impls
.cur
);