Skip to content

Commit f925afa

Browse files
committed
Merge origin/main into fix/elixir-extraction-defects
Signed-off-by: Martin Vogel <[email protected]>
2 parents bd19732 + d807b1e commit f925afa

14 files changed

Lines changed: 1072 additions & 33 deletions

‎internal/cbm/extract_usages.c‎

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,9 +1794,25 @@ static uint32_t active_lexical_scope_id(const WalkState *state) {
17941794
static uint32_t lexical_ancestor_of_kind(const WalkState *state, uint32_t start_id,
17951795
bool want_function, bool want_block);
17961796

1797-
static bool python_default_value_reference(TSNode node) {
1798-
for (TSNode parent = ts_node_parent(node); !ts_node_is_null(parent);
1799-
parent = ts_node_parent(parent)) {
1797+
/* One step up: on the occurrence cursor when there is one, else with
1798+
* ts_node_parent, which descends from the ROOT, so a climb costs O(depth^2). */
1799+
static TSNode usage_parent_step(TSTreeCursor *cursor, TSNode current) {
1800+
if (cursor) {
1801+
return ts_tree_cursor_goto_parent(cursor) ? ts_tree_cursor_current_node(cursor)
1802+
: (TSNode){0};
1803+
}
1804+
usage_slow_parent_fallback_test_note();
1805+
return ts_node_parent(current);
1806+
}
1807+
1808+
/* Asked for every Python usage, and a deep expression puts O(depth) parents
1809+
* above each one. A tree without errors climbs them on the walk cursor, like
1810+
* python_scope_directive_kind; a tree with errors keeps ts_node_parent. */
1811+
static bool python_default_value_reference(CBMExtractCtx *ctx, WalkState *state, TSNode node) {
1812+
TSTreeCursor *cursor =
1813+
ts_node_has_error(ctx->root) ? NULL : reset_occurrence_cursor(state, node);
1814+
for (TSNode parent = usage_parent_step(cursor, node); !ts_node_is_null(parent);
1815+
parent = usage_parent_step(cursor, parent)) {
18001816
const char *kind = ts_node_type(parent);
18011817
/* A nested executable scope inside the default owns its own lookups;
18021818
* only a direct default expression is evaluated in the declaring
@@ -1817,10 +1833,10 @@ static bool python_default_value_reference(TSNode node) {
18171833
return false;
18181834
}
18191835

1820-
static uint32_t usage_lexical_scope_id_for_node(CBMExtractCtx *ctx, const WalkState *state,
1821-
TSNode node) {
1836+
static uint32_t usage_lexical_scope_id_for_node(CBMExtractCtx *ctx, WalkState *state, TSNode node) {
18221837
uint32_t scope_id = active_lexical_scope_id(state);
1823-
if (!ctx || ctx->language != CBM_LANG_PYTHON || !python_default_value_reference(node)) {
1838+
if (!ctx || ctx->language != CBM_LANG_PYTHON ||
1839+
!python_default_value_reference(ctx, state, node)) {
18241840
return scope_id;
18251841
}
18261842
uint32_t function_id = lexical_ancestor_of_kind(state, scope_id, true, false);
@@ -1896,15 +1912,43 @@ static bool ensure_lexical_binding_capacity(WalkState *state) {
18961912
}
18971913

18981914
static bool lexical_ancestor_kind(TSNode node, const char *kind) {
1899-
for (TSNode parent = ts_node_parent(node); !ts_node_is_null(parent);
1900-
parent = ts_node_parent(parent)) {
1915+
for (TSNode parent = usage_parent_step(NULL, node); !ts_node_is_null(parent);
1916+
parent = usage_parent_step(NULL, parent)) {
19011917
if (strcmp(ts_node_type(parent), kind) == 0) {
19021918
return true;
19031919
}
19041920
}
19051921
return false;
19061922
}
19071923

1924+
/* Is `node` a name listed by a Python global/nonlocal statement? The grammar
1925+
* makes those names direct children of the statement, so in a tree without
1926+
* errors one parent step on the walk cursor answers it. The full climb above
1927+
* steps with ts_node_parent, which descends from the ROOT each time: O(depth^2)
1928+
* per identifier, asked for every Python identifier, so one deep arithmetic
1929+
* expression went cubic. Error recovery can put a name under an ERROR child of
1930+
* the statement, so a tree with errors keeps the full climb. */
1931+
static CBMPythonDirectiveKind python_scope_directive_kind(CBMExtractCtx *ctx, WalkState *state,
1932+
TSNode node) {
1933+
if (ts_node_has_error(ctx->root)) {
1934+
if (lexical_ancestor_kind(node, "global_statement")) {
1935+
return CBM_PYTHON_DIRECTIVE_GLOBAL;
1936+
}
1937+
return lexical_ancestor_kind(node, "nonlocal_statement") ? CBM_PYTHON_DIRECTIVE_NONLOCAL
1938+
: 0;
1939+
}
1940+
TSNode parent;
1941+
const char *field;
1942+
if (!occurrence_parent(reset_occurrence_cursor(state, node), node, &parent, &field)) {
1943+
return 0;
1944+
}
1945+
const char *kind = ts_node_type(parent);
1946+
if (strcmp(kind, "global_statement") == 0) {
1947+
return CBM_PYTHON_DIRECTIVE_GLOBAL;
1948+
}
1949+
return strcmp(kind, "nonlocal_statement") == 0 ? CBM_PYTHON_DIRECTIVE_NONLOCAL : 0;
1950+
}
1951+
19081952
static TSNode declared_function_name_owner(TSNode node, const CBMLangSpec *spec) {
19091953
for (TSNode parent = ts_node_parent(node); !ts_node_is_null(parent);
19101954
parent = ts_node_parent(parent)) {
@@ -2251,13 +2295,11 @@ static void record_lexical_binding(CBMExtractCtx *ctx, WalkState *state, TSNode
22512295
TSNode function_declaration = declared_function_name_owner(node, spec);
22522296
TSNode class_declaration = declared_class_name_owner(node, spec);
22532297

2254-
if (ctx->language == CBM_LANG_PYTHON && (lexical_ancestor_kind(node, "global_statement") ||
2255-
lexical_ancestor_kind(node, "nonlocal_statement"))) {
2298+
CBMPythonDirectiveKind scope_directive =
2299+
ctx->language == CBM_LANG_PYTHON ? python_scope_directive_kind(ctx, state, node) : 0;
2300+
if (scope_directive) {
22562301
uint32_t function_id = lexical_ancestor_of_kind(state, current_id, true, false);
2257-
CBMPythonDirectiveKind directive = lexical_ancestor_kind(node, "global_statement")
2258-
? CBM_PYTHON_DIRECTIVE_GLOBAL
2259-
: CBM_PYTHON_DIRECTIVE_NONLOCAL;
2260-
record_python_directive(state, function_id, name, directive);
2302+
record_python_directive(state, function_id, name, scope_directive);
22612303
return;
22622304
}
22632305

@@ -2724,8 +2766,7 @@ void handle_usages(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Wal
27242766
}
27252767

27262768
bool python_scope_directive =
2727-
ctx->language == CBM_LANG_PYTHON && (lexical_ancestor_kind(node, "global_statement") ||
2728-
lexical_ancestor_kind(node, "nonlocal_statement"));
2769+
ctx->language == CBM_LANG_PYTHON && python_scope_directive_kind(ctx, state, node) != 0;
27292770
if (python_scope_directive || is_binding_occurrence(ctx, node, spec, state)) {
27302771
char *binding_name = reference_name(ctx, node);
27312772
if (binding_name && binding_name[0] && !cbm_is_keyword(binding_name, ctx->language)) {

‎src/discover/discover.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ CBMLanguage cbm_disambiguate_frm(const char *path);
5858
* CBM_LANG_RESCRIPT. On read failure, defaults to CBM_LANG_RESCRIPT. */
5959
CBMLanguage cbm_disambiguate_res(const char *path);
6060

61+
/* Disambiguate .d files by reading first 4KB of content.
62+
* Returns CBM_LANG_COUNT (not source) for a make-style dependency file written
63+
* by rustc/cargo, gcc/clang -MD or CMake ("target ...: prereq ..."), otherwise
64+
* CBM_LANG_DLANG. On read failure or any doubt, defaults to CBM_LANG_DLANG. */
65+
CBMLanguage cbm_disambiguate_d(const char *path);
66+
6167
/* Disambiguate .inc files by reading first 4KB of content.
6268
* Returns CBM_LANG_OBJECTSCRIPT_ROUTINE if it looks like an ObjectScript
6369
* include (a "ROUTINE <Uppercase>" header), otherwise CBM_LANG_BITBAKE.

‎src/discover/language.c‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,107 @@ CBMLanguage cbm_disambiguate_res(const char *path) {
14361436
return lang_res_bytes(head, n);
14371437
}
14381438

1439+
/* A make rule's targets end at a ':' followed by whitespace or the end of the
1440+
* file, which skips drive-letter colons ("C:/", "C:\"). */
1441+
static bool is_rule_colon(const char *p, bool whole_file) {
1442+
char next = p[SKIP_ONE];
1443+
return *p == ':' && (next == ' ' || next == '\t' || next == '\r' || next == '\n' ||
1444+
(next == '\0' && whole_file));
1445+
}
1446+
1447+
/* Bytes of D whitespace at p, or 0: space, tab, vertical tab, form feed, CR
1448+
* and the UTF-8 line and paragraph separators U+2028/U+2029, all of which split
1449+
* D tokens. '\n' ends the line and is the caller's. */
1450+
static size_t d_space_len(const char *p) {
1451+
if (*p == ' ' || *p == '\t' || *p == '\v' || *p == '\f' || *p == '\r') {
1452+
return SKIP_ONE;
1453+
}
1454+
if (strncmp(p, "\xE2\x80\xA8", SLEN("\xE2\x80\xA8")) == 0 ||
1455+
strncmp(p, "\xE2\x80\xA9", SLEN("\xE2\x80\xA9")) == 0) {
1456+
return SLEN("\xE2\x80\xA8");
1457+
}
1458+
return 0;
1459+
}
1460+
1461+
/* True if the line at p is a make rule "target...: prereq..." whose targets all
1462+
* look like paths (contain '/', '\' or '.'); a backslash-escaped space stays
1463+
* inside its target. D source fails this: "public:", "@safe:", "extern(C):"
1464+
* and "import a.b : c" all have a non-path word or D punctuation before the
1465+
* colon. An attribute ('@', as in "@1.0:") or a comment between tokens
1466+
* ("public/+ +/:") is D even with a '.' or '/' before the colon, so a target
1467+
* holding either is never dep-info. */
1468+
static bool is_dep_rule_line(const char *p, bool whole_file) {
1469+
bool in_target = false;
1470+
bool path_like = false;
1471+
bool any_target = false;
1472+
for (; *p && *p != '\n'; p++) {
1473+
if (is_rule_colon(p, whole_file)) {
1474+
return in_target ? path_like : any_target;
1475+
}
1476+
size_t space = d_space_len(p);
1477+
if (space) {
1478+
if (in_target && !path_like) {
1479+
return false;
1480+
}
1481+
in_target = false;
1482+
path_like = false;
1483+
p += space - SKIP_ONE;
1484+
continue;
1485+
}
1486+
if (strchr("(){};=\"',@", *p)) {
1487+
return false;
1488+
}
1489+
if (*p == '/' && (p[SKIP_ONE] == '*' || p[SKIP_ONE] == '+' || p[SKIP_ONE] == '/')) {
1490+
return false;
1491+
}
1492+
in_target = true;
1493+
any_target = true;
1494+
path_like = path_like || *p == '/' || *p == '\\' || *p == '.';
1495+
if (*p == '\\' && p[SKIP_ONE] == ' ') {
1496+
p++; /* escaped space: part of this target */
1497+
}
1498+
}
1499+
return false;
1500+
}
1501+
1502+
/* Disambiguate .d files: shared by D source and make-style dependency files
1503+
* written by rustc/cargo (target/<profile>/deps/<crate>-<hash>.d), gcc/clang -MD
1504+
* and CMake (<object>.o.d). Those are "target: prereq ..." rules, often tens of
1505+
* KB on one line, which the D grammar parses slowly into nothing but a module
1506+
* node, so they are reported as unsupported (CBM_LANG_COUNT). Defaults to D on
1507+
* any doubt (preserves existing behaviour). `n` is the count of head bytes buf
1508+
* was made from. */
1509+
static CBMLanguage lang_d_text(const char *buf, size_t n) {
1510+
/* A colon at the end of buf only ends the rule if nothing was cut off. */
1511+
bool whole_file = n < LANG_PROBE_HEAD && strlen(buf) == n;
1512+
1513+
const char *p = buf;
1514+
if (strncmp(p, "\xEF\xBB\xBF", SLEN("\xEF\xBB\xBF")) == 0) {
1515+
p += SLEN("\xEF\xBB\xBF");
1516+
}
1517+
while (*p && isspace((unsigned char)*p)) {
1518+
p++;
1519+
}
1520+
/* A shebang or a leading D comment (including the DUB single-file
1521+
* "/+ dub.sdl:" recipe) is D; dep-info never starts that way. */
1522+
if (*p == '#' ||
1523+
(p[0] == '/' && (p[SKIP_ONE] == '/' || p[SKIP_ONE] == '*' || p[SKIP_ONE] == '+'))) {
1524+
return CBM_LANG_DLANG;
1525+
}
1526+
return is_dep_rule_line(p, whole_file) ? CBM_LANG_COUNT : CBM_LANG_DLANG;
1527+
}
1528+
1529+
CBMLanguage cbm_disambiguate_d(const char *path) {
1530+
unsigned char head[LANG_PROBE_HEAD];
1531+
size_t n = 0;
1532+
if (!lang_read_head(path, head, sizeof(head), &n)) {
1533+
return CBM_LANG_DLANG;
1534+
}
1535+
char buf[LANG_PROBE_HEAD + SKIP_ONE];
1536+
lang_head_text(buf, LANG_PROBE_HEAD, head, n);
1537+
return lang_d_text(buf, n);
1538+
}
1539+
14391540
/* Disambiguate .cls files: shared by InterSystems ObjectScript UDL, Salesforce
14401541
* Apex and Visual Basic 6 class modules (#721). ObjectScript class files begin
14411542
* with a line of the form "Class <UppercasePackage>..."; VB6 class modules
@@ -1611,6 +1712,13 @@ static const char *lang_probe_extension(const char *filename) {
16111712
return (dot && lang_name_in(dot, PROBED)) ? dot : NULL;
16121713
}
16131714

1715+
/* .d is shared by D and make-style dep-info (cargo, gcc -MD). Probe only while
1716+
* .d maps to D, so a user override to another language wins. */
1717+
static bool lang_probe_d(CBMLanguage lang, const char *filename) {
1718+
const char *dot = strrchr(filename, '.');
1719+
return lang == CBM_LANG_DLANG && dot && strcmp(dot, ".d") == 0;
1720+
}
1721+
16141722
/* "<Export generator=" in the first line-probe bytes: an ObjectScript Studio
16151723
* export, which is XML by name. */
16161724
static bool lang_objectscript_export(const unsigned char *head, size_t n) {
@@ -1633,6 +1741,9 @@ size_t cbm_language_probe_bytes_with(const cbm_userconfig_t *config, const char
16331741
if (ext) {
16341742
return strcmp(ext, ".cfc") == 0 ? LANG_PROBE_CFC : LANG_PROBE_HEAD;
16351743
}
1744+
if (lang_probe_d(lang, filename)) {
1745+
return LANG_PROBE_HEAD;
1746+
}
16361747
return lang == CBM_LANG_XML ? LANG_PROBE_LINE : 0;
16371748
}
16381749

@@ -1676,6 +1787,11 @@ CBMLanguage cbm_language_classify_with(const cbm_userconfig_t *config, const cha
16761787
/* .res: ReScript, or a binary Godot / Windows resource (#2176) */
16771788
lang = readable ? lang_res_bytes(head, head_len) : CBM_LANG_RESCRIPT;
16781789
}
1790+
} else if (lang_probe_d(lang, filename)) {
1791+
/* D, or a make-style dep-info file */
1792+
char buf[LANG_PROBE_HEAD + SKIP_ONE];
1793+
lang_head_text(buf, LANG_PROBE_HEAD, head, head_len);
1794+
lang = readable ? lang_d_text(buf, head_len) : CBM_LANG_DLANG;
16791795
}
16801796
if (lang == CBM_LANG_XML && readable && lang_objectscript_export(head, head_len)) {
16811797
return CBM_LANG_OBJECTSCRIPT_EXPORT;

0 commit comments

Comments
 (0)