fix(extract): resolve ObjC member calls when a class splits across header/impl dirs

`_merge_decl_def_classes` folds an @interface/@implementation pair into one node
only when both live in the SAME directory (the id embeds the directory path,
#1556). A class whose `.h` sits in `include/` and `.m` in `src/` therefore mints
two same-labelled nodes that never merge, so `[w render]` saw two type-def
candidates, tripped the member-call resolver's single-definition god-node guard
(`if len(type_defs) != 1: continue`), and emitted NO edge -- the same corpus
resolved fine when the pair shared a directory. Header/impl directory splits are
common layout, so this hit ordinary code.

Replace the three per-arm god-node bails with a shared `_resolve_member_target`
that unions the candidates' members and binds the selector to the sole candidate
that DEFINES it (an @implementation in `.m`/`.mm`, or a merged node carrying
`definition_file`). A header-only node is a bare declaration and does not count,
so the split pair resolves to its one real definition. Two unrelated same-named
classes that each define the selector still yield two definitions and stay
ambiguous -- the guard holds for the genuinely-ambiguous case it was meant for.

Co-Authored-By: Claude Code <noreply@anthropic.com>
(cherry picked from commit 510934c686)
This commit is contained in:
xiongjianxu authored and safishamsi committed 2026-10-22 13:46:53 +01:00
1 parent a287c0795a
commit 6e6d5dbbfd
2 files changed
+68 -14

No files matched your search

+40 -14
View File
@@ -6429,6 +6429,40 @@ def _resolve_objc_member_calls(
return
_park_unresolved_member_call(node_by_id.get(caller), callee, type_name, "objc", rc)
def _defines_selector(n: dict | None) -> bool:
"""The node is a method DEFINITION, not just a declaration.
An @implementation lives in a ``.m``/``.mm``; a merged decl/def node keeps
the impl site in ``definition_file`` while its own ``source_file`` is the
header. A header-only node is a bare declaration.
"""
if not n:
return False
if n.get("definition_file"):
return True
return Path(str(n.get("source_file", ""))).suffix.lower() in (".m", ".mm")
def _resolve_member_target(type_defs: list[str], callee: str):
"""Target node + relation for a selector sent to ``type_defs`` (or (None, None)).
One candidate: the selector's method, else the class node (``references``).
Several candidates with the same class name are a class split across
directories or categories that ``_merge_decl_def_classes`` left unmerged
(its fold is dir-local, #1556). Union their members and bind the selector
only when exactly ONE candidate DEFINES it — two unrelated same-named
classes that both define it stay ambiguous, so the god-node guard holds.
"""
ck = _key(callee)
if len(type_defs) == 1:
mnid = method_index.get((type_defs[0], ck))
return (mnid, "calls") if mnid else (type_defs[0], "references")
defs = {m for t in type_defs
if (m := method_index.get((t, ck))) is not None
and _defines_selector(node_by_id.get(m))}
if len(defs) == 1:
return (next(iter(defs)), "calls")
return (None, None)
for rc in all_raw_calls:
if not rc.get("is_member_call"):
continue
@@ -6452,23 +6486,18 @@ def _resolve_objc_member_calls(
if not type_defs:
_park_absent(caller, callee, type_name, rc)
continue
if len(type_defs) != 1: # ambiguous -> bail (god-node guard)
continue
type_nid = type_defs[0]
type_qualified = False
elif receiver in ("self", "super"):
type_nid = enclosing_type.get(caller)
if not type_nid:
enc = enclosing_type.get(caller)
if not enc:
continue
type_defs = [enc]
type_qualified = True
elif receiver[:1].isupper():
type_defs = type_def_nids.get(_key(receiver), [])
if not type_defs:
_park_absent(caller, callee, receiver, rc)
continue
if len(type_defs) != 1: # ambiguous -> bail (god-node guard)
continue
type_nid = type_defs[0]
type_qualified = True
else:
# Locals shadow fields: the file's `Foo *f` local table first, then the
@@ -6483,13 +6512,10 @@ def _resolve_objc_member_calls(
if not type_defs:
_park_absent(caller, callee, type_name, rc)
continue
if len(type_defs) != 1: # ambiguous -> bail (god-node guard)
continue
type_nid = type_defs[0]
type_qualified = False
method_nid = method_index.get((type_nid, _key(callee)))
target = method_nid or type_nid
relation = "calls" if method_nid else "references"
target, relation = _resolve_member_target(type_defs, callee)
if target is None:
continue
if target == caller or (caller, target) in existing_pairs:
continue
existing_pairs.add((caller, target))
+28
View File
@@ -120,3 +120,31 @@ def test_objc_same_named_categories_in_different_directories_stay_distinct(tmp_p
assert len(_nodes_labelled(result, "Thing")) == 2
assert [e for e in result["edges"]
if e.get("relation") == "calls" and _label(result, e["source"]) == "-go"] == []
def test_objc_class_split_across_header_and_impl_dirs_resolves_members(tmp_path: Path):
"""One class with `.h` in `include/` and `.m` in `src/` still resolves `[w render]`.
`_merge_decl_def_classes` folds decl/def pairs only within a directory, so this
split mints two same-labelled `Widget` nodes that never merge. The resolver unions
their members and binds the selector to the sole DEFINITION (the `.m` node), rather
than bailing on "two candidates" — the header-only node is a bare declaration.
"""
_write(tmp_path / "include" / "Widget.h",
"@interface Widget : NSObject\n- (void)render;\n@end\n")
_write(tmp_path / "src" / "Widget.m",
'#import "Widget.h"\n@implementation Widget\n- (void)render {}\n@end\n')
_write(tmp_path / "src" / "Caller.m",
'#import "Widget.h"\n@implementation Caller\n'
"- (void)go { Widget *w = [Widget new]; [w render]; }\n@end\n")
result = extract(sorted(tmp_path.rglob("*.[hm]")), cache_root=tmp_path / "cache")
assert len(_nodes_labelled(result, "Widget")) == 2
render_defs = [n for n in _nodes_labelled(result, "-render")
if str(n.get("source_file", "")).endswith(".m")]
assert len(render_defs) == 1
hits = [e for e in result["edges"]
if e.get("relation") == "calls" and _label(result, e["source"]) == "-go"]
assert len(hits) == 1
assert hits[0]["target"] == render_defs[0]["id"]
assert hits[0].get("confidence") == "INFERRED"