AST-Based C++ Checks

The text-based style checks (header guards, keyword order, license headers) cannot express anything that depends on meaning — the type of a parameter, whether a name resolves to std::, what a macro expands to. For that, cpp_checks can optionally parse each file with libclang and run a set of semantic checks over the resulting AST.

Requirements & enabling

AST checks need the optional ast extra (bundles libclang):

pip install devops[ast]

They are on by default once installed. Toggle and tune them from the [cpp] section:

Key

Purpose

ast_checks

Master on/off switch (default true).

ast_check_compile_commands_db

Directory containing compile_commands.json, for accurate per-file compile flags. Strongly recommended for CMake projects.

ast_check_compile_args

Fallback compiler flags (e.g. -std=c++23) used when no compile commands database is set, or a file isn’t listed in it.

ast_check_enabled_ids / ast_check_disabled_ids

Allowlist / denylist of check ids, by the Check id column below.

See Configuration File for the full key reference, defaults, and a worked TOML example.

How checks run

All enabled checks share a single preorder walk over each file’s AST (see devops.cpp.ast.engine) — adding another check never costs another parse. Each one implements devops.cpp.ast.base.Check and reports zero or more Diagnostics, formatted like cppcheck output:

src/box.cpp:42:18: style: parameter of type 'molsys::SimulationBox' named 'box' should be named 'simulationBox' [paramNameForType]

The trailing [check-id] is what you put in ast_check_enabled_ids / ast_check_disabled_ids to select or silence that check.

Available checks

Check id

Flags

Configuration

memberLeadingUnderscore

A private or protected member variable not starting with _.

None

memberFunctionLeadingUnderscore

A private or protected member function not starting with _.

None

noPublicLeadingUnderscore

A public member variable/function starting with _.

None

classMemberOrder

A member variable/function declared before a section that must precede it (public/protected/private members, then public/protected/private member functions).

Optional (excluded_macros)

paramNameForType

A parameter of a configured type not using its canonical name.

Required (type_to_name)

macroReplacement

Use of a banned macro that has a required replacement.

Optional (ships with a default mapping)

noGlobalUsing

using namespace X; / using X::Y; at global or namespace scope.

Optional (name filters)

noGlobalUsingEnum

using enum X; at global or namespace scope.

Optional (name filters)

noThrowParen

throw(...) wrapping the whole thrown expression.

None

noDeleteInGtestTeardown

delete/delete[] inside a GTest TearDown/TearDownTestSuite/ TearDownTestCase function.

None

noNewInGtestSetup

new inside a GTest SetUp/SetUpTestSuite/SetUpTestCase function.

None

noClangFormatToggle

// clang-format off/on comments (line or block form).

None

noConstValueParam

A named by-value parameter declared with top-level const (e.g. const int x, int *const p).

None

noFinalKeyword

final on a class/struct declaration or a virtual method.

None

enforceTypenameOverClass

A template type parameter introduced with class instead of typename.

None

enforceSystemHeaderLibs

A quoted #include "lib/..." for a configured library that must be included as a system header.

Required (libs)

includeIwyuPragmaOnly

A trailing comment on an #include line that isn’t an IWYU pragma, an IWYU pragma keyword outside an allowlist, or a required pragma missing its reason.

Optional (allowed_pragmas, pragma_required_reasons)

noForbiddenInclude

An #include of a configured library that must never be included (e.g. mp-units and all its subfiles).

Required (libs)

enforceCppOnlyInclude

A configured header #included from another header instead of directly from a .cpp file.

Required (libs)

passByType

A parameter of a configured type not passed by (const) reference or by value as required.

Required (types)

noInClassMemberDefinition

A member function whose body is written inside the class definition.

None

maxAnonymousNamespaceSize

An anonymous namespace defining a class/struct or holding more than max_declarations top-level declarations.

Optional (max_declarations)

preferMstdCopyMoveMacros

A hand-written = delete copy/move constructor or assignment instead of MSTD_NO_COPY/MSTD_NO_MOVE.

Optional (copy_macro, move_macro)

noMaybeUnusedParam

[[maybe_unused]] on a function, method or lambda parameter.

None

noVoidPointerParam

A void * parameter of a public or protected function.

None