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.
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
Requirements
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.
Called by _variable_subject
Tested by
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
Requirements
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.
Called by none
Tested by 9 tests
test_a_parsed_row_is_distinguishable_from_a_doxygen_row_at_every_surface, test_an_unmatched_selector_returns_nothing_rather_than_the_union, test_name_accessors_degrade_gracefully_on_table_less_db, test_source_caps_and_flags_truncation, test_source_returns_the_verbatim_body, test_source_unambiguous_has_no_candidates, test_source_unknown_function_is_none, test_source_unreadable_repo_root_is_none, test_the_recovered_body_is_readable_source_which_it_previously_was_not
Calls _body_extent, _read_body, candidate_rows, connect, function_candidates, is_overloaded, symbol_provenance
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