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
Attribute out-of-line C++ method definitions to their class (distill #428)
Martin Vogela50b086809be
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.cindex 34aef1bc3..913268d8b 100644--- a/internal/cbm/extract_defs.c+++ b/internal/cbm/extract_defs.c@@ -478,6 +478,56 @@ static TSNode resolve_qualified_name(TSNode decl) {return null_node;}+// C++/CUDA: out-of-line method definitions name the function with a qualified+// declarator (`Foo::bar`, or `ns::Foo::bar`). Return the immediate enclosing+// class name (the scope segment directly left of the function name, e.g. "Foo"),+// or NULL when the declarator is unqualified (a plain free function). Without+// this, an out-of-line definition — whose class body lives declaration-only in a+// header — would be recorded as a free Function with no link to its class.+static char *cpp_out_of_line_parent_class(CBMArena *a, TSNode node, const char *source) {+ // Descend the declarator chain to its qualified_identifier, if any.+ TSNode qid = {0};+ TSNode decl = ts_node_child_by_field_name(node, TS_FIELD("declarator"));+ for (int depth = 0; depth < DECLARATOR_DEPTH_LIMIT && !ts_node_is_null(decl); depth++) {+ const char *dk = ts_node_type(decl);+ if (strcmp(dk, "qualified_identifier") == 0 || strcmp(dk, "scoped_identifier") == 0) {+ qid = decl;+ break;+ }+ TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));+ if (ts_node_is_null(inner) && ts_node_named_child_count(decl) > 0) {+ inner = ts_node_named_child(decl, 0);+ }+ if (ts_node_is_null(inner)) {+ break;+ }+ decl = inner;+ }+ if (ts_node_is_null(qid)) {+ return NULL;+ }+ // The qualified_identifier's `scope` is the parent. For a nested scope+ // (`ns::Foo`) descend through its `name` field to the innermost segment so+ // the direct parent ("Foo") is returned, not the outer namespace.+ TSNode scope = ts_node_child_by_field_name(qid, TS_FIELD("scope"));+ if (ts_node_is_null(scope)) {+ return NULL;+ }+ for (int depth = 0; depth < DECLARATOR_DEPTH_LIMIT; depth++) {+ const char *sk = ts_node_type(scope);+ if (strcmp(sk, "qualified_identifier") != 0 && strcmp(sk, "scoped_identifier") != 0) {+ break;+ }+ TSNode name = ts_node_child_by_field_name(scope, TS_FIELD("name"));+ if (ts_node_is_null(name)) {+ break;+ }+ scope = name;+ }+ char *text = cbm_node_text(a, scope, source);+ return (text && text[0]) ? text : NULL;+}+// Resolve function name from C/C++/CUDA/GLSL declarator chain.static TSNode resolve_c_declarator_name(TSNode node) {TSNode decl = ts_node_child_by_field_name(node, TS_FIELD("declarator"));@@ -2678,6 +2728,22 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec}}+ // C++/CUDA: out-of-line method definition (`Foo::bar` in a .cc/.cpp). The+ // class body in the header is declaration-only, so without this the+ // definition is recorded as a free Function. Promote it to a Method whose QN+ // is scoped to its class and whose parent_class links it back (matching the+ // class node QN computed the same way) so DEFINES_METHOD edges resolve.+ if ((ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA) &&+ strcmp(ts_node_type(node), "function_definition") == 0) {+ char *scope_name = cpp_out_of_line_parent_class(a, node, ctx->source);+ if (scope_name && scope_name[0]) {+ const char *class_qn = cbm_fqn_compute(a, ctx->project, ctx->rel_path, scope_name);+ def.qualified_name = cbm_arena_sprintf(a, "%s.%s", class_qn, name);+ def.label = "Method";+ def.parent_class = class_qn;+ }+ }+// Decorators + route extraction from decorator ASTdef.decorators = extract_decorators(a, node, ctx->source, ctx->language, spec);extract_route_from_decorators(a, node, ctx->source, spec, &def.route_path, &def.route_method);diff --git a/tests/test_extraction.c b/tests/test_extraction.cindex f126c682f..0308372c1 100644--- a/tests/test_extraction.c+++ b/tests/test_extraction.c@@ -1204,6 +1204,37 @@ TEST(cpp_function) {PASS();}+/* --- C++ out-of-line method definitions (#428) ---+ * A .cpp defining methods of a class declared elsewhere (not in this TU).+ * Pre-fix these were recorded as free Functions (label "Function", no+ * parent_class); they must be Methods linked to their enclosing class via+ * parent_class. (The helper also descends nested scopes `ns::Class::method` to+ * the immediate class, but tree-sitter-cpp parses a synthetic doubly-qualified+ * out-of-line def in isolation as an ERROR node, so that path is exercised by+ * real codebases rather than this isolated unit fixture.) */+TEST(cpp_out_of_line_method_issue428) {+ CBMFileResult *r = extract("void Foo::bar() {}\n"+ "int Foo::baz() { return 0; }\n",+ CBM_LANG_CPP, "t", "foo.cpp");+ ASSERT_NOT_NULL(r);+ ASSERT(has_def(r, "Method", "bar"));+ ASSERT(has_def(r, "Method", "baz"));+ ASSERT(!has_def(r, "Function", "bar")); /* not a free function */+ /* parent_class links to the enclosing class QN */+ int checked = 0;+ for (int i = 0; i < r->defs.count; i++) {+ const CBMDefinition *d = &r->defs.items[i];+ if (strcmp(d->name, "bar") == 0 && strcmp(d->label, "Method") == 0) {+ ASSERT_NOT_NULL(d->parent_class);+ ASSERT(strstr(d->parent_class, "Foo") != NULL);+ checked = 1;+ }+ }+ ASSERT(checked);+ cbm_free_result(r);+ PASS();+}+/* --- COBOL paragraph --- */TEST(cobol_paragraph) {CBMFileResult *r =@@ -2887,6 +2918,7 @@ SUITE(extraction) {RUN_TEST(rust_enum);RUN_TEST(zig_struct);RUN_TEST(cpp_function);+ RUN_TEST(cpp_out_of_line_method_issue428);RUN_TEST(cobol_paragraph);RUN_TEST(verilog_module);RUN_TEST(cuda_kernel);