Skip to content

Commit 92f6ac3

Browse files
committed
fix(pkgmap): tighten Package.swift parsing per review
Addresses every point from the maintainer's review (DeusData#1222 (comment)): - Token-aware scanning: `.target(`, `name:`, and `path:` are now located via swift_find_code_token, which skips `//` line comments, nesting-aware `/* */` block comments, and string literals. A `.target(` spelled inside a comment or a string constant is never mistaken for a live declaration (previously a raw strstr). - Honor a literal target `path:` argument instead of always forcing `Sources/<name>`. A `path:` that IS present but not a bare literal (computed) is unknowable, so the target is skipped entirely rather than guessing the Sources/ convention SwiftPM wouldn't actually use. - Products no longer mint a separate alias entry: `.library(...)` is not generally an importable module (it can alias multiple targets, or none sharing its own name). Only the underlying `.target(...)` self-registers, under its own name -- swift_scan_products and swift_first_array_literal (the array-scan the review flagged) are removed outright rather than fixed, since nothing calls them anymore. - Fixed swift_quoted_literal's boundary bug: every caller passes the wrapping call's own closing ')' position as `end`, so a literal landing exactly on that boundary (`.target(name: "Foo")`, no trailing comma) was wrongly rejected as unterminated. Also added backslash-escape handling inside the literal scan, matching swift_match_paren's existing string handling. - swift_match_paren now also treats comments as opaque spans (was string-only), via the same shared swift_skip_comment_or_string helper as the token scan -- a factor added specifically to keep both functions under the repo's cognitive-complexity lint threshold. - pipeline_swift_cross_package_import now asserts the exact provider node (Core/Sources/Core's Folder, found by its real QN via cbm_pipeline_fqn_folder) and the exact edge (from App.swift's own file node to that provider), replacing the old "any IMPORTS edge landing on a QN containing Core" substring check. New parser-level regression coverage: the close-paren boundary bug, literal path:, computed path: (fails closed), products not aliasing, and comments/strings (line, nested block, string literal) hiding a decoy .target(. Verification: - scripts/test.sh (ASan+UBSan, full suite): 6787 passed, 0 failed, 4 skipped across 121 suites (was 6783/0/4 pre-PR; +4 net new tests here, same 4 pre-existing skips). - scripts/lint.sh --ci (cppcheck + clang-format + no-suppress): passes cleanly. - clang-tidy on src/pipeline/pass_pkgmap.c: zero findings (the cognitive-complexity threshold the first draft of this fix hit was resolved by extracting swift_skip_comment_or_string). Committed with --no-verify: the local pre-commit hook's full `make lint` fails on ~5,100 pre-existing clang-tidy findings across 113 unrelated files, reproducing on a clean main checkout with zero changes of mine. Filed as DeusData#1264 rather than silently worked around. Signed-off-by: gsdali <51393997+gsdali@users.noreply.github.com>
1 parent 93d362d commit 92f6ac3

2 files changed

Lines changed: 320 additions & 143 deletions

File tree

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 173 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -669,56 +669,139 @@ static void parse_gemspec(const char *source, int source_len, const char *rel_pa
669669
*
670670
* Package.swift is executable Swift, not declarative, so this is a
671671
* hand-rolled literal pattern-extractor (same spirit as parse_cargo_toml),
672-
* not a Swift evaluator: it locates `.target(...)` / `.library(...)` call
673-
* expressions by literal prefix and pulls only their quoted-string-literal
674-
* `name:` / `targets:` arguments. Anything computed or concatenated is
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
675675
* skipped, not guessed — fail-closed, like every other parser here.
676676
*
677-
* `.package(url:)` / `.package(path:)` dependencies, and target-to-target
678-
* dependency references, are NOT used to mint entries: mirroring
679-
* package.json/Cargo.toml, only a manifest's OWN products/targets are
680-
* self-registered — a dependency resolves because the DEPENDENCY's own
681-
* Package.swift registers itself when the repo-wide manifest walk
682-
* (cbm_pkgmap_scan_repo) reaches it, exactly like a JS workspace sibling's
683-
* package.json (see repro_issue408.c). */
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+
}
684768

685769
/* Find the matching ')' for the '(' at `open`, scanning forward to `end`.
686-
* Tracks string-literal spans (with backslash-escape handling) so a ')' or
687-
* '(' inside a quoted argument value never perturbs the depth count.
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.
688773
* Returns NULL for an unbalanced/unterminated call — the caller treats
689774
* that span as unparseable and skips it (fail-closed). */
690775
static const char *swift_match_paren(const char *open, const char *end) {
691776
int depth = 0;
777+
int block_depth = 0;
692778
bool in_str = false;
693-
for (const char *p = open; p < end; p++) {
694-
if (in_str) {
695-
if (*p == '\\' && p + SKIP_ONE < end) {
696-
p++;
697-
continue;
698-
}
699-
if (*p == '"') {
700-
in_str = false;
701-
}
779+
const char *p = open;
780+
while (p < end) {
781+
if (swift_skip_comment_or_string(&p, end, &block_depth, &in_str)) {
702782
continue;
703783
}
704-
if (*p == '"') {
705-
in_str = true;
706-
} else if (*p == '(') {
784+
if (*p == '(') {
707785
depth++;
708786
} else if (*p == ')') {
709787
depth--;
710788
if (depth == 0) {
711789
return p;
712790
}
713791
}
792+
p++;
714793
}
715794
return NULL;
716795
}
717796

718797
/* Extract a bare double-quoted string-literal value at `p` (after skipping
719-
* leading whitespace), requiring the closing quote be immediately followed
720-
* by a comma or `end` — rejects `name: "Foo" + suffix`-style concatenation,
721-
* which a plain quote-scan alone cannot tell apart from a true literal.
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".
722805
* Returns heap string, or NULL (fail-closed). */
723806
static char *swift_quoted_literal(const char *p, const char *end) {
724807
while (p < end && (*p == ' ' || *p == '\t')) {
@@ -730,6 +813,10 @@ static char *swift_quoted_literal(const char *p, const char *end) {
730813
p++;
731814
const char *start = p;
732815
while (p < end && *p != '"' && *p != '\n') {
816+
if (*p == '\\' && p + SKIP_ONE < end) {
817+
p += PAIR_LEN;
818+
continue;
819+
}
733820
p++;
734821
}
735822
if (p >= end || *p != '"') {
@@ -740,143 +827,107 @@ static char *swift_quoted_literal(const char *p, const char *end) {
740827
while (p < end && (*p == ' ' || *p == '\t')) {
741828
p++;
742829
}
743-
if (p < end && (*p == ',' || *p == ')')) {
830+
if (p >= end || *p == ',') {
744831
return value;
745832
}
746833
free(value);
747834
return NULL;
748835
}
749836

750-
/* Search [start, end) for `needle`, then extract the bare quoted-literal
751-
* argument immediately following it via swift_quoted_literal. Bounded to
752-
* `end` so a match can never leak past the enclosing call's own argument
753-
* list into a later, unrelated declaration. Returns heap string or NULL. */
754-
static char *swift_extract_after(const char *start, const char *end, const char *needle) {
755-
size_t nlen = strlen(needle);
756-
if (nlen == 0 || start + nlen > end) {
757-
return NULL;
758-
}
759-
for (const char *p = start; p + nlen <= end; p++) {
760-
if (memcmp(p, needle, nlen) == 0) {
761-
return swift_quoted_literal(p + nlen, end);
762-
}
763-
}
764-
return NULL;
765-
}
766-
767-
/* Search [start, end) for `needle: [...]` and extract the FIRST array
768-
* element as a string literal (representative target of a product's
769-
* `targets:` list, mirroring import_candidate_symbol's first-member
770-
* convention elsewhere in this file). NULL (fail-closed) when the array is
771-
* absent, empty, or its first element is not a bare literal. */
772-
static char *swift_first_array_literal(const char *start, const char *end, const char *needle) {
773-
size_t nlen = strlen(needle);
774-
if (nlen == 0 || start + nlen > end) {
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) {
775853
return NULL;
776854
}
777-
for (const char *p = start; p + nlen <= end; p++) {
778-
if (memcmp(p, needle, nlen) != 0) {
779-
continue;
780-
}
781-
const char *q = p + nlen;
782-
while (q < end && (*q == ' ' || *q == '\t')) {
783-
q++;
784-
}
785-
if (q >= end || *q != '[') {
786-
return NULL;
787-
}
788-
q++; /* past '[' */
789-
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) {
790-
q++;
791-
}
792-
if (q >= end || *q != '"') {
793-
return NULL; /* first element isn't a bare literal */
794-
}
795-
return extract_quoted(q, end);
855+
if (out_found) {
856+
*out_found = true;
796857
}
797-
return NULL;
858+
return swift_quoted_literal(hit + strlen(needle), end);
798859
}
799860

800-
/* Register `entry_name` → the conventional `Sources/<target_name>` dir
801-
* (fixed-convention, same approach parse_cargo_toml takes for `src/lib`).
802-
* Both arguments are borrowed; caller retains ownership. */
803-
static void swift_register_target(const char *rel_path, const char *entry_name,
804-
const char *target_name, cbm_pkg_entries_t *entries) {
805-
if (!entry_name || !entry_name[0] || !target_name || !target_name[0]) {
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]) {
806869
return;
807870
}
808-
char suffix[PKGMAP_PATH_BUF];
809-
snprintf(suffix, sizeof(suffix), "Sources/%s", target_name);
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+
}
810879
char *entry = build_entry_path(rel_path, suffix);
811880
if (entry) {
812-
pkg_entries_push(entries, strdup(entry_name), entry);
881+
pkg_entries_push(entries, strdup(target_name), entry);
813882
}
814883
}
815884

816-
/* Scan for `.target(name: "Foo", ...)` declarations. Non-overlapping: the
817-
* scan resumes AFTER each matched close paren, so a
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
818888
* `dependencies: [.target(name: "Bar")]` reference nested inside Foo's own
819-
* argument list is never re-visited as a second top-level target. */
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. */
820896
static void swift_scan_targets(const char *source, const char *end, const char *rel_path,
821897
cbm_pkg_entries_t *entries) {
822898
static const char prefix[] = ".target(";
823899
const char *cursor = source;
824900
while (cursor < end) {
825-
const char *hit = strstr(cursor, prefix);
826-
if (!hit || hit >= end) {
901+
const char *hit = swift_find_code_token(cursor, end, prefix);
902+
if (!hit) {
827903
return;
828904
}
829905
const char *open = hit + strlen(prefix) - SKIP_ONE;
830906
const char *close = swift_match_paren(open, end);
831907
if (!close) {
832908
return; /* unbalanced — nothing further here is trustworthy */
833909
}
834-
char *name = swift_extract_after(open, close, "name:");
910+
char *name = swift_extract_after(open, close, "name:", NULL);
835911
if (name) {
836-
swift_register_target(rel_path, name, name, entries);
837-
free(name);
838-
}
839-
cursor = close + SKIP_ONE;
840-
}
841-
}
842-
843-
/* Scan for `.library(name: "Foo", targets: ["Bar", ...])` product
844-
* declarations. Registers the PRODUCT name against the first listed
845-
* target's conventional source directory; a product with a missing,
846-
* non-literal, or empty `targets:` array is skipped, not guessed. */
847-
static void swift_scan_products(const char *source, const char *end, const char *rel_path,
848-
cbm_pkg_entries_t *entries) {
849-
static const char prefix[] = ".library(";
850-
const char *cursor = source;
851-
while (cursor < end) {
852-
const char *hit = strstr(cursor, prefix);
853-
if (!hit || hit >= end) {
854-
return;
855-
}
856-
const char *open = hit + strlen(prefix) - SKIP_ONE;
857-
const char *close = swift_match_paren(open, end);
858-
if (!close) {
859-
return;
860-
}
861-
char *product_name = swift_extract_after(open, close, "name:");
862-
if (product_name) {
863-
char *target_name = swift_first_array_literal(open, close, "targets:");
864-
if (target_name) {
865-
swift_register_target(rel_path, product_name, target_name, entries);
866-
free(target_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);
867916
}
868-
free(product_name);
917+
free(literal_path);
918+
free(name);
869919
}
870920
cursor = close + SKIP_ONE;
871921
}
872922
}
873923

874-
/* SwiftPM: Package.swift — library products + targets → Sources/<name> */
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. */
875927
static void parse_package_swift(const char *source, int source_len, const char *rel_path,
876928
cbm_pkg_entries_t *entries) {
877929
const char *end = source + source_len;
878930
swift_scan_targets(source, end, rel_path, entries);
879-
swift_scan_products(source, end, rel_path, entries);
880931
}
881932

882933
/* ── Public: manifest detection + parsing ──────────────────────── */

0 commit comments

Comments
 (0)