pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

Add Include What You Use (IWYU) directives to headers

Open yeganer opened this issue 7 years ago • 3 comments

Issue description

Include what you use and similiar tools (clangd) provide header suggestions for symbols used in a translation unit. For pybind11 most of the implementation is spread around multiple headers, however most of the time, one wants to include the "pybind11/pybind11.h" header.

There are directives to tell these tools, that the current header is private and to suggest a different header to include. This looks like this:

// IWYU pragma: private, include "pybind11.h"

For the documentation of the IWYU pragmas see: https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUPragmas.md#iwyu-pragma-private

It would be great if we could add these directives to the pybind11 source code, what do you think?

yeganer avatar Jun 17 '19 15:06 yeganer

It's the first time I've heard of this, and I wouldn't consider it unless this is extremely widely used.

wjakob avatar Jun 19 '19 08:06 wjakob

5 years later, IWYU is now integrated in clang-tidy.

Qt starts to support it.

The biggest problem is that IWYU doesn't know which headers are internal and which headers are public.

To force to include pybind11.h instead of all other includes, the idea is to enclose pybind11 headers by // IWYU pragma: begin_exports and // IWYU pragma: end_exports in pybind11.h. See IWYU pragma: begin_exports/end_exports.

I think that this issue should be opened. A PR can easily be send.

bansan85 avatar Apr 29 '24 09:04 bansan85

I also think it would make sense to add these directives, but in the meantime, for those who use clang-tidy, the following entry in the .clang-tidy configuration disables the misc-include-cleaner warnings from pybind11:

CheckOptions:
  - { key: misc-include-cleaner.IgnoreHeaders, value: pybind11/detail/.* }

tobiasleibner avatar Feb 25 '25 17:02 tobiasleibner