devops.cpp.ast.base

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.

Classes

Check()

Base class every custom AST check must subclass.

Diagnostic(file, line, column, message, check_id)

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

class devops.cpp.ast.base.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

class devops.cpp.ast.base.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]