source.py

clew/query/source.py

Verbatim source retrieval for one function — the "show me the code" query.

body_excerpt

BodyExcerpt | None clew.query.source.body_excerpt(sqlite3.Connection conn, int rowid, Path|str repo_root, int max_lines=DEFAULT_BODY_LINES)

A bounded body excerpt for one ALREADY-RESOLVED rowid.

clew/query/source.py:201

Show on the map →

Called by _anchored_pick, _body_and_external, _enum_subject

Calls _anchored, _body_extent, _read_body

Source · 48 lines
def body_excerpt(
    conn: sqlite3.Connection,
    rowid: int,
    repo_root: Path | str,
    max_lines: int = DEFAULT_BODY_LINES,
) -> BodyExcerpt | None:
    """The dossier's body panel. Takes a ROWID rather than a name on purpose: the
    dossier has already chosen which of several same-named functions it describes
    (gh#26/gh#37), and re-resolving from the name here would be a second, independent
    notion of identity able to quote a DIFFERENT function's source text under the
    described function's heading — the worst possible version of that bug, because a
    body looks like proof.

    None is a real answer with several distinct causes, all of them "no body to show":
    a header-only declaration, a macro-defined symbol, an unreadable working tree, a
    body path that escapes `repo_root`.

    THE NAME USED TO BE READ AND DISCARDED, one underscore away from the check this function
    most needed. The lines come from the LIVE working tree at the span the INDEX recorded, so a
    file edited since the build yields whatever now occupies those numbers — and the docstring
    above already names quoting another function's text as "the worst possible version of that
    bug, because a body looks like proof" while guarding only the re-resolution route to it. The
    staler route was left open: same rowid, same identity, moved file. `anchor_mismatch` closes
    it by DISCLOSURE, since the lines may still help and hiding them would be worse.

    @brief Read a resolved rowid's bounded body excerpt.
    @return BodyExcerpt or None.
    @version 2
    """
    extent = _body_extent(conn, rowid)
    if extent is None:
        return None
    name, file, start, end = extent
    result = _read_body(Path(repo_root).expanduser(), file, start, end, max(1, max_lines))
    if result is None:
        return None
    lines, end_line, truncated = result
    return BodyExcerpt(
        file=file,
        start_line=start,
        end_line=end_line,
        total_lines=end - start + 1,
        truncated=truncated,
        lines=tuple(lines),
        anchor_mismatch=not _anchored(name, lines),
    )

Activity · 9 steps
lines 230–230extent = _body_extent(conn, rowid)lines 231–231◇ if extent is Nonelines 232–232return Nonelines 233–233name, file, start, end = extentlines 234–234result = _read_body(Path(repo_root).expanduser(), file, start, end, max(1, max_lines))lines 235–235◇ if result is Nonelines 236–236return Nonelines 237–237lines, end_line, truncated = resultlines 238–246return BodyExcerpt( file=file, start_line=start, end_line=end_line, total_lines=end - start + 1, truncated=truncated, lines=tuple(lines), anchor_mismatch=not _anchored(name, lines), )

Requirements

REQ-DDB-QUERY-001

declaration_excerpt

BodyExcerpt | None clew.query.source.declaration_excerpt(sqlite3.Connection conn, int rowid, Path|str repo_root, int max_lines=DECLARATION_MAX_LINES)

Version 2: a declaration whose end the build recorded is read to that end, under the body cap.

clew/query/source.py:266

Show on the map →

Called by _variable_subject

Tested by

1 teststest_the_declaration_excerpt_stops_at_the_statement_terminator

Calls _known_extent, _read_body, _recorded_end, _to_terminator

Source · 53 lines
def declaration_excerpt(
    conn: sqlite3.Connection,
    rowid: int,
    repo_root: Path | str,
    max_lines: int = DECLARATION_MAX_LINES,
) -> BodyExcerpt | None:
    """WHAT `body_excerpt` CANNOT DO, AND WHY THAT MATTERS. It reads `bodystart`..
    `bodyend`, and a variable has neither — both columns are 0 for every one of the
    10,498 memberdef rows on the public mbedtls index whose kind is not 'function'. So
    the one panel that would show a variable's binding was unreachable through the
    function-shaped path, and the binding is exactly what a caller asking about a
    function pointer wants.

    The extent is `memberdef.line` through the first line containing a `;`, bounded by
    `max_lines`. THAT IS A HEURISTIC AND IT IS STATED AS ONE: `truncated` is True when
    the bound was reached without finding a terminator, so a caller can see that the
    statement continues rather than reading a clipped excerpt as complete. A `;` inside
    a string literal or a trailing comment would end the excerpt early — the cost of
    that is a short excerpt, never a wrong file or a wrong line.

    Reads the DECLARING file (`file_id`), not the body file: a variable has no body
    file, and falling back to one would quote a different translation unit.

    @brief Read the source statement a symbol is declared on.
    @return BodyExcerpt over the declaration, or None.
    @version 1
    """
    row = conn.execute(
        "SELECT COALESCE(p.name,''), m.line FROM memberdef m "
        "LEFT JOIN path p ON p.rowid = m.file_id WHERE m.rowid=?",
        (rowid,),
    ).fetchone()
    if row is None or not row[0] or not row[1]:
        return None
    file, start = row[0], int(row[1])
    known = _recorded_end(conn, rowid)
    if known is not None and known >= start:
        return _known_extent(Path(repo_root).expanduser(), file, start, known)
    cap = max(1, min(max_lines, DECLARATION_MAX_LINES))
    result = _read_body(Path(repo_root).expanduser(), file, start, start + cap - 1, cap)
    if result is None or not result[0]:
        return None
    lines = _to_terminator(result[0])
    return BodyExcerpt(
        file=file,
        start_line=start,
        end_line=start + len(lines) - 1,
        total_lines=len(lines),
        truncated=len(lines) == cap and ";" not in lines[-1],
        lines=tuple(lines),
    )

Activity · 13 steps
lines 293–297row = conn.execute( "SELECT COALESCE(p.name,''), m.line FROM memberdef m " "LEFT JOIN path p ON p.rowid = m.file_id WHERE m.rowid=?", (rowid,), ).fetchone()lines 298–298◇ if row is None or not row[0] or not row[1]lines 299–299return Nonelines 300–300file, start = row[0], int(row[1])lines 301–301known = _recorded_end(conn, rowid)lines 302–302◇ if known is not None and known >= startlines 303–303return _known_extent(Path(repo_root).expanduser(), file, start, known)lines 304–304cap = max(1, min(max_lines, DECLARATION_MAX_LINES))lines 305–305result = _read_body(Path(repo_root).expanduser(), file, start, start + cap - 1, cap)lines 306–306◇ if result is None or not result[0]lines 307–307return Nonelines 308–308lines = _to_terminator(result[0])lines 309–316return BodyExcerpt( file=file, start_line=start, end_line=start + len(lines) - 1, total_lines=len(lines), truncated=len(lines) == cap and ";" not in lines[-1], lines=tuple(lines), )

Requirements

REQ-DDB-QUERY-001

source

SourceListing | None clew.query.source.source(DbSource db, str function, Path|str repo_root, int max_lines=DEFAULT_MAX_LINES, str|None qualified=None)

Verbatim, capped source body for one function.

clew/query/source.py:463

Source · 52 lines
def source(
    db: DbSource,
    function: str,
    repo_root: Path | str,
    max_lines: int = DEFAULT_MAX_LINES,
    qualified: str | None = None,
) -> SourceListing | None:
    """Return the function's actual source text, read from `repo_root` at the
    body extent the database recorded (definition-preferring resolution, so a
    header declaration never shadows the definition). When the name is a
    genuine overload the body shown is the definition-preferring pick and the
    alternatives ride along in `candidates`.

    None when the name is not indexed, when no body extent exists (header-only
    declaration), when the file cannot be read from `repo_root`, or when the
    recorded body path escapes `repo_root` (a tampered/foreign db — refused).

    `qualified` (gh#37) picks WHICH same-named function to read: pass a
    `candidates[i].qualified` from any prior reply. This is the accessor where the
    bare-name guess is most visibly wrong — it returns a body, so a model reads the
    wrong function's actual source text and has no signal that it did.

    @brief Read one function's verbatim body, capped at `max_lines`.
    @return SourceListing or None.
    @version 5
    """
    with connect(db) as conn:
        cands = function_candidates(conn, function, qualified)
        rowid = cands[0][0] if cands else None
        extent = _body_extent(conn, rowid) if rowid is not None else None
        ## Ambiguity is a property of the NAME, so it is measured over every
        ## same-named row — a narrowed call must still disclose the alternatives.
        all_cands = function_candidates(conn, function) if qualified is not None else cands
        overloads = candidate_rows(conn, function, all_cands) if is_overloaded(all_cands) else []
        provenance = symbol_provenance(conn, rowid) if rowid is not None else None
    if extent is None:
        return None
    name, file, start, end = extent
    result = _read_body(Path(repo_root).expanduser(), file, start, end, max(1, max_lines))
    if result is None:
        return None
    lines, end_line, truncated = result
    return SourceListing(
        name=name,
        file=file,
        start_line=start,
        end_line=end_line,
        lines=lines,
        truncated=truncated,
        candidates=overloads,
        provenance=provenance,
    )

1 more lines, not shown

Activity · 15 steps
lines 489–489with connect(db) as connlines 490–490cands = function_candidates(conn, function, qualified)lines 491–491rowid = cands[0][0] if cands else Nonelines 492–492extent = _body_extent(conn, rowid) if rowid is not None else Nonelines 495–495all_cands = function_candidates(conn, function) if qualified is not None else candslines 496–496overloads = candidate_rows(conn, function, all_cands) if is_overloaded(all_cands) else []lines 497–497provenance = symbol_provenance(conn, rowid) if rowid is not None else Nonelines 498–498◇ if extent is Nonelines 499–499return Nonelines 500–500name, file, start, end = extentlines 501–501result = _read_body(Path(repo_root).expanduser(), file, start, end, max(1, max_lines))lines 502–502◇ if result is Nonelines 503–503return Nonelines 504–504lines, end_line, truncated = resultlines 505–514return SourceListing( name=name, file=file, start_line=start, end_line=end_line, lines=lines, truncated=truncated, candidates=overloads, provenance=provenance, )

Requirements

REQ-DDB-QUERY-001