devops.cpp.ast

AST-based (libclang) C++ checks for devops.

Add a new check by subclassing Check in devops.cpp.ast.checks, then registering an instance in devops.cpp.ast.registry.ALL_CHECKS. It will automatically run in the same shared AST walk as every other check, with no changes needed to engine.py or rule.py.

Requires the optional libclang dependency (pip install devops[ast]).

class devops.cpp.ast.ASTChecksRule(compile_args=None, compile_commands_db=None, enabled_check_ids=None, disabled_check_ids=None, check_config=None)[source]

Bases: Rule

Run every enabled libclang AST-based check in a single pass.

Slots into build_cpp_rules as one ordinary FILE rule: each file is parsed once by libclang, and every enabled check in devops.cpp.ast.registry.ALL_CHECKS is dispatched against that single AST walk. All diagnostics found are combined into one ResultType.

Parameters:
  • compile_args (list[str] | None)

  • compile_commands_db (str | None)

  • enabled_check_ids (list[str] | None)

  • disabled_check_ids (list[str] | None)

  • check_config (dict[str, dict] | None)

check_file(path, content)[source]

Run all enabled AST checks against a single file’s content.

Public equivalent of _run, usable directly (without constructing a FileRuleInput) by callers like the process-pool worker in run_ast_checks_in_worker.

Parameters:
  • path (Path) – The file being checked.

  • content (str) – The file’s full content.

Returns:

ResultType – Same as _run.

Return type:

ResultType

finalize_run()[source]

Call global_finalize() on every active check after all files are done.

Returns:

bool – True if all checks passed, False if any check reported an error.

Return type:

bool

worker_spec()[source]

Return a picklable spec that can rebuild an equivalent instance.

Used by devops.cpp.checks to run this rule’s checks across a ProcessPoolExecutor: the spec (not this instance, which holds unpicklable libclang objects) is sent to each worker, which calls ASTChecksRule.from_spec to build its own local instance.

Returns:

ASTWorkerSpec – The constructor args this instance was built with.

Return type:

ASTWorkerSpec

classmethod from_spec(spec)[source]

Build a fresh ASTChecksRule from a previously captured spec.

Parameters:

spec (ASTWorkerSpec) – As returned by another instance’s worker_spec().

Returns:

ASTChecksRule – A new, independently-constructed instance with the same configuration (fresh Check instances, its own clang.CompilationDatabase).

Return type:

ASTChecksRule

collect_state()[source]

Return every check’s exportable cross-file state, keyed by check id.

Returns:

dict[str, dict] – {check.id: check.collect_state()} for each active check.

Return type:

dict[str, dict]

merge_state(states)[source]

Merge a collect_state() snapshot from another instance.

Parameters:

states (dict[str, dict]) – A snapshot as returned by another instance’s collect_state.

Return type:

None

class devops.cpp.ast.ASTWorkerSpec(compile_args, compile_commands_db, enabled_check_ids, disabled_check_ids, check_config)[source]

Bases: object

Picklable snapshot of ASTChecksRule’s constructor args.

Sent to a ProcessPoolExecutor worker so it can build its own ASTChecksRule locally (fresh Check instances, its own clang.CompilationDatabase), since the rule instance itself holds unpicklable libclang/ctypes objects.

Parameters:
  • compile_args (list[str] | None)

  • compile_commands_db (str | None)

  • enabled_check_ids (list[str] | None)

  • disabled_check_ids (list[str] | None)

  • check_config (dict[str, dict] | None)

compile_args: list[str] | None
compile_commands_db: str | None
enabled_check_ids: list[str] | None
disabled_check_ids: list[str] | None
check_config: dict[str, dict] | None
class devops.cpp.ast.Check[source]

Bases: object

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.

id: str = 'unnamed-check'

unique id, used as the cppcheck-style [id] suffix

exclude_paths: tuple[str, ...] = ()

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

visit(cursor, filename)[source]

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.

Return type:

list[Diagnostic]

global_finalize()[source]

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 type:

bool

configure(config)[source]

Apply check-specific configuration from the user’s TOML block.

Called once during setup with the sub-table keyed by this check’s .id in cpp.ast_check_config. Override to read your check’s own settings. The default implementation ignores all config.

The generic exclude_paths key is understood by every check and is applied before this method is called (see configure_checks), so overrides need not handle it — but must not reject it as unknown.

Parameters:

config (dict) – The raw configuration dict for this check, e.g. {"type_to_name": {"SimulationBox": "simulationBox"}}.

Return type:

None

collect_state()[source]

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 type:

dict

merge_state(state)[source]

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.

Return type:

None

finalize(filename)[source]

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.

Return type:

list[Diagnostic]

class devops.cpp.ast.Diagnostic(file, line, column, message, check_id, severity='style')[source]

Bases: object

A single issue found by an AST check at a specific source location.

Parameters:
file: str
line: int
column: int
message: str
check_id: str
severity: str = 'style'
format()[source]

Format the diagnostic in a cppcheck-style single line.

Returns:

str – The formatted diagnostic, e.g. foo.cpp:12:5: style: <message> [<check_id>].

Return type:

str

Modules

base

Base interface for libclang AST-based C++ checks.

checks

Individual AST check implementations.

engine

Shared single-pass AST-walk engine for libclang-based C++ checks.

registry

Registry of active AST checks.

rule

Rule adapter exposing the AST-check engine through devops's Rule interface.