Skip to content

Commit 70f1022

Browse files
xiongjianxusafishamsi
authored andcommitted
Mark ObjC declarations with _callable / _callable_class (#3228)
Every other extractor stamps its definitions through the generic engine: `_callable` for "a real callable, not a same-named data symbol" (#2438), narrowed to `_callable_class` for a type, which is callable only through a constructor (#2137). The ObjC extractor builds its nodes by hand and set neither, so an ObjC class was invisible to every pass that indexes declarations by those markers — the `same_type_as` pass from #3007, the qualified-name indexes, and anything new that needs to tell a declaration from a reference — and an ObjC method could not be told apart from a data symbol of the same name. `@interface`, `@implementation` and `@protocol` nodes now carry both markers (a protocol is a type declaration, the same way the engine marks a Java or C# interface), and method nodes carry `_callable`. The file node and the stubs minted for dangling references stay unmarked: a stub has no declaration behind it. (cherry picked from commit 9a64af9)
1 parent 83a393b commit 70f1022

2 files changed

Lines changed: 126 additions & 7 deletions

File tree

‎graphify/extractors/objc.py‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
# `C++Bridge.h` and `Foo+.h` are left intact.
1414
_OBJC_STEM_PART = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
1515

16+
# Declaration markers, matching what the generic engine puts on every other
17+
# language's definitions (#2438): `_callable` says "a real callable, not a
18+
# same-named data symbol", and `_callable_class` narrows that to a type, which is
19+
# callable only through a constructor. Passes that index declarations gate on
20+
# these, so a node without them is invisible to them.
21+
_CALLABLE = ("_callable",)
22+
_CALLABLE_CLASS = ("_callable", "_callable_class")
23+
1624

1725
def _objc_category_base_stem(stem: str) -> str:
1826
"""Strip an ObjC category/extension suffix from a file stem (``Foo+Cat`` -> ``Foo``).
@@ -116,11 +124,14 @@ def extract_objc(path: Path) -> dict:
116124
# same (class, field) tombstones the entry (None) — drop, don't guess.
117125
objc_field_types: dict[str, dict[str, str | None]] = {}
118126

119-
def add_node(nid: str, label: str, line: int) -> None:
127+
def add_node(nid: str, label: str, line: int, markers: tuple[str, ...] = ()) -> None:
120128
if nid not in seen_ids:
121129
seen_ids.add(nid)
122-
nodes.append({"id": nid, "label": label, "file_type": "code",
123-
"source_file": str_path, "source_location": f"L{line}"})
130+
node = {"id": nid, "label": label, "file_type": "code",
131+
"source_file": str_path, "source_location": f"L{line}"}
132+
for marker in markers:
133+
node[marker] = True
134+
nodes.append(node)
124135

125136
def add_edge(src: str, tgt: str, relation: str, line: int,
126137
confidence: str = "EXTRACTED", weight: float = 1.0,
@@ -481,7 +492,7 @@ def walk(node, parent_nid: str | None = None) -> None:
481492
# produced fine when the members lived in `Foo.h` (#1556).
482493
cls_stem = _objc_category_base_stem(stem) if _objc_is_category(node) else stem
483494
cls_nid = _make_id(cls_stem, name)
484-
add_node(cls_nid, name, line)
495+
add_node(cls_nid, name, line, _CALLABLE_CLASS)
485496
add_edge(file_nid, cls_nid, "contains", line)
486497
# superclass is second identifier after ':'
487498
colon_seen = False
@@ -546,7 +557,7 @@ def walk(node, parent_nid: str | None = None) -> None:
546557
impl_stem = _objc_category_base_stem(stem) if _objc_is_category(node) else stem
547558
impl_nid = _make_id(impl_stem, name)
548559
if impl_nid not in seen_ids:
549-
add_node(impl_nid, name, line)
560+
add_node(impl_nid, name, line, _CALLABLE_CLASS)
550561
add_edge(file_nid, impl_nid, "contains", line)
551562
for child in node.children:
552563
if child.type == "instance_variables":
@@ -564,7 +575,9 @@ def walk(node, parent_nid: str | None = None) -> None:
564575
break
565576
if name:
566577
proto_nid = _make_id(stem, name)
567-
add_node(proto_nid, f"<{name}>", line)
578+
# A protocol is a type declaration like any other interface, and
579+
# the engine marks a Java/C# interface the same way.
580+
add_node(proto_nid, f"<{name}>", line, _CALLABLE_CLASS)
568581
add_edge(file_nid, proto_nid, "contains", line)
569582
# Adopted protocols: `@protocol Derived <Base, Other>`. These
570583
# nest under a protocol_reference_list node (distinct from the
@@ -599,7 +612,7 @@ def walk(node, parent_nid: str | None = None) -> None:
599612
method_name = "".join(parts) if parts else None
600613
if method_name:
601614
method_nid = _make_id(container, method_name)
602-
add_node(method_nid, f"{prefix}{method_name}", line)
615+
add_node(method_nid, f"{prefix}{method_name}", line, _CALLABLE)
603616
add_edge(container, method_nid, "method", line)
604617
if t == "method_definition":
605618
method_bodies.append((method_nid, node, container))
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""ObjC declarations carry the `_callable` / `_callable_class` markers.
2+
3+
Every other extractor stamps its definitions with `_callable` — "a real callable,
4+
not a same-named data symbol" (#2438) — and narrows a type to `_callable_class`,
5+
callable only through a constructor (#2137). The generic engine does it for all of
6+
them in one place (`extractors/engine.py`, `callable_def_nids` /
7+
`callable_class_nids`).
8+
9+
The ObjC extractor builds its nodes by hand and set neither, so an ObjC class was
10+
invisible to every pass that indexes declarations by those markers, and an ObjC
11+
method could never be told apart from a data symbol of the same name. This pins the
12+
markers onto the four node kinds ObjC produces, including the two that must stay
13+
unmarked.
14+
"""
15+
from __future__ import annotations
16+
17+
from pathlib import Path
18+
19+
from graphify.extract import extract
20+
21+
GREETER_H = (
22+
"@interface Greeter : NSObject\n"
23+
"- (void)greet;\n"
24+
"+ (instancetype)shared;\n"
25+
"@end\n"
26+
)
27+
GREETER_M = (
28+
"#import \"Greeter.h\"\n"
29+
"@implementation Greeter\n"
30+
"- (void)greet {}\n"
31+
"@end\n"
32+
)
33+
34+
35+
def _extract(tmp_path: Path, files: dict[str, str]) -> dict:
36+
paths = []
37+
for name, body in files.items():
38+
path = tmp_path / name
39+
path.parent.mkdir(parents=True, exist_ok=True)
40+
path.write_text(body, encoding="utf-8")
41+
paths.append(path)
42+
return extract(paths, cache_root=tmp_path / "graphify-out")
43+
44+
45+
def _node(result: dict, label: str) -> dict:
46+
matches = [n for n in result["nodes"] if n.get("label") == label]
47+
assert len(matches) == 1, [n.get("label") for n in result["nodes"]]
48+
return matches[0]
49+
50+
51+
def test_a_class_is_marked_as_a_type(tmp_path: Path):
52+
result = _extract(tmp_path, {"Greeter.h": GREETER_H})
53+
greeter = _node(result, "Greeter")
54+
assert greeter.get("_callable") is True
55+
assert greeter.get("_callable_class") is True
56+
57+
58+
def test_an_implementation_only_class_is_marked_too(tmp_path: Path):
59+
# A class whose `@interface` is not in this corpus is still a declaration.
60+
result = _extract(tmp_path, {"Greeter.m": "@implementation Greeter\n- (void)greet {}\n@end\n"})
61+
greeter = _node(result, "Greeter")
62+
assert greeter.get("_callable") is True
63+
assert greeter.get("_callable_class") is True
64+
65+
66+
def test_a_header_and_impl_pair_keeps_the_markers_after_folding(tmp_path: Path):
67+
# `_merge_decl_def_classes` folds the `.h`/`.m` pair into one node; the markers
68+
# have to be on whichever node survives.
69+
result = _extract(tmp_path, {"Greeter.h": GREETER_H, "Greeter.m": GREETER_M})
70+
greeter = _node(result, "Greeter")
71+
assert greeter.get("_callable") is True
72+
assert greeter.get("_callable_class") is True
73+
74+
75+
def test_a_method_is_callable_but_is_not_a_type(tmp_path: Path):
76+
result = _extract(tmp_path, {"Greeter.h": GREETER_H})
77+
for label in ("-greet", "+shared"):
78+
method = _node(result, label)
79+
assert method.get("_callable") is True, label
80+
assert "_callable_class" not in method, label
81+
82+
83+
def test_a_protocol_is_marked_like_any_other_interface(tmp_path: Path):
84+
# A Java or C# interface gets `_callable_class` from the generic engine, and a
85+
# protocol is the same kind of declaration.
86+
result = _extract(tmp_path, {"Greeting.h": "@protocol Greeting\n- (void)greet;\n@end\n"})
87+
protocol = _node(result, "<Greeting>")
88+
assert protocol.get("_callable") is True
89+
assert protocol.get("_callable_class") is True
90+
91+
92+
def test_a_dangling_reference_is_not_marked(tmp_path: Path):
93+
# `NSObject` is a stub minted for a name this corpus never declares, so it has
94+
# no source file and no declaration behind it.
95+
result = _extract(tmp_path, {"Greeter.h": GREETER_H})
96+
stub = _node(result, "NSObject")
97+
assert not stub.get("source_file")
98+
assert "_callable" not in stub
99+
assert "_callable_class" not in stub
100+
101+
102+
def test_the_file_node_is_not_marked(tmp_path: Path):
103+
result = _extract(tmp_path, {"Greeter.h": GREETER_H})
104+
file_node = _node(result, "Greeter.h")
105+
assert "_callable" not in file_node
106+
assert "_callable_class" not in file_node

0 commit comments

Comments
 (0)