What does HackerNews think of include-what-you-use?

A tool for use with clang to analyze #includes in C and C++ source files

Language: C++

> * Remove all unnecessary headers. Template expansion is slow, and preprocessing is even slower. Some of the standard includes (like `` and ``) are notorious for slowing individual translation units to a crawl. `#pragma once` for your own headers also helps with cpp-time performance.

> * Forward-declare as much as you can. Forward type declarations mean that the compiler doesn't need to process all of `Foo` when it sees `Foo&` or `Foo`.

I've found the include-what-you-use (IWYU) tool [1][2] can help immensely with automating this process, especially on large code-bases.

It uses LLVM/Clang to analyze a .cpp file / translation unit and produces the minimal subset of exactly which includes are necessary and what types can be forward declared.

[1] https://include-what-you-use.org/

[2] https://github.com/include-what-you-use/include-what-you-use

This looks really useful. I just submitted a pull request to make it easier to use in automated checks: https://github.com/include-what-you-use/include-what-you-use...

Fingers crossed.

If the issue is with including transitive dependencies that are in your own codebase, then you should annotate the public interface header to the implementation details with IWYU Pragmas [1] that export the implementation (for example [2]).

If this is in third-party libraries, you can use IWYU Mappings [3] to map the "private" headers (usually the transitive include) to the public interface. An example that I use for the PEGTL library [4].

[1]: https://github.com/include-what-you-use/include-what-you-use...

[2]: https://github.com/anand-bala/signal-temporal-logic/blob/800...

[3]: https://github.com/include-what-you-use/include-what-you-use...

[4]: https://github.com/anand-bala/signal-temporal-logic/blob/800...

I've found that using IWYU Pragmas [1] for codebases you own and IWYU Mappings [2] for third-party libraries __almost__ entirely eliminates weird IWYU suggestions (there are a few annoyingly stupid suggestions from the tool I just ignore).

I've also recently been making libraries I write compatible with users that run IWYU by annotating all public headers with IWYU pragma comments that export symbols/transitive includes correctly, etc.

[1]: https://github.com/include-what-you-use/include-what-you-use...

[2]: https://github.com/include-what-you-use/include-what-you-use...

Take note:

> CAVEAT

> This is alpha quality software -- at best (as of July 2018). It was originally written to work specifically in the Google source tree, and may make assumptions, or have gaps, that are immediately and embarrassingly evident in other types of code.

> While we work to get IWYU quality up, we will be stinting new features, and will prioritize reported bugs along with the many existing, known bugs. The best chance of getting a problem fixed is to submit a patch that fixes it (along with a test case that verifies the fix)!

https://github.com/include-what-you-use/include-what-you-use...

Further useful docs:

Why Include What You Use? https://github.com/include-what-you-use/include-what-you-use...

What Is A Use? https://github.com/include-what-you-use/include-what-you-use...

Why Include What You Use Is Difficult https://github.com/include-what-you-use/include-what-you-use...

iwyu is Google's project originally. It has worked for them for more than a decade on their ginormous monorepo.

Sometimes it gets some things wrong, so you have these escape hatches to control it: https://github.com/include-what-you-use/include-what-you-use...