devops.cpp.ast.checks.include_iwyu_pragma_only

Flag a #include line followed by a comment that isn’t an allowed IWYU pragma.

A trailing comment on a #include line (same line, after the directive) is only accepted when it is an include-what-you-use pragma, i.e. matches // IWYU pragma: <keyword> ... (or the /* ... */ form). Any other trailing comment — a plain note, a disabled-linter comment, etc. — is reported. An #include with no trailing comment at all is always allowed.

To disable this check for a project set:

[cpp]
ast_check_disabled_ids = ["includeIwyuPragmaOnly"]

By default every IWYU pragma keyword (export, keep, no_include, private, associated, friend, …) is accepted. Restrict which keywords are allowed via the per-check table; an empty/unset list keeps the default of allowing all of them:

[cpp.ast_check_config.includeIwyuPragmaOnly]
allowed_pragmas = ["export", "keep"]

Some pragma keywords can additionally be required to carry a specific trailing reason, e.g. only allowing keep when it documents why:

[cpp.ast_check_config.includeIwyuPragmaOnly]
allowed_pragmas = ["export", "keep"]
pragma_required_reasons = { keep = "-- for template specialization" }

With the above, // IWYU pragma: keep -- for template specialization is allowed, but a bare // IWYU pragma: keep (or one with a different trailing reason) is flagged.

A pragma may accept several alternative reasons by giving a list; the trailing reason must then match any one of them:

pragma_required_reasons = { keep = ["-- for ADL", "-- for templates"] }

Classes

IncludeIwyuPragmaOnly()

Flag non-IWYU-pragma trailing comments on #include directives.

class devops.cpp.ast.checks.include_iwyu_pragma_only.IncludeIwyuPragmaOnly[source]

Bases: Check

Flag non-IWYU-pragma trailing comments on #include directives.

id: str = 'includeIwyuPragmaOnly'

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

configure(config)[source]

Load the allowed IWYU pragma keywords from the check’s TOML config block.

Parameters:

config (dict) – Expected shape: {"allowed_pragmas": ["export", "keep"], "pragma_required_reasons": {"keep": "-- for template specialization"}}. Each reason value is either a single string or a non-empty list of strings (any one of which is accepted). When allowed_pragmas is absent or empty, every IWYU pragma keyword is allowed. When pragma_required_reasons is absent or empty, no pragma requires a trailing reason.

Raises:

ConfigError – If allowed_pragmas is present but is not a list of strings, or if pragma_required_reasons is present but is not a dict of strings to strings or non-empty lists of strings.

Return type:

None

visit(cursor, filename)[source]

Flag the cursor’s trailing comment if it is a disallowed one.

Parameters:
  • cursor (clang.Cursor) – The AST node currently being visited.

  • filename (str) – Path of the file being checked.

Returns:

list[Diagnostic] – A single diagnostic if cursor is a #include directive followed by a disallowed trailing comment, otherwise an empty list.

Return type:

list[Diagnostic]