Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | LSParrot PHP LSP Extension |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) LSParrot GitHub Organization |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to the 0BSD license that is |
8 : | bundled with this package in the file LICENSE. |
9 : +----------------------------------------------------------------------+
10 : | Author: Go Kudo <zeriyoshi@gmail.com> |
11 : +----------------------------------------------------------------------+
12 : */
13 :
14 : #include "lsp_internal.h"
15 :
16 : #define LSP_INDEX_CACHE_MAGIC "LSPARROTIDX"
17 : #define LSP_INDEX_CACHE_MAGIC_SIZE (sizeof(LSP_INDEX_CACHE_MAGIC) - 1u)
18 : #define LSP_INDEX_CACHE_VERSION 1u
19 : #define LSP_INDEX_CACHE_FILE "lsparrot-index.bin"
20 : #define LSP_INDEX_SIGNATURE_OFFSET 1469598103934665603ULL
21 : #define LSP_INDEX_SIGNATURE_PRIME 1099511628211ULL
22 :
23 : typedef struct _lsp_index_cache_header {
24 : char magic[LSP_INDEX_CACHE_MAGIC_SIZE];
25 : uint32_t version;
26 : uint32_t symbol_index_version;
27 : uint64_t signature;
28 : uint64_t file_count;
29 : uint64_t used;
30 : uint64_t capacity;
31 : uint32_t symbol_count;
32 : uint32_t root_length;
33 : uint32_t lsp_version_length;
34 : uint32_t reserved;
35 : } lsp_index_cache_header;
36 :
37 : static inline void lsp_index_declared_symbols_in_file(lsp_server *server, zend_string *path);
38 : static inline void lsp_index_wait_process_bounded(lsp_process_id pid, int *status);
39 :
40 4712 : static inline bool lsp_path_contains_vendor(zend_string *path)
41 : {
42 4712 : return lsp_path_value_contains_vendor(ZSTR_VAL(path), ZSTR_LEN(path));
43 : }
44 :
45 : static inline bool lsp_path_is_under_root(zend_string *path, zend_string *root)
46 : {
47 : return lsp_path_is_same_or_under(path, root);
48 : }
49 :
50 1272 : static inline bool lsp_composer_project_has_tool(zend_string *project_root, const char *name)
51 : {
52 : zend_string *tool;
53 :
54 1272 : tool = lsp_tool_project_candidate(project_root, name);
55 1272 : if (!tool) {
56 1228 : return false;
57 : }
58 :
59 44 : zend_string_release(tool);
60 :
61 44 : return true;
62 : }
63 :
64 3732 : static inline bool lsp_workspace_composer_project_has_tool(zend_string *workspace_root, zend_string *dir, const char *name, uint32_t depth)
65 : {
66 : const char *entry_name;
67 : lsp_dir *handle;
68 : zend_string *composer, *child;
69 : zend_stat_t st;
70 :
71 3732 : if (depth > 8 || (depth > 0 && lsp_path_is_in_workspace_composer_vendor(workspace_root, dir))) {
72 8 : return false;
73 : }
74 :
75 3724 : composer = lsp_join_path2(dir, "composer.json");
76 3724 : if (lsp_is_regular_file(composer) && lsp_composer_project_has_tool(dir, name)) {
77 8 : zend_string_release(composer);
78 :
79 8 : return true;
80 : }
81 3716 : zend_string_release(composer);
82 :
83 3716 : handle = lsp_dir_open(dir);
84 3716 : if (!handle) {
85 156 : return false;
86 : }
87 :
88 24040 : while ((entry_name = lsp_dir_read(handle)) != NULL) {
89 20488 : if (lsp_scan_should_skip_dir_name(entry_name)) {
90 8296 : continue;
91 : }
92 :
93 12192 : child = lsp_join_path2(dir, entry_name);
94 12192 : if (VCWD_STAT(ZSTR_VAL(child), &st) != 0) {
95 0 : zend_string_release(child);
96 0 : continue;
97 : }
98 :
99 12192 : if (S_ISDIR(st.st_mode) && lsp_workspace_composer_project_has_tool(workspace_root, child, name, depth + 1)) {
100 8 : zend_string_release(child);
101 8 : lsp_dir_close(handle);
102 :
103 8 : return true;
104 : }
105 :
106 12184 : zend_string_release(child);
107 : }
108 :
109 3552 : lsp_dir_close(handle);
110 :
111 3552 : return false;
112 : }
113 :
114 1096 : static inline bool lsp_tool_available_in_workspace(const char *name, zend_string *root)
115 : {
116 1096 : if (lsp_tool_available(name, root)) {
117 160 : return true;
118 : }
119 :
120 936 : return lsp_workspace_composer_project_has_tool(root, root, name, 0);
121 : }
122 :
123 4176 : static inline uint64_t lsp_index_signature_mix_bytes(uint64_t signature, const char *value, size_t length)
124 : {
125 : size_t i;
126 :
127 176760 : for (i = 0; i < length; i++) {
128 172584 : signature ^= (uint64_t) ((unsigned char) value[i]);
129 172584 : signature *= LSP_INDEX_SIGNATURE_PRIME;
130 : }
131 :
132 4176 : return signature;
133 : }
134 :
135 9576 : static inline uint64_t lsp_index_signature_mix_u64(uint64_t signature, uint64_t value)
136 : {
137 : uint32_t i;
138 : unsigned char byte;
139 :
140 86184 : for (i = 0; i < 8; i++) {
141 76608 : byte = (unsigned char) ((value >> (i * 8)) & 0xffu);
142 76608 : signature ^= (uint64_t) byte;
143 76608 : signature *= LSP_INDEX_SIGNATURE_PRIME;
144 : }
145 :
146 9576 : return signature;
147 : }
148 :
149 35206 : static inline bool lsp_string_ends_with_literal(zend_string *value, const char *suffix)
150 : {
151 35206 : size_t suffix_length = strlen(suffix);
152 :
153 35206 : if (ZSTR_LEN(value) < suffix_length) {
154 0 : return false;
155 : }
156 :
157 35206 : return memcmp(ZSTR_VAL(value) + ZSTR_LEN(value) - suffix_length, suffix, suffix_length) == 0;
158 : }
159 :
160 564 : static inline zend_string *lsp_index_cache_dir(lsp_server *server)
161 : {
162 564 : return strpprintf(0, "%s/.lsparrot", ZSTR_VAL(server->root));
163 : }
164 :
165 1504 : static inline zend_string *lsp_index_cache_path(lsp_server *server)
166 : {
167 1504 : return strpprintf(0, "%s/.lsparrot/%s", ZSTR_VAL(server->root), LSP_INDEX_CACHE_FILE);
168 : }
169 :
170 2048 : static inline bool lsp_index_cache_disabled(void)
171 : {
172 : const char *value;
173 :
174 2048 : value = getenv("LSPARROT_NO_INDEX");
175 :
176 2048 : return value && strcmp(value, "1") == 0;
177 : }
178 :
179 32480 : static inline bool lsp_index_signature_should_include_file(const char *name, zend_string *path)
180 : {
181 64632 : return strcmp(name, "composer.json") == 0 ||
182 64628 : strcmp(name, "composer.lock") == 0 ||
183 32148 : lsp_string_ends_with_literal(path, ".php")
184 : ;
185 : }
186 :
187 828 : static inline bool lsp_index_signature_should_include_vendor_composer_file(zend_string *path)
188 : {
189 836 : return lsp_string_ends_with_literal(path, ".php") ||
190 8 : lsp_string_ends_with_literal(path, ".json")
191 : ;
192 : }
193 :
194 2996 : static inline void lsp_index_signature_add_file(zend_string *path, zend_stat_t *st, uint64_t *signature, uint64_t *file_count)
195 : {
196 2996 : *signature = lsp_index_signature_mix_bytes(*signature, ZSTR_VAL(path), ZSTR_LEN(path));
197 2996 : *signature = lsp_index_signature_mix_u64(*signature, (uint64_t) st->st_size);
198 2996 : *signature = lsp_index_signature_mix_u64(*signature, (uint64_t) st->st_mtime);
199 2996 : *signature = lsp_index_signature_mix_u64(*signature, (uint64_t) lsp_stat_mtime_nsec(st));
200 2996 : (*file_count)++;
201 2996 : }
202 :
203 452 : static inline void lsp_index_signature_scan_vendor_composer(zend_string *vendor_dir, uint64_t *signature, uint64_t *file_count)
204 : {
205 : const char *entry_name;
206 : lsp_dir *handle;
207 : zend_string *composer_dir, *child;
208 : zend_stat_t st;
209 :
210 452 : composer_dir = lsp_join_path2(vendor_dir, "composer");
211 452 : handle = lsp_dir_open(composer_dir);
212 452 : if (!handle) {
213 52 : zend_string_release(composer_dir);
214 :
215 52 : return;
216 : }
217 :
218 2040 : while ((entry_name = lsp_dir_read(handle)) != NULL) {
219 1640 : if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
220 800 : continue;
221 : }
222 :
223 840 : child = lsp_join_path2(composer_dir, entry_name);
224 840 : if (VCWD_STAT(ZSTR_VAL(child), &st) != 0) {
225 0 : zend_string_release(child);
226 0 : continue;
227 : }
228 :
229 840 : if (S_ISREG(st.st_mode) && lsp_index_signature_should_include_vendor_composer_file(child)) {
230 824 : lsp_index_signature_add_file(child, &st, signature, file_count);
231 : }
232 :
233 840 : zend_string_release(child);
234 : }
235 :
236 400 : lsp_dir_close(handle);
237 :
238 400 : zend_string_release(composer_dir);
239 : }
240 :
241 2180 : static inline void lsp_index_signature_scan_dir(zend_string *dir, uint32_t depth, uint64_t *signature, uint64_t *file_count)
242 : {
243 : const char *entry_name;
244 : lsp_dir *handle;
245 : zend_string *child;
246 : zend_stat_t st;
247 : bool is_vendor;
248 :
249 2180 : if (depth > 64) {
250 52 : return;
251 : }
252 :
253 2180 : handle = lsp_dir_open(dir);
254 2180 : if (!handle) {
255 52 : return;
256 : }
257 :
258 41112 : while ((entry_name = lsp_dir_read(handle)) != NULL) {
259 38984 : if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
260 4256 : continue;
261 : }
262 :
263 34728 : is_vendor = strcmp(entry_name, "vendor") == 0;
264 34728 : if (!is_vendor && (lsp_scan_should_skip_dir_name(entry_name) || strcmp(entry_name, ".lsparrot") == 0)) {
265 208 : continue;
266 : }
267 :
268 34520 : child = lsp_join_path2(dir, entry_name);
269 34520 : if (VCWD_STAT(ZSTR_VAL(child), &st) != 0) {
270 0 : zend_string_release(child);
271 0 : continue;
272 : }
273 :
274 34520 : if (S_ISDIR(st.st_mode)) {
275 2040 : if (is_vendor) {
276 452 : lsp_index_signature_scan_vendor_composer(child, signature, file_count);
277 : } else {
278 1588 : lsp_index_signature_scan_dir(child, depth + 1, signature, file_count);
279 : }
280 32480 : } else if (S_ISREG(st.st_mode) && lsp_index_signature_should_include_file(entry_name, child)) {
281 2172 : lsp_index_signature_add_file(child, &st, signature, file_count);
282 : }
283 :
284 34520 : zend_string_release(child);
285 : }
286 :
287 2128 : lsp_dir_close(handle);
288 : }
289 :
290 : typedef struct _lsp_index_signature_ctx {
291 : uint64_t *signature;
292 : uint64_t *file_count;
293 : } lsp_index_signature_ctx;
294 :
295 592 : static void lsp_index_signature_root(lsp_server *server, zend_string *root, void *context)
296 : {
297 592 : lsp_index_signature_ctx *ctx = (lsp_index_signature_ctx *) context;
298 :
299 : (void) server;
300 :
301 592 : *ctx->signature = lsp_index_signature_mix_bytes(*ctx->signature, ZSTR_VAL(root), ZSTR_LEN(root));
302 592 : lsp_index_signature_scan_dir(root, 0, ctx->signature, ctx->file_count);
303 592 : }
304 :
305 588 : static inline void lsp_index_signature(lsp_server *server, uint64_t *signature, uint64_t *file_count)
306 : {
307 : lsp_index_signature_ctx ctx;
308 :
309 588 : *signature = LSP_INDEX_SIGNATURE_OFFSET;
310 588 : *file_count = 0;
311 588 : *signature = lsp_index_signature_mix_bytes(*signature, PHP_LSPARROT_VERSION, strlen(PHP_LSPARROT_VERSION));
312 588 : *signature = lsp_index_signature_mix_u64(*signature, LSP_SYMBOL_INDEX_PAYLOAD_VERSION);
313 :
314 588 : ctx.signature = signature;
315 588 : ctx.file_count = file_count;
316 588 : lsp_workspace_roots_each(server, lsp_index_signature_root, &ctx);
317 588 : }
318 :
319 2216 : static inline bool lsp_index_cache_write_all(FILE *fp, const void *data, size_t length)
320 : {
321 2216 : return length == 0 || fwrite(data, 1, length, fp) == length;
322 : }
323 :
324 1704 : static inline bool lsp_index_cache_read_all(FILE *fp, void *data, size_t length)
325 : {
326 1704 : return length == 0 || fread(data, 1, length, fp) == length;
327 : }
328 :
329 556 : static inline bool lsp_index_cache_save(lsp_server *server)
330 : {
331 : lsp_symbol_index_header *symbol_index_header;
332 : lsp_index_cache_header cache_header;
333 : zend_string *dir, *path, *tmp_path;
334 : uint64_t signature, file_count;
335 556 : bool result = false;
336 : FILE *fp;
337 :
338 556 : if (lsp_index_cache_disabled()) {
339 0 : return false;
340 : }
341 :
342 556 : if (!server->symbol_index.available || !server->symbol_index.addr) {
343 0 : return false;
344 : }
345 :
346 556 : symbol_index_header = (lsp_symbol_index_header *) server->symbol_index.addr;
347 556 : if (symbol_index_header->magic != LSP_SYMBOL_INDEX_MAGIC || symbol_index_header->used > symbol_index_header->capacity) {
348 0 : return false;
349 : }
350 :
351 556 : lsp_index_signature(server, &signature, &file_count);
352 :
353 556 : dir = lsp_index_cache_dir(server);
354 556 : path = lsp_index_cache_path(server);
355 556 : tmp_path = strpprintf(0, "%s.tmp.%ld", ZSTR_VAL(path), (long) lsp_current_process_id());
356 556 : lsp_mkdir_p(dir);
357 :
358 556 : memset(&cache_header, 0, sizeof(cache_header));
359 556 : memcpy(cache_header.magic, LSP_INDEX_CACHE_MAGIC, LSP_INDEX_CACHE_MAGIC_SIZE);
360 556 : cache_header.version = LSP_INDEX_CACHE_VERSION;
361 556 : cache_header.symbol_index_version = LSP_SYMBOL_INDEX_PAYLOAD_VERSION;
362 556 : cache_header.signature = signature;
363 556 : cache_header.file_count = file_count;
364 556 : cache_header.used = (uint64_t) symbol_index_header->used;
365 556 : cache_header.capacity = (uint64_t) symbol_index_header->capacity;
366 556 : cache_header.symbol_count = symbol_index_header->symbol_count;
367 556 : cache_header.root_length = (uint32_t) ZSTR_LEN(server->root);
368 556 : cache_header.lsp_version_length = (uint32_t) strlen(PHP_LSPARROT_VERSION);
369 :
370 556 : fp = fopen(ZSTR_VAL(tmp_path), "wb");
371 556 : if (fp) {
372 1108 : result = lsp_index_cache_write_all(fp, &cache_header, sizeof(cache_header)) &&
373 1108 : lsp_index_cache_write_all(fp, ZSTR_VAL(server->root), ZSTR_LEN(server->root)) &&
374 1662 : lsp_index_cache_write_all(fp, PHP_LSPARROT_VERSION, strlen(PHP_LSPARROT_VERSION)) &&
375 554 : lsp_index_cache_write_all(fp, server->symbol_index.addr, symbol_index_header->used)
376 : ;
377 554 : result = fclose(fp) == 0 && result;
378 :
379 554 : if (result) {
380 554 : result = VCWD_RENAME(ZSTR_VAL(tmp_path), ZSTR_VAL(path)) == 0;
381 : }
382 :
383 554 : if (!result) {
384 0 : VCWD_UNLINK(ZSTR_VAL(tmp_path));
385 : }
386 : }
387 :
388 556 : zend_string_release(tmp_path);
389 556 : zend_string_release(path);
390 556 : zend_string_release(dir);
391 :
392 556 : return result;
393 : }
394 :
395 948 : static inline bool lsp_index_cache_load_ex(lsp_server *server, bool verify_signature)
396 : {
397 : lsp_index_cache_header cache_header;
398 : lsp_symbol_index_header *payload_header, *symbol_index_header;
399 : zend_string *path;
400 : uint64_t signature, file_count;
401 948 : bool result = false;
402 : void *payload;
403 : char *root_buffer, *version_buffer;
404 : FILE *fp;
405 :
406 948 : if (lsp_index_cache_disabled()) {
407 0 : return false;
408 : }
409 :
410 948 : if (!server->symbol_index.available || !server->symbol_index.addr) {
411 0 : return false;
412 : }
413 :
414 948 : path = lsp_index_cache_path(server);
415 948 : fp = fopen(ZSTR_VAL(path), "rb");
416 948 : if (!fp) {
417 512 : zend_string_release(path);
418 :
419 512 : return false;
420 : }
421 :
422 436 : root_buffer = NULL;
423 436 : version_buffer = NULL;
424 436 : payload = NULL;
425 :
426 436 : if (!lsp_index_cache_read_all(fp, &cache_header, sizeof(cache_header)) ||
427 436 : memcmp(cache_header.magic, LSP_INDEX_CACHE_MAGIC, LSP_INDEX_CACHE_MAGIC_SIZE) != 0 ||
428 432 : cache_header.version != LSP_INDEX_CACHE_VERSION ||
429 432 : cache_header.symbol_index_version != LSP_SYMBOL_INDEX_PAYLOAD_VERSION ||
430 432 : cache_header.root_length != ZSTR_LEN(server->root) ||
431 432 : cache_header.lsp_version_length != strlen(PHP_LSPARROT_VERSION) ||
432 432 : cache_header.root_length > 1024u * 1024u ||
433 432 : cache_header.lsp_version_length > 1024u ||
434 432 : cache_header.used < sizeof(lsp_symbol_index_header) ||
435 432 : cache_header.used > (uint64_t) server->symbol_index.size
436 : ) {
437 4 : goto done;
438 : }
439 :
440 432 : root_buffer = emalloc((size_t) cache_header.root_length + 1);
441 432 : version_buffer = emalloc((size_t) cache_header.lsp_version_length + 1);
442 432 : if (!lsp_index_cache_read_all(fp, root_buffer, (size_t) cache_header.root_length) ||
443 432 : !lsp_index_cache_read_all(fp, version_buffer, (size_t) cache_header.lsp_version_length)
444 : ) {
445 0 : goto done;
446 : }
447 :
448 432 : root_buffer[cache_header.root_length] = '\0';
449 432 : version_buffer[cache_header.lsp_version_length] = '\0';
450 432 : if (memcmp(root_buffer, ZSTR_VAL(server->root), ZSTR_LEN(server->root)) != 0 ||
451 432 : memcmp(version_buffer, PHP_LSPARROT_VERSION, strlen(PHP_LSPARROT_VERSION)) != 0
452 : ) {
453 0 : goto done;
454 : }
455 :
456 432 : if (verify_signature) {
457 32 : lsp_index_signature(server, &signature, &file_count);
458 32 : if (signature != cache_header.signature || file_count != cache_header.file_count) {
459 28 : goto done;
460 : }
461 : }
462 :
463 404 : payload = emalloc((size_t) cache_header.used);
464 404 : if (!lsp_index_cache_read_all(fp, payload, (size_t) cache_header.used)) {
465 0 : goto done;
466 : }
467 :
468 404 : payload_header = (lsp_symbol_index_header *) payload;
469 404 : if (payload_header->magic != LSP_SYMBOL_INDEX_MAGIC ||
470 404 : payload_header->used != (size_t) cache_header.used ||
471 404 : payload_header->used > payload_header->capacity ||
472 404 : payload_header->symbol_count != cache_header.symbol_count
473 : ) {
474 0 : goto done;
475 : }
476 :
477 404 : memcpy(server->symbol_index.addr, payload, (size_t) cache_header.used);
478 404 : symbol_index_header = (lsp_symbol_index_header *) server->symbol_index.addr;
479 404 : symbol_index_header->capacity = server->symbol_index.size;
480 404 : symbol_index_header->generation++;
481 404 : lsp_symbol_index_rebuild_keys(&server->symbol_index);
482 404 : result = true;
483 :
484 436 : done:
485 436 : if (payload) {
486 404 : efree(payload);
487 : }
488 :
489 436 : if (version_buffer) {
490 432 : efree(version_buffer);
491 : }
492 :
493 436 : if (root_buffer) {
494 432 : efree(root_buffer);
495 : }
496 :
497 436 : fclose(fp);
498 436 : zend_string_release(path);
499 :
500 436 : return result;
501 : }
502 :
503 3052 : static inline char lsp_ast_class_like_symbol_kind(zend_ast_decl *decl)
504 : {
505 3052 : if ((decl->flags & ZEND_ACC_INTERFACE) != 0) {
506 528 : return LSP_SYMBOL_INTERFACE;
507 : }
508 :
509 2524 : if ((decl->flags & ZEND_ACC_TRAIT) != 0) {
510 20 : return LSP_SYMBOL_TRAIT;
511 : }
512 :
513 2504 : if ((decl->flags & ZEND_ACC_ENUM) != 0) {
514 496 : return LSP_SYMBOL_ENUM;
515 : }
516 :
517 2008 : return LSP_SYMBOL_CLASS;
518 : }
519 :
520 160 : static inline char lsp_class_like_symbol_kind_from_token(zval *token)
521 : {
522 160 : if (lsp_token_name_equals(token, "T_INTERFACE")) {
523 0 : return LSP_SYMBOL_INTERFACE;
524 : }
525 :
526 160 : if (lsp_token_name_equals(token, "T_TRAIT")) {
527 0 : return LSP_SYMBOL_TRAIT;
528 : }
529 :
530 160 : if (lsp_token_name_equals(token, "T_ENUM")) {
531 0 : return LSP_SYMBOL_ENUM;
532 : }
533 :
534 160 : return LSP_SYMBOL_CLASS;
535 : }
536 :
537 : static inline bool lsp_index_ast_name_matches(zend_string *name, const char *label_value, size_t label_length)
538 : {
539 : return name &&
540 : ZSTR_LEN(name) == label_length &&
541 : strncasecmp(ZSTR_VAL(name), label_value, label_length) == 0
542 : ;
543 : }
544 :
545 : static inline bool lsp_index_find_class_like_kind_in_ast(zend_ast *ast, const char *label_value, size_t label_length, char *kind)
546 : {
547 : zend_ast_list *list;
548 : zend_ast_decl *decl;
549 : uint32_t i, count;
550 :
551 : if (!ast) {
552 : return false;
553 : }
554 :
555 : if (zend_ast_is_list(ast)) {
556 : list = zend_ast_get_list(ast);
557 : for (i = 0; i < list->children; i++) {
558 : if (lsp_index_find_class_like_kind_in_ast(list->child[i], label_value, label_length, kind)) {
559 : return true;
560 : }
561 : }
562 :
563 : return false;
564 : }
565 :
566 : if (ast->kind == ZEND_AST_CLASS) {
567 : decl = (zend_ast_decl *) ast;
568 : if ((decl->flags & ZEND_ACC_ANON_CLASS) == 0 && lsp_index_ast_name_matches(decl->name, label_value, label_length)) {
569 : *kind = lsp_ast_class_like_symbol_kind(decl);
570 :
571 : return true;
572 : }
573 :
574 : return false;
575 : }
576 :
577 : if (zend_ast_is_special(ast) || php_ver_abstract.ast_is_opaque_node(ast->kind)) {
578 : return false;
579 : }
580 :
581 : count = zend_ast_get_num_children(ast);
582 : for (i = 0; i < count; i++) {
583 : if (lsp_index_find_class_like_kind_in_ast(ast->child[i], label_value, label_length, kind)) {
584 : return true;
585 : }
586 : }
587 :
588 : return false;
589 : }
590 :
591 : static inline char lsp_class_like_symbol_kind_for_file_tokens(zend_string *contents, zend_string *fqcn)
592 : {
593 : const char *label_value;
594 : zend_string *label;
595 : zval tokens_zv, *token;
596 : HashTable *tokens;
597 : uint32_t i, count;
598 : size_t label_length;
599 : char kind;
600 :
601 : kind = LSP_SYMBOL_CLASS;
602 : ZVAL_UNDEF(&tokens_zv);
603 : lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
604 : if (Z_TYPE(tokens_zv) != IS_ARRAY) {
605 : if (!Z_ISUNDEF(tokens_zv)) {
606 : zval_ptr_dtor(&tokens_zv);
607 : }
608 :
609 : return kind;
610 : }
611 :
612 : label_value = lsp_basename_from_fqcn(ZSTR_VAL(fqcn), ZSTR_LEN(fqcn), &label_length);
613 : tokens = Z_ARRVAL(tokens_zv);
614 : count = zend_hash_num_elements(tokens);
615 : for (i = 0; i < count; i++) {
616 : token = zend_hash_index_find(tokens, i);
617 : if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_is_class_like(token)) {
618 : continue;
619 : }
620 :
621 : label = lsp_next_string_token(tokens, i + 1);
622 : if (!label) {
623 : continue;
624 : }
625 :
626 : if (ZSTR_LEN(label) == label_length && strncasecmp(ZSTR_VAL(label), label_value, label_length) == 0) {
627 : kind = lsp_class_like_symbol_kind_from_token(token);
628 : break;
629 : }
630 : }
631 :
632 : zval_ptr_dtor(&tokens_zv);
633 :
634 : return kind;
635 : }
636 :
637 : static inline char lsp_class_like_symbol_kind_for_file(zend_string *path, zend_string *fqcn)
638 : {
639 : const char *label_value;
640 : zend_arena *ast_arena;
641 : zend_ast *ast;
642 : zend_string *contents;
643 : size_t label_length;
644 : char kind;
645 :
646 : kind = LSP_SYMBOL_CLASS;
647 : contents = lsp_read_file(path);
648 : if (contents == zend_empty_string) {
649 : return kind;
650 : }
651 :
652 : ast = lsp_compile_string_to_ast_silent(contents, path, &ast_arena);
653 : if (!ast) {
654 : kind = lsp_class_like_symbol_kind_for_file_tokens(contents, fqcn);
655 : zend_string_release(contents);
656 :
657 : return kind;
658 : }
659 : zend_string_release(contents);
660 :
661 : label_value = lsp_basename_from_fqcn(ZSTR_VAL(fqcn), ZSTR_LEN(fqcn), &label_length);
662 : lsp_index_find_class_like_kind_in_ast(ast, label_value, label_length, &kind);
663 : lsp_compiled_ast_destroy(ast, ast_arena);
664 :
665 : return kind;
666 : }
667 :
668 : /* --- multi-worker initial indexing -----------------------------------------
669 : * The initial project scan reads and lexes/compiles every reachable PHP file,
670 : * which dominates start-up on large trees. The scan is split into a cheap
671 : * discovery pass (directory walks + composer autoload maps, run once in the
672 : * coordinating process) and an expensive per-file symbol extraction pass that
673 : * fans out across worker processes. Discovery runs with the two collect sinks
674 : * set, turning the existing scan functions into work-list producers; workers
675 : * run with the emit sink set, streaming (kind, fqcn, path) records to a part
676 : * file instead of touching the shared-memory index, which only the
677 : * coordinator writes. */
678 : static zval *lsp_index_collect_files_sink = NULL;
679 : static zval *lsp_index_collect_classmap_sink = NULL;
680 : static FILE *lsp_index_emit_sink = NULL;
681 :
682 3820 : static inline void lsp_index_emit_symbol(lsp_server *server, char kind, zend_string *fqcn, zend_string *path)
683 : {
684 : uint32_t fqcn_length, path_length;
685 :
686 3820 : if (!lsp_index_emit_sink) {
687 2862 : lsp_symbol_index_add_symbol_kind(&server->symbol_index, kind, fqcn, path);
688 :
689 2862 : return;
690 : }
691 :
692 958 : fqcn_length = (uint32_t) ZSTR_LEN(fqcn);
693 958 : path_length = (uint32_t) ZSTR_LEN(path);
694 1916 : if (fwrite(&kind, sizeof(kind), 1, lsp_index_emit_sink) != 1 ||
695 1916 : fwrite(&fqcn_length, sizeof(fqcn_length), 1, lsp_index_emit_sink) != 1 ||
696 958 : fwrite(&path_length, sizeof(path_length), 1, lsp_index_emit_sink) != 1 ||
697 958 : fwrite(ZSTR_VAL(fqcn), 1, fqcn_length, lsp_index_emit_sink) != fqcn_length ||
698 958 : fwrite(ZSTR_VAL(path), 1, path_length, lsp_index_emit_sink) != path_length
699 : ) {
700 : /* Truncated part files are detected (and discarded from that point)
701 : * by the reader; nothing useful to do here. */
702 : }
703 : }
704 :
705 632 : static inline void lsp_scan_psr4_dir(lsp_server *server, zend_string *dir, uint32_t depth)
706 : {
707 : const char *entry_name;
708 : lsp_dir *handle;
709 : zend_string *child;
710 : zend_stat_t st;
711 :
712 632 : if (depth > 32 || !lsp_path_is_under_any_root(server, dir) || lsp_path_contains_vendor(dir)) {
713 304 : return;
714 : }
715 :
716 328 : handle = lsp_dir_open(dir);
717 328 : if (!handle) {
718 0 : return;
719 : }
720 :
721 1768 : while ((entry_name = lsp_dir_read(handle)) != NULL) {
722 1440 : if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
723 656 : continue;
724 : }
725 :
726 784 : child = lsp_join_path2(dir, entry_name);
727 784 : if (VCWD_STAT(ZSTR_VAL(child), &st) != 0) {
728 0 : zend_string_release(child);
729 0 : continue;
730 : }
731 :
732 784 : if (S_ISDIR(st.st_mode)) {
733 164 : lsp_scan_psr4_dir(server, child, depth + 1);
734 620 : } else if (S_ISREG(st.st_mode) && lsp_string_ends_with_literal(child, ".php")) {
735 620 : lsp_index_declared_symbols_in_file(server, child);
736 : }
737 :
738 784 : zend_string_release(child);
739 : }
740 :
741 328 : lsp_dir_close(handle);
742 : }
743 :
744 122 : static inline void lsp_scan_psr0_dir(lsp_server *server, zend_string *dir, uint32_t depth)
745 : {
746 : const char *entry_name;
747 : lsp_dir *handle;
748 : zend_string *child;
749 : zend_stat_t st;
750 :
751 122 : if (depth > 32 || !lsp_path_is_under_any_root(server, dir) || lsp_path_contains_vendor(dir)) {
752 26 : return;
753 : }
754 :
755 96 : handle = lsp_dir_open(dir);
756 96 : if (!handle) {
757 0 : return;
758 : }
759 :
760 384 : while ((entry_name = lsp_dir_read(handle)) != NULL) {
761 288 : if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
762 192 : continue;
763 : }
764 :
765 96 : child = lsp_join_path2(dir, entry_name);
766 96 : if (VCWD_STAT(ZSTR_VAL(child), &st) != 0) {
767 0 : zend_string_release(child);
768 0 : continue;
769 : }
770 :
771 96 : if (S_ISDIR(st.st_mode)) {
772 64 : lsp_scan_psr0_dir(server, child, depth + 1);
773 32 : } else if (S_ISREG(st.st_mode) && lsp_string_ends_with_literal(child, ".php")) {
774 32 : lsp_index_declared_symbols_in_file(server, child);
775 : }
776 :
777 96 : zend_string_release(child);
778 : }
779 :
780 96 : lsp_dir_close(handle);
781 : }
782 :
783 7552 : static inline void lsp_index_collect_class_like_kinds_in_ast(zend_ast *ast, zval *kinds)
784 : {
785 : zend_ast_list *list;
786 : zend_ast_decl *decl;
787 : zend_string *key;
788 : uint32_t i, count;
789 :
790 7552 : if (!ast) {
791 1484 : return;
792 : }
793 :
794 6068 : if (zend_ast_is_list(ast)) {
795 1608 : list = zend_ast_get_list(ast);
796 4652 : for (i = 0; i < list->children; i++) {
797 3044 : lsp_index_collect_class_like_kinds_in_ast(list->child[i], kinds);
798 : }
799 :
800 1608 : return;
801 : }
802 :
803 4460 : if (ast->kind == ZEND_AST_CLASS) {
804 956 : decl = (zend_ast_decl *) ast;
805 956 : if ((decl->flags & ZEND_ACC_ANON_CLASS) == 0 && decl->name) {
806 956 : key = zend_string_tolower(decl->name);
807 956 : add_assoc_long_ex(kinds, ZSTR_VAL(key), ZSTR_LEN(key), (zend_long) lsp_ast_class_like_symbol_kind(decl));
808 956 : zend_string_release(key);
809 : }
810 :
811 956 : return;
812 : }
813 :
814 3504 : if (zend_ast_is_special(ast) || php_ver_abstract.ast_is_opaque_node(ast->kind)) {
815 1716 : return;
816 : }
817 :
818 1788 : count = zend_ast_get_num_children(ast);
819 5372 : for (i = 0; i < count; i++) {
820 3584 : lsp_index_collect_class_like_kinds_in_ast(ast->child[i], kinds);
821 : }
822 : }
823 :
824 52 : static inline void lsp_index_collect_class_like_kinds_from_tokens(zend_string *contents, zval *kinds)
825 : {
826 : zend_string *label, *key;
827 : zval tokens_zv, *token;
828 : HashTable *tokens;
829 : uint32_t i, count;
830 :
831 52 : ZVAL_UNDEF(&tokens_zv);
832 52 : lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
833 52 : if (Z_TYPE(tokens_zv) != IS_ARRAY) {
834 0 : if (!Z_ISUNDEF(tokens_zv)) {
835 0 : zval_ptr_dtor(&tokens_zv);
836 : }
837 :
838 0 : return;
839 : }
840 :
841 52 : tokens = Z_ARRVAL(tokens_zv);
842 52 : count = zend_hash_num_elements(tokens);
843 3644 : for (i = 0; i < count; i++) {
844 3592 : token = zend_hash_index_find(tokens, i);
845 3592 : if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_is_class_like(token)) {
846 3540 : continue;
847 : }
848 :
849 52 : label = lsp_next_string_token(tokens, i + 1);
850 52 : if (!label) {
851 0 : continue;
852 : }
853 :
854 52 : key = zend_string_tolower(label);
855 52 : add_assoc_long_ex(kinds, ZSTR_VAL(key), ZSTR_LEN(key), (zend_long) lsp_class_like_symbol_kind_from_token(token));
856 52 : zend_string_release(key);
857 : }
858 :
859 52 : zval_ptr_dtor(&tokens_zv);
860 : }
861 :
862 : /* Files declaring several classmap entries were previously re-read and
863 : * re-compiled once PER ENTRY; parse each unique path once and answer the
864 : * remaining entries from the memoized declaration map. */
865 1000 : static inline char lsp_class_like_symbol_kind_for_file_memoized(HashTable *memo, zend_string *path, zend_string *fqcn)
866 : {
867 : const char *label_value;
868 : zend_arena *ast_arena;
869 : zend_ast *ast;
870 : zend_string *contents, *label;
871 : zval *entry, kinds, *kind_zv;
872 : size_t label_length;
873 1000 : char kind = LSP_SYMBOL_CLASS;
874 :
875 1000 : entry = zend_hash_find(memo, path);
876 1000 : if (!entry) {
877 980 : array_init(&kinds);
878 980 : contents = lsp_read_file(path);
879 980 : if (contents != zend_empty_string) {
880 976 : ast = lsp_compile_string_to_ast_silent(contents, path, &ast_arena);
881 976 : if (ast) {
882 924 : lsp_index_collect_class_like_kinds_in_ast(ast, &kinds);
883 924 : lsp_compiled_ast_destroy(ast, ast_arena);
884 : } else {
885 52 : lsp_index_collect_class_like_kinds_from_tokens(contents, &kinds);
886 : }
887 976 : zend_string_release(contents);
888 : }
889 :
890 980 : entry = zend_hash_update(memo, path, &kinds);
891 : }
892 :
893 1000 : label_value = lsp_basename_from_fqcn(ZSTR_VAL(fqcn), ZSTR_LEN(fqcn), &label_length);
894 1000 : label = zend_string_alloc(label_length, 0);
895 1000 : memcpy(ZSTR_VAL(label), label_value, label_length);
896 1000 : ZSTR_VAL(label)[label_length] = '\0';
897 1000 : zend_str_tolower(ZSTR_VAL(label), label_length);
898 :
899 1000 : kind_zv = entry && Z_TYPE_P(entry) == IS_ARRAY ? zend_hash_find(Z_ARRVAL_P(entry), label) : NULL;
900 1000 : if (kind_zv && Z_TYPE_P(kind_zv) == IS_LONG) {
901 996 : kind = (char) Z_LVAL_P(kind_zv);
902 : }
903 :
904 1000 : zend_string_release(label);
905 :
906 1000 : return kind;
907 : }
908 :
909 1020 : static inline void lsp_index_classmap(lsp_server *server, zend_string *composer_dir)
910 : {
911 1020 : zend_string *classmap_path = lsp_join_path2(composer_dir, "autoload_classmap.php"), *class_name;
912 : zval *path_zv, classmap;
913 : HashTable file_kinds;
914 :
915 1020 : if (!lsp_include_array_file(classmap_path, &classmap)) {
916 480 : zend_string_release(classmap_path);
917 704 : return;
918 : }
919 :
920 540 : if (lsp_index_collect_classmap_sink) {
921 : /* Discovery pass: defer the per-file kind detection to the workers. */
922 832 : ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(classmap), class_name, path_zv) {
923 : zval pair;
924 :
925 608 : if (!class_name || Z_TYPE_P(path_zv) != IS_STRING) {
926 0 : continue;
927 : }
928 :
929 608 : array_init(&pair);
930 608 : add_next_index_str(&pair, zend_string_copy(class_name));
931 608 : add_next_index_str(&pair, zend_string_copy(Z_STR_P(path_zv)));
932 608 : add_next_index_zval(lsp_index_collect_classmap_sink, &pair);
933 : } ZEND_HASH_FOREACH_END();
934 :
935 224 : zval_ptr_dtor(&classmap);
936 224 : zend_string_release(classmap_path);
937 224 : return;
938 : }
939 :
940 316 : zend_hash_init(&file_kinds, 64, NULL, ZVAL_PTR_DTOR, 0);
941 1092 : ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(classmap), class_name, path_zv) {
942 776 : if (!class_name || Z_TYPE_P(path_zv) != IS_STRING) {
943 0 : continue;
944 : }
945 776 : lsp_index_emit_symbol(server, lsp_class_like_symbol_kind_for_file_memoized(&file_kinds, Z_STR_P(path_zv), class_name), class_name, Z_STR_P(path_zv));
946 : } ZEND_HASH_FOREACH_END();
947 316 : zend_hash_destroy(&file_kinds);
948 :
949 316 : zval_ptr_dtor(&classmap);
950 316 : zend_string_release(classmap_path);
951 : }
952 :
953 1020 : static inline void lsp_index_psr4(lsp_server *server, zend_string *composer_dir)
954 : {
955 1020 : zend_string *psr4_path = lsp_join_path2(composer_dir, "autoload_psr4.php"), *prefix;
956 : zval *dirs_zv, psr4, *dir_zv;
957 :
958 1020 : if (!lsp_include_array_file(psr4_path, &psr4)) {
959 688 : zend_string_release(psr4_path);
960 688 : return;
961 : }
962 :
963 776 : ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(psr4), prefix, dirs_zv) {
964 444 : if (!prefix || Z_TYPE_P(dirs_zv) != IS_ARRAY) {
965 0 : continue;
966 : }
967 :
968 912 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(dirs_zv), dir_zv) {
969 468 : if (Z_TYPE_P(dir_zv) != IS_STRING) {
970 0 : continue;
971 : }
972 468 : lsp_scan_psr4_dir(server, Z_STR_P(dir_zv), 0);
973 : } ZEND_HASH_FOREACH_END();
974 : } ZEND_HASH_FOREACH_END();
975 :
976 332 : zval_ptr_dtor(&psr4);
977 332 : zend_string_release(psr4_path);
978 : }
979 :
980 1020 : static inline void lsp_index_psr0(lsp_server *server, zend_string *composer_dir)
981 : {
982 1020 : zend_string *psr0_path = lsp_join_path2(composer_dir, "autoload_namespaces.php"), *prefix;
983 : zval *dirs_zv, *dir_zv, psr0;
984 :
985 1020 : if (!lsp_include_array_file(psr0_path, &psr0)) {
986 752 : zend_string_release(psr0_path);
987 :
988 752 : return;
989 : }
990 :
991 326 : ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(psr0), prefix, dirs_zv) {
992 58 : if (!prefix) {
993 0 : continue;
994 : }
995 :
996 58 : if (Z_TYPE_P(dirs_zv) == IS_STRING) {
997 0 : lsp_scan_psr0_dir(server, Z_STR_P(dirs_zv), 0);
998 0 : continue;
999 : }
1000 :
1001 58 : if (Z_TYPE_P(dirs_zv) != IS_ARRAY) {
1002 0 : continue;
1003 : }
1004 :
1005 116 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(dirs_zv), dir_zv) {
1006 58 : if (Z_TYPE_P(dir_zv) != IS_STRING) {
1007 0 : continue;
1008 : }
1009 58 : lsp_scan_psr0_dir(server, Z_STR_P(dir_zv), 0);
1010 : } ZEND_HASH_FOREACH_END();
1011 : } ZEND_HASH_FOREACH_END();
1012 :
1013 268 : zval_ptr_dtor(&psr0);
1014 268 : zend_string_release(psr0_path);
1015 : }
1016 :
1017 144 : static inline void lsp_index_function_symbol(lsp_server *server, zend_string *namespace_name, zend_string *name, zend_string *path)
1018 : {
1019 : zend_string *fqfn;
1020 :
1021 144 : if (namespace_name != zend_empty_string && ZSTR_LEN(namespace_name) > 0) {
1022 144 : fqfn = strpprintf(0, "%s\\%s", ZSTR_VAL(namespace_name), ZSTR_VAL(name));
1023 : } else {
1024 0 : fqfn = zend_string_copy(name);
1025 : }
1026 :
1027 144 : lsp_index_emit_symbol(server, LSP_SYMBOL_FUNCTION, fqfn, path);
1028 :
1029 144 : zend_string_release(fqfn);
1030 144 : }
1031 :
1032 2676 : static inline void lsp_index_named_symbol(lsp_server *server, char kind, zend_string *namespace_name, zend_string *name, zend_string *path)
1033 : {
1034 : zend_string *fqcn;
1035 :
1036 2676 : if (namespace_name != zend_empty_string && ZSTR_LEN(namespace_name) > 0) {
1037 2140 : fqcn = strpprintf(0, "%s\\%s", ZSTR_VAL(namespace_name), ZSTR_VAL(name));
1038 : } else {
1039 536 : fqcn = zend_string_copy(name);
1040 : }
1041 :
1042 2676 : lsp_index_emit_symbol(server, kind, fqcn, path);
1043 :
1044 2676 : zend_string_release(fqcn);
1045 2676 : }
1046 :
1047 76 : static inline bool lsp_index_token_is_namespace_name(zval *token)
1048 : {
1049 112 : return lsp_token_name_equals(token, "T_STRING") ||
1050 36 : lsp_token_name_equals(token, "T_NAME_QUALIFIED") ||
1051 0 : lsp_token_name_equals(token, "T_NAME_FULLY_QUALIFIED") ||
1052 112 : lsp_token_name_equals(token, "T_NAME_RELATIVE") ||
1053 0 : lsp_token_name_equals(token, "T_NS_SEPARATOR")
1054 : ;
1055 : }
1056 :
1057 76 : static inline zend_string *lsp_index_namespace_from_tokens(HashTable *tokens, uint32_t start)
1058 : {
1059 : zend_string *text;
1060 : zval *token;
1061 76 : smart_str namespace_name = {0};
1062 : uint32_t i, count;
1063 :
1064 76 : count = zend_hash_num_elements(tokens);
1065 228 : for (i = start; i < count; i++) {
1066 228 : token = zend_hash_index_find(tokens, i);
1067 228 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
1068 0 : continue;
1069 : }
1070 :
1071 228 : if (lsp_token_name_equals(token, "T_WHITESPACE")) {
1072 76 : continue;
1073 : }
1074 :
1075 152 : if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{')) {
1076 : break;
1077 : }
1078 :
1079 76 : if (!lsp_index_token_is_namespace_name(token)) {
1080 0 : continue;
1081 : }
1082 :
1083 76 : text = lsp_token_string(token, "text");
1084 76 : if (!text) {
1085 0 : continue;
1086 : }
1087 :
1088 76 : if (ZSTR_LEN(text) > 0 && ZSTR_VAL(text)[0] == '\\') {
1089 0 : smart_str_appendl(&namespace_name, ZSTR_VAL(text) + 1, ZSTR_LEN(text) - 1);
1090 : } else {
1091 76 : smart_str_append(&namespace_name, text);
1092 : }
1093 : }
1094 :
1095 76 : if (!namespace_name.s) {
1096 0 : return zend_empty_string;
1097 : }
1098 :
1099 76 : smart_str_0(&namespace_name);
1100 :
1101 76 : return namespace_name.s;
1102 : }
1103 :
1104 76 : static inline void lsp_index_declared_symbols_from_tokens(lsp_server *server, zend_string *contents, zend_string *path)
1105 : {
1106 : zend_long depth, class_depth;
1107 : zend_string *namespace_name, *name;
1108 : zval tokens_zv, *token;
1109 : HashTable *tokens;
1110 : uint32_t i, count;
1111 : bool pending_class_like;
1112 :
1113 76 : depth = 0;
1114 76 : class_depth = -1;
1115 76 : pending_class_like = false;
1116 76 : ZVAL_UNDEF(&tokens_zv);
1117 76 : lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
1118 76 : if (Z_TYPE(tokens_zv) != IS_ARRAY) {
1119 0 : if (!Z_ISUNDEF(tokens_zv)) {
1120 0 : zval_ptr_dtor(&tokens_zv);
1121 : }
1122 0 : return;
1123 : }
1124 :
1125 76 : tokens = Z_ARRVAL(tokens_zv);
1126 76 : count = zend_hash_num_elements(tokens);
1127 76 : namespace_name = zend_empty_string;
1128 6268 : for (i = 0; i < count; i++) {
1129 6192 : token = zend_hash_index_find(tokens, i);
1130 6192 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
1131 0 : continue;
1132 : }
1133 :
1134 6192 : if (lsp_token_name_equals(token, "T_NAMESPACE")) {
1135 76 : if (namespace_name != zend_empty_string) {
1136 0 : zend_string_release(namespace_name);
1137 : }
1138 76 : namespace_name = lsp_index_namespace_from_tokens(tokens, i + 1);
1139 :
1140 76 : continue;
1141 : }
1142 :
1143 6116 : if (lsp_token_is_class_like(token)) {
1144 108 : name = lsp_next_string_token(tokens, i + 1);
1145 108 : if (name && class_depth < 0) {
1146 108 : lsp_index_named_symbol(server, lsp_class_like_symbol_kind_from_token(token), namespace_name, name, path);
1147 : }
1148 :
1149 108 : pending_class_like = true;
1150 :
1151 108 : continue;
1152 : }
1153 :
1154 6008 : if (lsp_token_is_char(token, '{')) {
1155 244 : depth++;
1156 :
1157 244 : if (pending_class_like) {
1158 108 : class_depth = depth;
1159 108 : pending_class_like = false;
1160 : }
1161 :
1162 244 : continue;
1163 : }
1164 :
1165 5764 : if (lsp_token_is_char(token, '}')) {
1166 244 : if (class_depth >= 0 && depth == class_depth) {
1167 108 : class_depth = -1;
1168 : }
1169 :
1170 244 : if (depth > 0) {
1171 244 : depth--;
1172 : }
1173 :
1174 244 : continue;
1175 : }
1176 :
1177 5520 : if (lsp_token_name_equals(token, "T_FUNCTION") && class_depth < 0) {
1178 0 : name = lsp_next_function_name_token(tokens, i + 1);
1179 0 : if (name) {
1180 0 : lsp_index_function_symbol(server, namespace_name, name, path);
1181 : }
1182 :
1183 0 : continue;
1184 : }
1185 :
1186 5520 : if (lsp_token_name_equals(token, "T_CONST") && class_depth < 0) {
1187 8 : name = lsp_next_string_token(tokens, i + 1);
1188 8 : if (name) {
1189 8 : lsp_index_named_symbol(server, LSP_SYMBOL_CONSTANT, namespace_name, name, path);
1190 : }
1191 : }
1192 : }
1193 :
1194 76 : if (namespace_name != zend_empty_string) {
1195 76 : zend_string_release(namespace_name);
1196 : }
1197 :
1198 76 : zval_ptr_dtor(&tokens_zv);
1199 : }
1200 :
1201 1676 : static inline zend_string *lsp_index_ast_name_copy(zend_ast *ast)
1202 : {
1203 : zend_string *name;
1204 :
1205 1676 : name = lsp_ast_string_value(ast);
1206 1676 : if (!name || ZSTR_LEN(name) == 0) {
1207 0 : return zend_empty_string;
1208 : }
1209 :
1210 1676 : if (ZSTR_VAL(name)[0] == '\\') {
1211 0 : if (ZSTR_LEN(name) == 1) {
1212 0 : return zend_empty_string;
1213 : }
1214 :
1215 0 : return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1216 : }
1217 :
1218 1676 : return zend_string_copy(name);
1219 : }
1220 :
1221 464 : static inline void lsp_index_const_decl_symbols(lsp_server *server, zend_ast *ast, zend_string *namespace_name, zend_string *path)
1222 : {
1223 : zend_ast_list *list;
1224 : zend_ast *elem;
1225 : zend_string *name;
1226 : uint32_t i;
1227 :
1228 464 : if (!ast || !zend_ast_is_list(ast)) {
1229 0 : return;
1230 : }
1231 :
1232 464 : list = zend_ast_get_list(ast);
1233 928 : for (i = 0; i < list->children; i++) {
1234 464 : elem = list->child[i];
1235 464 : if (!elem || elem->kind != ZEND_AST_CONST_ELEM) {
1236 0 : continue;
1237 : }
1238 :
1239 464 : name = lsp_ast_string_value(elem->child[0]);
1240 464 : if (name) {
1241 464 : lsp_index_named_symbol(server, LSP_SYMBOL_CONSTANT, namespace_name, name, path);
1242 : }
1243 : }
1244 : }
1245 :
1246 8856 : static inline void lsp_index_declared_symbols_from_ast(lsp_server *server, zend_ast *ast, zend_string **namespace_name, zend_string *path, bool in_class)
1247 : {
1248 : zend_ast_list *list;
1249 : zend_ast_decl *decl;
1250 : zend_string *node_namespace;
1251 : uint32_t i, count;
1252 :
1253 8856 : if (!ast) {
1254 7776 : return;
1255 : }
1256 :
1257 8208 : if (ast->kind == ZEND_AST_CONST_DECL) {
1258 464 : if (!in_class) {
1259 464 : lsp_index_const_decl_symbols(server, ast, *namespace_name, path);
1260 : }
1261 :
1262 464 : return;
1263 : }
1264 :
1265 7744 : if (zend_ast_is_list(ast)) {
1266 2256 : list = zend_ast_get_list(ast);
1267 7264 : for (i = 0; i < list->children; i++) {
1268 5008 : lsp_index_declared_symbols_from_ast(server, list->child[i], namespace_name, path, in_class);
1269 : }
1270 :
1271 2256 : return;
1272 : }
1273 :
1274 5488 : if (ast->kind == ZEND_AST_NAMESPACE) {
1275 1676 : node_namespace = lsp_index_ast_name_copy(ast->child[0]);
1276 1676 : if (ast->child[1]) {
1277 8 : lsp_index_declared_symbols_from_ast(server, ast->child[1], &node_namespace, path, false);
1278 8 : if (node_namespace != zend_empty_string) {
1279 8 : zend_string_release(node_namespace);
1280 : }
1281 : } else {
1282 1668 : if (*namespace_name != zend_empty_string) {
1283 0 : zend_string_release(*namespace_name);
1284 : }
1285 1668 : *namespace_name = node_namespace;
1286 : }
1287 :
1288 1676 : return;
1289 : }
1290 :
1291 3812 : if (ast->kind == ZEND_AST_CLASS) {
1292 2096 : decl = (zend_ast_decl *) ast;
1293 2096 : if (!in_class && (decl->flags & ZEND_ACC_ANON_CLASS) == 0 && decl->name) {
1294 2096 : lsp_index_named_symbol(server, lsp_ast_class_like_symbol_kind(decl), *namespace_name, decl->name, path);
1295 : }
1296 :
1297 2096 : return;
1298 : }
1299 :
1300 1716 : if (ast->kind == ZEND_AST_FUNC_DECL) {
1301 144 : decl = (zend_ast_decl *) ast;
1302 144 : if (!in_class && decl->name) {
1303 144 : lsp_index_function_symbol(server, *namespace_name, decl->name, path);
1304 : }
1305 144 : if (!in_class) {
1306 144 : lsp_index_declared_symbols_from_ast(server, decl->child[2], namespace_name, path, false);
1307 : }
1308 :
1309 144 : return;
1310 : }
1311 :
1312 1572 : if (zend_ast_is_special(ast) || php_ver_abstract.ast_is_opaque_node(ast->kind)) {
1313 492 : return;
1314 : }
1315 :
1316 1080 : count = zend_ast_get_num_children(ast);
1317 3008 : for (i = 0; i < count; i++) {
1318 1928 : lsp_index_declared_symbols_from_ast(server, ast->child[i], namespace_name, path, in_class);
1319 : }
1320 : }
1321 :
1322 1844 : static inline void lsp_index_declared_symbols_in_contents(lsp_server *server, zend_string *contents, zend_string *path)
1323 : {
1324 : zend_arena *ast_arena;
1325 : zend_ast *ast;
1326 : zend_string *namespace_name;
1327 :
1328 1844 : if (contents == zend_empty_string) {
1329 76 : return;
1330 : }
1331 :
1332 1844 : ast = lsp_compile_string_to_ast_silent(contents, path, &ast_arena);
1333 1844 : if (!ast) {
1334 76 : lsp_index_declared_symbols_from_tokens(server, contents, path);
1335 :
1336 76 : return;
1337 : }
1338 :
1339 1768 : namespace_name = zend_empty_string;
1340 1768 : lsp_index_declared_symbols_from_ast(server, ast, &namespace_name, path, false);
1341 :
1342 1768 : if (namespace_name != zend_empty_string) {
1343 1668 : zend_string_release(namespace_name);
1344 : }
1345 :
1346 1768 : lsp_compiled_ast_destroy(ast, ast_arena);
1347 : }
1348 :
1349 2648 : static inline void lsp_index_declared_symbols_in_file(lsp_server *server, zend_string *path)
1350 : {
1351 : zend_string *contents;
1352 :
1353 2648 : if (lsp_index_collect_files_sink) {
1354 816 : add_next_index_str(lsp_index_collect_files_sink, zend_string_copy(path));
1355 :
1356 816 : return;
1357 : }
1358 :
1359 1832 : contents = lsp_read_file(path);
1360 1832 : if (contents == zend_empty_string) {
1361 0 : return;
1362 : }
1363 :
1364 1832 : lsp_index_declared_symbols_in_contents(server, contents, path);
1365 1832 : zend_string_release(contents);
1366 : }
1367 :
1368 896 : static inline void lsp_index_declared_symbols_in_dir(lsp_server *server, zend_string *dir, uint32_t depth)
1369 : {
1370 : const char *entry_name;
1371 : lsp_dir *handle;
1372 : zend_string *child;
1373 : zend_stat_t st;
1374 :
1375 896 : if (depth > 32 || lsp_path_contains_vendor(dir) || (depth > 0 && lsp_path_is_in_workspace_composer_vendor(server->root, dir))) {
1376 8 : return;
1377 : }
1378 :
1379 888 : handle = lsp_dir_open(dir);
1380 888 : if (!handle) {
1381 0 : return;
1382 : }
1383 :
1384 4588 : while ((entry_name = lsp_dir_read(handle)) != NULL) {
1385 3700 : if (lsp_scan_should_skip_dir_name(entry_name)) {
1386 1776 : continue;
1387 : }
1388 :
1389 1924 : child = lsp_join_path2(dir, entry_name);
1390 1924 : if (VCWD_STAT(ZSTR_VAL(child), &st) != 0) {
1391 0 : zend_string_release(child);
1392 :
1393 0 : continue;
1394 : }
1395 :
1396 1924 : if (S_ISDIR(st.st_mode)) {
1397 416 : lsp_index_declared_symbols_in_dir(server, child, depth + 1);
1398 1508 : } else if (S_ISREG(st.st_mode) && lsp_string_ends_with_literal(child, ".php")) {
1399 1508 : lsp_index_declared_symbols_in_file(server, child);
1400 : }
1401 :
1402 1924 : zend_string_release(child);
1403 : }
1404 :
1405 888 : lsp_dir_close(handle);
1406 : }
1407 :
1408 526 : static inline void lsp_index_declared_symbols_in_path(lsp_server *server, zend_string *path)
1409 : {
1410 : zend_stat_t st;
1411 :
1412 526 : if (!lsp_path_is_under_any_root(server, path) || VCWD_STAT(ZSTR_VAL(path), &st) != 0) {
1413 480 : return;
1414 : }
1415 :
1416 526 : if (S_ISDIR(st.st_mode)) {
1417 480 : lsp_index_declared_symbols_in_dir(server, path, 0);
1418 480 : return;
1419 : }
1420 :
1421 46 : if (S_ISREG(st.st_mode) && lsp_string_ends_with_literal(path, ".php")) {
1422 46 : lsp_index_declared_symbols_in_file(server, path);
1423 : }
1424 : }
1425 :
1426 1020 : static inline void lsp_index_composer_project(lsp_server *server, zend_string *project_root)
1427 : {
1428 : zend_string *vendor_dir, *composer_dir;
1429 : zval paths, *path_zv;
1430 :
1431 1020 : vendor_dir = lsp_composer_vendor_dir(project_root);
1432 1020 : composer_dir = lsp_join_path2(vendor_dir, "composer");
1433 :
1434 1020 : lsp_index_classmap(server, composer_dir);
1435 1020 : lsp_index_psr4(server, composer_dir);
1436 1020 : lsp_index_psr0(server, composer_dir);
1437 :
1438 1020 : lsp_composer_analysis_paths(project_root, &paths);
1439 1020 : if (zend_hash_num_elements(Z_ARRVAL(paths)) > 0) {
1440 954 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL(paths), path_zv) {
1441 526 : if (Z_TYPE_P(path_zv) == IS_STRING) {
1442 526 : lsp_index_declared_symbols_in_path(server, Z_STR_P(path_zv));
1443 : }
1444 : } ZEND_HASH_FOREACH_END();
1445 : }
1446 1020 : zval_ptr_dtor(&paths);
1447 :
1448 1020 : zend_string_release(composer_dir);
1449 1020 : zend_string_release(vendor_dir);
1450 1020 : }
1451 :
1452 3384 : static inline void lsp_index_workspace_composer_projects(lsp_server *server, zend_string *dir, uint32_t depth)
1453 : {
1454 : const char *entry_name;
1455 : lsp_dir *handle;
1456 : zend_string *composer, *child;
1457 : zend_stat_t st;
1458 :
1459 3384 : if (depth > 8 || lsp_path_contains_vendor(dir) || (depth > 0 && lsp_path_is_in_workspace_composer_vendor(server->root, dir))) {
1460 108 : return;
1461 : }
1462 :
1463 3376 : composer = lsp_join_path2(dir, "composer.json");
1464 3376 : if (depth > 0 && lsp_is_regular_file(composer)) {
1465 38 : lsp_index_composer_project(server, dir);
1466 : }
1467 3376 : zend_string_release(composer);
1468 :
1469 3376 : handle = lsp_dir_open(dir);
1470 3376 : if (!handle) {
1471 100 : return;
1472 : }
1473 :
1474 62430 : while ((entry_name = lsp_dir_read(handle)) != NULL) {
1475 59154 : if (lsp_scan_should_skip_dir_name(entry_name)) {
1476 7580 : continue;
1477 : }
1478 :
1479 51574 : child = lsp_join_path2(dir, entry_name);
1480 51574 : if (VCWD_STAT(ZSTR_VAL(child), &st) != 0) {
1481 0 : zend_string_release(child);
1482 :
1483 0 : continue;
1484 : }
1485 :
1486 51574 : if (S_ISDIR(st.st_mode)) {
1487 2402 : lsp_index_workspace_composer_projects(server, child, depth + 1);
1488 : }
1489 :
1490 51574 : zend_string_release(child);
1491 : }
1492 :
1493 3276 : lsp_dir_close(handle);
1494 : }
1495 :
1496 982 : static void lsp_build_project_index_root(lsp_server *server, zend_string *root, void *context)
1497 : {
1498 : (void) context;
1499 :
1500 982 : lsp_index_composer_project(server, root);
1501 982 : lsp_index_workspace_composer_projects(server, root, 0);
1502 982 : }
1503 :
1504 978 : static inline void lsp_build_project_index_work(lsp_server *server)
1505 : {
1506 978 : lsp_workspace_roots_each(server, lsp_build_project_index_root, NULL);
1507 978 : }
1508 :
1509 : #if LSP_HAVE_POSIX_PROCESS
1510 : #define LSP_INDEX_PARALLEL_MIN_WORK 32u
1511 : #define LSP_INDEX_PARALLEL_MAX_WORKERS 16
1512 :
1513 56 : static inline zend_string *lsp_index_part_path(lsp_server *server, uint32_t worker_id, long stamp)
1514 : {
1515 56 : return strpprintf(0, "%s/.lsparrot/index.part.%u.%ld", ZSTR_VAL(server->root), worker_id, stamp);
1516 : }
1517 :
1518 : /* Deterministic classmap partition: entries that share a path land on the
1519 : * same worker so the per-file declaration memo stays effective. Workers and
1520 : * the coordinator recompute the identical assignment from iteration order. */
1521 896 : static inline uint32_t lsp_index_classmap_bucket(HashTable *assignments, zend_string *path, uint32_t worker_count, uint32_t *next_bucket)
1522 : {
1523 : zval *existing, bucket_zv;
1524 : uint32_t bucket;
1525 :
1526 896 : existing = zend_hash_find(assignments, path);
1527 896 : if (existing) {
1528 0 : return (uint32_t) Z_LVAL_P(existing);
1529 : }
1530 :
1531 896 : bucket = *next_bucket;
1532 896 : *next_bucket = (bucket + 1) % worker_count;
1533 896 : ZVAL_LONG(&bucket_zv, (zend_long) bucket);
1534 896 : zend_hash_add(assignments, path, &bucket_zv);
1535 :
1536 896 : return bucket;
1537 : }
1538 :
1539 24 : static inline void lsp_index_parallel_child_run(lsp_server *server, zval *files, zval *classmap_pairs, uint32_t worker_id, uint32_t worker_count, zend_string *part_path)
1540 : {
1541 : HashTable assignments, file_kinds;
1542 : zval *pair_zv, *fqcn_zv, *path_zv, *file_zv;
1543 24 : uint32_t next_bucket = 0, index = 0;
1544 :
1545 24 : lsp_index_emit_sink = fopen(ZSTR_VAL(part_path), "wb");
1546 24 : if (!lsp_index_emit_sink) {
1547 0 : _exit(1);
1548 : }
1549 :
1550 24 : zend_hash_init(&assignments, 64, NULL, NULL, 0);
1551 24 : zend_hash_init(&file_kinds, 64, NULL, ZVAL_PTR_DTOR, 0);
1552 920 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(classmap_pairs), pair_zv) {
1553 896 : if (Z_TYPE_P(pair_zv) != IS_ARRAY) {
1554 0 : continue;
1555 : }
1556 :
1557 896 : fqcn_zv = zend_hash_index_find(Z_ARRVAL_P(pair_zv), 0);
1558 896 : path_zv = zend_hash_index_find(Z_ARRVAL_P(pair_zv), 1);
1559 896 : if (!fqcn_zv || !path_zv || Z_TYPE_P(fqcn_zv) != IS_STRING || Z_TYPE_P(path_zv) != IS_STRING) {
1560 0 : continue;
1561 : }
1562 :
1563 896 : if (lsp_index_classmap_bucket(&assignments, Z_STR_P(path_zv), worker_count, &next_bucket) != worker_id) {
1564 672 : continue;
1565 : }
1566 :
1567 224 : lsp_index_emit_symbol(server, lsp_class_like_symbol_kind_for_file_memoized(&file_kinds, Z_STR_P(path_zv), Z_STR_P(fqcn_zv)), Z_STR_P(fqcn_zv), Z_STR_P(path_zv));
1568 : } ZEND_HASH_FOREACH_END();
1569 24 : zend_hash_destroy(&file_kinds);
1570 24 : zend_hash_destroy(&assignments);
1571 :
1572 1776 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(files), file_zv) {
1573 1752 : if (Z_TYPE_P(file_zv) == IS_STRING && index % worker_count == worker_id) {
1574 438 : lsp_index_declared_symbols_in_file(server, Z_STR_P(file_zv));
1575 : }
1576 1752 : index++;
1577 : } ZEND_HASH_FOREACH_END();
1578 :
1579 24 : if (fclose(lsp_index_emit_sink) != 0) {
1580 0 : lsp_index_emit_sink = NULL;
1581 0 : _exit(1);
1582 : }
1583 24 : lsp_index_emit_sink = NULL;
1584 24 : }
1585 :
1586 24 : static inline bool lsp_index_part_merge(lsp_server *server, zend_string *part_path)
1587 : {
1588 : FILE *fp;
1589 : zend_string *fqcn, *path;
1590 : uint32_t fqcn_length, path_length;
1591 : char kind;
1592 :
1593 24 : fp = fopen(ZSTR_VAL(part_path), "rb");
1594 24 : if (!fp) {
1595 0 : return false;
1596 : }
1597 :
1598 : for (;;) {
1599 982 : if (fread(&kind, sizeof(kind), 1, fp) != 1) {
1600 24 : break;
1601 : }
1602 :
1603 1916 : if (fread(&fqcn_length, sizeof(fqcn_length), 1, fp) != 1 ||
1604 958 : fread(&path_length, sizeof(path_length), 1, fp) != 1 ||
1605 958 : fqcn_length > 64u * 1024u ||
1606 958 : path_length > 64u * 1024u
1607 : ) {
1608 0 : fclose(fp);
1609 :
1610 0 : return false;
1611 : }
1612 :
1613 958 : fqcn = zend_string_alloc(fqcn_length, 0);
1614 958 : path = zend_string_alloc(path_length, 0);
1615 958 : if (fread(ZSTR_VAL(fqcn), 1, fqcn_length, fp) != fqcn_length ||
1616 958 : fread(ZSTR_VAL(path), 1, path_length, fp) != path_length
1617 : ) {
1618 0 : zend_string_release(fqcn);
1619 0 : zend_string_release(path);
1620 0 : fclose(fp);
1621 :
1622 0 : return false;
1623 : }
1624 :
1625 958 : ZSTR_VAL(fqcn)[fqcn_length] = '\0';
1626 958 : ZSTR_VAL(path)[path_length] = '\0';
1627 958 : lsp_symbol_index_add_symbol_kind(&server->symbol_index, kind, fqcn, path);
1628 958 : zend_string_release(fqcn);
1629 958 : zend_string_release(path);
1630 : }
1631 :
1632 24 : fclose(fp);
1633 :
1634 24 : return true;
1635 : }
1636 :
1637 : /* Fan the per-file extraction across worker processes. Returns false when the
1638 : * workload is too small, a fork fails, or a worker dies -- the caller then
1639 : * falls back to the serial scan. */
1640 544 : static inline bool lsp_build_project_index_parallel(lsp_server *server)
1641 : {
1642 : zval files, classmap_pairs;
1643 : zend_string *cache_dir, *part_path;
1644 : pid_t *pids;
1645 : zend_long configured;
1646 : long stamp;
1647 : uint32_t worker_count, work_count, i, j;
1648 : int status;
1649 544 : bool ok = true;
1650 :
1651 544 : configured = lsp_analyzer_parallel_workers(server);
1652 544 : if (configured < 2) {
1653 104 : return false;
1654 : }
1655 440 : worker_count = configured > LSP_INDEX_PARALLEL_MAX_WORKERS ? LSP_INDEX_PARALLEL_MAX_WORKERS : (uint32_t) configured;
1656 :
1657 : /* Discovery: run the normal scan with the collect sinks set, producing
1658 : * the work lists without reading any PHP file. */
1659 440 : array_init(&files);
1660 440 : array_init(&classmap_pairs);
1661 440 : lsp_index_collect_files_sink = &files;
1662 440 : lsp_index_collect_classmap_sink = &classmap_pairs;
1663 440 : lsp_build_project_index_work(server);
1664 440 : lsp_index_collect_files_sink = NULL;
1665 440 : lsp_index_collect_classmap_sink = NULL;
1666 :
1667 440 : work_count = zend_hash_num_elements(Z_ARRVAL(files)) + zend_hash_num_elements(Z_ARRVAL(classmap_pairs));
1668 440 : if (work_count < LSP_INDEX_PARALLEL_MIN_WORK) {
1669 432 : zval_ptr_dtor(&files);
1670 432 : zval_ptr_dtor(&classmap_pairs);
1671 :
1672 432 : return false;
1673 : }
1674 :
1675 8 : cache_dir = lsp_index_cache_dir(server);
1676 8 : lsp_mkdir_p(cache_dir);
1677 8 : zend_string_release(cache_dir);
1678 :
1679 : /* Part names carry the coordinator's pid; computed before forking so the
1680 : * children and the merge below agree on them. */
1681 8 : stamp = lsp_current_process_id();
1682 8 : pids = ecalloc(worker_count, sizeof(pid_t));
1683 8 : fflush(stdout);
1684 8 : fflush(stderr);
1685 :
1686 40 : for (i = 0; i < worker_count; i++) {
1687 32 : pids[i] = fork();
1688 56 : if (pids[i] < 0) {
1689 0 : ok = false;
1690 0 : break;
1691 : }
1692 :
1693 56 : if (pids[i] == 0) {
1694 : /* Worker: keep stray output away from the LSP transport, drop
1695 : * inherited analyzer pipe references (extra fds delay EOF-based
1696 : * exit detection in the parent), stream symbol records to the
1697 : * part file, and exit without running any inherited shutdown
1698 : * handlers. */
1699 24 : if (!freopen("/dev/null", "r", stdin) || !freopen("/dev/null", "w", stdout)) {
1700 0 : _exit(1);
1701 : }
1702 :
1703 24 : lsp_runner_close_pipes_in_child(server);
1704 24 : lsp_psalm_ls_close_pipes_in_child(server);
1705 24 : part_path = lsp_index_part_path(server, i, stamp);
1706 24 : lsp_index_parallel_child_run(server, &files, &classmap_pairs, i, worker_count, part_path);
1707 24 : lsp_coverage_flush();
1708 0 : _exit(0);
1709 : }
1710 : }
1711 :
1712 : /* Reap every started worker even after a failed fork. */
1713 40 : for (j = 0; j < worker_count; j++) {
1714 32 : if (pids[j] > 0) {
1715 32 : status = 0;
1716 32 : lsp_index_wait_process_bounded(pids[j], &status);
1717 : #if defined(WIFEXITED)
1718 32 : if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) {
1719 8 : ok = false;
1720 : }
1721 : #else
1722 : if (status != 0) {
1723 : ok = false;
1724 : }
1725 : #endif
1726 : }
1727 : }
1728 :
1729 8 : if (ok) {
1730 30 : for (i = 0; i < worker_count; i++) {
1731 24 : part_path = lsp_index_part_path(server, i, stamp);
1732 24 : if (!lsp_index_part_merge(server, part_path)) {
1733 0 : ok = false;
1734 : }
1735 24 : VCWD_UNLINK(ZSTR_VAL(part_path));
1736 24 : zend_string_release(part_path);
1737 : }
1738 : } else {
1739 10 : for (i = 0; i < worker_count; i++) {
1740 8 : part_path = lsp_index_part_path(server, i, stamp);
1741 8 : VCWD_UNLINK(ZSTR_VAL(part_path));
1742 8 : zend_string_release(part_path);
1743 : }
1744 : }
1745 :
1746 8 : efree(pids);
1747 8 : zval_ptr_dtor(&files);
1748 8 : zval_ptr_dtor(&classmap_pairs);
1749 :
1750 8 : if (!ok) {
1751 : /* Partial merges may have landed; rebuild from scratch serially. */
1752 2 : lsp_symbol_index_reset(&server->symbol_index);
1753 :
1754 2 : return false;
1755 : }
1756 :
1757 6 : return true;
1758 : }
1759 : #endif
1760 :
1761 544 : static inline void lsp_build_project_index_sync(lsp_server *server)
1762 : {
1763 544 : if (!server->symbol_index.available) {
1764 0 : return;
1765 : }
1766 :
1767 544 : lsp_symbol_index_reset(&server->symbol_index);
1768 :
1769 : #if LSP_HAVE_POSIX_PROCESS
1770 544 : if (lsp_build_project_index_parallel(server)) {
1771 6 : lsp_index_cache_save(server);
1772 :
1773 6 : return;
1774 : }
1775 : #endif
1776 :
1777 538 : lsp_build_project_index_work(server);
1778 538 : lsp_index_cache_save(server);
1779 : }
1780 :
1781 3260 : extern bool lsp_include_array_file(zend_string *path, zval *return_value)
1782 : {
1783 : zend_file_handle file_handle;
1784 :
1785 3260 : if (VCWD_ACCESS(ZSTR_VAL(path), F_OK) != 0) {
1786 2064 : return false;
1787 : }
1788 :
1789 1196 : ZVAL_UNDEF(return_value);
1790 1196 : zend_stream_init_filename_ex(&file_handle, path);
1791 1196 : if (zend_execute_scripts(ZEND_REQUIRE, return_value, 1, &file_handle) != SUCCESS || EG(exception)) {
1792 0 : if (EG(exception)) {
1793 0 : zend_clear_exception();
1794 : }
1795 :
1796 0 : if (!Z_ISUNDEF_P(return_value)) {
1797 0 : zval_ptr_dtor(return_value);
1798 0 : ZVAL_UNDEF(return_value);
1799 : }
1800 :
1801 0 : return false;
1802 : }
1803 :
1804 1196 : if (Z_TYPE_P(return_value) != IS_ARRAY) {
1805 0 : if (!Z_ISUNDEF_P(return_value)) {
1806 0 : zval_ptr_dtor(return_value);
1807 0 : ZVAL_UNDEF(return_value);
1808 : }
1809 :
1810 0 : return false;
1811 : }
1812 :
1813 1196 : return true;
1814 : }
1815 :
1816 : #if LSP_HAVE_POSIX_PROCESS
1817 : /* Fork a child that performs the full project scan and persists the result
1818 : * to the .lsparrot cache file (atomic tmp+rename). The parent keeps serving
1819 : * requests; the first consumer that needs project symbols joins the worker
1820 : * and loads the freshly written cache. fork(2) inherits the already booted
1821 : * Zend engine copy-on-write, so the child starts with zero warm-up cost. */
1822 544 : static inline bool lsp_index_worker_start(lsp_server *server)
1823 : {
1824 : pid_t pid;
1825 :
1826 544 : if (lsp_index_cache_disabled()) {
1827 0 : return false;
1828 : }
1829 :
1830 544 : fflush(stdout);
1831 544 : fflush(stderr);
1832 544 : pid = fork();
1833 1088 : if (pid < 0) {
1834 0 : return false;
1835 : }
1836 :
1837 1088 : if (pid == 0) {
1838 : /* Child: detach from the LSP transport so no stray frames reach the
1839 : * client, drop inherited analyzer pipe references, build + persist
1840 : * the index, and exit without running any inherited shutdown
1841 : * handlers. */
1842 544 : if (!freopen("/dev/null", "r", stdin)) {
1843 0 : _exit(1);
1844 : }
1845 544 : if (!freopen("/dev/null", "w", stdout)) {
1846 0 : _exit(1);
1847 : }
1848 :
1849 544 : lsp_runner_close_pipes_in_child(server);
1850 544 : lsp_psalm_ls_close_pipes_in_child(server);
1851 544 : lsp_build_project_index_sync(server);
1852 544 : lsp_coverage_flush();
1853 0 : _exit(0);
1854 : }
1855 :
1856 544 : server->index_worker_pid = pid;
1857 544 : server->index_worker_running = true;
1858 :
1859 544 : return true;
1860 : }
1861 : #endif
1862 :
1863 400 : static inline void lsp_index_worker_finished(lsp_server *server)
1864 : {
1865 400 : server->index_worker_running = false;
1866 : /* The worker has been reaped; drop the pid so no later stop/terminate
1867 : * path can signal a reused process id. */
1868 400 : server->index_worker_pid = LSP_INVALID_PROCESS_ID;
1869 :
1870 400 : if (lsp_index_cache_load_ex(server, false)) {
1871 400 : lsp_analyzer_status("index", "idle", "Project index ready.");
1872 :
1873 400 : return;
1874 : }
1875 :
1876 : /* The worker failed to produce a loadable cache; fall back to an inline
1877 : * synchronous build so project symbols stay available. */
1878 0 : lsp_build_project_index_sync(server);
1879 0 : lsp_analyzer_status("index", "idle", "Project index ready.");
1880 : }
1881 :
1882 : /* Waiting on a worker must never hang the single-threaded request loop
1883 : * forever: a healthy build can be slow (large repos), so the cap is generous,
1884 : * but a wedged worker gets escalated SIGTERM -> SIGKILL and the caller falls
1885 : * back to the synchronous build. */
1886 : #define LSP_INDEX_WORKER_WAIT_CAP_SECONDS 300.0
1887 :
1888 336 : static inline void lsp_index_wait_process_bounded(lsp_process_id pid, int *status)
1889 : {
1890 336 : if (lsp_process_wait_timeout(pid, status, LSP_INDEX_WORKER_WAIT_CAP_SECONDS)) {
1891 336 : return;
1892 : }
1893 :
1894 0 : lsp_process_terminate(pid);
1895 0 : if (!lsp_process_wait_timeout(pid, status, 2.0)) {
1896 0 : lsp_process_terminate_force(pid);
1897 0 : lsp_process_wait_timeout(pid, status, 2.0);
1898 : }
1899 :
1900 0 : *status = -1;
1901 : }
1902 :
1903 1112 : extern void lsp_index_join_worker(lsp_server *server)
1904 : {
1905 1112 : int status = 0;
1906 :
1907 1112 : if (!server->index_worker_running) {
1908 808 : return;
1909 : }
1910 :
1911 304 : lsp_index_wait_process_bounded(server->index_worker_pid, &status);
1912 304 : lsp_index_worker_finished(server);
1913 : }
1914 :
1915 3104 : extern void lsp_index_poll_worker(lsp_server *server)
1916 : {
1917 3104 : int status = 0;
1918 :
1919 3104 : if (!server->index_worker_running) {
1920 1720 : return;
1921 : }
1922 :
1923 1384 : if (lsp_process_wait_nonblocking(server->index_worker_pid, &status)) {
1924 96 : lsp_index_worker_finished(server);
1925 : }
1926 : }
1927 :
1928 548 : extern void lsp_index_stop_worker(lsp_server *server)
1929 : {
1930 548 : int status = 0;
1931 :
1932 548 : if (!server->index_worker_running) {
1933 404 : return;
1934 : }
1935 :
1936 : /* The cache write is atomic, so terminating mid-build is safe; the next
1937 : * start simply rebuilds. */
1938 144 : lsp_process_terminate(server->index_worker_pid);
1939 144 : if (!lsp_process_wait_timeout(server->index_worker_pid, &status, 5.0)) {
1940 0 : lsp_process_terminate_force(server->index_worker_pid);
1941 0 : lsp_process_wait_timeout(server->index_worker_pid, &status, 2.0);
1942 : }
1943 144 : server->index_worker_running = false;
1944 : }
1945 :
1946 548 : extern void lsp_build_project_index(lsp_server *server)
1947 : {
1948 548 : zend_hash_clean(&server->member_cache);
1949 :
1950 548 : if (lsp_index_cache_load_ex(server, true)) {
1951 4 : lsp_analyzer_status("index", "idle", "Project index loaded from .lsparrot cache.");
1952 4 : return;
1953 : }
1954 :
1955 544 : lsp_analyzer_status("index", "running", "Indexing PHP project.");
1956 :
1957 : #if LSP_HAVE_POSIX_PROCESS
1958 544 : if (lsp_index_worker_start(server)) {
1959 544 : return;
1960 : }
1961 : #endif
1962 :
1963 0 : lsp_build_project_index_sync(server);
1964 0 : lsp_analyzer_status("index", "idle", "Project index ready.");
1965 : }
1966 :
1967 : /* Incrementally refresh the symbols declared by one file: drop the file's
1968 : * stale records and re-scan its current contents (preferring the open
1969 : * document text over the on-disk copy). Keeps newly added classes available
1970 : * to completion/definition without a server restart. */
1971 16 : extern void lsp_index_refresh_file(lsp_server *server, zend_string *path)
1972 : {
1973 : lsp_document *document;
1974 : zend_stat_t st;
1975 :
1976 16 : if (!server->symbol_index.available ||
1977 16 : !path ||
1978 32 : !lsp_string_ends_with_literal(path, ".php") ||
1979 16 : lsp_path_value_contains_analysis_helper(ZSTR_VAL(path), ZSTR_LEN(path))
1980 : ) {
1981 0 : return;
1982 : }
1983 :
1984 16 : lsp_index_join_worker(server);
1985 16 : lsp_symbol_index_remove_path(&server->symbol_index, path);
1986 :
1987 16 : document = lsp_document_for_path(server, path);
1988 16 : if (document) {
1989 12 : lsp_index_declared_symbols_in_contents(server, document->text, path);
1990 4 : } else if (VCWD_STAT(ZSTR_VAL(path), &st) == 0 && S_ISREG(st.st_mode)) {
1991 4 : lsp_index_declared_symbols_in_file(server, path);
1992 : }
1993 :
1994 16 : server->symbol_index_dirty = true;
1995 : }
1996 :
1997 548 : extern void lsp_project_index_persist(lsp_server *server)
1998 : {
1999 548 : if (!server->symbol_index_dirty) {
2000 536 : return;
2001 : }
2002 :
2003 12 : if (lsp_index_cache_save(server)) {
2004 12 : server->symbol_index_dirty = false;
2005 : }
2006 : }
2007 :
2008 548 : extern void lsp_resolve_analyzers(lsp_server *server)
2009 : {
2010 : bool phpstan_available, psalm_available, psalm_ls_available;
2011 :
2012 548 : phpstan_available = lsp_tool_available_in_workspace("phpstan", server->root);
2013 548 : psalm_available = lsp_tool_available_in_workspace("psalm", server->root);
2014 1060 : psalm_ls_available = lsp_composer_project_has_tool(server->root, "psalm-language-server") ||
2015 512 : lsp_workspace_composer_project_has_tool(server->root, server->root, "psalm-language-server", 0)
2016 : ;
2017 :
2018 548 : server->phpstan_enabled = false;
2019 548 : server->psalm_enabled = false;
2020 548 : server->psalm_ls_enabled = false;
2021 :
2022 548 : if (server->options.analyzer_auto) {
2023 16 : server->phpstan_enabled = phpstan_available;
2024 : /* A composer install of Psalm ships both vendor/bin/psalm and
2025 : * vendor/bin/psalm-language-server; running both duplicates every
2026 : * diagnostic and doubles hover latency. Prefer the resident language
2027 : * server (incremental, faster) and fall back to the CLI only when
2028 : * the LS binary is absent. Explicit configuration can still enable
2029 : * both deliberately. */
2030 16 : server->psalm_ls_enabled = psalm_ls_available;
2031 16 : server->psalm_enabled = psalm_available && !psalm_ls_available;
2032 : } else {
2033 532 : if (server->options.analyzer_phpstan) {
2034 112 : if (phpstan_available) {
2035 108 : server->phpstan_enabled = true;
2036 : } else {
2037 4 : lsp_analyzer_unavailable("phpstan", "PHPStan");
2038 : }
2039 : }
2040 :
2041 532 : if (server->options.analyzer_psalm) {
2042 48 : if (psalm_available) {
2043 48 : server->psalm_enabled = true;
2044 : } else {
2045 0 : lsp_analyzer_unavailable("psalm", "Psalm");
2046 : }
2047 : }
2048 :
2049 532 : if (server->options.analyzer_psalm_ls) {
2050 32 : if (psalm_ls_available || lsp_tool_available_in_workspace("psalm", server->root)) {
2051 32 : server->psalm_ls_enabled = true;
2052 : } else {
2053 0 : lsp_analyzer_unavailable("psalm-ls", "Psalm LS");
2054 : }
2055 : }
2056 : }
2057 :
2058 548 : lsp_driver_status(server);
2059 548 : }
|