cpp: search local dir only for quoted headers
[m6w6/ext-psi] / src / validate.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 "data.h"
29
30 typedef bool (*psi_validate_list_entry)(struct psi_validate_scope *scope,
31 struct psi_data *dst, void *ptr);
32
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;
38 };
39
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)
43 {
44 if (!list->cur) {
45 list->cur = list->src;
46 }
47
48 if (psi_plist_count(list->cur)) {
49 size_t i = 0;
50 void *ptr = NULL;
51
52 list->chk = psi_plist_init(NULL);
53
54 while (psi_plist_get(list->cur, i++, &ptr)) {
55 *dst->last_error = 0;
56
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);
61 } else {
62 PSI_DEBUG_PRINT(dst, " %s (%s)\n", "✘", dst->last_error);
63 list->chk = psi_plist_add(list->chk, &ptr);
64 }
65 }
66
67 if (list->cur != list->src) {
68 psi_plist_free(list->cur);
69 }
70 list->cur = list->chk;
71 }
72 }
73
74 static bool psi_validate_type(struct psi_validate_scope *scope,
75 struct psi_data *dst, void *ptr)
76 {
77 struct psi_decl_arg *def = ptr;
78
79 PSI_DEBUG_PRINT(dst, "typedef %s", def->var->name);
80 return psi_decl_arg_validate_typedef(dst, def, scope);
81 }
82
83 static bool psi_validate_struct(struct psi_validate_scope *scope,
84 struct psi_data *dst, void *ptr)
85 {
86 struct psi_decl_struct *str = ptr;
87
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);
91 return true;
92 }
93 return false;
94 }
95
96 static bool psi_validate_union(struct psi_validate_scope *scope,
97 struct psi_data *dst, void *ptr)
98 {
99 struct psi_decl_union *unn = ptr;
100
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);
104 return true;
105 }
106 return false;
107 }
108
109 static bool psi_validate_enum(struct psi_validate_scope *scope,
110 struct psi_data *dst, void *ptr)
111 {
112 struct psi_decl_enum *enm = ptr;
113
114 PSI_DEBUG_PRINT(dst, "enum %s", enm->name);
115 return psi_decl_enum_validate(dst, enm);
116 }
117
118 static bool psi_validate_extvar(struct psi_validate_scope *scope,
119 struct psi_data *dst, void *ptr)
120 {
121 struct psi_decl_extvar *evar = ptr;
122
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);
127 return true;
128 }
129 return false;
130 }
131
132 static bool psi_validate_decl(struct psi_validate_scope *scope,
133 struct psi_data *dst, void *ptr)
134 {
135 struct psi_decl *decl = ptr;
136
137 PSI_DEBUG_PRINT(dst, "decl %s", decl->func->var->name);
138 return psi_decl_validate(dst, decl, scope);
139 }
140
141 static bool psi_validate_const(struct psi_validate_scope *scope,
142 struct psi_data *dst, void *ptr)
143 {
144 struct psi_const *cnst = ptr;
145
146 PSI_DEBUG_PRINT(dst, "constant %s", cnst->name);
147 return psi_const_validate(dst, cnst, scope);
148 }
149
150 static bool psi_validate_impl(struct psi_validate_scope *scope,
151 struct psi_data *dst, void *ptr)
152 {
153 struct psi_impl *impl = ptr;
154
155 PSI_DEBUG_PRINT(dst, "impl %s", impl->func->name);
156 return psi_impl_validate(dst, impl, scope);
157 }
158
159 bool psi_validate(struct psi_validate_scope *scope,
160 struct psi_data *dst, struct psi_data *src)
161 {
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;
172
173 /* fail early if libraries are not found */
174 if (!psi_decl_file_validate(dst, src)) {
175 return false;
176 }
177
178 /* silence loop */
179 dst->flags |= PSI_SILENT;
180
181 while (true) {
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);
188
189 if (check_count != count_all) {
190 check_count = count_all;
191
192 PSI_DEBUG_PRINT(dst,
193 "PSI: validate data(%p) %zu type checks remaining\n",
194 src, check_count);
195
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);
202 continue;
203 }
204
205 /* nothing changed; bail out */
206 if (count_all && (dst->flags & PSI_SILENT) && !(flags & PSI_SILENT)) {
207 /* one last error-spitting round, if not explicitly suppressed */
208 dst->flags ^= PSI_SILENT;
209 check_count = ~0;
210
211 PSI_DEBUG_PRINT(dst, "PSI: validation bail out with %zu"
212 " type checks remaining, errors follow\n", count_all);
213 continue;
214 }
215
216 src->errors += count_all;
217 break;
218 }
219
220 /* reset original flags */
221 dst->flags = flags;
222
223 psi_validate_list(scope, dst, &consts, psi_validate_const);
224 psi_validate_list(scope, dst, &impls, psi_validate_impl);
225
226 return true;
227 }