Scans C files to create a map of function/struct/union/enum names to their file paths.
(files: list[Path], parser_wrapper)
| 509 | |
| 510 | |
| 511 | def pre_scan_c(files: list[Path], parser_wrapper) -> dict: |
| 512 | """Scans C files to create a map of function/struct/union/enum names to their file paths.""" |
| 513 | imports_map = {} |
| 514 | query_str = """ |
| 515 | (function_definition |
| 516 | declarator: (function_declarator |
| 517 | declarator: (identifier) @name |
| 518 | ) |
| 519 | ) |
| 520 | |
| 521 | (function_definition |
| 522 | declarator: (function_declarator |
| 523 | declarator: (pointer_declarator |
| 524 | declarator: (identifier) @name |
| 525 | ) |
| 526 | ) |
| 527 | ) |
| 528 | |
| 529 | (struct_specifier |
| 530 | name: (type_identifier) @name |
| 531 | ) |
| 532 | |
| 533 | (union_specifier |
| 534 | name: (type_identifier) @name |
| 535 | ) |
| 536 | |
| 537 | (enum_specifier |
| 538 | name: (type_identifier) @name |
| 539 | ) |
| 540 | |
| 541 | (type_definition |
| 542 | declarator: (type_identifier) @name |
| 543 | ) |
| 544 | |
| 545 | (preproc_def |
| 546 | name: (identifier) @name |
| 547 | ) |
| 548 | """ |
| 549 | |
| 550 | |
| 551 | for path in files: |
| 552 | try: |
| 553 | with open(path, "r", encoding="utf-8", errors="ignore") as f: |
| 554 | tree = parser_wrapper.parser.parse(bytes(f.read(), "utf8")) |
| 555 | |
| 556 | for capture, _ in execute_query(parser_wrapper.language, query_str, tree.root_node): |
| 557 | name = capture.text.decode('utf-8') |
| 558 | if name not in imports_map: |
| 559 | imports_map[name] = [] |
| 560 | imports_map[name].append(str(path.resolve())) |
| 561 | except Exception as e: |
| 562 | warning_logger(f"Tree-sitter pre-scan failed for {path}: {e}") |
| 563 | return imports_map |
nothing calls this directly
no test coverage detected