llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.77k stars 11.9k forks source link

[clang-tidy] -line-filter only works with windows path separators on Windows #112038

Open meastp opened 2 weeks ago

meastp commented 2 weeks ago

Hi,

I'm on Windows and trying to use the -line-filter-argument to clang-tidy. This only worked on paths without folders (myfile.cpp). To ensure unique file names in the filter, I wanted to use paths with folders (instead of myfile.cpp use src/subfolder/myfile.cpp. I saw in the source that endswith is used on the file name (which I assume is a relative or absolute file path).

https://github.com/llvm/llvm-project/blob/58d97034c9c149d175c66440d31f46e9dfd4b760/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp#L468

I got it to work by using windows path separators (that had to be escaped), i.e. src\\subfolder\\myfile.cpp instead of src/subfolder/myfile.cpp. (even when using MINGW64/Git bash that is included in Git for Windows, this solution is neccessary)

-line-filter='[{"name":"subfolder\\myfile.cpp","lines":[[10,35]]}]'

This is a problem because I want to use clang-tidy-diff.py. It does work by modifying clang-tidy-diff.py

from lines_by_file.setdefault(filename, []).append([start_line, end_line]) to lines_by_file.setdefault(filename.replace("/", "\\"), []).append([start_line, end_line])

but it doesn't feel right that clang-tidy can't handle this internally.

The example usage of clang-tidy-diff.py is with git diff, which outputs unix path separators /.

llvmbot commented 2 weeks ago

@llvm/issue-subscribers-clang-tidy

Author: Mats Taraldsvik (meastp)

Hi, I'm on Windows and trying to use the `-line-filter`-argument to clang-tidy. This only worked on paths without folders (`myfile.cpp`). To ensure unique file names in the filter, I wanted to use paths with folders (instead of `myfile.cpp` use `src/subfolder/myfile.cpp`. I saw in the source that `endswith` is used on the file name (which I assume is a relative or absolute file path). https://github.com/llvm/llvm-project/blob/58d97034c9c149d175c66440d31f46e9dfd4b760/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp#L468 I got it to work by using windows path separators (that had to be escaped), i.e. `src\\subfolder\\myfile.cpp` instead of `src/subfolder/myfile.cpp`. (even when using MINGW64/Git bash that is included in Git for Windows, this solution is neccessary) `-line-filter='[{"name":"subfolder\\myfile.cpp","lines":[[10,35]]}]'` This is a problem because I want to use `clang-tidy-diff.py`. It does work by modifying `clang-tidy-diff.py` from `lines_by_file.setdefault(filename, []).append([start_line, end_line])` to `lines_by_file.setdefault(filename.replace("/", "\\"), []).append([start_line, end_line])` but it doesn't feel right that clang-tidy can't handle this internally. The example usage of `clang-tidy-diff.py` is with git diff, which outputs unix path separators `/`.