enforceCppOnlyInclude ========================= Flags an ``#include`` of a configured header found inside another header file. Useful for headers that are implementation details of a single translation unit (e.g. a ``*_impl.hpp`` meant to be included at the bottom of exactly one ``.cpp``) and must never leak into other headers. .. code-block:: cpp // other.hpp // Bad — flagged: 'detail/foo_impl.hpp' is configured and this is a header. #include "detail/foo_impl.hpp" // foo.cpp // Good — including it directly from a .cpp file is always allowed. #include "detail/foo_impl.hpp" Header matching ------------------ Each configured pattern is matched with `fnmatch` rules against the ``#include`` path exactly as written in the source — **never** against just its basename. A pattern of ``"units.hpp"`` matches a bare ``#include "units.hpp"``, but not ``#include ``: that's a distinct header from a different library that merely happens to share the name, and shouldn't be swept in just because the filenames match. `fnmatch`'s ``*`` matches across ``/`` too, so a glob still matches regardless of directory when that's what you want — e.g. ``"*_impl.hpp"`` matches ``detail/foo_impl.hpp``, and ``"*/units.hpp"`` matches any ``units.hpp`` one directory deep. Configuration -------------- .. list-table:: :header-rows: 1 :widths: 20 15 15 50 * - Key - Type - Default - Description * - ``libs`` - list of strings - ``[]`` - Patterns identifying headers that may only be included from ``.cpp`` files. An empty/unset list means this check never flags anything. * - ``exclude_paths`` - list of strings - ``[]`` - Path patterns (trailing ``/`` = directory prefix, otherwise an `fnmatch` glob) for *including* files that are exempt — e.g. the header that itself wraps/defines a cpp-only header and must therefore include it unconditionally. * - ``extensions`` - list of strings - ``[]`` - If set, only *including* files with one of these extensions (e.g. ``[".tpp"]``) are checked, instead of all headers. Empty/unset keeps the default of checking every header. .. code-block:: toml [cpp.ast_check_config.enforceCppOnlyInclude] libs = ["units.hpp"] exclude_paths = ["include/config/units.hpp"] extensions = [".tpp"] # optional: only check .tpp files Like ``paramNameForType``, ``libs`` is required for the check to have any effect — it is a no-op with an empty list, so it isn't meaningful to enable without configuring it. Disabling ---------- .. code-block:: toml [cpp] ast_check_disabled_ids = ["enforceCppOnlyInclude"] Whole-project pre-check ------------------------- Unlike every other AST check, ``enforceCppOnlyInclude`` is also enforced by a second, independent pass: a plain-text scan over **every** header file in the entire repository (minus ``exclude_dirs``), run on every invocation of ``cpp_checks`` regardless of ``--base-ref``, ``check_only_staged_files``, the incremental state file, or even ``check_dirs``. This closes a gap the normal AST check can't: when a run is scoped to a git diff, an incremental cache, or a narrowed ``check_dirs``, a violation sitting in a header outside that scope would otherwise go unnoticed. The pre-check always walks the whole project instead, so it cannot be bypassed by narrowing what a given run checks — it shares the same ``libs`` configuration and is turned off the same way, via ``ast_check_disabled_ids = ["enforceCppOnlyInclude"]`` or an empty/unset ``libs`` list, but nothing else (not ``--base-ref``, not incremental caching, not ``check_dirs``) can silence it on its own. It runs *before* the normal check pipeline, and honors ``fail_fast`` (the default) the same way every other check does: on the first violation found, scanning stops and ``cpp_checks`` exits immediately without running the normal file/line/AST checks at all. With ``fail_fast = false`` (or ``--no-fail-fast``), it instead scans every header and reports every violation before the normal pipeline still runs afterwards.