1

I would like to use the misc-include-cleaner check from clang-tidy in my codebase, but unfortunately, the <windows.h> header used throughout WinAPI conflicts with IWYU philosophy.

Technically, I can include only the necessary WinAPI headers explicitly, but the order of includes is important, and managing it seems tedious.

Is there an elegant solution to this? It feels wrong to disable this check for the entire project just because of this. I'm aware of the IgnoreHeaders option, but I can't figure out if it solves the issue or how to apply it properly. For compiler warnings, I use /external:anglebrackets along with /external:W0, which works great. Is there something similar I can use in this situation?

5
  • You cannot reasonably avoid including <Windows.h>. You can, however, limit what this header includes. See Using the Windows Headers for instructions. Commented Sep 27, 2024 at 7:18
  • 1
    Yeah, I understand that, but don't see how's it's relevant to the issue I'm having with clang-tidy. No matter what <windows.h> includes, it's still all going to be transitive includes from the perspective of misc-include-cleaner check. Commented Sep 27, 2024 at 8:19
  • WIN32_LEAN_AND_MEAN and the individual NOxyz preprocessor macros are the only supported way to control the transitive includes of <Windows.h>. If the documentation instructs you to #include <Windows.h>, you are best advised to do just that. The alternative would be to try to untangle the mess of interdependencies that evolved over the course of four decades. Assuming limited developer resources, those are likely better spent elsewhere. Commented Sep 27, 2024 at 9:11
  • Add what you want in a namespace Commented Sep 27, 2024 at 13:25
  • I know this is old, would you mind giving more information on how IgnoreHeaders did or didn't work - I'm looking at something similar (not windows) but can't get IgnoreHeaders to work. Hence finding this issue. Commented Nov 20 at 1:24

1 Answer 1

1

I think I've found a decent solution.

Clang-tidy supports placing a config file in the same location as the source file. Normally, this would override the parent folder's config, but enabling InheritParentConfig allows it to inherit the settings instead:

Checks: '
      -misc-include-cleaner
      '
InheritParentConfig: true 

This makes it possible to disable certain checks only in selected folders while preserving global checks. Obviously, this will affect more than just the <windows.h> header, but I think it's a reasonable solution given how easy it is to maintain. It works best if all file that include <windows.h> are in the same folder and separate from the rest of the project but I think it only reinforces good design and is something you should be doing anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.