Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Merge branch 'main' into fix/dotd-depinfo-sniff

Nelson Spencec461ed00d40f
diff --cc src/discover/language.c
index c0f6f1894,116cec024..b5a383137
--- a/src/discover/language.c
+++ b/src/discover/language.c
@@@ -1314,124 -1357,20 +1357,121 @@@ CBMLanguage cbm_disambiguate_frm(const
* format for imported meshes) and Windows compiled resource files. Binary
* content has no ReScript meaning, and a NUL byte never occurs in ReScript
* text while both binary formats carry NULs in their first bytes. */
- CBMLanguage cbm_disambiguate_res(const char *path) {
- if (!path) {
- return CBM_LANG_RESCRIPT;
- }
+ static CBMLanguage lang_res_bytes(const unsigned char *head, size_t n) {
+ size_t len = n < LANG_PROBE_HEAD ? n : LANG_PROBE_HEAD;
+ return (len > 0 && memchr(head, '\0', len)) ? CBM_LANG_COUNT : CBM_LANG_RESCRIPT;
+ }
- FILE *f = cbm_fopen(path, "rb");
- if (!f) {
+ CBMLanguage cbm_disambiguate_res(const char *path) {
+ unsigned char head[LANG_PROBE_HEAD];
+ size_t n = 0;
+ if (!lang_read_head(path, head, sizeof(head), &n)) {
return CBM_LANG_RESCRIPT;
}
-
- char buf[CBM_SZ_4K];
- size_t n = fread(buf, SKIP_ONE, sizeof(buf), f);
- (void)fclose(f);
- return memchr(buf, '\0', n) ? CBM_LANG_COUNT : CBM_LANG_RESCRIPT;
+ return lang_res_bytes(head, n);
}
+/* A make rule's targets end at a ':' followed by whitespace or the end of the
+ * file, which skips drive-letter colons ("C:/", "C:\"). */
+static bool is_rule_colon(const char *p, bool whole_file) {
+ char next = p[SKIP_ONE];
+ return *p == ':' && (next == ' ' || next == '\t' || next == '\r' || next == '\n' ||
+ (next == '\0' && whole_file));
+}
+
+/* Bytes of D whitespace at p, or 0: space, tab, vertical tab, form feed, CR
+ * and the UTF-8 line and paragraph separators U+2028/U+2029, all of which split
+ * D tokens. '\n' ends the line and is the caller's. */
+static size_t d_space_len(const char *p) {
+ if (*p == ' ' || *p == '\t' || *p == '\v' || *p == '\f' || *p == '\r') {
+ return SKIP_ONE;
+ }
+ if (strncmp(p, "\xE2\x80\xA8", SLEN("\xE2\x80\xA8")) == 0 ||
+ strncmp(p, "\xE2\x80\xA9", SLEN("\xE2\x80\xA9")) == 0) {
+ return SLEN("\xE2\x80\xA8");
+ }
+ return 0;
+}
+
+/* True if the line at p is a make rule "target...: prereq..." whose targets all
+ * look like paths (contain '/', '\' or '.'); a backslash-escaped space stays
+ * inside its target. D source fails this: "public:", "@safe:", "extern(C):"
+ * and "import a.b : c" all have a non-path word or D punctuation before the
+ * colon. An attribute ('@', as in "@1.0:") or a comment between tokens
+ * ("public/+ +/:") is D even with a '.' or '/' before the colon, so a target
+ * holding either is never dep-info. */
+static bool is_dep_rule_line(const char *p, bool whole_file) {
+ bool in_target = false;
+ bool path_like = false;
+ bool any_target = false;
+ for (; *p && *p != '\n'; p++) {
+ if (is_rule_colon(p, whole_file)) {
+ return in_target ? path_like : any_target;
+ }
+ size_t space = d_space_len(p);
+ if (space) {
+ if (in_target && !path_like) {
+ return false;
+ }
+ in_target = false;
+ path_like = false;
+ p += space - SKIP_ONE;
+ continue;
+ }
+ if (strchr("(){};=\"',@", *p)) {
+ return false;
+ }
+ if (*p == '/' && (p[SKIP_ONE] == '*' || p[SKIP_ONE] == '+' || p[SKIP_ONE] == '/')) {
+ return false;
+ }
+ in_target = true;
+ any_target = true;
+ path_like = path_like || *p == '/' || *p == '\\' || *p == '.';
+ if (*p == '\\' && p[SKIP_ONE] == ' ') {
+ p++; /* escaped space: part of this target */
+ }
+ }
+ return false;
+}
+
+/* Disambiguate .d files: shared by D source and make-style dependency files
+ * written by rustc/cargo (target/<profile>/deps/<crate>-<hash>.d), gcc/clang -MD
+ * and CMake (<object>.o.d). Those are "target: prereq ..." rules, often tens of
+ * KB on one line, which the D grammar parses slowly into nothing but a module
+ * node, so they are reported as unsupported (CBM_LANG_COUNT). Defaults to D on
- * any doubt (preserves existing behaviour). */
- CBMLanguage cbm_disambiguate_d(const char *path) {
- if (!path) {
- return CBM_LANG_DLANG;
- }
-
- FILE *f = cbm_fopen(path, "r");
- if (!f) {
- return CBM_LANG_DLANG;
- }
-
- char buf[CBM_SZ_4K + SKIP_ONE];
- size_t n = fread(buf, SKIP_ONE, CBM_SZ_4K, f);
- buf[n] = '\0';
- (void)fclose(f);
++ * any doubt (preserves existing behaviour). `n` is the count of head bytes buf
++ * was made from. */
++static CBMLanguage lang_d_text(const char *buf, size_t n) {
+ /* A colon at the end of buf only ends the rule if nothing was cut off. */
- bool whole_file = n < CBM_SZ_4K && strlen(buf) == n;
++ bool whole_file = n < LANG_PROBE_HEAD && strlen(buf) == n;
+
+ const char *p = buf;
+ if (strncmp(p, "\xEF\xBB\xBF", SLEN("\xEF\xBB\xBF")) == 0) {
+ p += SLEN("\xEF\xBB\xBF");
+ }
+ while (*p && isspace((unsigned char)*p)) {
+ p++;
+ }
+ /* A shebang or a leading D comment (including the DUB single-file
+ * "/+ dub.sdl:" recipe) is D; dep-info never starts that way. */
+ if (*p == '#' ||
+ (p[0] == '/' && (p[SKIP_ONE] == '/' || p[SKIP_ONE] == '*' || p[SKIP_ONE] == '+'))) {
+ return CBM_LANG_DLANG;
+ }
+ return is_dep_rule_line(p, whole_file) ? CBM_LANG_COUNT : CBM_LANG_DLANG;
+}
+
++CBMLanguage cbm_disambiguate_d(const char *path) {
++ unsigned char head[LANG_PROBE_HEAD];
++ size_t n = 0;
++ if (!lang_read_head(path, head, sizeof(head), &n)) {
++ return CBM_LANG_DLANG;
++ }
++ char buf[LANG_PROBE_HEAD + SKIP_ONE];
++ lang_head_text(buf, LANG_PROBE_HEAD, head, n);
++ return lang_d_text(buf, n);
++}
++
/* Disambiguate .cls files: shared by InterSystems ObjectScript UDL, Salesforce
* Apex and Visual Basic 6 class modules (#721). ObjectScript class files begin
* with a line of the form "Class <UppercasePackage>..."; VB6 class modules
@@@ -1589,3 -1506,149 +1607,164 @@@ static CBMLanguage lang_cfc_text(const
}
return CBM_LANG_CFSCRIPT;
}
+
+ CBMLanguage cbm_disambiguate_cfc(const char *path) {
+ unsigned char head[LANG_PROBE_CFC];
+ size_t n = 0;
+ if (!lang_read_head(path, head, sizeof(head), &n)) {
+ return CBM_LANG_CFSCRIPT;
+ }
+ char buf[LANG_PROBE_CFC + SKIP_ONE];
+ lang_head_text(buf, LANG_PROBE_CFC, head, n);
+ return lang_cfc_text(buf);
+ }
+
+ /* ── One classifier for a name plus its first bytes ──────────────── */
+
+ /* JSON files that are configuration, not source worth indexing. */
+ static const char *const IGNORED_JSON_FILES[] = {
+ "package.json", "package-lock.json", "tsconfig.json",
+ "jsconfig.json", "composer.json", "composer.lock",
+ "yarn.lock", "openapi.json", "swagger.json",
+ "jest.config.json", ".eslintrc.json", ".prettierrc.json",
+ ".babelrc.json", "tslint.json", "angular.json",
+ "firebase.json", "renovate.json", "lerna.json",
+ "turbo.json", ".stylelintrc.json", "pnpm-lock.json",
+ "deno.json", "biome.json", "devcontainer.json",
+ ".devcontainer.json", "launch.json", "settings.json",
+ "extensions.json", "tasks.json", NULL};
+
+ static bool lang_name_in(const char *name, const char *const *list) {
+ for (int i = 0; list[i]; i++) {
+ if (strcmp(name, list[i]) == 0) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /* The content rule a name needs, or NULL. */
+ static const char *lang_probe_extension(const char *filename) {
+ static const char *const PROBED[] = {".m", ".cls", ".inc", ".cfc", ".frm", ".res", NULL};
+ const char *dot = strrchr(filename, '.');
+ return (dot && lang_name_in(dot, PROBED)) ? dot : NULL;
+ }
+
++/* .d is shared by D and make-style dep-info (cargo, gcc -MD). Probe only while
++ * .d maps to D, so a user override to another language wins. */
++static bool lang_probe_d(CBMLanguage lang, const char *filename) {
++ const char *dot = strrchr(filename, '.');
++ return lang == CBM_LANG_DLANG && dot && strcmp(dot, ".d") == 0;
++}
++
+ /* "<Export generator=" in the first line-probe bytes: an ObjectScript Studio
+ * export, which is XML by name. */
+ static bool lang_objectscript_export(const unsigned char *head, size_t n) {
+ char buf[LANG_PROBE_LINE + SKIP_ONE];
+ lang_head_text(buf, LANG_PROBE_LINE, head, n);
+ return strstr(buf, "<Export generator=") != NULL;
+ }
+
+ _Static_assert(LANG_PROBE_CFC == CBM_LANGUAGE_PROBE_MAX, "the longest probe is the public bound");
+
+ size_t cbm_language_probe_bytes_with(const cbm_userconfig_t *config, const char *filename) {
+ if (!filename) {
+ return 0;
+ }
+ CBMLanguage lang = lang_for_filename(config, filename);
+ if (lang == CBM_LANG_COUNT) {
+ return LANG_PROBE_LINE; /* the shebang fallback */
+ }
+ const char *ext = lang_probe_extension(filename);
+ if (ext) {
+ return strcmp(ext, ".cfc") == 0 ? LANG_PROBE_CFC : LANG_PROBE_HEAD;
+ }
++ if (lang_probe_d(lang, filename)) {
++ return LANG_PROBE_HEAD;
++ }
+ return lang == CBM_LANG_XML ? LANG_PROBE_LINE : 0;
+ }
+
+ CBMLanguage cbm_language_classify_with(const cbm_userconfig_t *config, const char *filename,
+ const unsigned char *head, size_t head_len, bool more,
+ bool readable) {
+ if (!filename) {
+ return CBM_LANG_COUNT;
+ }
+ if (!readable || !head) {
+ head = NULL;
+ head_len = 0;
+ }
+ CBMLanguage lang = lang_for_filename(config, filename);
+ if (lang == CBM_LANG_COUNT) {
+ /* The name says nothing: a conservative shebang probe, so extensionless
+ * scripts get indexed (#1199). The name stays authoritative otherwise. */
+ return readable ? lang_shebang(head, head_len, more) : CBM_LANG_COUNT;
+ }
+ const char *ext = lang_probe_extension(filename);
+ if (ext) {
+ char buf[LANG_PROBE_CFC + SKIP_ONE];
+ bool cfc = strcmp(ext, ".cfc") == 0;
+ lang_head_text(buf, cfc ? LANG_PROBE_CFC : LANG_PROBE_HEAD, head, head_len);
+ if (strcmp(ext, ".m") == 0) {
+ /* Objective-C, Magma or MATLAB */
+ lang = readable ? lang_m_text(buf) : CBM_LANG_MATLAB;
+ } else if (strcmp(ext, ".cls") == 0) {
+ /* ObjectScript UDL, Apex or a Visual Basic 6 class module */
+ lang = readable ? lang_cls_text(buf) : CBM_LANG_APEX;
+ } else if (strcmp(ext, ".inc") == 0) {
+ /* BitBake or an ObjectScript include */
+ lang = readable ? lang_inc_text(buf) : CBM_LANG_BITBAKE;
+ } else if (cfc) {
+ /* script or tag dialect */
+ lang = readable ? lang_cfc_text(buf) : CBM_LANG_CFSCRIPT;
+ } else if (strcmp(ext, ".frm") == 0) {
+ /* FORM or a Visual Basic 6 form (#721) */
+ lang = readable ? lang_frm_text(buf) : CBM_LANG_FORM;
+ } else {
+ /* .res: ReScript, or a binary Godot / Windows resource (#2176) */
+ lang = readable ? lang_res_bytes(head, head_len) : CBM_LANG_RESCRIPT;
+ }
++ } else if (lang_probe_d(lang, filename)) {
++ /* D, or a make-style dep-info file */
++ char buf[LANG_PROBE_HEAD + SKIP_ONE];
++ lang_head_text(buf, LANG_PROBE_HEAD, head, head_len);
++ lang = readable ? lang_d_text(buf, head_len) : CBM_LANG_DLANG;
+ }
+ if (lang == CBM_LANG_XML && readable && lang_objectscript_export(head, head_len)) {
+ return CBM_LANG_OBJECTSCRIPT_EXPORT;
+ }
+ if (lang == CBM_LANG_JSON && lang_name_in(filename, IGNORED_JSON_FILES)) {
+ return CBM_LANG_COUNT;
+ }
+ return lang;
+ }
+
+ size_t cbm_language_probe_bytes(const char *filename) {
+ return cbm_language_probe_bytes_with(cbm_get_user_lang_config(), filename);
+ }
+
+ CBMLanguage cbm_language_classify(const char *filename, const unsigned char *head, size_t head_len,
+ bool more, bool readable) {
+ return cbm_language_classify_with(cbm_get_user_lang_config(), filename, head, head_len, more,
+ readable);
+ }
+
+ CBMLanguage cbm_language_for_file(const char *filename, const char *path) {
+ size_t probe = cbm_language_probe_bytes(filename);
+ if (probe == 0) {
+ return cbm_language_classify(filename, NULL, 0, false, true);
+ }
+ /* One byte past the probe tells a file that ends there from a longer one. */
+ unsigned char head[LANG_PROBE_CFC + SKIP_ONE];
+ size_t n = 0;
+ FILE *f = path ? cbm_fopen(path, "rb") : NULL;
+ if (!f) {
+ return cbm_language_classify(filename, NULL, 0, false, false);
+ }
+ n = fread(head, SKIP_ONE, probe + SKIP_ONE, f);
+ bool failed = ferror(f) != 0;
+ (void)fclose(f);
+ bool more = n > probe;
+ return cbm_language_classify(filename, head, more ? probe : n, more, !failed);
+ }