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:
RuleRun 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:
- 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:
- 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:
- 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:
- 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:
- class devops.cpp.ast.ASTWorkerSpec(compile_args, compile_commands_db, enabled_check_ids, disabled_check_ids, check_config)[source]
Bases:
objectPicklable 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:
- class devops.cpp.ast.Check[source]
Bases:
objectBase 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.
- exclude_paths: tuple[str, ...] = ()
path patterns (see devops.cpp.path_patterns.matches_path) of files this check never runs on; set from the
exclude_pathskey of the check’scpp.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:
- 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_erroris enabled).- Return type:
- 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_pathskey 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:
- 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:
- class devops.cpp.ast.Diagnostic(file, line, column, message, check_id, severity='style')[source]
Bases:
objectA single issue found by an AST check at a specific source location.
Modules
Base interface for libclang AST-based C++ checks. |
|
Individual AST check implementations. |
|
Shared single-pass AST-walk engine for libclang-based C++ checks. |
|
Registry of active AST checks. |
|
Rule adapter exposing the AST-check engine through devops's Rule interface. |