@@ -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. */
16161724static 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