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"
30 typedef bool (*psi_validate_list_entry
)(struct psi_validate_scope
*scope
,
31 struct psi_data
*dst
, void *ptr
);
33 struct psi_validate_list
{
34 struct psi_plist
*src
;
35 struct psi_plist
**dst
;
36 struct psi_plist
*cur
;
37 struct psi_plist
*chk
;
40 static inline void psi_validate_list(struct psi_validate_scope
*scope
,
41 struct psi_data
*dst
, struct psi_validate_list
*list
,
42 psi_validate_list_entry validate
)
45 list
->cur
= list
->src
;
48 if (psi_plist_count(list
->cur
)) {
52 list
->chk
= psi_plist_init(NULL
);
54 while (psi_plist_get(list
->cur
, i
++, &ptr
)) {
57 PSI_DEBUG_PRINT(dst
, "PSI: validate %s ", "»");
58 if (validate(scope
, dst
, ptr
)) {
59 PSI_DEBUG_PRINT(dst
, " %s\n", "✔");
60 *list
->dst
= psi_plist_add(*list
->dst
, &ptr
);
62 PSI_DEBUG_PRINT(dst
, " %s (%s)\n", "✘", dst
->last_error
);
63 list
->chk
= psi_plist_add(list
->chk
, &ptr
);
67 if (list
->cur
!= list
->src
) {
68 psi_plist_free(list
->cur
);
70 list
->cur
= list
->chk
;
74 static bool psi_validate_type(struct psi_validate_scope
*scope
,
75 struct psi_data
*dst
, void *ptr
)
77 struct psi_decl_arg
*def
= ptr
;
79 PSI_DEBUG_PRINT(dst
, "typedef %s", def
->var
->name
);
80 return psi_decl_arg_validate_typedef(dst
, def
, scope
);
83 static bool psi_validate_struct(struct psi_validate_scope
*scope
,
84 struct psi_data
*dst
, void *ptr
)
86 struct psi_decl_struct
*str
= ptr
;
88 PSI_DEBUG_PRINT(dst
, "struct %s", str
->name
);
89 if (psi_decl_struct_validate(dst
, str
, scope
)) {
90 PSI_DEBUG_PRINT(dst
, "::(%zu, %zu)", str
->align
, str
->size
);
96 static bool psi_validate_union(struct psi_validate_scope
*scope
,
97 struct psi_data
*dst
, void *ptr
)
99 struct psi_decl_union
*unn
= ptr
;
101 PSI_DEBUG_PRINT(dst
, "union %s", unn
->name
);
102 if (psi_decl_union_validate(dst
, unn
, scope
)) {
103 PSI_DEBUG_PRINT(dst
, "::(%zu, %zu)", unn
->align
, unn
->size
);
109 static bool psi_validate_enum(struct psi_validate_scope
*scope
,
110 struct psi_data
*dst
, void *ptr
)
112 struct psi_decl_enum
*enm
= ptr
;
114 PSI_DEBUG_PRINT(dst
, "enum %s", enm
->name
);
115 return psi_decl_enum_validate(dst
, enm
);
118 static bool psi_validate_extvar(struct psi_validate_scope
*scope
,
119 struct psi_data
*dst
, void *ptr
)
121 struct psi_decl_extvar
*evar
= ptr
;
123 PSI_DEBUG_PRINT(dst
, "extvar %s", evar
->arg
->var
->name
);
124 if (psi_decl_extvar_validate(dst
, evar
, scope
)) {
125 dst
->decls
= psi_plist_add(dst
->decls
, &evar
->getter
);
126 dst
->decls
= psi_plist_add(dst
->decls
, &evar
->setter
);
132 static bool psi_validate_decl(struct psi_validate_scope
*scope
,
133 struct psi_data
*dst
, void *ptr
)
135 struct psi_decl
*decl
= ptr
;
137 PSI_DEBUG_PRINT(dst
, "decl %s", decl
->func
->var
->name
);
138 return psi_decl_validate(dst
, decl
, scope
);
141 static bool psi_validate_const(struct psi_validate_scope
*scope
,
142 struct psi_data
*dst
, void *ptr
)
144 struct psi_const
*cnst
= ptr
;
146 PSI_DEBUG_PRINT(dst
, "constant %s", cnst
->name
);
147 return psi_const_validate(dst
, cnst
, scope
);
150 static bool psi_validate_impl(struct psi_validate_scope
*scope
,
151 struct psi_data
*dst
, void *ptr
)
153 struct psi_impl
*impl
= ptr
;
155 PSI_DEBUG_PRINT(dst
, "impl %s", impl
->func
->name
);
156 return psi_impl_validate(dst
, impl
, scope
);
159 bool psi_validate(struct psi_validate_scope
*scope
,
160 struct psi_data
*dst
, struct psi_data
*src
)
162 struct psi_validate_list types
= {src
->types
, &dst
->types
};
163 struct psi_validate_list structs
= {src
->structs
, &dst
->structs
};
164 struct psi_validate_list unions
= {src
->unions
, &dst
->unions
};
165 struct psi_validate_list enums
= {src
->enums
, &dst
->enums
};
166 struct psi_validate_list vars
= {src
->vars
, &dst
->vars
};
167 struct psi_validate_list decls
= {src
->decls
, &dst
->decls
};
168 struct psi_validate_list consts
= {src
->consts
, &dst
->consts
};
169 struct psi_validate_list impls
= {src
->impls
, &dst
->impls
};
170 unsigned flags
= dst
->flags
;
171 size_t check_count
= ~0;
173 /* fail early if libraries are not found */
174 if (!psi_decl_file_validate(dst
, src
)) {
179 dst
->flags
|= PSI_SILENT
;
182 size_t count_all
= psi_plist_count(types
.cur
?: types
.src
)
183 + psi_plist_count(structs
.cur
?: structs
.src
)
184 + psi_plist_count(unions
.cur
?: unions
.src
)
185 + psi_plist_count(enums
.cur
?: enums
.src
)
186 + psi_plist_count(vars
.cur
?: vars
.src
)
187 + psi_plist_count(decls
.cur
?: decls
.src
);
189 if (check_count
!= count_all
) {
190 check_count
= count_all
;
193 "PSI: validate data(%p) %zu type checks remaining\n",
196 psi_validate_list(scope
, dst
, &types
, psi_validate_type
);
197 psi_validate_list(scope
, dst
, &structs
, psi_validate_struct
);
198 psi_validate_list(scope
, dst
, &unions
, psi_validate_union
);
199 psi_validate_list(scope
, dst
, &enums
, psi_validate_enum
);
200 psi_validate_list(scope
, dst
, &vars
, psi_validate_extvar
);
201 psi_validate_list(scope
, dst
, &decls
, psi_validate_decl
);
205 /* nothing changed; bail out */
207 src
->errors
+= count_all
;
209 if ((dst
->flags
& PSI_SILENT
) && !(flags
& PSI_SILENT
)) {
210 /* one last error-spitting round, if not explicitly suppressed */
211 dst
->flags
^= PSI_SILENT
;
214 PSI_DEBUG_PRINT(dst
, "PSI: validation bail out with %zu"
215 " type checks remaining, errors follow\n", count_all
);
223 /* reset original flags */
226 psi_validate_list(scope
, dst
, &consts
, psi_validate_const
);
227 psi_validate_list(scope
, dst
, &impls
, psi_validate_impl
);
229 if (types
.cur
!= types
.src
) {
230 psi_plist_free(types
.cur
);
232 if (structs
.cur
!= structs
.src
) {
233 psi_plist_free(structs
.cur
);
235 if (unions
.cur
!= unions
.src
) {
236 psi_plist_free(unions
.cur
);
238 if (enums
.cur
!= enums
.src
) {
239 psi_plist_free(enums
.cur
);
241 if (vars
.cur
!= vars
.src
) {
242 psi_plist_free(vars
.cur
);
244 if (decls
.cur
!= decls
.src
) {
245 psi_plist_free(decls
.cur
);
247 if (consts
.cur
!= consts
.src
) {
248 psi_plist_free(consts
.cur
);
250 if (impls
.cur
!= impls
.src
) {
251 psi_plist_free(impls
.cur
);