@@ -1573,38 +1573,150 @@ CBMLanguage cbm_disambiguate_cls(const char *path) {
15731573 return lang_cls_text (buf );
15741574}
15751575
1576- /* Disambiguate .inc files: shared by BitBake include fragments and
1577- * InterSystems ObjectScript include (macro) files. ObjectScript .inc files are
1578- * predominantly macro definitions ("#define NAME ..." / "#def1arg NAME ...");
1579- * some also carry a "ROUTINE <Name>" header. The macro-preprocessor directives
1580- * are the strongest signal because that is the primary content of an .inc file,
1581- * whereas BitBake uses '#' only for "# comment" lines (always '#' + space).
1582- * We therefore match ObjectScript preprocessor directives ('#' immediately
1583- * followed by 'def'/';'), which BitBake never produces. Defaults to BitBake on
1584- * any doubt (preserves existing behaviour). */
1576+ /* A complete leading token, followed by whitespace or punctuation, within a
1577+ * single line. In particular #include_extra is not #include. */
1578+ static const char * lang_inc_word (const char * line , const char * end , const char * word ) {
1579+ size_t n = strlen (word );
1580+ if ((size_t )(end - line ) < n || strncmp (line , word , n ) != 0 ||
1581+ (line + n < end && (isalnum ((unsigned char )line [n ]) || line [n ] == '_' ))) {
1582+ return NULL ;
1583+ }
1584+ line += n ;
1585+ while (line < end && isspace ((unsigned char )* line )) {
1586+ line ++ ;
1587+ }
1588+ return line ;
1589+ }
1590+
1591+ /* Recipes can embed C in quoted strings or shell/Python tasks. Recognize the
1592+ * recipe's own syntax before inspecting those bodies as standalone C code. */
1593+ static bool lang_inc_recipe_line (const char * line , const char * end ) {
1594+ static const char * const words [] = {"require" , "include" , "inherit" , "addtask" , "python" , NULL };
1595+ for (int i = 0 ; words [i ]; i ++ ) {
1596+ const char * tail = lang_inc_word (line , end , words [i ]);
1597+ if (tail && tail > line + strlen (words [i ])) {
1598+ return true;
1599+ }
1600+ }
1601+ if ((size_t )(end - line ) >= SLEN ("do_" ) && strncmp (line , "do_" , SLEN ("do_" )) == 0 ) {
1602+ return true;
1603+ }
1604+ const char * p = line ;
1605+ while (p < end && (isalnum ((unsigned char )* p ) || * p == '_' || * p == ':' || * p == '[' ||
1606+ * p == ']' || * p == '-' || * p == '.' )) {
1607+ p ++ ;
1608+ }
1609+ if (p == line ) {
1610+ return false;
1611+ }
1612+ while (p < end && isspace ((unsigned char )* p )) {
1613+ p ++ ;
1614+ }
1615+ return p < end && (* p == '=' || ((* p == '?' || * p == '+' || * p == ':' || * p == '.' ) &&
1616+ p + SKIP_ONE < end && p [SKIP_ONE ] == '=' ));
1617+ }
1618+
1619+ /* Skip only leading whitespace and C comments, keeping block-comment state
1620+ * across lines. Recipe strings and bodies are handled before looking for C. */
1621+ static const char * lang_inc_code (const char * line , const char * end , bool * block_comment ) {
1622+ while (line < end ) {
1623+ if (* block_comment ) {
1624+ const char * close = strstr (line , "*/" );
1625+ if (!close || close >= end ) {
1626+ return end ;
1627+ }
1628+ line = close + SLEN ("*/" );
1629+ * block_comment = false;
1630+ continue ;
1631+ }
1632+ if (isspace ((unsigned char )* line )) {
1633+ line ++ ;
1634+ continue ;
1635+ }
1636+ if ((size_t )(end - line ) >= SLEN ("/*" ) && strncmp (line , "/*" , SLEN ("/*" )) == 0 ) {
1637+ * block_comment = true;
1638+ line += SLEN ("/*" );
1639+ continue ;
1640+ }
1641+ break ;
1642+ }
1643+ return line ;
1644+ }
1645+
1646+ static bool lang_inc_c_code (const char * line , const char * end ) {
1647+ static const char * const words [] = {
1648+ "extern" , "static" , "typedef" , "namespace" , "template" ,
1649+ "struct" , "class" , "int" , "void" , NULL ,
1650+ };
1651+ for (int i = 0 ; words [i ]; i ++ ) {
1652+ const char * tail = lang_inc_word (line , end , words [i ]);
1653+ if (tail && tail < end && * tail != '=' ) {
1654+ return true;
1655+ }
1656+ }
1657+ const char * tail = lang_inc_word (line , end , "if" );
1658+ return tail && tail < end && * tail == '(' ;
1659+ }
1660+
1661+ static bool lang_inc_directive (const char * line , const char * end , bool * c_family ,
1662+ bool * objectscript_define ) {
1663+ line += SKIP_ONE ; /* '#' */
1664+ while (line < end && isspace ((unsigned char )* line )) {
1665+ line ++ ;
1666+ }
1667+ if (lang_inc_word (line , end , "def1arg" ) || (line < end && * line == ';' )) {
1668+ return true; /* Explicit ObjectScript directive. */
1669+ }
1670+ if (lang_inc_word (line , end , "define" )) {
1671+ * objectscript_define = true;
1672+ }
1673+ const char * tail = lang_inc_word (line , end , "include" );
1674+ if (tail && tail < end && (* tail == '"' || * tail == '<' )) {
1675+ * c_family = true;
1676+ }
1677+ return false;
1678+ }
1679+
1680+ /* .inc is shared by C/C++, BitBake and ObjectScript. Select C++ for explicit
1681+ * C-family code (including C fragments); do not infer an enclosing include
1682+ * context. A lone #define and unknown content retain their existing defaults. */
15851683static CBMLanguage lang_inc_text (const char * buf ) {
1684+ bool c_family = false;
1685+ bool objectscript_define = false;
1686+ bool block_comment = false;
15861687 const char * line = buf ;
15871688 while (* line ) {
1588- /* ObjectScript include header: a line beginning "ROUTINE <Uppercase>". */
1589- if (strncmp (line , "ROUTINE " , SLEN ("ROUTINE " )) == 0 &&
1590- isupper ((unsigned char )line [SLEN ("ROUTINE " )])) {
1591- return CBM_LANG_OBJECTSCRIPT_ROUTINE ;
1689+ const char * end = strchr (line , '\n' );
1690+ if (!end ) {
1691+ end = line + strlen (line );
1692+ }
1693+ const char * code = lang_inc_code (line , end , & block_comment );
1694+ line = * end ? end + SKIP_ONE : end ;
1695+ if (code == end ) {
1696+ continue ;
15921697 }
1593- /* ObjectScript macro directives — the primary content of .inc files.
1594- * "#define"/"#def1arg" (macro defs) and "#;" (line comment). BitBake's
1595- * only '#' use is "# comment" (hash + space), so these never collide. */
1596- if (strncmp (line , "#define" , SLEN ("#define" )) == 0 ||
1597- strncmp (line , "#def1arg" , SLEN ("#def1arg" )) == 0 ||
1598- strncmp (line , "#;" , SLEN ("#;" )) == 0 ) {
1698+ const char * tail = lang_inc_word (code , end , "ROUTINE" );
1699+ if (tail && tail < end && isupper ((unsigned char )* tail )) {
15991700 return CBM_LANG_OBJECTSCRIPT_ROUTINE ;
16001701 }
1601- const char * nl = strchr (line , '\n' );
1602- if (!nl ) {
1603- break ;
1702+ if (* code == '#' ) {
1703+ if (lang_inc_directive (code , end , & c_family , & objectscript_define )) {
1704+ return CBM_LANG_OBJECTSCRIPT_ROUTINE ;
1705+ }
1706+ continue ;
16041707 }
1605- line = nl + SKIP_ONE ;
1708+ if ((size_t )(end - code ) >= SLEN ("//" ) && strncmp (code , "//" , SLEN ("//" )) == 0 ) {
1709+ continue ;
1710+ }
1711+ if (!c_family && lang_inc_recipe_line (code , end )) {
1712+ return CBM_LANG_BITBAKE ;
1713+ }
1714+ c_family = c_family || lang_inc_c_code (code , end );
1715+ }
1716+ if (c_family ) {
1717+ return CBM_LANG_CPP ;
16061718 }
1607- return CBM_LANG_BITBAKE ;
1719+ return objectscript_define ? CBM_LANG_OBJECTSCRIPT_ROUTINE : CBM_LANG_BITBAKE ;
16081720}
16091721
16101722CBMLanguage cbm_disambiguate_inc (const char * path ) {
@@ -1775,7 +1887,7 @@ CBMLanguage cbm_language_classify_with(const cbm_userconfig_t *config, const cha
17751887 /* ObjectScript UDL, Apex or a Visual Basic 6 class module */
17761888 lang = readable ? lang_cls_text (buf ) : CBM_LANG_APEX ;
17771889 } else if (strcmp (ext , ".inc" ) == 0 ) {
1778- /* BitBake or an ObjectScript include */
1890+ /* C/C++, BitBake or an ObjectScript include */
17791891 lang = readable ? lang_inc_text (buf ) : CBM_LANG_BITBAKE ;
17801892 } else if (cfc ) {
17811893 /* script or tag dialect */
0 commit comments