devops.cpp.ast.checks.enforce_class_member_order
Enforce a fixed section order for member variables and member functions.
Within a single class/struct/union body (including templates), the direct members must appear grouped, in exactly this order:
public member variables
protected member variables
private member variables
public member functions
protected member functions
private member functions
Anything not covered by that list — nested types, using declarations,
enums, friend declarations, static_assert, the access specifiers
themselves — is ignored for ordering purposes: it neither has to fit
anywhere in particular nor resets the sequence.
Only declarations that lexically appear inside the class body are
considered, so an out-of-line member-function definition
(void C::f() { ... }) never affects its class’s ordering — only the
in-class declaration does.
Declarations synthesized by a configured macro (matched by the macro’s own
name, at its invocation line) are excluded entirely from ordering: they are
neither flagged themselves nor counted when checking what came before or
after them — exactly like the nested-type/using-declaration exclusions
above. This is for macros such as Qt’s Q_OBJECT that expand to
boilerplate members (and possibly their own access-specifier changes) whose
position isn’t the author’s choice:
[cpp.ast_check_config.classMemberOrder]
excluded_macros = ["Q_OBJECT", "MY_DECLARE_PROPERTY"]
To disable this check entirely for a project set:
[cpp]
ast_check_disabled_ids = ["classMemberOrder"]
Classes
Flag member variables/functions declared out of the required section order. |
- class devops.cpp.ast.checks.enforce_class_member_order.EnforceClassMemberOrder[source]
Bases:
CheckFlag member variables/functions declared out of the required section order.
Reporting is deferred to finalize(): a macro invoked inside a class body (e.g.
Q_OBJECT) shows up in libclang’s preprocessing record as a cursor local to the translation unit rather than as a lexical child of the class, and may be visited before or after the class itself in a single preorder walk. Whether a given member’s line is covered by an excluded macro invocation can therefore only be known once the whole file has been walked.- configure(config)[source]
Load the
excluded_macroslist from the check’s TOML config block.- Parameters:
config (dict) – Expected shape:
{"excluded_macros": ["Q_OBJECT"]}.- Raises:
ConfigError – If
excluded_macrosis present but is not a list of strings.- Return type:
None
- visit(cursor, filename)[source]
Record excluded-macro lines and classify one record’s direct children.
- Parameters:
cursor (clang.Cursor) – The AST node currently being visited.
filename (str) – Path of the file being checked.
- Returns:
list[Diagnostic] – Always empty — diagnostics are emitted from finalize() once the whole file (including its macro instantiations) is known.
- Return type:
- finalize(filename)[source]
Evaluate ordering for every record buffered for filename.
- Parameters:
filename (str) – Path of the file being checked.
- Returns:
list[Diagnostic] – One diagnostic per member declared before a section that must precede it, skipping members whose line is covered by an excluded macro invocation.
- Return type: