call_edges.py¶
clew/call_edges.py
Two-layer call-graph importer for the unified `call_edges` table.
build_call_edges
None clew.call_edges.build_call_edges(Path db_path)
Create call_edges table keyed on memberdef.rowid and import.
Called by _build_stages, _make_db, run_real_stages, schema_db
Tested by 11 tests
test_a_bare_call_reaches_a_RECOVERED_definition, test_a_bare_call_reaches_a_pair_doxygen_wrote_both_halves_of, test_a_macro_with_no_recorded_expansion_is_still_named, test_a_pair_reachable_through_two_macros_names_one_deterministically, test_build_call_edges_creates_indexes, test_build_call_edges_imports_only_function_inline_xrefs, test_macro_hop_adds_nothing_when_no_macro_mediates_a_call, test_macro_hop_composes_the_two_hops_doxygen_already_recorded, test_macro_hop_is_resolved_not_exact, test_macro_hop_names_the_macro_that_made_the_edge, test_the_query_layer_quotes_doxygens_own_expansion_verbatim
Calls none
Source · 57 lines
def build_call_edges(db_path: Path) -> None:
"""Create call_edges table keyed on memberdef.rowid and import
inline-context xrefs emitted by doxygen's sqlite3 backend.
'inline' in doxygen's xref context means "referenced from within
a function body" — the closest proxy for a call edge in the
sqlite3 backend. The `source` CHECK includes 'fnptr' for the
sibling `callback_edges` module's Layer 4, which inserts into this
same table after Layers 1-3 have run.
@brief Build call_edges table and import doxygen sqlite3 inline xrefs.
@version 6
"""
conn = sqlite3.connect(str(db_path))
conn.execute("DROP TABLE IF EXISTS call_edges")
conn.execute(
f"""
CREATE TABLE call_edges (
caller_rowid INTEGER NOT NULL REFERENCES memberdef(rowid),
callee_rowid INTEGER NOT NULL REFERENCES memberdef(rowid),
source TEXT NOT NULL {check("call_edges", "source")},
confidence TEXT NOT NULL {check("call_edges", "confidence")},
-- WHY this edge exists, when the answer is a macro (gh#350). NULL on every
-- other layer, because only the macro hop is COMPOSED from two observations
-- and only there is the intermediary a thing a caller cannot see. It is the
-- macro's memberdef ROWID and not its expansion TEXT: doxygen already stores
-- that text in `memberdef.initializer`, and copying it here would create a
-- second place for it to be stale. The query layer joins for it.
via_macro_rowid INTEGER REFERENCES memberdef(rowid),
UNIQUE(caller_rowid, callee_rowid, source)
)
""",
)
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_call_edges_caller ON call_edges(caller_rowid)",
)
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_call_edges_callee ON call_edges(callee_rowid)",
)
imported = conn.execute(
"""
INSERT OR IGNORE INTO call_edges
(caller_rowid, callee_rowid, source, confidence)
SELECT x.src_rowid, x.dst_rowid, 'doxygen_sqlite', 'exact'
FROM xrefs x
JOIN memberdef m1 ON m1.rowid = x.src_rowid
JOIN memberdef m2 ON m2.rowid = x.dst_rowid
WHERE x.context = 'inline'
AND m1.kind = 'function'
AND m2.kind = 'function'
""",
).rowcount
conn.commit()
conn.close()
logger.info("call_edges: imported %d edges from doxygen sqlite3 xrefs", imported)
Activity · 9 steps
Requirements
import_macro_hop_edges
int clew.call_edges.import_macro_hop_edges(Path db_path)
Compose caller→callee edges through the macro hop doxygen already records.
Called by _build_stages
Tested by 7 tests
test_a_macro_with_no_recorded_expansion_is_still_named, test_a_pair_reachable_through_two_macros_names_one_deterministically, test_macro_hop_adds_nothing_when_no_macro_mediates_a_call, test_macro_hop_composes_the_two_hops_doxygen_already_recorded, test_macro_hop_is_resolved_not_exact, test_macro_hop_names_the_macro_that_made_the_edge, test_the_query_layer_quotes_doxygens_own_expansion_verbatim
Calls _table_exists
Source · 92 lines
def import_macro_hop_edges(db_path: Path) -> int:
"""Recover the calls a codebase makes THROUGH function-like macros.
THE HOPS WERE ALWAYS THERE. doxygen models a `#define` as a first-class memberdef of
`kind='macro definition'` and records both halves in `xrefs`:
`caller (function) → STORE_SET_FOO (macro definition)` and
`STORE_SET_FOO → store_set (function)`. `import_call_edges` requires BOTH endpoints
to be `kind='function'`, so every one of those pairs was discarded — and a codebase
whose accessor convention is entirely macros therefore produced ZERO outgoing call
edges for its macro-only callers. Not a doxygen limitation; ours.
Composing the two hops is sound: expanding the macro at that call site produces a
call to the callee. A macro that wraps SEVERAL calls emits several edges, and all of
them are real — the caller does reach each one.
NOT FIXED BY THE PREPROCESSOR, which is where I looked first and was wrong.
`MACRO_EXPANSION=YES` + `EXPAND_ONLY_PREDEF=NO` is a measured NO-OP here: zero delta
on memberdef, functions, bodies, call_edges by source and shared_key_edges, on a
purpose-built fixture AND on entropic, with the instrument validated (on a macro that
GENERATES definitions the same flag moves functions 1→5 and edges 1→13).
CONFIDENCE is 'resolved', not 'exact'. Both hops are doxygen's own observations, so
this is stronger than any name match — but the join is ours, and 'exact' is reserved
for what doxygen stated directly. `CALL_SOURCE` ranks it accordingly.
SCOPE IS THE REAL GATE, and it is worth knowing before trusting a zero here: with the
macro's DEFINING HEADER outside the indexed set, doxygen has no macro memberdef to
hang either hop on — measured, macro rows 7→1 and function→macro xrefs 15→0, so the
call sites leave no trace at all. Documentation is NOT the gate (undocumented macros
index fine under the forced `EXTRACT_ALL`). So an empty result here means "no
macro-mediated calls IN SCOPE", which is a claim about `index_scope`, not about the
repo.
Self-referential and non-function endpoints are excluded: a macro referencing itself
or another macro would compose a hop that is not a call.
THE MACRO ITSELF IS NOW RECORDED (gh#350), in `via_macro_rowid`. Before it, a caller saw
an edge at confidence `resolved` and could not see the thing that made it — the one layer
here whose edge does not correspond to any text in the caller's body. Naming the
intermediary PUBLISHES A FACT: it infers no role, needs no threshold and needs no ranking.
The caller reads the macro and judges for itself. That is deliberately the opposite of the
rejected auto-discovery direction, which tried to infer a role FROM the expansion and could
not, because `do { } while (0)` is a disabled trace macro AND a compiler hint AND a
portability stub.
`MIN(macro.rowid)` RATHER THAN AN ARBITRARY PICK, and it is a real choice. `UNIQUE(caller,
callee, source)` means one caller→callee pair holds ONE macro-hop row even when the pair is
genuinely reachable through two different macros, so something has to choose. Joining the
macro to the UNIQUE key would multiply rows per neighbour, which is the inflation gh#38
removed — every neighbour list doubled and every depth cap truncating half the real edges.
So the pair stays one row and `MIN` makes which macro it names DETERMINISTIC; a bare
`DISTINCT` left it to whatever the query planner returned first, which would differ between
two builds of the same commit. The macro named is therefore ONE witness, not the only one.
@brief Import call edges composed through a macro definition, naming the macro.
@return Count of edges imported.
@version 4
"""
conn = sqlite3.connect(str(db_path))
try:
if not _table_exists(conn, "xrefs"):
logger.info("call_edges: no xrefs table — skipping the macro hop layer")
return 0
imported = conn.execute(
f"""
INSERT OR IGNORE INTO call_edges
(caller_rowid, callee_rowid, source, confidence, via_macro_rowid)
SELECT xin.src_rowid, xout.dst_rowid,
'{CALL_SOURCE_MACRO_HOP}', '{CALL_MATCH_RESOLVED}',
MIN(macro.rowid)
FROM xrefs xin
JOIN memberdef caller ON caller.rowid = xin.src_rowid
JOIN memberdef macro ON macro.rowid = xin.dst_rowid
JOIN xrefs xout ON xout.src_rowid = macro.rowid
JOIN memberdef callee ON callee.rowid = xout.dst_rowid
WHERE caller.kind = 'function'
AND macro.kind = 'macro definition'
AND callee.kind = 'function'
AND xin.src_rowid <> xout.dst_rowid
GROUP BY xin.src_rowid, xout.dst_rowid
""",
).rowcount
conn.commit()
finally:
conn.close()
logger.info("call_edges: imported %d edges composed through a macro definition", imported)
return imported
# ─── Layer 3: tree-sitter AST walk ──────────────────────────────────────────