Skip to content

C/C++/ObjC decl/def merge discards the definition site — the merged node reports only the declaration #2990

Closed

Description

@winesoft-namjin-yun

Summary

For C/C++/ObjC, a symbol declared in Foo.h and defined in the sibling Foo.cpp is deliberately collapsed into one node keyed to the header (_merge_decl_def_classes, #1547/#1556). That merge is correct and I am not asking to change it — downstream resolvers need exactly one definition node.

The problem is that the merge discards the impl node entirely, so nothing in the graph records where the symbol is actually implemented. The surviving node's source_file/source_location name the declaration, and the definition's file and line — the thing a reader asking "where is this implemented?" wants — are lost with the dropped node.

Same class of harm as #2092 (a symbol whose reported source_location isn't where it lives), different cause.

Repro

Two files, no config:

// foo.h
class Foo {
public:
    void Alpha();
    void Beta();
};
// foo.cpp
#include "foo.h"
void Foo::Alpha() { Beta(); }
void Foo::Beta()  { }
$ graphify update .
[graphify watch] Rebuilt: 5 nodes, 7 edges, 2 communities

graph.json:

label source_file source_location id
Foo foo.h L3 foo_foo
Alpha foo.h L4 foo_foo_alpha
Beta foo.h L5 foo_foo_beta
foo.h foo.h L1 foo_h_foo
foo.cpp foo.cpp L1 foo_cpp_foo

foo.cpp contributes only its file node. Foo::Alpha() at foo.cpp L2 and Foo::Beta() at L3 are gone, and no attribute anywhere points at them.

Expected: the merged node keeps naming the declaration (that is the design), but the definition site remains reachable from it.

Also: the drop is silent — no note or warning on any verbosity. _report_id_collision in dedup.py would warn about a two-file id collision, but this merge happens earlier, in extract(), so nothing is reported.

Renaming the impl file so the stems differ (fooimpl.cpp) keeps all 4 nodes, which confirms the path: the ids collide only because both stems slugify to foo.

Why it matters

The merge fires for the ordinary C++ layout — declarations in a header, definitions in the sibling .cpp — so it is the common case, not an edge case. On a 1,767-file C++ codebase I measured, 541 of 700 implementation files larger than 3 KB contribute nothing but their file node; one 188 KB .cpp holds 198 method definitions whose lines are all unreachable from the graph.

The practical cost is that get_node answers "where is this implemented?" with a header line number, and the reader then has to scan a very large .cpp by hand. Call edges and symbol coverage are unaffected — the surviving node keeps the id, so edges still land correctly (I verified edge density is identical between a merged class and an unmerged one). This is purely a loss of provenance.

Proposed fix

Record the dropped impl node's provenance on the survivor before discarding it:

  • definition_file / definition_location on the merged node
  • get_node prints a Defined in: line when present

Additive only: id, label, source_file, node count and edges are all unchanged, so no existing graph is re-keyed and the single-definition guarantee the resolvers depend on is untouched. When several impls fold onto one header (ObjC categories) the choice is deterministic — lowest (source_file, source_location) — so it does not depend on node arrival order.

Alternatives I considered and rejected:

Happy to switch to either if you prefer one — the additive version just seemed like the smallest change that fixes the reported harm.

Environment

  • reproduces on v8 @ b2cd362 (0.9.48, current default branch) and on the released 0.9.46
  • Python 3.12.3, tree-sitter 0.25.2, Linux
  • AST-only build, no LLM backend configured

PR follows.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions