ast_includes.py¶
clew/ast_includes.py
Every C/C++ `#include`, read from the parse and resolved to an indexed file (T3; 2.0.0).
ensure_ast_includes_table
None clew.ast_includes.ensure_ast_includes_table(sqlite3.Connection conn)
Create the ast_includes table.
Source · 18 lines
def ensure_ast_includes_table(conn: sqlite3.Connection) -> None:
"""Created unconditionally, so a tree with no `#include` yields an EMPTY table, not an absent
one. @brief Create the table. @return None."""
conn.executescript(
"""
CREATE TABLE IF NOT EXISTS ast_includes (
src_id INTEGER NOT NULL REFERENCES path(rowid),
dst_id INTEGER REFERENCES path(rowid),
spec TEXT NOT NULL,
local INTEGER NOT NULL,
line INTEGER NOT NULL,
UNIQUE(src_id, line)
);
CREATE INDEX IF NOT EXISTS idx_ast_includes_dst ON ast_includes(dst_id);
"""
)
Activity · 1 steps
Requirements
harvest
Any clew.ast_includes._IncludesHarvester.harvest(self, Any tree, bytes src_bytes)
Harvest one file's include directives.
Source · 18 lines
def harvest(self, tree: Any, src_bytes: bytes) -> Any:
"""@brief Walk one file's `#include`s. @return Entries."""
found: list[list[Any]] = []
stack = [tree.root_node]
while stack:
node = stack.pop()
stack.extend(node.children)
if node.type != _INCLUDE_NODE:
continue
path = node.child_by_field_name("path")
if path is None or path.type not in (_QUOTED, _SYSTEM):
continue
spec = path.text.decode("utf-8", "replace")[1:-1].strip()
if spec:
found.append([spec, 1 if path.type == _QUOTED else 0, node.start_point[0] + 1])
return found
Activity · 14 steps
Requirements
import_ast_includes
int clew.ast_includes.import_ast_includes(Path db_path, Path repo_root, IndexCache|None cache=None)
Record every indexed file's #include directives.
Called by _build_stages, run_real_stages
Tested by
Calls _resolver, _rows, ensure_ast_includes_table, includes_harvester, run_harvest
Source · 19 lines
def import_ast_includes(db_path: Path, repo_root: Path, cache: IndexCache | None = None) -> int:
"""@brief Fill ast_includes. @return Row count."""
conn = sqlite3.connect(str(db_path))
try:
ensure_ast_includes_table(conn)
conn.execute("DELETE FROM ast_includes")
resolver, names = _resolver(conn)
rows = [
row
for src, payload in run_harvest(conn, repo_root, includes_harvester(), cache)
for row in _rows(resolver, src, names.get(src, ""), payload)
]
conn.executemany(_INSERT, rows)
conn.commit()
finally:
conn.close()
resolved = sum(1 for r in rows if r[1] is not None)
logger.info("includes: %d directive(s), %d resolved to an indexed file", len(rows), resolved)
return len(rows)1 more lines, not shown
Activity · 11 steps
Requirements
includes_harvester
Harvester clew.ast_includes.includes_harvester()
The include stage's harvester.
Source · 5 lines
def includes_harvester() -> Harvester:
"""ONE CONSTRUCTION SITE, shared with the plan's parse pass. @brief Build it. @return It."""
return _IncludesHarvester()
Activity · 1 steps
Requirements
refresh_ast_includes
int clew.ast_includes.refresh_ast_includes(sqlite3.Connection conn, Path repo_root, IndexCache cache, dict[str, int] files)
Rewrite the edited files' #include rows (the fast tier).
Source · 27 lines
def refresh_ast_includes(
conn: sqlite3.Connection, repo_root: Path, cache: IndexCache, files: dict[str, int]
) -> int:
"""EXACT, NOT APPROXIMATE (T3 served, 2.0.0). The fast tier runs only when the file set is
unchanged, so every spec resolves against the same paths a full build would see; only the
edited files' own directives can differ, and those are re-read here. NOT
`ensure_ast_includes_table`: its `executescript` would COMMIT mid-transaction, and an index
without the table never ran the layer.
@brief Refresh edited files' includes.
@return Rows written.
"""
from .harvest import HarvestTally, _harvest_one_file
if conn.execute("SELECT 1 FROM sqlite_master WHERE name = 'ast_includes'").fetchone() is None:
return 0
resolver, _names = _resolver(conn)
written = 0
for rel, rowid in files.items():
conn.execute("DELETE FROM ast_includes WHERE src_id = ?", (rowid,))
payload = _harvest_one_file(
rel, repo_root / rel, includes_harvester(), {}, cache, HarvestTally()
)
written += conn.executemany(_INSERT, _rows(resolver, rowid, rel, payload)).rowcount
return written
Activity · 10 steps
Requirements
resolve
int | None clew.ast_includes.IncludeResolver.resolve(self, str spec, bool quoted, str includer)
Resolve one spec from one including file.
Source · 10 lines
def resolve(self, spec: str, quoted: bool, includer: str) -> int | None:
"""@brief Resolve a spec. @return Rowid or None."""
if quoted:
beside = posixpath.normpath(posixpath.join(posixpath.dirname(includer), spec))
if beside in self.by_name:
return self.by_name[beside]
candidates = self.by_suffix.get(posixpath.normpath(spec), [])
return candidates[0] if len(candidates) == 1 else None