Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(discover): select C++ for C-family include fragments
knewstimek51fd445f663f
diff --git a/src/discover/discover.h b/src/discover/discover.hindex 85bc05294..a850df4e3 100644--- a/src/discover/discover.h+++ b/src/discover/discover.h@@ -59,8 +59,9 @@ CBMLanguage cbm_disambiguate_frm(const char *path);CBMLanguage cbm_disambiguate_res(const char *path);/* Disambiguate .inc files by reading first 4KB of content.- * Returns CBM_LANG_OBJECTSCRIPT_ROUTINE if it looks like an ObjectScript- * include (a "ROUTINE <Uppercase>" header), otherwise CBM_LANG_BITBAKE.+ * Returns CBM_LANG_CPP for explicit C-family code, or+ * CBM_LANG_OBJECTSCRIPT_ROUTINE for ObjectScript markers / a lone #define.+ * BitBake recipe syntax and unknown content retain CBM_LANG_BITBAKE.* On read failure, defaults to CBM_LANG_BITBAKE. */CBMLanguage cbm_disambiguate_inc(const char *path);diff --git a/src/discover/language.c b/src/discover/language.cindex 7d967d7f3..9c6dd041d 100644--- a/src/discover/language.c+++ b/src/discover/language.c@@ -1472,38 +1472,150 @@ CBMLanguage cbm_disambiguate_cls(const char *path) {return lang_cls_text(buf);}-/* Disambiguate .inc files: shared by BitBake include fragments and- * InterSystems ObjectScript include (macro) files. ObjectScript .inc files are- * predominantly macro definitions ("#define NAME ..." / "#def1arg NAME ...");- * some also carry a "ROUTINE <Name>" header. The macro-preprocessor directives- * are the strongest signal because that is the primary content of an .inc file,- * whereas BitBake uses '#' only for "# comment" lines (always '#' + space).- * We therefore match ObjectScript preprocessor directives ('#' immediately- * followed by 'def'/';'), which BitBake never produces. Defaults to BitBake on- * any doubt (preserves existing behaviour). */+/* A complete leading token, followed by whitespace or punctuation, within a+ * single line. In particular #include_extra is not #include. */+static const char *lang_inc_word(const char *line, const char *end, const char *word) {+ size_t n = strlen(word);+ if ((size_t)(end - line) < n || strncmp(line, word, n) != 0 ||+ (line + n < end && (isalnum((unsigned char)line[n]) || line[n] == '_'))) {+ return NULL;+ }+ line += n;+ while (line < end && isspace((unsigned char)*line)) {+ line++;+ }+ return line;+}++/* Recipes can embed C in quoted strings or shell/Python tasks. Recognize the+ * recipe's own syntax before inspecting those bodies as standalone C code. */+static bool lang_inc_recipe_line(const char *line, const char *end) {+ static const char *const words[] = {"require", "include", "inherit", "addtask", "python", NULL};+ for (int i = 0; words[i]; i++) {+ const char *tail = lang_inc_word(line, end, words[i]);+ if (tail && tail > line + strlen(words[i])) {+ return true;+ }+ }+ if ((size_t)(end - line) >= SLEN("do_") && strncmp(line, "do_", SLEN("do_")) == 0) {+ return true;+ }+ const char *p = line;+ while (p < end && (isalnum((unsigned char)*p) || *p == '_' || *p == ':' || *p == '[' ||+ *p == ']' || *p == '-' || *p == '.')) {+ p++;+ }+ if (p == line) {+ return false;+ }+ while (p < end && isspace((unsigned char)*p)) {+ p++;+ }+ return p < end && (*p == '=' || ((*p == '?' || *p == '+' || *p == ':' || *p == '.') &&+ p + SKIP_ONE < end && p[SKIP_ONE] == '='));+}++/* Skip only leading whitespace and C comments, keeping block-comment state+ * across lines. Recipe strings and bodies are handled before looking for C. */+static const char *lang_inc_code(const char *line, const char *end, bool *block_comment) {+ while (line < end) {+ if (*block_comment) {+ const char *close = strstr(line, "*/");+ if (!close || close >= end) {+ return end;+ }+ line = close + SLEN("*/");+ *block_comment = false;+ continue;+ }+ if (isspace((unsigned char)*line)) {+ line++;+ continue;+ }+ if ((size_t)(end - line) >= SLEN("/*") && strncmp(line, "/*", SLEN("/*")) == 0) {+ *block_comment = true;+ line += SLEN("/*");+ continue;+ }+ break;+ }+ return line;+}++static bool lang_inc_c_code(const char *line, const char *end) {+ static const char *const words[] = {+ "extern", "static", "typedef", "namespace", "template",+ "struct", "class", "int", "void", NULL,+ };+ for (int i = 0; words[i]; i++) {+ const char *tail = lang_inc_word(line, end, words[i]);+ if (tail && tail < end && *tail != '=') {+ return true;+ }+ }+ const char *tail = lang_inc_word(line, end, "if");+ return tail && tail < end && *tail == '(';+}++static bool lang_inc_directive(const char *line, const char *end, bool *c_family,+ bool *objectscript_define) {+ line += SKIP_ONE; /* '#' */+ while (line < end && isspace((unsigned char)*line)) {+ line++;+ }+ if (lang_inc_word(line, end, "def1arg") || (line < end && *line == ';')) {+ return true; /* Explicit ObjectScript directive. */+ }+ if (lang_inc_word(line, end, "define")) {+ *objectscript_define = true;+ }+ const char *tail = lang_inc_word(line, end, "include");+ if (tail && tail < end && (*tail == '"' || *tail == '<')) {+ *c_family = true;+ }+ return false;+}++/* .inc is shared by C/C++, BitBake and ObjectScript. Select C++ for explicit+ * C-family code (including C fragments); do not infer an enclosing include+ * context. A lone #define and unknown content retain their existing defaults. */static CBMLanguage lang_inc_text(const char *buf) {+ bool c_family = false;+ bool objectscript_define = false;+ bool block_comment = false;const char *line = buf;while (*line) {- /* ObjectScript include header: a line beginning "ROUTINE <Uppercase>". */- if (strncmp(line, "ROUTINE ", SLEN("ROUTINE ")) == 0 &&- isupper((unsigned char)line[SLEN("ROUTINE ")])) {- return CBM_LANG_OBJECTSCRIPT_ROUTINE;+ const char *end = strchr(line, '\n');+ if (!end) {+ end = line + strlen(line);+ }+ const char *code = lang_inc_code(line, end, &block_comment);+ line = *end ? end + SKIP_ONE : end;+ if (code == end) {+ continue;}- /* ObjectScript macro directives — the primary content of .inc files.- * "#define"/"#def1arg" (macro defs) and "#;" (line comment). BitBake's- * only '#' use is "# comment" (hash + space), so these never collide. */- if (strncmp(line, "#define", SLEN("#define")) == 0 ||- strncmp(line, "#def1arg", SLEN("#def1arg")) == 0 ||- strncmp(line, "#;", SLEN("#;")) == 0) {+ const char *tail = lang_inc_word(code, end, "ROUTINE");+ if (tail && tail < end && isupper((unsigned char)*tail)) {return CBM_LANG_OBJECTSCRIPT_ROUTINE;}- const char *nl = strchr(line, '\n');- if (!nl) {- break;+ if (*code == '#') {+ if (lang_inc_directive(code, end, &c_family, &objectscript_define)) {+ return CBM_LANG_OBJECTSCRIPT_ROUTINE;+ }+ continue;}- line = nl + SKIP_ONE;+ if ((size_t)(end - code) >= SLEN("//") && strncmp(code, "//", SLEN("//")) == 0) {+ continue;+ }+ if (!c_family && lang_inc_recipe_line(code, end)) {+ return CBM_LANG_BITBAKE;+ }+ c_family = c_family || lang_inc_c_code(code, end);+ }+ if (c_family) {+ return CBM_LANG_CPP;}- return CBM_LANG_BITBAKE;+ return objectscript_define ? CBM_LANG_OBJECTSCRIPT_ROUTINE : CBM_LANG_BITBAKE;}CBMLanguage cbm_disambiguate_inc(const char *path) {@@ -1664,7 +1776,7 @@ CBMLanguage cbm_language_classify_with(const cbm_userconfig_t *config, const cha/* ObjectScript UDL, Apex or a Visual Basic 6 class module */lang = readable ? lang_cls_text(buf) : CBM_LANG_APEX;} else if (strcmp(ext, ".inc") == 0) {- /* BitBake or an ObjectScript include */+ /* C/C++, BitBake or an ObjectScript include */lang = readable ? lang_inc_text(buf) : CBM_LANG_BITBAKE;} else if (cfc) {/* script or tag dialect */diff --git a/tests/test_extraction.c b/tests/test_extraction.cindex 234ad1d9e..35a8a9dec 100644--- a/tests/test_extraction.c+++ b/tests/test_extraction.c@@ -250,6 +250,29 @@ TEST(extract_c_macros_issue375) {PASS();}+TEST(extract_cpp_inc_linkage_and_callback) {+ CBMFileResult *r =+ extract("#define SAMPLE_CALLBACK(type) type\n"+ "extern \"C\" __declspec(dllexport) int sample_export() { return 0; }\n"+ "static SAMPLE_CALLBACK(int) sample_callback() { return sample_export(); }\n",+ CBM_LANG_CPP, "sample", "runtime/sample.inc");+ ASSERT_NOT_NULL(r);+ ASSERT_TRUE(has_def(r, "Function", "sample_export"));+ ASSERT_TRUE(has_def(r, "Function", "sample_callback"));+ ASSERT_FALSE(has_def_any(r, "extern"));+ cbm_free_result(r);++ /* A statement include has no enclosing function in the file. Keep any+ * syntax coverage diagnostic rather than inventing a callable owner. */+ r = extract("#ifdef ENABLE_SAMPLE\nif (sample_check()) return 1;\n#endif\n", CBM_LANG_CPP,+ "sample", "runtime/guard.inc");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(has_def_any(r, "extern"));+ ASSERT_FALSE(has_def(r, "Function", "if"));+ cbm_free_result(r);+ PASS();+}+TEST(extract_cpp_macros_issue375) {CBMFileResult *r = extract("#define MAX(a, b) ((a) > (b) ? (a) : (b))\n""#define PI 3.14159\n"@@ -13410,6 +13433,7 @@ SUITE(extraction) {RUN_TEST(traversal_stack_not_in_result_arena_issue2010);RUN_TEST(extract_c_macro_hidden_call_survives_preprocessed_pass_issue2010);RUN_TEST(extract_c_macros_issue375);+ RUN_TEST(extract_cpp_inc_linkage_and_callback);RUN_TEST(extract_cpp_macros_issue375);RUN_TEST(extract_cpp_functionlike_macro_type_arg_no_false_parse_partial_issue1071);RUN_TEST(extract_cpp_real_in_body_error_still_flagged_issue1071);diff --git a/tests/test_language.c b/tests/test_language.cindex 1ed72a0a2..8d7829a59 100644--- a/tests/test_language.c+++ b/tests/test_language.c@@ -1390,6 +1390,55 @@ static CBMLanguage classify_content(const char *name, const char *content, size_typedef CBMLanguage (*path_probe_fn)(const char *path);+/* Exercise the shared classifier and both disk entry points. All sources are+ * synthetic; the recipe bodies deliberately contain misleading C-like code. */+TEST(lang_inc_c_family_and_existing_dialects) {+ static const struct {+ const char *content;+ CBMLanguage expected;+ } cases[] = {+ {"#include \"sample.h\"\nextern \"C\" int sample_export() { return 0; }\n", CBM_LANG_CPP},+ {"extern \"C\" __declspec(dllexport) int sample_export() { return 0; }\n", CBM_LANG_CPP},+ {"#define SAMPLE_CALLBACK(T) T\nstatic SAMPLE_CALLBACK(int) sample() { return 0; }\n",+ CBM_LANG_CPP},+ {"/* in-function fragment */\n#ifdef SAMPLE_ENABLED\nif (check()) return 1;\n#endif\n",+ CBM_LANG_CPP},+ {"int sample(void) { return 0; }\n", CBM_LANG_CPP},+ {" \t# include <stdint.h>\r\n", CBM_LANG_CPP},+ {"/* example:\nextern \"C\" int example();\n*/\nSUMMARY = \"sample\"\n", CBM_LANG_BITBAKE},+ {"// extern \"C\" example only\n", CBM_LANG_BITBAKE},+ {"#include_extra \"sample.h\"\n", CBM_LANG_BITBAKE},+ {"#define Value 1\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},+ {"#ifndef SAMPLE\n#define SAMPLE 1\n#endif\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},+ {"ROUTINE Sample [Type=INC]\n#define Value 1\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},+ {"#def1arg Value(%x) %x\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},+ {"#; ObjectScript comment\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},+ {"#include <sample.h>\n#def1arg Value(%x) %x\n", CBM_LANG_OBJECTSCRIPT_ROUTINE},+ {"# recipe\nSUMMARY = \"sample\"\nrequire base.inc\n", CBM_LANG_BITBAKE},+ {"SRC_URI += \"file://sample.c\"\ndo_compile() {\nif (check) { return; }\n}\n",+ CBM_LANG_BITBAKE},+ {"python do_generate() {\nif (ready):\n pass\n}\n", CBM_LANG_BITBAKE},+ {"do_compile() {\ncat <<'EOF'\n#include <stdio.h>\nstatic int sample;\nEOF\n}\n",+ CBM_LANG_BITBAKE},+ {"require base.inc\nextern \"C\" example in recipe text\n", CBM_LANG_BITBAKE},+ {"", CBM_LANG_BITBAKE},+ };+ char path[512];+ snprintf(path, sizeof(path), "%s/cbm_lang_inc_routing.inc", cbm_tmpdir());+ for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {+ size_t len = strlen(cases[i].content);+ ASSERT_EQ(classify_content("sample.inc", cases[i].content, len), cases[i].expected);+ ASSERT_TRUE(write_exact(path, cases[i].content, len));+ CBMLanguage by_probe = cbm_disambiguate_inc(path);+ CBMLanguage by_file = cbm_language_for_file("sample.inc", path);+ remove(path);+ ASSERT_EQ(by_probe, cases[i].expected);+ ASSERT_EQ(by_file, cases[i].expected);+ }+ ASSERT_EQ(cbm_disambiguate_inc(NULL), CBM_LANG_BITBAKE);+ PASS();+}+TEST(lang_probe_bytes_follow_the_name) {ASSERT_EQ(cbm_language_probe_bytes("main.go"), 0);ASSERT_EQ(cbm_language_probe_bytes("Makefile"), 0);@@ -1805,6 +1854,7 @@ SUITE(language) {RUN_TEST(lang_ext_sosl);RUN_TEST(lang_all_have_names);+ RUN_TEST(lang_inc_c_family_and_existing_dialects);RUN_TEST(lang_probe_bytes_follow_the_name);RUN_TEST(lang_classify_matches_every_content_rule);RUN_TEST(lang_classify_unreadable_content_keeps_the_name_default);diff --git a/tests/test_pipeline.c b/tests/test_pipeline.cindex c8c49a468..852ba3ccf 100644--- a/tests/test_pipeline.c+++ b/tests/test_pipeline.c@@ -28535,7 +28535,40 @@ TEST(pipeline_calls_args_overlong_argument_stops_cleanly) {PASS();}+/* Discovery must choose the C++ grammar before the pipeline extracts .inc+ * symbols; a direct extractor-only test would miss that routing failure. */+TEST(pipeline_inc_c_family_fragments) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "%s/cbm_inc_cpp_XXXXXX", cbm_tmpdir());+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ write_temp_file(tmp, "runtime.inc",+ "#define SAMPLE_CALLBACK(T) T\n"+ "extern \"C\" int sample_export() { return 0; }\n"+ "static SAMPLE_CALLBACK(int) sample_callback() { return sample_export(); }\n");+ write_temp_file(tmp, "main.cpp", "#include \"runtime.inc\"\n");+ char db_path[512];+ snprintf(db_path, sizeof(db_path), "%s/graph.db", tmp);+ const cbm_index_mode_t modes[] = {CBM_MODE_FAST, CBM_MODE_FULL};+ for (size_t i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {+ cbm_pipeline_t *pipeline = cbm_pipeline_new(tmp, db_path, modes[i]);+ ASSERT_NOT_NULL(pipeline);+ ASSERT_EQ(cbm_pipeline_run(pipeline), 0);+ cbm_store_t *store = cbm_store_open_path(db_path);+ ASSERT_NOT_NULL(store);+ const char *project = cbm_pipeline_project_name(pipeline);+ ASSERT_EQ(fixture_node_count(store, project, "runtime.inc", "sample_export", "Function"),+ 1);+ ASSERT_EQ(fixture_node_count(store, project, "runtime.inc", "sample_callback", "Function"),+ 1);+ ASSERT_EQ(named_node_count(store, project, "extern"), 0);+ cbm_store_close(store);+ cbm_pipeline_free(pipeline);+ }+ th_rmtree(tmp);+ PASS();+}SUITE(pipeline) {+ RUN_TEST(pipeline_inc_c_family_fragments);RUN_TEST(pipeline_python_module_callers_sequential);RUN_TEST(pipeline_python_module_callers_parallel);RUN_TEST(pipeline_python_class_body_calls_sequential);