context.py¶
clew/context.py
What an interrupt handler reaches that it is not allowed to reach.
ensure_context_table
None clew.context.ensure_context_table(sqlite3.Connection conn)
Create the context_conflicts table.
Source · 31 lines
def ensure_context_table(conn: sqlite3.Connection) -> None:
"""Created unconditionally, like every other layer's tables, so "this repository has no
interrupt handler" and "this index predates the layer" stay distinguishable: the first is
an empty table, the second is no table.
@brief Create context_conflicts if it does not exist.
@return None.
@version 1
"""
conn.executescript(
f"""
CREATE TABLE IF NOT EXISTS context_conflicts (
id INTEGER PRIMARY KEY,
thread_id INTEGER NOT NULL REFERENCES threads(id),
member_rowid INTEGER NOT NULL REFERENCES memberdef(rowid),
verdict TEXT NOT NULL {check("context_conflicts", "verdict")},
reason TEXT NOT NULL {check("context_conflicts", "reason")},
evidence TEXT NOT NULL,
detail TEXT NOT NULL,
path_rowid INTEGER REFERENCES path(rowid),
line INTEGER,
depth INTEGER NOT NULL,
confidence TEXT NOT NULL {check("context_conflicts", "confidence")},
UNIQUE(thread_id, member_rowid, reason, evidence, line)
);
CREATE INDEX IF NOT EXISTS idx_context_member ON context_conflicts(member_rowid);
CREATE INDEX IF NOT EXISTS idx_context_thread ON context_conflicts(thread_id);
"""
)
Activity · 1 steps
Requirements
extract_context_conflicts
None clew.context.extract_context_conflicts(Path db_path)
Derive every interrupt-context conflict in the index.
Called by _build_stages, _run, run_real_stages
Tested by
Calls _call_rows, _isr_closure, _lock_rows, _multiply_defined, ensure_context_table
Source · 41 lines
def extract_context_conflicts(db_path: Path) -> None:
"""Runs after the thread stage, which is the first point at which the call graph, the lock
layer and interrupt membership are all final.
@brief Populate context_conflicts and log the count.
@return None.
@version 1
"""
conn = sqlite3.connect(str(db_path))
ensure_context_table(conn)
handlers = conn.execute(
"SELECT id, entry_memberdef_rowid FROM threads "
"WHERE kind = 'isr' AND entry_memberdef_rowid IS NOT NULL"
).fetchall()
ambiguous = _multiply_defined(conn)
rows: list[tuple] = []
for thread_id, entry_rowid in handlers:
closure = _isr_closure(conn, entry_rowid, ambiguous)
rows.extend(_lock_rows(conn, closure, thread_id))
rows.extend(_call_rows(conn, closure, thread_id))
inserted = conn.executemany(
"INSERT OR IGNORE INTO context_conflicts "
"(thread_id, member_rowid, verdict, reason, evidence, detail, path_rowid, line, "
" depth, confidence) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
rows,
).rowcount
conn.commit()
split = dict(
conn.execute("SELECT reason, COUNT(*) FROM context_conflicts GROUP BY reason").fetchall()
)
conflicts = conn.execute(
"SELECT COUNT(*) FROM context_conflicts WHERE verdict = ?", (VERDICT_CONFLICT,)
).fetchone()[0]
conn.close()
logger.info(
"context: %d conflicts across %d interrupt handlers, %d undecided (%s)",
conflicts,
len(handlers),
inserted - conflicts,
", ".join(f"{count} {reason}" for reason, count in sorted(split.items())) or "none",
)1 more lines, not shown