devops.cpp.ast.checks.no_forbidden_include

Flag #include directives of libraries that must never be included.

Some libraries (e.g. mp-units) must not be pulled into a code base at all. This check flags every #include whose path matches one of the configured patterns, whether written with quotes or angle brackets, in any file.

Patterns are matched against the #include path exactly as written in the source. A pattern matches if it equals the path (with fnmatch rules, where * also matches /) or if it names a directory prefix, so a bare library name excludes the library and all of its subfiles:

[cpp.ast_check_config.noForbiddenInclude]
libs = ["mp-units", "boost/hana.hpp", "*_deprecated.hpp"]

mp-units above matches <mp-units/systems/si.h> as well as <mp-units/core.h>. Files that are allowed to include a forbidden library anyway can be exempted via exclude_paths (a trailing / matches a directory prefix; anything else is an fnmatch glob):

[cpp.ast_check_config.noForbiddenInclude]
libs = ["mp-units"]
exclude_paths = ["src/units/"]

An empty/unset libs list disables the check entirely.

Functions

matches_forbidden_include(include_path, patterns)

Return whether include_path matches any forbidden-library pattern.

Classes

NoForbiddenInclude()

Flag #includes of configured libraries that must never be included.

devops.cpp.ast.checks.no_forbidden_include.matches_forbidden_include(include_path, patterns)[source]

Return whether include_path matches any forbidden-library pattern.

Parameters:
  • include_path (str) – The path of the #include as written in the source.

  • patterns (tuple[str, ...]) – Library names, directory prefixes or fnmatch globs.

Returns:

bool – True if a pattern equals the path, matches it as a glob, or names a directory containing it.

Return type:

bool

class devops.cpp.ast.checks.no_forbidden_include.NoForbiddenInclude[source]

Bases: Check

Flag #includes of configured libraries that must never be included.

id: str = 'noForbiddenInclude'

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

configure(config)[source]

Load the forbidden library patterns.

Parameters:

config (dict) – Expected shape: {"libs": ["mp-units"], "exclude_paths": ["src/units/"]}. When libs is absent or empty, this check never flags anything.

Raises:

ConfigError – If libs or exclude_paths is present but is not a list of strings, or if the table contains any other (e.g. misspelled) key.

Return type:

None

visit(cursor, filename)[source]

Flag the cursor if it’s an #include of a forbidden library.

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 an #include directive of a forbidden library, otherwise an empty list.

Return type:

list[Diagnostic]