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.

clew/context.py:70

Show on the map →

Called by extract_context_conflicts, schema_db

Calls none

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
lines 79–98conn.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); """ )

Requirements

REQ-DDB-SCHEMA-011

extract_context_conflicts

None clew.context.extract_context_conflicts(Path db_path)

Derive every interrupt-context conflict in the index.

clew/context.py:347

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

Activity · 15 steps
lines 355–355conn = sqlite3.connect(str(db_path))lines 356–356ensure_context_table(conn)lines 357–360handlers = conn.execute( "SELECT id, entry_memberdef_rowid FROM threads " "WHERE kind = 'isr' AND entry_memberdef_rowid IS NOT NULL" ).fetchall()lines 361–361ambiguous = _multiply_defined(conn)lines 362–362rows: list[tuple] = []lines 363–363◇ for thread_id, entry_rowid in handlerslines 364–364closure = _isr_closure(conn, entry_rowid, ambiguous)lines 365–365rows.extend(_lock_rows(conn, closure, thread_id))lines 366–366rows.extend(_call_rows(conn, closure, thread_id))lines 367–372inserted = conn.executemany( "INSERT OR IGNORE INTO context_conflicts " "(thread_id, member_rowid, verdict, reason, evidence, detail, path_rowid, line, " " depth, confidence) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", rows, ).rowcountlines 373–373conn.commit()lines 374–376split = dict( conn.execute("SELECT reason, COUNT(*) FROM context_conflicts GROUP BY reason").fetchall() )lines 377–379conflicts = conn.execute( "SELECT COUNT(*) FROM context_conflicts WHERE verdict = ?", (VERDICT_CONFLICT,) ).fetchone()[0]lines 380–380conn.close()lines 381–387logger.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", )

Requirements

REQ-DDB-SCHEMA-011