Skip to content

Instantly share code, notes, and snippets.

@nitrix
Last active March 29, 2025 19:11
Show Gist options
  • Save nitrix/eb5b20325ebcc01fc4ac07be4d112a72 to your computer and use it in GitHub Desktop.
Save nitrix/eb5b20325ebcc01fc4ac07be4d112a72 to your computer and use it in GitHub Desktop.
IWYU (Include What You Use)

Which pragma should I use?

  • Use IWYU pragma: keep to force IWYU to keep any #include directive that would be discarded under its normal policies.
  • Use IWYU pragma: always_keep to force IWYU to keep a header in all includers, whether they contribute any used symbols or not.
  • Use IWYU pragma: export to tell IWYU that one header serves as the provider for all symbols in another, included header (e.g. facade headers). Use IWYU pragma: begin_exports/end_exports for a whole group of included headers.
  • Use IWYU pragma: no_include to tell IWYU that the file in which the pragma is defined should never #include a specific header (the header may already be included via some other #include.)
  • Use IWYU pragma: no_forward_declare to tell IWYU that the file in which the pragma is defined should never forward-declare a specific symbol (a forward declaration may already be available via some other #include.)
  • Use IWYU pragma: private to tell IWYU that the header in which the pragma is defined is private, and should not be included directly.
  • Use IWYU pragma: private, include "public.h" to tell IWYU that the header in which the pragma is defined is private, and public.h should always be included instead.
  • Use IWYU pragma: friend ".*favorites.*" to override IWYU pragma: private selectively, so that a set of files identified by a regex can include the file even if it's private.

The pragmas come in three different classes;

  1. Ones that apply to a single #include directive (keep, export)
  2. Ones that apply to a file being included (private, friend, always_keep)
  3. Ones that apply to a file including other headers (no_include, no_forward_declare)

Some files are both included and include others, so it can make sense to mix and match.

@nitrix
Copy link
Author

nitrix commented Mar 29, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment