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
feat(extract): extract C/C++ preprocessor macros as Macro nodes
Martin Vogel10ce652152ff
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.cindex 7a877ea1b..2d487a610 100644--- a/internal/cbm/extract_defs.c+++ b/internal/cbm/extract_defs.c@@ -3577,6 +3577,47 @@ static void push_class_body_children(TSNode node, const CBMLangSpec *spec, walk_}}+// Languages that use the C preprocessor and therefore have #define macros.+static bool is_c_preprocessor_lang(CBMLanguage lang) {+ return lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||+ lang == CBM_LANG_GLSL || lang == CBM_LANG_OBJC || lang == CBM_LANG_ISPC;+}++// C/C++ preprocessor macros become Macro nodes (#375):+// #define SIMPLE 1 -> preproc_def+// #define FN(x) (2 * (x)) -> preproc_function_def+// The name is the `name` field; a function-like macro's parameter list is kept+// as the signature. The macro body (a preproc_arg) is not descended into.+static void extract_c_macro_def(CBMExtractCtx *ctx, TSNode node) {+ CBMArena *a = ctx->arena;+ TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("name"));+ if (ts_node_is_null(name_node)) {+ return;+ }+ char *name = cbm_node_text(a, name_node, ctx->source);+ if (!name || !name[0]) {+ return;+ }++ CBMDefinition def;+ memset(&def, 0, sizeof(def));+ def.name = name;+ def.qualified_name = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);+ def.label = "Macro";+ def.file_path = ctx->rel_path;+ def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;+ def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;+ def.lines = (int)(def.end_line - def.start_line + TS_LINE_OFFSET);+ def.is_exported = true; // macros have no translation-unit scoping — globally visible++ TSNode params = ts_node_child_by_field_name(node, TS_FIELD("parameters"));+ if (!ts_node_is_null(params)) {+ def.signature = cbm_node_text(a, params, ctx->source);+ }++ cbm_defs_push(&ctx->result->defs, a, def);+}+static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec, int depth_unused) {(void)depth_unused;walk_defs_frame_t stack[CBM_WALK_DEFS_STACK_CAP];@@ -3594,6 +3635,12 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,continue;}+ if (is_c_preprocessor_lang(ctx->language) &&+ (strcmp(kind, "preproc_def") == 0 || strcmp(kind, "preproc_function_def") == 0)) {+ extract_c_macro_def(ctx, node);+ continue; // the macro body is a preproc_arg — nothing more to extract+ }+if (cbm_kind_in_set(node, spec->function_node_types)) {if (!is_template_class_node(node, ctx->language)) {extract_func_def(ctx, node, spec);diff --git a/tests/test_extraction.c b/tests/test_extraction.cindex 4a3a65808..cfd74a536 100644--- a/tests/test_extraction.c+++ b/tests/test_extraction.c@@ -120,6 +120,38 @@ TEST(extract_ts_factory_object_methods_issue341) {PASS();}+/* --- C/C++ preprocessor macros become Macro nodes (#375) --- */+TEST(extract_c_macros_issue375) {+ CBMFileResult *r = extract("#define SIMPLE_MACRO 1\n"+ "#define FN_MACRO(x) (2 * (x))\n"+ "#define EMPTY_MACRO\n"+ "int main(void) { return FN_MACRO(SIMPLE_MACRO); }\n",+ CBM_LANG_C, "p", "macros.c");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);+ ASSERT(has_def(r, "Macro", "SIMPLE_MACRO"));+ ASSERT(has_def(r, "Macro", "FN_MACRO"));+ ASSERT(has_def(r, "Macro", "EMPTY_MACRO"));+ ASSERT(has_def(r, "Function", "main")); /* macros don't displace function defs */+ 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"+ "namespace n {\n"+ "int f() { return MAX(1, 2); }\n"+ "}\n",+ CBM_LANG_CPP, "p", "macros.cpp");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);+ ASSERT(has_def(r, "Macro", "MAX"));+ ASSERT(has_def(r, "Macro", "PI"));+ cbm_free_result(r);+ PASS();+}+/* --- Java --- */TEST(java_class) {CBMFileResult *r = extract(@@ -2385,6 +2417,8 @@ SUITE(extraction) {RUN_TEST(extract_r_box_use_imports_issue218);RUN_TEST(extract_r_dollar_call_issue219);RUN_TEST(extract_ts_factory_object_methods_issue341);+ RUN_TEST(extract_c_macros_issue375);+ RUN_TEST(extract_cpp_macros_issue375);/* OOP */RUN_TEST(java_class);