"""Base interface for libclang AST-based C++ checks.
Every check is a small class implementing `Check`. A single shared AST
walk (see `devops.cpp.ast.engine.run_ast_checks`) dispatches each cursor
to every registered check's `visit()`, so adding a new check never costs
another parse/walk pass over the file.
"""
from __future__ import annotations
import typing
from dataclasses import dataclass
if typing.TYPE_CHECKING:
import clang.cindex as clang
[docs]
@dataclass(frozen=True)
class Diagnostic:
"""A single issue found by an AST check at a specific source location."""
file: str
line: int
column: int
message: str
check_id: str
severity: str = "style"
[docs]
class Check:
"""Base class every custom AST check must subclass.
Not an ABC: both `visit` and `finalize` have usable no-op defaults, so
a check only needs to override whichever one it actually uses.
"""
#: unique id, used as the cppcheck-style [id] suffix
id: str = "unnamed-check"
#: path patterns (see `devops.cpp.path_patterns.matches_path`) of files
#: this check never runs on; set from the ``exclude_paths`` key of the
#: check's ``cpp.ast_check_config.<id>`` table by `configure_checks`
exclude_paths: tuple[str, ...] = ()
[docs]
def visit(self, cursor: clang.Cursor, filename: str) -> list[Diagnostic]:
"""Inspect a single AST node during the shared preorder walk.
Called once per AST node in `filename`. Override to inspect
`cursor` and return zero or more Diagnostics. The default
implementation does nothing, which is useful for checks that only
need `finalize`.
Parameters
----------
cursor: clang.Cursor
The AST node currently being visited.
filename: str
Path of the file being checked.
Returns
-------
list[Diagnostic]
Diagnostics raised by this node, if any.
"""
del cursor, filename
return []
[docs]
def global_finalize(self) -> bool:
"""Call once after ALL files have been checked.
Override for post-run validation, such as warning about configured
type names that were never encountered in any file's AST. The default
implementation does nothing and returns True (no errors).
Returns
-------
bool
True if no errors were found, False if the check should be
considered failed (e.g. a configured type was never seen and
``unseen_type_is_error`` is enabled).
"""
return True
[docs]
def collect_state(self) -> dict:
"""Return this check's exportable cross-file state, if any.
Used to merge a check's accumulated state (e.g.
`EnforceParamNameForType`'s seen-type-names set) back into a single
instance after files were checked across parallel worker processes,
so `global_finalize()` sees the full picture. The default
implementation returns an empty dict (nothing to merge).
Returns
-------
dict
Picklable snapshot of this check's cross-file state.
"""
return {}
[docs]
def merge_state(self, state: dict) -> None:
"""Merge state previously returned by `collect_state` into this check.
Override alongside `collect_state` for checks with cross-file
accumulated state. The default implementation ignores `state`.
Parameters
----------
state: dict
A snapshot as returned by another instance's `collect_state`.
"""
del state
[docs]
def finalize(self, filename: str) -> list[Diagnostic]:
"""Run once after the whole file's AST has been walked.
Override for checks needing whole-translation-unit state (counts,
cross-references) rather than a single-node check.
Parameters
----------
filename: str
Path of the file being checked.
Returns
-------
list[Diagnostic]
Diagnostics raised at end-of-file, if any.
"""
del filename
return []