Skip to content

C++ method calls to same-named classes in different files all resolve to the first definition #1079

Description

@Dshuishui

Summary

For C++, a method call obj.method() is resolved by a global method-name lookup.
When two files each define a class with the same name and method, every caller
collapses onto the first matching definition, and the other file's method
receives no incoming calls edge. With distinct class names the same code
resolves precisely, so this only affects same-named classes across files.

Reproduction

Two files, each defining class Logger { void log(); } and a caller:

// a/svc.cpp
class Logger { public: void log() { int a = 1; } };
void useA() { Logger lg; lg.log(); }
// b/svc.cpp
class Logger { public: void log() { int b = 2; } };
void useB() { Logger lg; lg.log(); }
codegraph init
sqlite3 .codegraph/codegraph.db \
  "SELECT s.name, t.name, t.file_path FROM edges e
   JOIN nodes s ON e.source=s.id JOIN nodes t ON e.target=t.id
   WHERE e.kind='calls' AND s.language='cpp' ORDER BY s.name;"

Output — useB lives in b/svc.cpp but its call resolves to a/svc.cpp:

useA|log|a/svc.cpp
useB|log|a/svc.cpp

b/svc.cpp's Logger::log has no incoming calls edge.

Control: distinct class names resolve correctly

With class LoggerA in a/svc.cpp and class LoggerB in b/svc.cpp (callers
unchanged), the same query gives:

useA|log|a/svc.cpp
useB|log|b/svc.cpp

Expected

useB constructs a local Logger and calls .log(), so the call would resolve
to b/svc.cpp's Logger::log (each caller to its own file's definition).

Likely cause

In src/resolution/name-matcher.ts, resolveMethodOnType() looks up the method
by name globally (getNodesByName); when several definitions match
Logger::log, disambiguation by file path appears to be gated on preferredFqn,
which seems to be populated only for Java/Kotlin (from import hints). C++ call
sites have no such hint, so resolution falls through to the first match
(matches[0]).

Environment

  • codegraph 1.1.0
  • macOS (Darwin arm64)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions