@@ -1472,38 +1472,150 @@ CBMLanguage cbm_disambiguate_cls(const char *path) {
14721472 return lang_cls_text (buf );
14731473}
14741474
1475- /* Disambiguate .inc files: shared by BitBake include fragments and
1476- * InterSystems ObjectScript include (macro) files. ObjectScript .inc files are
1477- * predominantly macro definitions ("#define NAME ..." / "#def1arg NAME ...");
1478- * some also carry a "ROUTINE <Name>" header. The macro-preprocessor directives
1479- * are the strongest signal because that is the primary content of an .inc file,
1480- * whereas BitBake uses '#' only for "# comment" lines (always '#' + space).
1481- * We therefore match ObjectScript preprocessor directives ('#' immediately
1482- * followed by 'def'/';'), which BitBake never produces. Defaults to BitBake on
1483- * any doubt (preserves existing behaviour). */
1475+ /* A complete leading token, followed by whitespace or punctuation, within a
1476+ * single line. In particular #include_extra is not #include. */
1477+ static const char * lang_inc_word (const char * line , const char * end , const char * word ) {
1478+ size_t n = strlen (word );
1479+ if ((size_t )(end - line ) < n || strncmp (line , word , n ) != 0 ||
1480+ (line + n < end && (isalnum ((unsigned char )line [n ]) || line [n ] == '_' ))) {
1481+ return NULL ;
1482+ }
1483+ line += n ;
1484+ while (line < end && isspace ((unsigned char )* line )) {
1485+ line ++ ;
1486+ }
1487+ return line ;
1488+ }
1489+
1490+ /* Recipes can embed C in quoted strings or shell/Python tasks. Recognize the
1491+ * recipe's own syntax before inspecting those bodies as standalone C code. */
1492+ static bool lang_inc_recipe_line (const char * line , const char * end ) {
1493+ static const char * const words [] = {"require" , "include" , "inherit" , "addtask" , "python" , NULL };
1494+ for (int i = 0 ; words [i ]; i ++ ) {
1495+ const char * tail = lang_inc_word (line , end , words [i ]);
1496+ if (tail && tail > line + strlen (words [i ])) {
1497+ return true;
1498+ }
1499+ }
1500+ if ((size_t )(end - line ) >= SLEN ("do_" ) && strncmp (line , "do_" , SLEN ("do_" )) == 0 ) {
1501+ return true;
1502+ }
1503+ const char * p = line ;
1504+ while (p < end && (isalnum ((unsigned char )* p ) || * p == '_' || * p == ':' || * p == '[' ||
1505+ * p == ']' || * p == '-' || * p == '.' )) {
1506+ p ++ ;
1507+ }
1508+ if (p == line ) {
1509+ return false;
1510+ }
1511+ while (p < end && isspace ((unsigned char )* p )) {
1512+ p ++ ;
1513+ }
1514+ return p < end && (* p == '=' || ((* p == '?' || * p == '+' || * p == ':' || * p == '.' ) &&
1515+ p + SKIP_ONE < end && p [SKIP_ONE ] == '=' ));
1516+ }
1517+
1518+ /* Skip only leading whitespace and C comments, keeping block-comment state
1519+ * across lines. Recipe strings and bodies are handled before looking for C. */
1520+ static const char * lang_inc_code (const char * line , const char * end , bool * block_comment ) {
1521+ while (line < end ) {
1522+ if (* block_comment ) {
1523+ const char * close = strstr (line , "*/" );
1524+ if (!close || close >= end ) {
1525+ return end ;
1526+ }
1527+ line = close + SLEN ("*/" );
1528+ * block_comment = false;
1529+ continue ;
1530+ }
1531+ if (isspace ((unsigned char )* line )) {
1532+ line ++ ;
1533+ continue ;
1534+ }
1535+ if ((size_t )(end - line ) >= SLEN ("/*" ) && strncmp (line , "/*" , SLEN ("/*" )) == 0 ) {
1536+ * block_comment = true;
1537+ line += SLEN ("/*" );
1538+ continue ;
1539+ }
1540+ break ;
1541+ }
1542+ return line ;
1543+ }
1544+
1545+ static bool lang_inc_c_code (const char * line , const char * end ) {
1546+ static const char * const words [] = {
1547+ "extern" , "static" , "typedef" , "namespace" , "template" ,
1548+ "struct" , "class" , "int" , "void" , NULL ,
1549+ };
1550+ for (int i = 0 ; words [i ]; i ++ ) {
1551+ const char * tail = lang_inc_word (line , end , words [i ]);
1552+ if (tail && tail < end && * tail != '=' ) {
1553+ return true;
1554+ }
1555+ }
1556+ const char * tail = lang_inc_word (line , end , "if" );
1557+ return tail && tail < end && * tail == '(' ;
1558+ }
1559+
1560+ static bool lang_inc_directive (const char * line , const char * end , bool * c_family ,
1561+ bool * objectscript_define ) {
1562+ line += SKIP_ONE ; /* '#' */
1563+ while (line < end && isspace ((unsigned char )* line )) {
1564+ line ++ ;
1565+ }
1566+ if (lang_inc_word (line , end , "def1arg" ) || (line < end && * line == ';' )) {
1567+ return true; /* Explicit ObjectScript directive. */
1568+ }
1569+ if (lang_inc_word (line , end , "define" )) {
1570+ * objectscript_define = true;
1571+ }
1572+ const char * tail = lang_inc_word (line , end , "include" );
1573+ if (tail && tail < end && (* tail == '"' || * tail == '<' )) {
1574+ * c_family = true;
1575+ }
1576+ return false;
1577+ }
1578+
1579+ /* .inc is shared by C/C++, BitBake and ObjectScript. Select C++ for explicit
1580+ * C-family code (including C fragments); do not infer an enclosing include
1581+ * context. A lone #define and unknown content retain their existing defaults. */
14841582static CBMLanguage lang_inc_text (const char * buf ) {
1583+ bool c_family = false;
1584+ bool objectscript_define = false;
1585+ bool block_comment = false;
14851586 const char * line = buf ;
14861587 while (* line ) {
1487- /* ObjectScript include header: a line beginning "ROUTINE <Uppercase>". */
1488- if (strncmp (line , "ROUTINE " , SLEN ("ROUTINE " )) == 0 &&
1489- isupper ((unsigned char )line [SLEN ("ROUTINE " )])) {
1490- return CBM_LANG_OBJECTSCRIPT_ROUTINE ;
1588+ const char * end = strchr (line , '\n' );
1589+ if (!end ) {
1590+ end = line + strlen (line );
1591+ }
1592+ const char * code = lang_inc_code (line , end , & block_comment );
1593+ line = * end ? end + SKIP_ONE : end ;
1594+ if (code == end ) {
1595+ continue ;
14911596 }
1492- /* ObjectScript macro directives — the primary content of .inc files.
1493- * "#define"/"#def1arg" (macro defs) and "#;" (line comment). BitBake's
1494- * only '#' use is "# comment" (hash + space), so these never collide. */
1495- if (strncmp (line , "#define" , SLEN ("#define" )) == 0 ||
1496- strncmp (line , "#def1arg" , SLEN ("#def1arg" )) == 0 ||
1497- strncmp (line , "#;" , SLEN ("#;" )) == 0 ) {
1597+ const char * tail = lang_inc_word (code , end , "ROUTINE" );
1598+ if (tail && tail < end && isupper ((unsigned char )* tail )) {
14981599 return CBM_LANG_OBJECTSCRIPT_ROUTINE ;
14991600 }
1500- const char * nl = strchr (line , '\n' );
1501- if (!nl ) {
1502- break ;
1601+ if (* code == '#' ) {
1602+ if (lang_inc_directive (code , end , & c_family , & objectscript_define )) {
1603+ return CBM_LANG_OBJECTSCRIPT_ROUTINE ;
1604+ }
1605+ continue ;
15031606 }
1504- line = nl + SKIP_ONE ;
1607+ if ((size_t )(end - code ) >= SLEN ("//" ) && strncmp (code , "//" , SLEN ("//" )) == 0 ) {
1608+ continue ;
1609+ }
1610+ if (!c_family && lang_inc_recipe_line (code , end )) {
1611+ return CBM_LANG_BITBAKE ;
1612+ }
1613+ c_family = c_family || lang_inc_c_code (code , end );
1614+ }
1615+ if (c_family ) {
1616+ return CBM_LANG_CPP ;
15051617 }
1506- return CBM_LANG_BITBAKE ;
1618+ return objectscript_define ? CBM_LANG_OBJECTSCRIPT_ROUTINE : CBM_LANG_BITBAKE ;
15071619}
15081620
15091621CBMLanguage cbm_disambiguate_inc (const char * path ) {
@@ -1664,7 +1776,7 @@ CBMLanguage cbm_language_classify_with(const cbm_userconfig_t *config, const cha
16641776 /* ObjectScript UDL, Apex or a Visual Basic 6 class module */
16651777 lang = readable ? lang_cls_text (buf ) : CBM_LANG_APEX ;
16661778 } else if (strcmp (ext , ".inc" ) == 0 ) {
1667- /* BitBake or an ObjectScript include */
1779+ /* C/C++, BitBake or an ObjectScript include */
16681780 lang = readable ? lang_inc_text (buf ) : CBM_LANG_BITBAKE ;
16691781 } else if (cfc ) {
16701782 /* script or tag dialect */
0 commit comments