winapiclang-tidyiwyu

Using `misc-include-cleaner` with <windows.h>


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?


Solution

  • 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.