Skip to content

Commit

Permalink
CODING_CONVENTIONS.md: Add IWYU policy
Browse files Browse the repository at this point in the history
This makes an implicit agreement that code should include the headers
is uses, no more and no less, explicit.

It also recommends using tooling (such as clangd) and adding IWUY
pragma comments to aid those tools in common cases they otherwise would
provide false positives.
  • Loading branch information
maribu committed Apr 14, 2024
1 parent 3cdc437 commit df40706
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CODING_CONVENTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@
#endif
```

### Include What You Use (IWYU)

`#include` directives that are not actually needed should be removed to reduce
clutter and improve compilation speed. Similar: Try to add the corresponding
`#include`s for all the functions, macros, types, etc. used and do not rely on
`bar.h` to implicitly include `foo.h`, unless this is documented behavior.

Tools such as [clang's Include Cleaner][clangd-include-cleaner] can help with
that. These tools may show false positives in cases where headers are *expected*
to be included indirectly: E.g. if `foo.h` is the public header that contains
common helpers and implementations, but a per platform `foo_arch.h` is included
from within `foo.h` for platform specific implementations. If in this scenario
only functions provided by `foo_arch.h` are included, the `#include` of `foo.h`
is considered as unused. To avoid this, one should add
`/* IWYU pragma: export */` after `#include "foo_arch.h"` in `foo.h`.

[clangd-include-cleaner]: https://clangd.llvm.org/design/include-cleaner

## Header Guards

All files are required to have header guards of the form
Expand Down

0 comments on commit df40706

Please sign in to comment.