Skip to content

Commit 51fd445

Browse files
committed
fix(discover): select C++ for C-family include fragments
Recognize explicit C-family code in .inc files before extraction while preserving BitBake recipe bodies and ObjectScript markers. Exercise disk and byte classifiers, callback/linkage extraction and fast/full indexing. Signed-off-by: knewstimek <knewstimek@users.noreply.github.com>
1 parent dca2a44 commit 51fd445

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
@@ -59,8 +59,9 @@ CBMLanguage cbm_disambiguate_frm(const char *path);
5959
CBMLanguage cbm_disambiguate_res(const char *path);
6060

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

‎src/discover/language.c‎

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

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

‎tests/test_extraction.c‎

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

253+
TEST(extract_cpp_inc_linkage_and_callback) {
254+
CBMFileResult *r =
255+
extract("#define SAMPLE_CALLBACK(type) type\n"
256+
"extern \"C\" __declspec(dllexport) int sample_export() { return 0; }\n"
257+
"static SAMPLE_CALLBACK(int) sample_callback() { return sample_export(); }\n",
258+
CBM_LANG_CPP, "sample", "runtime/sample.inc");
259+
ASSERT_NOT_NULL(r);
260+
ASSERT_TRUE(has_def(r, "Function", "sample_export"));
261+
ASSERT_TRUE(has_def(r, "Function", "sample_callback"));
262+
ASSERT_FALSE(has_def_any(r, "extern"));
263+
cbm_free_result(r);
264+
265+
/* A statement include has no enclosing function in the file. Keep any
266+
* syntax coverage diagnostic rather than inventing a callable owner. */
267+
r = extract("#ifdef ENABLE_SAMPLE\nif (sample_check()) return 1;\n#endif\n", CBM_LANG_CPP,
268+
"sample", "runtime/guard.inc");
269+
ASSERT_NOT_NULL(r);
270+
ASSERT_FALSE(has_def_any(r, "extern"));
271+
ASSERT_FALSE(has_def(r, "Function", "if"));
272+
cbm_free_result(r);
273+
PASS();
274+
}
275+
253276
TEST(extract_cpp_macros_issue375) {
254277
CBMFileResult *r = extract("#define MAX(a, b) ((a) > (b) ? (a) : (b))\n"
255278
"#define PI 3.14159\n"
@@ -13410,6 +13433,7 @@ SUITE(extraction) {
1341013433
RUN_TEST(traversal_stack_not_in_result_arena_issue2010);
1341113434
RUN_TEST(extract_c_macro_hidden_call_survives_preprocessed_pass_issue2010);
1341213435
RUN_TEST(extract_c_macros_issue375);
13436+
RUN_TEST(extract_cpp_inc_linkage_and_callback);
1341313437
RUN_TEST(extract_cpp_macros_issue375);
1341413438
RUN_TEST(extract_cpp_functionlike_macro_type_arg_no_false_parse_partial_issue1071);
1341513439
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
@@ -1390,6 +1390,55 @@ static CBMLanguage classify_content(const char *name, const char *content, size_
13901390

13911391
typedef CBMLanguage (*path_probe_fn)(const char *path);
13921392

1393+
/* Exercise the shared classifier and both disk entry points. All sources are
1394+
* synthetic; the recipe bodies deliberately contain misleading C-like code. */
1395+
TEST(lang_inc_c_family_and_existing_dialects) {
1396+
static const struct {
1397+
const char *content;
1398+
CBMLanguage expected;
1399+
} cases[] = {
1400+
{"#include \"sample.h\"\nextern \"C\" int sample_export() { return 0; }\n", CBM_LANG_CPP},
1401+
{"extern \"C\" __declspec(dllexport) int sample_export() { return 0; }\n", CBM_LANG_CPP},
1402+
{"#define SAMPLE_CALLBACK(T) T\nstatic SAMPLE_CALLBACK(int) sample() { return 0; }\n",
1403+
CBM_LANG_CPP},
1404+
{"/* in-function fragment */\n#ifdef SAMPLE_ENABLED\nif (check()) return 1;\n#endif\n",
1405+
CBM_LANG_CPP},
1406+
{"int sample(void) { return 0; }\n", CBM_LANG_CPP},
1407+
{" \t# include <stdint.h>\r\n", CBM_LANG_CPP},
1408+
{"/* example:\nextern \"C\" int example();\n*/\nSUMMARY = \"sample\"\n", CBM_LANG_BITBAKE},
1409+
{"// extern \"C\" example only\n", CBM_LANG_BITBAKE},
1410+
{"#include_extra \"sample.h\"\n", CBM_LANG_BITBAKE},
1411+
{"#define Value 1\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1412+
{"#ifndef SAMPLE\n#define SAMPLE 1\n#endif\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1413+
{"ROUTINE Sample [Type=INC]\n#define Value 1\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1414+
{"#def1arg Value(%x) %x\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1415+
{"#; ObjectScript comment\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1416+
{"#include <sample.h>\n#def1arg Value(%x) %x\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},
1417+
{"# recipe\nSUMMARY = \"sample\"\nrequire base.inc\n", CBM_LANG_BITBAKE},
1418+
{"SRC_URI += \"file://sample.c\"\ndo_compile() {\nif (check) { return; }\n}\n",
1419+
CBM_LANG_BITBAKE},
1420+
{"python do_generate() {\nif (ready):\n pass\n}\n", CBM_LANG_BITBAKE},
1421+
{"do_compile() {\ncat <<'EOF'\n#include <stdio.h>\nstatic int sample;\nEOF\n}\n",
1422+
CBM_LANG_BITBAKE},
1423+
{"require base.inc\nextern \"C\" example in recipe text\n", CBM_LANG_BITBAKE},
1424+
{"", CBM_LANG_BITBAKE},
1425+
};
1426+
char path[512];
1427+
snprintf(path, sizeof(path), "%s/cbm_lang_inc_routing.inc", cbm_tmpdir());
1428+
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
1429+
size_t len = strlen(cases[i].content);
1430+
ASSERT_EQ(classify_content("sample.inc", cases[i].content, len), cases[i].expected);
1431+
ASSERT_TRUE(write_exact(path, cases[i].content, len));
1432+
CBMLanguage by_probe = cbm_disambiguate_inc(path);
1433+
CBMLanguage by_file = cbm_language_for_file("sample.inc", path);
1434+
remove(path);
1435+
ASSERT_EQ(by_probe, cases[i].expected);
1436+
ASSERT_EQ(by_file, cases[i].expected);
1437+
}
1438+
ASSERT_EQ(cbm_disambiguate_inc(NULL), CBM_LANG_BITBAKE);
1439+
PASS();
1440+
}
1441+
13931442
TEST(lang_probe_bytes_follow_the_name) {
13941443
ASSERT_EQ(cbm_language_probe_bytes("main.go"), 0);
13951444
ASSERT_EQ(cbm_language_probe_bytes("Makefile"), 0);
@@ -1805,6 +1854,7 @@ SUITE(language) {
18051854
RUN_TEST(lang_ext_sosl);
18061855

18071856
RUN_TEST(lang_all_have_names);
1857+
RUN_TEST(lang_inc_c_family_and_existing_dialects);
18081858
RUN_TEST(lang_probe_bytes_follow_the_name);
18091859
RUN_TEST(lang_classify_matches_every_content_rule);
18101860
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
@@ -28535,7 +28535,40 @@ TEST(pipeline_calls_args_overlong_argument_stops_cleanly) {
2853528535
PASS();
2853628536
}
2853728537

28538+
/* Discovery must choose the C++ grammar before the pipeline extracts .inc
28539+
* symbols; a direct extractor-only test would miss that routing failure. */
28540+
TEST(pipeline_inc_c_family_fragments) {
28541+
char tmp[256];
28542+
snprintf(tmp, sizeof(tmp), "%s/cbm_inc_cpp_XXXXXX", cbm_tmpdir());
28543+
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
28544+
write_temp_file(tmp, "runtime.inc",
28545+
"#define SAMPLE_CALLBACK(T) T\n"
28546+
"extern \"C\" int sample_export() { return 0; }\n"
28547+
"static SAMPLE_CALLBACK(int) sample_callback() { return sample_export(); }\n");
28548+
write_temp_file(tmp, "main.cpp", "#include \"runtime.inc\"\n");
28549+
char db_path[512];
28550+
snprintf(db_path, sizeof(db_path), "%s/graph.db", tmp);
28551+
const cbm_index_mode_t modes[] = {CBM_MODE_FAST, CBM_MODE_FULL};
28552+
for (size_t i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
28553+
cbm_pipeline_t *pipeline = cbm_pipeline_new(tmp, db_path, modes[i]);
28554+
ASSERT_NOT_NULL(pipeline);
28555+
ASSERT_EQ(cbm_pipeline_run(pipeline), 0);
28556+
cbm_store_t *store = cbm_store_open_path(db_path);
28557+
ASSERT_NOT_NULL(store);
28558+
const char *project = cbm_pipeline_project_name(pipeline);
28559+
ASSERT_EQ(fixture_node_count(store, project, "runtime.inc", "sample_export", "Function"),
28560+
1);
28561+
ASSERT_EQ(fixture_node_count(store, project, "runtime.inc", "sample_callback", "Function"),
28562+
1);
28563+
ASSERT_EQ(named_node_count(store, project, "extern"), 0);
28564+
cbm_store_close(store);
28565+
cbm_pipeline_free(pipeline);
28566+
}
28567+
th_rmtree(tmp);
28568+
PASS();
28569+
}
2853828570
SUITE(pipeline) {
28571+
RUN_TEST(pipeline_inc_c_family_fragments);
2853928572
RUN_TEST(pipeline_python_module_callers_sequential);
2854028573
RUN_TEST(pipeline_python_module_callers_parallel);
2854128574
RUN_TEST(pipeline_python_class_body_calls_sequential);

0 commit comments

Comments
 (0)