Skip to content

Commit 28b4093

Browse files
authored
Merge pull request DeusData#1976 from CaptainMittens/fix/swift-nested-url-constructor
fix(extraction): reach a Swift URL built by a constructor
2 parents bf4c476 + eb80a4e commit 28b4093

3 files changed

Lines changed: 160 additions & 9 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,61 @@ static const char *extract_binary_concat_suffix(CBMExtractCtx *ctx, TSNode node)
22802280
}
22812281

22822282
// Try to extract URL/topic from a positional argument (string or constant).
2283+
/* Swift names its call arguments, so each one is a value_argument that may lead
2284+
* with a value_argument_label — the `from:` in `data(from: url)`. Return the
2285+
* value itself, and leave any other node exactly as it came in. */
2286+
static TSNode swift_argument_value(TSNode arg) {
2287+
if (strcmp(ts_node_type(arg), "value_argument") != 0 || ts_node_named_child_count(arg) == 0) {
2288+
return arg;
2289+
}
2290+
TSNode val = ts_node_named_child(arg, 0);
2291+
if (strcmp(ts_node_type(val), "value_argument_label") == 0 &&
2292+
ts_node_named_child_count(arg) > 1) {
2293+
val = ts_node_named_child(arg, 1);
2294+
}
2295+
return val;
2296+
}
2297+
2298+
/* Swift has no URL literal, so almost no real code passes a bare string to a
2299+
* request. It writes `URL(string: "https://…")!` instead, and the literal then
2300+
* sits two levels down: past the trailing `!`, which the grammar models as a
2301+
* postfix_expression, and inside the constructor's own argument list.
2302+
*
2303+
* Unwrap both so that literal is as reachable as a bare one. Only the three
2304+
* Foundation types that take a URL string are unwrapped — any other call keeps
2305+
* its own meaning, and a non-literal argument such as `URL(string: base + path)`
2306+
* falls through to the ordinary handling unchanged. */
2307+
static TSNode swift_unwrap_url_constructor(CBMExtractCtx *ctx, TSNode arg) {
2308+
/* Step past a trailing "!" or "?". */
2309+
if (strcmp(ts_node_type(arg), "postfix_expression") == 0) {
2310+
TSNode target = ts_node_child_by_field_name(arg, TS_FIELD("target"));
2311+
if (!ts_node_is_null(target)) {
2312+
arg = target;
2313+
}
2314+
}
2315+
if (strcmp(ts_node_type(arg), "call_expression") != 0) {
2316+
return arg;
2317+
}
2318+
TSNode callee = ts_node_named_child(arg, 0);
2319+
if (ts_node_is_null(callee) || strcmp(ts_node_type(callee), "simple_identifier") != 0) {
2320+
return arg;
2321+
}
2322+
const char *name = cbm_node_text(ctx->arena, callee, ctx->source);
2323+
if (!name || (strcmp(name, "URL") != 0 && strcmp(name, "URLComponents") != 0 &&
2324+
strcmp(name, "URLRequest") != 0)) {
2325+
return arg;
2326+
}
2327+
TSNode suffix = cbm_find_child_by_kind(arg, "call_suffix");
2328+
if (ts_node_is_null(suffix)) {
2329+
return arg;
2330+
}
2331+
TSNode inner = cbm_find_child_by_kind(suffix, "value_arguments");
2332+
if (ts_node_is_null(inner) || ts_node_named_child_count(inner) == 0) {
2333+
return arg;
2334+
}
2335+
return swift_argument_value(ts_node_named_child(inner, 0));
2336+
}
2337+
22832338
static const char *extract_positional_url(CBMExtractCtx *ctx, TSNode arg, const char *ak) {
22842339
/* JS/TS template literals: `/things/${id}` normalizes to "/things/{}" so the
22852340
* client URL joins the server route's canonical placeholder (issue #1006). */
@@ -2323,15 +2378,12 @@ static const char *extract_url_or_topic_arg(CBMExtractCtx *ctx, TSNode args) {
23232378
}
23242379
/* Swift wraps each argument in a value_argument that may lead with its
23252380
* label, so `data(from: url)` would otherwise yield the label `from`
2326-
* rather than the value. Step past a leading value_argument_label. */
2327-
if (strcmp(ts_node_type(arg), "value_argument") == 0 &&
2328-
ts_node_named_child_count(arg) > 0) {
2329-
TSNode val = ts_node_named_child(arg, 0);
2330-
if (strcmp(ts_node_type(val), "value_argument_label") == 0 &&
2331-
ts_node_named_child_count(arg) > 1) {
2332-
val = ts_node_named_child(arg, 1);
2333-
}
2334-
arg = val;
2381+
* rather than the value. */
2382+
arg = swift_argument_value(arg);
2383+
/* A Swift URL is usually built rather than written bare, and the
2384+
* literal then sits inside that constructor. */
2385+
if (ctx->language == CBM_LANG_SWIFT) {
2386+
arg = swift_unwrap_url_constructor(ctx, arg);
23352387
}
23362388
const char *ak = ts_node_type(arg);
23372389

‎tests/test_extraction.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4459,6 +4459,52 @@ TEST(swift_labeled_call_string_arg_issue1892) {
44594459
PASS();
44604460
}
44614461

4462+
/* Swift has no URL literal, so real code builds one and force-unwraps it. The
4463+
* string then sits two levels below the argument list. */
4464+
TEST(swift_nested_url_constructor_issue1892) {
4465+
CBMFileResult *r = extract("func fetch() { URLSession.shared.dataTask(with: URL(string: "
4466+
"\"https://example.com/api/v1/widgets\")!) }\n",
4467+
CBM_LANG_SWIFT, "t", "Fetch.swift");
4468+
ASSERT_NOT_NULL(r);
4469+
ASSERT_FALSE(r->has_error);
4470+
const CBMCall *c = find_call_by_callee(r, "URLSession.shared.dataTask");
4471+
ASSERT_NOT_NULL(c);
4472+
ASSERT_NOT_NULL(c->first_string_arg);
4473+
ASSERT_STR_EQ(c->first_string_arg, "https://example.com/api/v1/widgets");
4474+
cbm_free_result(r);
4475+
PASS();
4476+
}
4477+
4478+
/* Without the trailing "!" the constructor is not wrapped in a
4479+
* postfix_expression, so this covers the other shape. */
4480+
TEST(swift_nested_url_no_bang_issue1892) {
4481+
CBMFileResult *r =
4482+
extract("func fetch() { client.send(to: URLRequest(url: \"/api/v1/widgets/1\")) }\n",
4483+
CBM_LANG_SWIFT, "t", "Send.swift");
4484+
ASSERT_NOT_NULL(r);
4485+
ASSERT_FALSE(r->has_error);
4486+
const CBMCall *c = find_call_by_callee(r, "client.send");
4487+
ASSERT_NOT_NULL(c);
4488+
ASSERT_NOT_NULL(c->first_string_arg);
4489+
ASSERT_STR_EQ(c->first_string_arg, "/api/v1/widgets/1");
4490+
cbm_free_result(r);
4491+
PASS();
4492+
}
4493+
4494+
/* A constructor that is not one of the three URL types keeps its own meaning:
4495+
* the outer call must not borrow the inner call's string. */
4496+
TEST(swift_non_url_constructor_untouched_issue1892) {
4497+
CBMFileResult *r = extract("func f() { log.write(to: Formatter(pattern: \"%s-%d\")) }\n",
4498+
CBM_LANG_SWIFT, "t", "Log.swift");
4499+
ASSERT_NOT_NULL(r);
4500+
ASSERT_FALSE(r->has_error);
4501+
const CBMCall *c = find_call_by_callee(r, "log.write");
4502+
ASSERT_NOT_NULL(c);
4503+
ASSERT_NULL(c->first_string_arg);
4504+
cbm_free_result(r);
4505+
PASS();
4506+
}
4507+
44624508
/* Issue #1009: URL-builder helper pattern — a function returning a URL-shaped
44634509
* literal, consumed as client(buildPath(id)). The builder's URL is recorded in
44644510
* the per-file constant map and resolved at the call site, for both return
@@ -8280,6 +8326,9 @@ SUITE(extraction) {
82808326
RUN_TEST(swift_chained_call);
82818327
RUN_TEST(swift_force_unwrap_scanner_shift);
82828328
RUN_TEST(swift_call_string_arg_issue1892);
8329+
RUN_TEST(swift_nested_url_constructor_issue1892);
8330+
RUN_TEST(swift_nested_url_no_bang_issue1892);
8331+
RUN_TEST(swift_non_url_constructor_untouched_issue1892);
82838332
RUN_TEST(swift_labeled_call_string_arg_issue1892);
82848333
RUN_TEST(objc_interface);
82858334
RUN_TEST(objc_implementation);

‎tests/test_pipeline.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6745,6 +6745,55 @@ TEST(pipeline_native_fetch_classified_as_http_calls) {
67456745
* no call arguments at all. Alamofire/URLSession were already in the service
67466746
* pattern table; the URL simply never reached it. This is the Swift twin of
67476747
* the TypeScript fetch case above. */
6748+
/* The shape real Swift actually writes: the URL is built by a constructor and
6749+
* force-unwrapped, so the literal is two levels below the argument list. This
6750+
* is what issue #1892 reported from a real project. */
6751+
TEST(pipeline_swift_nested_url_makes_route_issue1892) {
6752+
char tmp[256];
6753+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_swiftnested_XXXXXX");
6754+
if (!cbm_mkdtemp(tmp)) {
6755+
FAIL("tmpdir");
6756+
}
6757+
6758+
write_temp_file(tmp, "Sources/Client.swift",
6759+
"import Foundation\n"
6760+
"final class Client {\n"
6761+
" func listWidgets() {\n"
6762+
" URLSession.shared.dataTask(with: "
6763+
"URL(string: \"/api/v1/widgets\")!)\n"
6764+
" }\n"
6765+
"}\n");
6766+
6767+
char db_path[512];
6768+
snprintf(db_path, sizeof(db_path), "%s/swiftnested.db", tmp);
6769+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
6770+
ASSERT_NOT_NULL(p);
6771+
ASSERT_EQ(cbm_pipeline_run(p), 0);
6772+
const char *project = cbm_pipeline_project_name(p);
6773+
6774+
cbm_store_t *s = cbm_store_open_path(db_path);
6775+
ASSERT_NOT_NULL(s);
6776+
6777+
ASSERT_GTE(cbm_store_count_edges_by_type(s, project, "HTTP_CALLS"), 1);
6778+
6779+
cbm_node_t *routes = NULL;
6780+
int route_count = 0;
6781+
cbm_store_find_nodes_by_label(s, project, "Route", &routes, &route_count);
6782+
int widget_routes = 0;
6783+
for (int i = 0; i < route_count; i++) {
6784+
if (routes[i].qualified_name && strstr(routes[i].qualified_name, "/api/v1/widgets")) {
6785+
widget_routes++;
6786+
}
6787+
}
6788+
cbm_store_free_nodes(routes, route_count);
6789+
ASSERT_GTE(widget_routes, 1);
6790+
6791+
cbm_store_close(s);
6792+
cbm_pipeline_free(p);
6793+
th_rmtree(tmp);
6794+
PASS();
6795+
}
6796+
67486797
TEST(pipeline_swift_http_call_makes_route_issue1892) {
67496798
char tmp[256];
67506799
snprintf(tmp, sizeof(tmp), "/tmp/cbm_swifthttp_XXXXXX");
@@ -15081,6 +15130,7 @@ SUITE(pipeline) {
1508115130
RUN_TEST(pipeline_parallel_rust_cross_only_macro_hidden_gets_synthetic_carrier);
1508215131
RUN_TEST(pipeline_arg_url_rejects_non_http_slash_arguments);
1508315132
RUN_TEST(pipeline_native_fetch_classified_as_http_calls);
15133+
RUN_TEST(pipeline_swift_nested_url_makes_route_issue1892);
1508415134
RUN_TEST(pipeline_swift_http_call_makes_route_issue1892);
1508515135
RUN_TEST(pipeline_native_fetch_parallel_classified_as_http_calls);
1508615136
RUN_TEST(pipeline_local_fetch_shadow_not_classified_as_http);

0 commit comments

Comments
 (0)