kconfig_gates.py¶
clew/kconfig_gates.py
symbol → `file:line` — which source lines a `CONFIG_*` symbol gates.
ensure_kconfig_gates_table
None clew.kconfig_gates.ensure_kconfig_gates_table(sqlite3.Connection conn)
Idempotent creation of the kconfig_gates table.
Source · 54 lines
def ensure_kconfig_gates_table(conn: sqlite3.Connection) -> None:
"""Created SEPARATELY from `kconfig_symbols`, next to the harvest that fills it,
for the reason `ensure_kconfig_tables` records: the two layers fail
independently — a repo can have a parseable Kconfig and no C sources, or gated C
and an unparseable Kconfig — and a shared creator would make one layer's absence
look like the other's.
`symbol` stores the Kconfig symbol name WITHOUT the `CONFIG_` prefix, so it joins
`kconfig_symbols.name` directly; `macro` keeps the token as the source wrote it,
because that is what a reader greps for.
No foreign key to `kconfig_symbols`: a gate on an undeclared symbol is a finding
this layer exists to keep, and a REFERENCES would make storing it impossible.
`origin` says how the symbol's NAME is accounted for — Kconfig's prefix, the target's
own declared preprocessor configuration, or nothing this build could see. It is a label
and never a filter: an undeclared gate is a finding about the target (a symbol nobody
has declared, or dead code behind one nobody can set), and dropping those rows would
delete the evidence — the same argument this module already makes for a gate on a
symbol no Kconfig declares.
`end_line` CLOSES THE RANGE, and the branch it closes is the one the row's own `form`
describes — NOT the whole conditional. Measured with `.claude/tmp/gate_extent_probe.py`: a
`preproc_ifdef` node spans lines 3-7 for `#ifdef FEATURE_A ... #else ... #endif`, so a range
join over the node's own extent reports the code in the `#else` as gated ON `FEATURE_A` when
it is present exactly when `FEATURE_A` is NOT set. A confidently inverted variant is worse
than no answer, so the row stops at its alternative and the alternative gets its own row with
the opposite polarity.
DEFAULT 0 MEANS "EXTENT UNKNOWN", which is what an index built before this column knew. A
consumer must not read 0 as "covers nothing" or as "covers everything" — `gates_covering`
skips those rows and says so, rather than guessing a range for them.
@brief Create the CONFIG-gating table.
@version 3
"""
conn.execute(
f"""
CREATE TABLE IF NOT EXISTS kconfig_gates (
symbol TEXT NOT NULL,
macro TEXT NOT NULL,
form TEXT NOT NULL {check("kconfig_gates", "form")},
file_path TEXT NOT NULL,
line INTEGER NOT NULL,
end_line INTEGER NOT NULL DEFAULT 0,
origin TEXT NOT NULL {check("kconfig_gates", "origin")},
UNIQUE(symbol, form, file_path, line)
)
"""
)
conn.execute("CREATE INDEX IF NOT EXISTS idx_kconfig_gates_symbol ON kconfig_gates(symbol)")
conn.commit()
Activity · 3 steps
Requirements
gate_harvester
Harvester clew.kconfig_gates.gate_harvester()
The kconfig-gate stage's harvester.
Source · 12 lines
def gate_harvester() -> Harvester:
"""Public factory so the gh#358 shared parse pass can warm this stage's cache
rows without importing a private class — and so there is ONE construction
site, which is what makes the pass's key and the stage's key equal by
construction rather than by two matching edits.
@brief Build this stage's harvester.
@version 1
"""
return _GateHarvester()
Activity · 1 steps
Requirements
harvest
Any clew.kconfig_gates._GateHarvester.harvest(self, Any tree, bytes src_bytes)
Harvest one file's CONFIG gating sites.
Source · 20 lines
def harvest(self, tree: Any, src_bytes: bytes) -> Any:
"""Returns LISTS rather than tuples because the payload is JSON-serialised
into the cache, where a tuple comes back as a list anyway — so producing
lists here keeps a cache hit and a fresh parse structurally identical, which
is the invariant `run_harvest`'s docstring depends on.
@brief Walk one file's preprocessor conditionals.
@return Gate entries for this file.
@version 1
"""
found: list[list[Any]] = []
stack = [tree.root_node]
while stack:
node = stack.pop()
if node.type == _IFDEF_NODE or node.type in _EXPR_NODES:
found.extend(list(gate) for gate in _gates_from_node(node))
stack.extend(node.children)
return found
Activity · 8 steps
Requirements
import_kconfig_gates
int clew.kconfig_gates.import_kconfig_gates(Path db_path, Path repo_root, IndexCache|None cache=None, Iterable[str] declared=())
Harvest and store every preprocessor gating site, labelled by its origin.
Called by _build_stages, run_real_stages
Tested by 22 tests
test_a_config_prefixed_symbol_is_labelled_kconfig_whatever_else_is_declared, test_a_config_symbol_does_not_outrank_a_function_it_gates, test_a_gate_covers_its_own_branch_and_the_else_gets_inverted_polarity, test_a_gate_on_an_undeclared_symbol_is_kept, test_a_gate_without_an_extent_is_reported_as_unknown_not_as_ungated, test_a_gated_on_symbol_is_a_dossier_subject, test_a_macro_only_dossier_carries_the_first_sites_gates, test_a_name_that_is_neither_declared_nor_gated_is_still_not_a_config_subject, test_a_plain_ifndef_gate_survives_the_include_guard_exclusion, test_a_repo_that_uses_no_kconfig_still_gets_a_gate_layer, test_a_runtime_is_enabled_branch_is_not_a_gate, test_an_include_guard_is_not_a_configuration_gate 10 more
test_defined_is_an_operator_and_never_a_gating_symbol, test_each_definition_site_reports_its_own_gate_polarity, test_gate_counts_reach_the_symbol_rows, test_gating_sites_record_the_form_and_the_line, test_one_conditional_naming_two_symbols_yields_two_rows, test_origin_labels_the_declared_variant_without_filtering_the_rest, test_search_finds_a_gating_symbol_by_name, test_the_gate_layer_reports_its_own_state_independently_of_kconfig, test_the_gate_list_can_be_narrowed_to_one_symbol, test_the_include_guard_is_not_reported_as_a_configuration_gate
Calls _origin, _rows_from_harvest, declared_macro_names, ensure_kconfig_gates_table, gate_harvester, run_harvest
Source · 50 lines
def import_kconfig_gates(
db_path: Path,
repo_root: Path,
cache: IndexCache | None = None,
declared: Iterable[str] = (),
) -> int:
"""CREATES THE TABLE UNCONDITIONALLY once tree-sitter is available, even at zero
rows, and does NOT create it when tree-sitter is missing. That asymmetry is the
point: an empty table means "we walked the tree and this repo gates nothing",
an absent one means "this layer never ran", and a consumer distinguishing a
correct negative from a detector failure needs both. Writing down the wrong one
is the mistake this repo has made three times.
Runs INDEPENDENTLY of whether a Kconfig was found — see the module docstring on
why a gate is recorded for an undeclared symbol. `declared` only LABELS the rows, so
a target that has declared nothing still gets a complete layer.
@brief Fill kconfig_gates from the indexed source tree.
@return Row count.
@version 5
"""
conn = sqlite3.connect(str(db_path))
try:
ensure_kconfig_gates_table(conn)
harvested = run_harvest(conn, repo_root, gate_harvester(), cache)
names = declared_macro_names(declared)
rows = [
(symbol, macro, form, path, line, end, _origin(symbol, macro, names))
for symbol, macro, form, path, line, end in _rows_from_harvest(conn, harvested)
]
conn.executemany(
"INSERT OR IGNORE INTO kconfig_gates"
"(symbol, macro, form, file_path, line, end_line, origin) VALUES(?,?,?,?,?,?,?)",
rows,
)
conn.commit()
finally:
conn.close()
logger.info(
"kconfig gates: %d gating site(s) over %d distinct symbol(s) "
"(%d kconfig, %d declared, %d undeclared)",
len(rows),
len({row[0] for row in rows}),
sum(1 for row in rows if row[6] == GATE_ORIGIN_KCONFIG),
sum(1 for row in rows if row[6] == GATE_ORIGIN_DECLARED),
sum(1 for row in rows if row[6] == GATE_ORIGIN_UNDECLARED),
)
return len(rows)