fix(cpp): bind alias references by exact spelling, keep a salted alias's contains edge
Two follow-ups to the declaration-id registry (#4854, #4904), both visible
on C++ aliases.
Since #4904 split case-only twins apart, `using Session = int;` (or
`typedef int Session;`) and `void session()` in one file are two nodes, but
DeclIdRegistry.lookup normalizes the name and walks kinds in a fixed order
with "fn" before "alias", so `Session s;` bound to session(). lookup now
prefers the one declaration spelled exactly like the reference and falls
back to the kind order only without one. Registering aliases as "type"
instead would merge them with a same-kind case twin: doctest's
`using string = basic_string<...>` and `class String` collapsed into one
node. On nlohmann/json this change moves 14 `std::string` references in
doctest.h from `class String` to the `string` alias they name; fmt, leveldb,
googletest, express, zod, okio, guzzle and 12 Swift, Java, C#, Scala,
Python and Ruby repos are unchanged.
The C++ alias site also decided whether it minted a node before the
registry could salt its id, so an alias whose id spells its file's own id
(`using cpp = int;` in a.cpp) got a node but no `contains` edge. Read that
off seen_ids after decl_node_fn, as the Swift alias site does.
AST cache schema 34 -> 35.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit d0da57d8b1)
This commit is contained in:
1 parent
d3b314d92e
commit
b492c8f05b
3 files changed
+35
-2
No files matched your search
@@ -5214,11 +5214,15 @@ def _cpp_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: str
|
||||
line = node.start_point[0] + 1
|
||||
for alias_name in _cpp_alias_names(node, source):
|
||||
alias_nid = _make_id(stem, alias_name)
|
||||
_alias_new = alias_nid not in seen_ids
|
||||
# decl_node_fn may salt the id (#4854), so whether a node was minted
|
||||
# is read off seen_ids after the call, not tested on the unsalted id.
|
||||
known = len(seen_ids)
|
||||
if decl_node_fn is not None:
|
||||
alias_nid = decl_node_fn(alias_nid, alias_name, "alias",
|
||||
alias_name, line)
|
||||
if _alias_new:
|
||||
else:
|
||||
add_node_fn(alias_nid, alias_name, line)
|
||||
if len(seen_ids) > known:
|
||||
add_edge_fn(file_nid, alias_nid, "contains", line)
|
||||
return False
|
||||
if node.type == "enumerator" and parent_class_nid:
|
||||
|
||||
@@ -143,11 +143,20 @@ class DeclIdRegistry:
|
||||
self._owners[candidate] = key
|
||||
self._assigned[key] = candidate
|
||||
self._salted.add(candidate)
|
||||
self._spelled[candidate] = name
|
||||
self._by_name.setdefault((kind, normalize_id(name)), set()).add(candidate)
|
||||
return candidate
|
||||
|
||||
def lookup(self, name: str, kinds=("type", "fn", "var", "ns", "alias")) -> str | None:
|
||||
norm = normalize_id(name)
|
||||
# A declaration spelled exactly like the reference wins over one that
|
||||
# only normalizes alike: once #4904 keeps `Session` and `session()`
|
||||
# apart, `Session s;` names the declaration spelled `Session`, whatever
|
||||
# its kind. Without an exact spelling the kind order decides, as before.
|
||||
exact = {i for kind in kinds for i in self._by_name.get((kind, norm), ())
|
||||
if self._spelled.get(i) == name}
|
||||
if len(exact) == 1:
|
||||
return next(iter(exact))
|
||||
for kind in kinds:
|
||||
ids = self._by_name.get((kind, norm))
|
||||
if ids:
|
||||
|
||||
@@ -172,3 +172,23 @@ def test_aliases_inside_include_guards_and_conditionals_are_nodes(tmp_path: Path
|
||||
def test_a_member_alias_inside_a_conditional_is_still_a_member(tmp_path: Path):
|
||||
result = _extract(tmp_path, {"config.hpp": _GUARDED})
|
||||
assert [n for n in result["nodes"] if n["label"] == "debug_type"] == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("decl", ["using Session = int;", "typedef int Session;"])
|
||||
def test_a_type_reference_lands_on_the_alias_not_a_function_differing_in_case(
|
||||
tmp_path: Path, decl: str):
|
||||
"""`Session` and `session()` in one file are two declarations since #4904,
|
||||
and `Session s;` names the alias, not the function."""
|
||||
result = _extract(tmp_path, {"box.cpp": f"{decl}\nvoid session() {{}}\nstruct Holder {{ Session s; }};\n"})
|
||||
refs = _pairs(result, "references")
|
||||
assert ("Holder", "Session") in refs
|
||||
assert ("Holder", "session()") not in refs
|
||||
|
||||
|
||||
def test_an_alias_named_after_the_file_extension_is_contained_by_its_file(tmp_path: Path):
|
||||
"""`using cpp = int;` in `a.cpp` spells the file's own id, so the registry
|
||||
salts the alias's id (#4854); the file must still contain it."""
|
||||
result = _extract(tmp_path, {"a.cpp": "using cpp = int;\nstruct Box { cpp v; };\n"})
|
||||
assert len([n for n in result["nodes"] if n["label"] == "cpp" and n.get("source_file")]) == 1
|
||||
assert ("a.cpp", "cpp") in _pairs(result, "contains")
|
||||
assert ("Box", "cpp") in _pairs(result, "references")
|
||||
Reference in new issue
Block a user