Issue 237692
Summary [clang-tidy] misc-include-cleaner: IgnoreHeaders patterns with '/' never match on Windows, where header paths have backslashes
Labels clang-tidy
Assignees
Reporter YevheniiKotyrlo
    `misc-include-cleaner`'s `IgnoreHeaders` documents its patterns with forward slashes ("E.g., `foo/.*` disables insertion/removal for all headers under the directory `foo`"), but on Windows the check matches a physical header against `getFileEntry().tryGetRealPathName()`, which is a backslashed path there (`D:\...\foo\bar.h`). So any pattern that contains `/` never matches a header on Windows, and the same `.clang-tidy` that silences a header on Linux and macOS reports it on Windows.

clangd's own `Diagnostics.Includes.IgnoreHeader` doesn't have this problem: `isIgnored` in `clangd/IncludeCleaner.cpp` converts the path to POSIX slashes before matching. The tidy check's `shouldIgnore` in `clang-tidy/misc/IncludeCleanerCheck.cpp` doesn't.

## Steps to reproduce

Reproducer: https://gist.github.com/YevheniiKotyrlo/e0d0234d2eaa7b53cb403d4e38140846 (`.clang-tidy`, `main.cpp`, `vendored.h`).

```yaml
# .clang-tidy
Checks: '-*,misc-include-cleaner'
CheckOptions:
 misc-include-cleaner.IgnoreHeaders: '.*/vendored\.h'
```

```cpp
// main.cpp
#include "vendored.h"

int main() { return 0; }
```

On Windows, with clang-tidy from an MSVC build:

```console
> git clone https://gist.github.com/e0d0234d2eaa7b53cb403d4e38140846.git ignore-headers
> cd ignore-headers
> clang-tidy main.cpp --
1 warning generated.
D:\llvm-main-build\repro\ignore-headers\main.cpp:1:1: warning: included header vendored.h is not used directly [misc-include-cleaner]
    1 | #include "vendored.h"
      | ^~~~~~~~~~~~~~~~~~~~~
    2 |
```

Expected: no warning, since `vendored.h` matches `.*/vendored\.h` (it does on Linux and macOS).

Actual: the warning above. Writing the separator as `[/\\]` (`.*[/\\]vendored\.h`) makes the warning go away, which is what I use as a workaround now.

A build configured with `LLVM_WINDOWS_PREFER_FORWARD_SLASH=ON` (the default for MinGW builds such as llvm-mingw's nightly) gets forward-slashed real paths and doesn't show it; the official Windows release and any MSVC build do.

## Versions

- clang-tidy built from `main` at 2b54d70a764a61c2565f8a7f9890c9f71c60d9b3 with MSVC 14.51 (`-DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra"`, Release)
- the official 23.1.3 Windows release (`clang+llvm-23.1.3-x86_64-pc-windows-msvc`) behaves the same
- Windows 11 (10.0.26300)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to