Skip to content

Commit aaf9021

Browse files
authored
Merge pull request DeusData#1730 from henry-hz/upstream-pr/elixir-channels
fix(elixir): Phoenix channel extraction is unreachable in both branches
2 parents 4edc6b7 + f0f88b9 commit aaf9021

2 files changed

Lines changed: 123 additions & 11 deletions

File tree

‎internal/cbm/extract_channels.c‎

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,42 @@ static void elixir_emit_second_arg(CBMExtractCtx *ctx, TSNode call, TSNode args,
901901
}
902902
}
903903

904-
static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
904+
/* tree-sitter-elixir gives a call's arguments node NO field name — its whole
905+
* field set is key, left, operand, operator, quoted_start, quoted_end, right,
906+
* target and value — so the generic field lookup is always null here and every
907+
* channel below was silently skipped. extract_defs.c has carried the same
908+
* positional fallback for the definition side since Elixir was added. */
909+
static TSNode elixir_channel_call_args(TSNode call) {
910+
TSNode args = ts_node_child_by_field_name(call, TS_FIELD("arguments"));
911+
if (ts_node_is_null(args) && ts_node_child_count(call) > 1) {
912+
args = ts_node_child(call, 1);
913+
}
914+
return args;
915+
}
916+
917+
/* The callee of an Elixir `call`. The grammar labels it `target`; child(0) is
918+
* the same node and is what extract_defs.c reads, so accept either. */
919+
static TSNode elixir_channel_call_target(TSNode call) {
905920
TSNode target = ts_node_child_by_field_name(call, TS_FIELD("target"));
921+
if (ts_node_is_null(target) && ts_node_child_count(call) > 0) {
922+
target = ts_node_child(call, 0);
923+
}
924+
return target;
925+
}
926+
927+
/* Elixir def* macros. `def`/`defp` cover Phoenix channel callbacks; the macro
928+
* forms are included because `handle_in` is generated by macros in some
929+
* libraries. */
930+
static bool elixir_channel_is_def_macro(const char *macro) {
931+
if (!macro) {
932+
return false;
933+
}
934+
return strcmp(macro, "def") == 0 || strcmp(macro, "defp") == 0 ||
935+
strcmp(macro, "defmacro") == 0 || strcmp(macro, "defmacrop") == 0;
936+
}
937+
938+
static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
939+
TSNode target = elixir_channel_call_target(call);
906940
if (ts_node_is_null(target)) {
907941
return;
908942
}
@@ -911,7 +945,7 @@ static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
911945
return;
912946
}
913947

914-
TSNode args = ts_node_child_by_field_name(call, TS_FIELD("arguments"));
948+
TSNode args = elixir_channel_call_args(call);
915949

916950
if (strstr(target_text, "PubSub.broadcast") || strstr(target_text, "PubSub.local_broadcast")) {
917951
elixir_emit_second_arg(ctx, call, args, "phoenix_pubsub", CBM_CHANNEL_EMIT);
@@ -927,18 +961,39 @@ static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
927961
}
928962
}
929963

930-
/* Detect handle_in("event", payload, socket) — Phoenix Channel listener */
931-
static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode func_def) {
932-
TSNode name_node = ts_node_child_by_field_name(func_def, TS_FIELD("name"));
933-
if (ts_node_is_null(name_node)) {
964+
/* Detect handle_in("event", payload, socket) — Phoenix Channel listener.
965+
* `def_call` is the `call` node of a def* macro. Elixir has no `def` node type
966+
* and no `name`/`parameters` fields: a definition is a call whose first
967+
* argument is the head, itself a call of name(params) — wrapped in a `when`
968+
* binary_operator when the clause carries a guard. */
969+
static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode def_call) {
970+
TSNode args = elixir_channel_call_args(def_call);
971+
if (ts_node_is_null(args) || ts_node_named_child_count(args) == 0) {
972+
return;
973+
}
974+
TSNode head = ts_node_named_child(args, 0);
975+
if (strcmp(ts_node_type(head), "binary_operator") == 0) {
976+
TSNode op = ts_node_child_by_field_name(head, TS_FIELD("operator"));
977+
TSNode left = ts_node_child_by_field_name(head, TS_FIELD("left"));
978+
if (!ts_node_is_null(op) && !ts_node_is_null(left) &&
979+
ts_node_end_byte(op) - ts_node_start_byte(op) == 4 &&
980+
strncmp(ctx->source + ts_node_start_byte(op), "when", 4) == 0) {
981+
head = left;
982+
}
983+
}
984+
if (strcmp(ts_node_type(head), "call") != 0 || ts_node_child_count(head) == 0) {
985+
return;
986+
}
987+
TSNode name_node = ts_node_child(head, 0);
988+
if (ts_node_is_null(name_node) || strcmp(ts_node_type(name_node), "identifier") != 0) {
934989
return;
935990
}
936991
char *name = cbm_node_text(ctx->arena, name_node, ctx->source);
937992
if (!name || strcmp(name, "handle_in") != 0) {
938993
return;
939994
}
940995
/* First parameter is the event name pattern */
941-
TSNode params = ts_node_child_by_field_name(func_def, TS_FIELD("parameters"));
996+
TSNode params = elixir_channel_call_args(head);
942997
if (ts_node_is_null(params)) {
943998
return;
944999
}
@@ -952,7 +1007,7 @@ static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode func_def) {
9521007
val = literal_from_first_child(ctx, first_param);
9531008
}
9541009
if (val) {
955-
push_channel(ctx, val, "phoenix_channel", CBM_CHANNEL_LISTEN, func_def);
1010+
push_channel(ctx, val, "phoenix_channel", CBM_CHANNEL_LISTEN, def_call);
9561011
}
9571012
}
9581013

@@ -964,10 +1019,18 @@ static void extract_channels_elixir(CBMExtractCtx *ctx) {
9641019
while (stack.count > 0) {
9651020
TSNode node = ts_nstack_pop(&stack);
9661021
const char *kind = ts_node_type(node);
1022+
/* Everything in Elixir is a `call`, including `def`. The old dispatch
1023+
* tested for a "def" node type the grammar does not have, so the
1024+
* handle_in listener branch was unreachable. */
9671025
if (strcmp(kind, "call") == 0) {
968-
elixir_process_call(ctx, node);
969-
} else if (strcmp(kind, "def") == 0) {
970-
elixir_process_function_def(ctx, node);
1026+
TSNode target = elixir_channel_call_target(node);
1027+
char *macro =
1028+
ts_node_is_null(target) ? NULL : cbm_node_text(ctx->arena, target, ctx->source);
1029+
if (elixir_channel_is_def_macro(macro)) {
1030+
elixir_process_function_def(ctx, node);
1031+
} else {
1032+
elixir_process_call(ctx, node);
1033+
}
9711034
}
9721035
uint32_t count = ts_node_child_count(node);
9731036
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {

‎tests/test_extraction.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,54 @@ TEST(elixir_call_string_argument) {
18481848
PASS();
18491849
}
18501850

1851+
/* True when a channel with this name/transport/direction was extracted. */
1852+
static int has_channel(CBMFileResult *r, const char *name, const char *transport,
1853+
CBMChannelDirection dir) {
1854+
for (int i = 0; i < r->channels.count; i++) {
1855+
const CBMChannel *c = &r->channels.items[i];
1856+
if (c->channel_name && strcmp(c->channel_name, name) == 0 && c->transport &&
1857+
strcmp(c->transport, transport) == 0 && c->direction == dir)
1858+
return 1;
1859+
}
1860+
return 0;
1861+
}
1862+
1863+
/* Phoenix pub/sub and channel listeners. Both branches were unreachable: the
1864+
* emit side looked up an "arguments" FIELD that tree-sitter-elixir does not
1865+
* define, and the listener side dispatched on a "def" NODE TYPE that does not
1866+
* exist (Elixir defs are `call` nodes), so a Phoenix app produced no channels
1867+
* at all. */
1868+
TEST(elixir_channels_pubsub_and_handle_in) {
1869+
CBMFileResult *r =
1870+
extract("defmodule Acme.Notifier do\n"
1871+
" def announce(p) do\n"
1872+
" Phoenix.PubSub.broadcast(Acme.PubSub, \"user:created\", p)\n"
1873+
" end\n"
1874+
" def watch, do: Phoenix.PubSub.subscribe(Acme.PubSub, \"user:updated\")\n"
1875+
"end\n"
1876+
"defmodule AcmeWeb.RoomChannel do\n"
1877+
" def handle_in(\"new_msg\", payload, socket) do\n"
1878+
" push(socket, \"pong\", %{})\n"
1879+
" {:noreply, socket}\n"
1880+
" end\n"
1881+
" def handle_in(\"ping\", _p, socket) when is_map(socket) do\n"
1882+
" {:noreply, socket}\n"
1883+
" end\n"
1884+
"end\n",
1885+
CBM_LANG_ELIXIR, "t", "chan.ex");
1886+
ASSERT_NOT_NULL(r);
1887+
ASSERT_FALSE(r->has_error);
1888+
ASSERT(has_channel(r, "user:created", "phoenix_pubsub", CBM_CHANNEL_EMIT));
1889+
ASSERT(has_channel(r, "user:updated", "phoenix_pubsub", CBM_CHANNEL_LISTEN));
1890+
/* handle_in clauses are listeners — including the guarded one. */
1891+
ASSERT(has_channel(r, "new_msg", "phoenix_channel", CBM_CHANNEL_LISTEN));
1892+
ASSERT(has_channel(r, "ping", "phoenix_channel", CBM_CHANNEL_LISTEN));
1893+
/* A bare push/broadcast inside the channel module is an emit. */
1894+
ASSERT(has_channel(r, "pong", "phoenix_channel", CBM_CHANNEL_EMIT));
1895+
cbm_free_result(r);
1896+
PASS();
1897+
}
1898+
18511899
/* --- Haskell --- */
18521900
TEST(haskell_function) {
18531901
CBMFileResult *r = extract("add :: Int -> Int -> Int\nadd x y = x + y\n\nmultiply :: Int -> "
@@ -13290,6 +13338,7 @@ SUITE(extraction) {
1329013338
/* Functional */
1329113339
RUN_TEST(elixir_function);
1329213340
RUN_TEST(elixir_call_string_argument);
13341+
RUN_TEST(elixir_channels_pubsub_and_handle_in);
1329313342
RUN_TEST(haskell_function);
1329413343
RUN_TEST(haskell_infix_definition_name);
1329513344
RUN_TEST(ocaml_function);

0 commit comments

Comments
 (0)