Skip to content

Commit 47474b6

Browse files
authored
Merge pull request DeusData#2826 from knewstimek/fix/inc-c-family-routing
fix(discover): select C++ for C-family .inc fragments
2 parents a33878c + d841ddd commit 47474b6

5 files changed

Lines changed: 247 additions & 27 deletions

File tree

‎src/discover/discover.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ CBMLanguage cbm_disambiguate_res(const char *path);
6565
CBMLanguage cbm_disambiguate_d(const char *path);
6666

6767
/* Disambiguate .inc files by reading first 4KB of content.
68-
* Returns CBM_LANG_OBJECTSCRIPT_ROUTINE if it looks like an ObjectScript
69-
* include (a "ROUTINE <Uppercase>" header), otherwise CBM_LANG_BITBAKE.
68+
* Returns CBM_LANG_CPP for explicit C-family code, or
69+
* CBM_LANG_OBJECTSCRIPT_ROUTINE for ObjectScript markers / a lone #define.
70+
* BitBake recipe syntax and unknown content retain CBM_LANG_BITBAKE.
7071
* On read failure, defaults to CBM_LANG_BITBAKE. */
7172
CBMLanguage cbm_disambiguate_inc(const char *path);
7273

‎src/discover/language.c‎

Lines changed: 137 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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. */
15851683
static 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

16101722
CBMLanguage 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 */

‎tests/test_extraction.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,29 @@ TEST(extract_c_macros_issue375) {
251251
PASS();
252252
}
253253

254+
TEST(extract_cpp_inc_linkage_and_callback) {
255+
CBMFileResult *r =
256+
extract("#define SAMPLE_CALLBACK(type) type\n"
257+
"extern \"C\" __declspec(dllexport) int sample_export() { return 0; }\n"
258+
"static SAMPLE_CALLBACK(int) sample_callback() { return sample_export(); }\n",
259+
CBM_LANG_CPP, "sample", "runtime/sample.inc");
260+
ASSERT_NOT_NULL(r);
261+
ASSERT_TRUE(has_def(r, "Function", "sample_export"));
262+
ASSERT_TRUE(has_def(r, "Function", "sample_callback"));
263+
ASSERT_FALSE(has_def_any(r, "extern"));
264+
cbm_free_result(r);
265+
266+
/* A statement include has no enclosing function in the file. Keep any
267+
* syntax coverage diagnostic rather than inventing a callable owner. */
268+
r = extract("#ifdef ENABLE_SAMPLE\nif (sample_check()) return 1;\n#endif\n", CBM_LANG_CPP,
269+
"sample", "runtime/guard.inc");
270+
ASSERT_NOT_NULL(r);
271+
ASSERT_FALSE(has_def_any(r, "extern"));
272+
ASSERT_FALSE(has_def(r, "Function", "if"));
273+
cbm_free_result(r);
274+
PASS();
275+
}
276+
254277
TEST(extract_cpp_macros_issue375) {
255278
CBMFileResult *r = extract("#define MAX(a, b) ((a) > (b) ? (a) : (b))\n"
256279
"#define PI 3.14159\n"
@@ -13559,6 +13582,7 @@ SUITE(extraction) {
1355913582
RUN_TEST(traversal_stack_not_in_result_arena_issue2010);
1356013583
RUN_TEST(extract_c_macro_hidden_call_survives_preprocessed_pass_issue2010);
1356113584
RUN_TEST(extract_c_macros_issue375);
13585+
RUN_TEST(extract_cpp_inc_linkage_and_callback);
1356213586
RUN_TEST(extract_cpp_macros_issue375);
1356313587
RUN_TEST(extract_cpp_functionlike_macro_type_arg_no_false_parse_partial_issue1071);
1356413588
RUN_TEST(extract_cpp_real_in_body_error_still_flagged_issue1071);

‎tests/test_language.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,55 @@ static CBMLanguage classify_content(const char *name, const char *content, size_
14571457

14581458
typedef CBMLanguage (*path_probe_fn)(const char *path);
14591459

1460+
/* Exercise the shared classifier and both disk entry points. All sources are
1461+
* synthetic; the recipe bodies deliberately contain misleading C-like code. */
1462+
TEST(lang_inc_c_family_and_existing_dialects) {
1463+
static const struct {
1464+
const char *content;
1465+
CBMLanguage expected;
1466+
} cases[] = {
1467+
{"#include \"sample.h\"\nextern \"C\" int sample_export() { return 0; }\n", CBM_LANG_CPP},
1468+
{"extern \"C\" __declspec(dllexport) int sample_export() { return 0; }\n", CBM_LANG_CPP},
1469+
{"#define SAMPLE_CALLBACK(T) T\nstatic SAMPLE_CALLBACK(int) sample() { return 0; }\n",
1470+
CBM_LANG_CPP},
1471+
{"/* in-function fragment */\n#ifdef SAMPLE_ENABLED\nif (check()) return 1;\n#endif\n",
1472+
CBM_LANG_CPP},
1473+
{"int sample(void) { return 0; }\n", CBM_LANG_CPP},
1474+
{" \t# include <stdint.h>\r\n", CBM_LANG_CPP},
1475+
{"/* example:\nextern \"C\" int example();\n*/\nSUMMARY = \"sample\"\n", CBM_LANG_BITBAKE},
1476+
{"// extern \"C\" example only\n", CBM_LANG_BITBAKE},
1477+
{"#include_extra \"sample.h\"\n", CBM_LANG_BITBAKE},
1478+
{"#define Value 1\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1479+
{"#ifndef SAMPLE\n#define SAMPLE 1\n#endif\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1480+
{"ROUTINE Sample [Type=INC]\n#define Value 1\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1481+
{"#def1arg Value(%x) %x\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1482+
{"#; ObjectScript comment\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1483+
{"#include <sample.h>\n#def1arg Value(%x) %x\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1484+
{"# recipe\nSUMMARY = \"sample\"\nrequire base.inc\n", CBM_LANG_BITBAKE},
1485+
{"SRC_URI += \"file://sample.c\"\ndo_compile() {\nif (check) { return; }\n}\n",
1486+
CBM_LANG_BITBAKE},
1487+
{"python do_generate() {\nif (ready):\n pass\n}\n", CBM_LANG_BITBAKE},
1488+
{"do_compile() {\ncat <<'EOF'\n#include <stdio.h>\nstatic int sample;\nEOF\n}\n",
1489+
CBM_LANG_BITBAKE},
1490+
{"require base.inc\nextern \"C\" example in recipe text\n", CBM_LANG_BITBAKE},
1491+
{"", CBM_LANG_BITBAKE},
1492+
};
1493+
char path[512];
1494+
snprintf(path, sizeof(path), "%s/cbm_lang_inc_routing.inc", cbm_tmpdir());
1495+
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
1496+
size_t len = strlen(cases[i].content);
1497+
ASSERT_EQ(classify_content("sample.inc", cases[i].content, len), cases[i].expected);
1498+
ASSERT_TRUE(write_exact(path, cases[i].content, len));
1499+
CBMLanguage by_probe = cbm_disambiguate_inc(path);
1500+
CBMLanguage by_file = cbm_language_for_file("sample.inc", path);
1501+
remove(path);
1502+
ASSERT_EQ(by_probe, cases[i].expected);
1503+
ASSERT_EQ(by_file, cases[i].expected);
1504+
}
1505+
ASSERT_EQ(cbm_disambiguate_inc(NULL), CBM_LANG_BITBAKE);
1506+
PASS();
1507+
}
1508+
14601509
TEST(lang_probe_bytes_follow_the_name) {
14611510
ASSERT_EQ(cbm_language_probe_bytes("main.go"), 0);
14621511
ASSERT_EQ(cbm_language_probe_bytes("Makefile"), 0);
@@ -1879,6 +1928,7 @@ SUITE(language) {
18791928
RUN_TEST(lang_ext_sosl);
18801929

18811930
RUN_TEST(lang_all_have_names);
1931+
RUN_TEST(lang_inc_c_family_and_existing_dialects);
18821932
RUN_TEST(lang_probe_bytes_follow_the_name);
18831933
RUN_TEST(lang_classify_matches_every_content_rule);
18841934
RUN_TEST(lang_classify_unreadable_content_keeps_the_name_default);

‎tests/test_pipeline.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29159,7 +29159,40 @@ TEST(pipeline_calls_args_overlong_argument_stops_cleanly) {
2915929159
PASS();
2916029160
}
2916129161

29162+
/* Discovery must choose the C++ grammar before the pipeline extracts .inc
29163+
* symbols; a direct extractor-only test would miss that routing failure. */
29164+
TEST(pipeline_inc_c_family_fragments) {
29165+
char tmp[256];
29166+
snprintf(tmp, sizeof(tmp), "%s/cbm_inc_cpp_XXXXXX", cbm_tmpdir());
29167+
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
29168+
write_temp_file(tmp, "runtime.inc",
29169+
"#define SAMPLE_CALLBACK(T) T\n"
29170+
"extern \"C\" int sample_export() { return 0; }\n"
29171+
"static SAMPLE_CALLBACK(int) sample_callback() { return sample_export(); }\n");
29172+
write_temp_file(tmp, "main.cpp", "#include \"runtime.inc\"\n");
29173+
char db_path[512];
29174+
snprintf(db_path, sizeof(db_path), "%s/graph.db", tmp);
29175+
const cbm_index_mode_t modes[] = {CBM_MODE_FAST, CBM_MODE_FULL};
29176+
for (size_t i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
29177+
cbm_pipeline_t *pipeline = cbm_pipeline_new(tmp, db_path, modes[i]);
29178+
ASSERT_NOT_NULL(pipeline);
29179+
ASSERT_EQ(cbm_pipeline_run(pipeline), 0);
29180+
cbm_store_t *store = cbm_store_open_path(db_path);
29181+
ASSERT_NOT_NULL(store);
29182+
const char *project = cbm_pipeline_project_name(pipeline);
29183+
ASSERT_EQ(fixture_node_count(store, project, "runtime.inc", "sample_export", "Function"),
29184+
1);
29185+
ASSERT_EQ(fixture_node_count(store, project, "runtime.inc", "sample_callback", "Function"),
29186+
1);
29187+
ASSERT_EQ(named_node_count(store, project, "extern"), 0);
29188+
cbm_store_close(store);
29189+
cbm_pipeline_free(pipeline);
29190+
}
29191+
th_rmtree(tmp);
29192+
PASS();
29193+
}
2916229194
SUITE(pipeline) {
29195+
RUN_TEST(pipeline_inc_c_family_fragments);
2916329196
RUN_TEST(pipeline_python_module_callers_sequential);
2916429197
RUN_TEST(pipeline_python_module_callers_parallel);
2916529198
RUN_TEST(pipeline_python_class_body_calls_sequential);

0 commit comments

Comments
 (0)