|
3 | 3 | * |
4 | 4 | * Scans discovered files for manifest files (package.json, go.mod, Cargo.toml, |
5 | 5 | * pyproject.toml, composer.json, pubspec.yaml, pom.xml, build.gradle, mix.exs, |
6 | | - * *.gemspec) and builds a hash table mapping bare package specifiers to resolved |
| 6 | + * *.gemspec, Package.swift) and builds a hash table mapping bare package specifiers to resolved |
7 | 7 | * module QNs. This enables IMPORTS edges for non-relative imports like |
8 | 8 | * "@myorg/pkg", "github.com/foo/bar", "use my_crate::foo". |
9 | 9 | * |
@@ -665,6 +665,271 @@ static void parse_gemspec(const char *source, int source_len, const char *rel_pa |
665 | 665 | } |
666 | 666 | } |
667 | 667 |
|
| 668 | +/* Swift: Package.swift — SwiftPM manifest. |
| 669 | + * |
| 670 | + * Package.swift is executable Swift, not declarative, so this is a |
| 671 | + * hand-rolled literal pattern-extractor (same spirit as parse_cargo_toml), |
| 672 | + * not a Swift evaluator: it locates `.target(...)` call expressions via a |
| 673 | + * comment/string-aware token scan and pulls only their quoted-string-literal |
| 674 | + * `name:` / `path:` arguments. Anything computed or concatenated is |
| 675 | + * skipped, not guessed — fail-closed, like every other parser here. |
| 676 | + * |
| 677 | + * Only a manifest's OWN `.target(...)` declarations self-register — never |
| 678 | + * `.library(...)` products (a product name is not generally an importable |
| 679 | + * module: SwiftPM lets it alias multiple targets, or none sharing its |
| 680 | + * name), and never `.package(url:)` / `.package(path:)` dependencies or |
| 681 | + * target-to-target dependency references. A dependency resolves because |
| 682 | + * the DEPENDENCY's own Package.swift registers itself when the repo-wide |
| 683 | + * manifest walk (cbm_pkgmap_scan_repo) reaches it, exactly like a JS |
| 684 | + * workspace sibling's package.json (see repro_issue408.c). |
| 685 | + * |
| 686 | + * Deliberately out of scope (matches the item-1 authorization): evaluating |
| 687 | + * `#if`/`#endif` conditional-compilation blocks. A `.target(...)` inside |
| 688 | + * one is scanned like any other — teaching the extractor which platform |
| 689 | + * conditions are "active" would need a Swift semantic tier, which this PR |
| 690 | + * was explicitly asked not to add. */ |
| 691 | + |
| 692 | +/* One step of comment/string-aware scanning, shared by swift_find_code_token |
| 693 | + * and swift_match_paren below. If `*pp` sits inside, or is entering, a `//` |
| 694 | + * line comment, a nesting-aware slash-star block comment (continued across |
| 695 | + * calls via `*block_depth`), or a "..." string literal (escape aware, |
| 696 | + * tracked via `*in_str`), advances `*pp` past that one step and returns |
| 697 | + * true. Otherwise leaves `*pp` untouched and returns false, so the caller |
| 698 | + * handles the "real code" character itself (paren tracking, needle |
| 699 | + * matching, ...). */ |
| 700 | +static bool swift_skip_comment_or_string(const char **pp, const char *end, int *block_depth, |
| 701 | + bool *in_str) { |
| 702 | + const char *p = *pp; |
| 703 | + if (*block_depth > 0) { |
| 704 | + if (p + SKIP_ONE < end && p[0] == '/' && p[SKIP_ONE] == '*') { |
| 705 | + (*block_depth)++; |
| 706 | + *pp = p + PAIR_LEN; |
| 707 | + } else if (p + SKIP_ONE < end && p[0] == '*' && p[SKIP_ONE] == '/') { |
| 708 | + (*block_depth)--; |
| 709 | + *pp = p + PAIR_LEN; |
| 710 | + } else { |
| 711 | + *pp = p + SKIP_ONE; |
| 712 | + } |
| 713 | + return true; |
| 714 | + } |
| 715 | + if (*in_str) { |
| 716 | + if (*p == '\\' && p + SKIP_ONE < end) { |
| 717 | + *pp = p + PAIR_LEN; |
| 718 | + } else { |
| 719 | + if (*p == '"') { |
| 720 | + *in_str = false; |
| 721 | + } |
| 722 | + *pp = p + SKIP_ONE; |
| 723 | + } |
| 724 | + return true; |
| 725 | + } |
| 726 | + if (p + SKIP_ONE < end && p[0] == '/' && p[SKIP_ONE] == '/') { |
| 727 | + while (p < end && *p != '\n') { |
| 728 | + p++; |
| 729 | + } |
| 730 | + *pp = p; |
| 731 | + return true; |
| 732 | + } |
| 733 | + if (p + SKIP_ONE < end && p[0] == '/' && p[SKIP_ONE] == '*') { |
| 734 | + *block_depth = SKIP_ONE; |
| 735 | + *pp = p + PAIR_LEN; |
| 736 | + return true; |
| 737 | + } |
| 738 | + if (*p == '"') { |
| 739 | + *in_str = true; |
| 740 | + *pp = p + SKIP_ONE; |
| 741 | + return true; |
| 742 | + } |
| 743 | + return false; |
| 744 | +} |
| 745 | + |
| 746 | +/* Scans [start, end) for the next occurrence of `needle` that is not inside |
| 747 | + * a `//` line comment, a nesting-aware slash-star block comment, or a "..." |
| 748 | + * string literal (backslash-escapes honored). This is the single source of |
| 749 | + * truth for "real code" scanning below — a `.target(`, `name:`, or `path:` |
| 750 | + * spelled inside a comment or a string constant must never be mistaken for |
| 751 | + * a live declaration. Returns a pointer to the match, or NULL. */ |
| 752 | +static const char *swift_find_code_token(const char *start, const char *end, const char *needle) { |
| 753 | + size_t nlen = strlen(needle); |
| 754 | + int block_depth = 0; |
| 755 | + bool in_str = false; |
| 756 | + const char *p = start; |
| 757 | + while (p < end) { |
| 758 | + if (swift_skip_comment_or_string(&p, end, &block_depth, &in_str)) { |
| 759 | + continue; |
| 760 | + } |
| 761 | + if ((size_t)(end - p) >= nlen && memcmp(p, needle, nlen) == 0) { |
| 762 | + return p; |
| 763 | + } |
| 764 | + p++; |
| 765 | + } |
| 766 | + return NULL; |
| 767 | +} |
| 768 | + |
| 769 | +/* Find the matching ')' for the '(' at `open`, scanning forward to `end`. |
| 770 | + * Delegates comment/string spans to swift_skip_comment_or_string — matching |
| 771 | + * swift_find_code_token — so a ')', '(', or comment delimiter inside a |
| 772 | + * quoted argument value or a comment never perturbs the depth count. |
| 773 | + * Returns NULL for an unbalanced/unterminated call — the caller treats |
| 774 | + * that span as unparseable and skips it (fail-closed). */ |
| 775 | +static const char *swift_match_paren(const char *open, const char *end) { |
| 776 | + int depth = 0; |
| 777 | + int block_depth = 0; |
| 778 | + bool in_str = false; |
| 779 | + const char *p = open; |
| 780 | + while (p < end) { |
| 781 | + if (swift_skip_comment_or_string(&p, end, &block_depth, &in_str)) { |
| 782 | + continue; |
| 783 | + } |
| 784 | + if (*p == '(') { |
| 785 | + depth++; |
| 786 | + } else if (*p == ')') { |
| 787 | + depth--; |
| 788 | + if (depth == 0) { |
| 789 | + return p; |
| 790 | + } |
| 791 | + } |
| 792 | + p++; |
| 793 | + } |
| 794 | + return NULL; |
| 795 | +} |
| 796 | + |
| 797 | +/* Extract a bare double-quoted string-literal value at `p` (after skipping |
| 798 | + * leading whitespace and honoring backslash-escapes inside the literal), |
| 799 | + * requiring the closing quote be immediately followed by a comma, or by |
| 800 | + * `end` — rejects `name: "Foo" + suffix`-style concatenation, which a plain |
| 801 | + * quote-scan alone cannot tell apart from a true literal. Every caller here |
| 802 | + * passes the enclosing call's own closing ')' position as `end`, so landing |
| 803 | + * exactly on it after the closing quote correctly means "immediately |
| 804 | + * followed by the close-paren", not just "ran off the end of the buffer". |
| 805 | + * Returns heap string, or NULL (fail-closed). */ |
| 806 | +static char *swift_quoted_literal(const char *p, const char *end) { |
| 807 | + while (p < end && (*p == ' ' || *p == '\t')) { |
| 808 | + p++; |
| 809 | + } |
| 810 | + if (p >= end || *p != '"') { |
| 811 | + return NULL; |
| 812 | + } |
| 813 | + p++; |
| 814 | + const char *start = p; |
| 815 | + while (p < end && *p != '"' && *p != '\n') { |
| 816 | + if (*p == '\\' && p + SKIP_ONE < end) { |
| 817 | + p += PAIR_LEN; |
| 818 | + continue; |
| 819 | + } |
| 820 | + p++; |
| 821 | + } |
| 822 | + if (p >= end || *p != '"') { |
| 823 | + return NULL; |
| 824 | + } |
| 825 | + char *value = cbm_strndup(start, (size_t)(p - start)); |
| 826 | + p++; /* past closing quote */ |
| 827 | + while (p < end && (*p == ' ' || *p == '\t')) { |
| 828 | + p++; |
| 829 | + } |
| 830 | + if (p >= end || *p == ',') { |
| 831 | + return value; |
| 832 | + } |
| 833 | + free(value); |
| 834 | + return NULL; |
| 835 | +} |
| 836 | + |
| 837 | +/* Search [start, end) for `needle` via swift_find_code_token (skipping |
| 838 | + * comments/strings), then extract the bare quoted-literal argument |
| 839 | + * immediately following it via swift_quoted_literal. Bounded to `end` so a |
| 840 | + * match can never leak past the enclosing call's own argument list into a |
| 841 | + * later, unrelated declaration. When `out_found` is non-NULL, it is set to |
| 842 | + * whether `needle` was located at all — independent of whether its value |
| 843 | + * parsed as a bare literal — so callers can tell "argument absent" (fine, |
| 844 | + * apply a default) apart from "argument present but not a literal" |
| 845 | + * (unknowable, fail closed). Returns heap string or NULL. */ |
| 846 | +static char *swift_extract_after(const char *start, const char *end, const char *needle, |
| 847 | + bool *out_found) { |
| 848 | + if (out_found) { |
| 849 | + *out_found = false; |
| 850 | + } |
| 851 | + const char *hit = swift_find_code_token(start, end, needle); |
| 852 | + if (!hit) { |
| 853 | + return NULL; |
| 854 | + } |
| 855 | + if (out_found) { |
| 856 | + *out_found = true; |
| 857 | + } |
| 858 | + return swift_quoted_literal(hit + strlen(needle), end); |
| 859 | +} |
| 860 | + |
| 861 | +/* Register `target_name` → its resolved source directory: the literal |
| 862 | + * `path:` argument the target declared, if any, else the conventional |
| 863 | + * `Sources/<target_name>` (fixed-convention, same approach parse_cargo_toml |
| 864 | + * takes for `src/lib`). `literal_path` is borrowed; caller retains |
| 865 | + * ownership. */ |
| 866 | +static void swift_register_target(const char *rel_path, const char *target_name, |
| 867 | + const char *literal_path, cbm_pkg_entries_t *entries) { |
| 868 | + if (!target_name || !target_name[0]) { |
| 869 | + return; |
| 870 | + } |
| 871 | + char suffix_buf[PKGMAP_PATH_BUF]; |
| 872 | + const char *suffix; |
| 873 | + if (literal_path && literal_path[0]) { |
| 874 | + suffix = literal_path; |
| 875 | + } else { |
| 876 | + snprintf(suffix_buf, sizeof(suffix_buf), "Sources/%s", target_name); |
| 877 | + suffix = suffix_buf; |
| 878 | + } |
| 879 | + char *entry = build_entry_path(rel_path, suffix); |
| 880 | + if (entry) { |
| 881 | + pkg_entries_push(entries, strdup(target_name), entry); |
| 882 | + } |
| 883 | +} |
| 884 | + |
| 885 | +/* Scan for `.target(name: "Foo", ...)` declarations, skipping any |
| 886 | + * `.target(` spelling that falls inside a comment or string literal. |
| 887 | + * Non-overlapping: the scan resumes AFTER each matched close paren, so a |
| 888 | + * `dependencies: [.target(name: "Bar")]` reference nested inside Foo's own |
| 889 | + * argument list is never re-visited as a second top-level target. |
| 890 | + * |
| 891 | + * A target with a `path:` argument that isn't a bare literal (computed, |
| 892 | + * concatenated, ...) is skipped entirely, even though its `name:` may be a |
| 893 | + * valid literal: SwiftPM would use the computed path, not the |
| 894 | + * `Sources/<name>` convention, and guessing that convention anyway would |
| 895 | + * mint a location that is not just unconfirmed but actively likely wrong. */ |
| 896 | +static void swift_scan_targets(const char *source, const char *end, const char *rel_path, |
| 897 | + cbm_pkg_entries_t *entries) { |
| 898 | + static const char prefix[] = ".target("; |
| 899 | + const char *cursor = source; |
| 900 | + while (cursor < end) { |
| 901 | + const char *hit = swift_find_code_token(cursor, end, prefix); |
| 902 | + if (!hit) { |
| 903 | + return; |
| 904 | + } |
| 905 | + const char *open = hit + strlen(prefix) - SKIP_ONE; |
| 906 | + const char *close = swift_match_paren(open, end); |
| 907 | + if (!close) { |
| 908 | + return; /* unbalanced — nothing further here is trustworthy */ |
| 909 | + } |
| 910 | + char *name = swift_extract_after(open, close, "name:", NULL); |
| 911 | + if (name) { |
| 912 | + bool path_present = false; |
| 913 | + char *literal_path = swift_extract_after(open, close, "path:", &path_present); |
| 914 | + if (literal_path || !path_present) { |
| 915 | + swift_register_target(rel_path, name, literal_path, entries); |
| 916 | + } |
| 917 | + free(literal_path); |
| 918 | + free(name); |
| 919 | + } |
| 920 | + cursor = close + SKIP_ONE; |
| 921 | + } |
| 922 | +} |
| 923 | + |
| 924 | +/* SwiftPM: Package.swift — targets → Sources/<name> (or their literal |
| 925 | + * `path:`). Products deliberately do not self-register: see the file |
| 926 | + * comment above swift_find_code_token. */ |
| 927 | +static void parse_package_swift(const char *source, int source_len, const char *rel_path, |
| 928 | + cbm_pkg_entries_t *entries) { |
| 929 | + const char *end = source + source_len; |
| 930 | + swift_scan_targets(source, end, rel_path, entries); |
| 931 | +} |
| 932 | + |
668 | 933 | /* ── Public: manifest detection + parsing ──────────────────────── */ |
669 | 934 |
|
670 | 935 | bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char *source, |
@@ -713,6 +978,10 @@ bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char |
713 | 978 | parse_gemspec(source, source_len, rel_path, entries); |
714 | 979 | return true; |
715 | 980 | } |
| 981 | + if (strcmp(basename, "Package.swift") == 0) { |
| 982 | + parse_package_swift(source, source_len, rel_path, entries); |
| 983 | + return true; |
| 984 | + } |
716 | 985 | return false; |
717 | 986 | } |
718 | 987 |
|
@@ -773,7 +1042,8 @@ static bool is_pkgmap_manifest_basename(const char *basename) { |
773 | 1042 | strcmp(basename, "Cargo.toml") == 0 || strcmp(basename, "pyproject.toml") == 0 || |
774 | 1043 | strcmp(basename, "composer.json") == 0 || strcmp(basename, "pubspec.yaml") == 0 || |
775 | 1044 | strcmp(basename, "pom.xml") == 0 || strcmp(basename, "build.gradle") == 0 || |
776 | | - strcmp(basename, "build.gradle.kts") == 0 || strcmp(basename, "mix.exs") == 0) { |
| 1045 | + strcmp(basename, "build.gradle.kts") == 0 || strcmp(basename, "mix.exs") == 0 || |
| 1046 | + strcmp(basename, "Package.swift") == 0) { |
777 | 1047 | return true; |
778 | 1048 | } |
779 | 1049 | return ends_with(basename, ".gemspec"); |
|
0 commit comments