[clang-tidy] Fix llvm-header-guard so that it works with Windows paths

Fixes pr40372 (https://bugs.llvm.org/show_bug.cgi?id=40372).

The llvm-header-guard check does not take into account that the path
separator on Windows is `\`, not `/`.

This means that instead of suggesting a header guard in the form of:
LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FOO_H

it incorrectly suggests:
C:\LLVM_PROJECT\CLANG_TOOLS_EXTRA\CLANG_TIDY\FOO_H

Differential Revision: https://reviews.llvm.org/D113450
This commit is contained in:
Salman Javed 2021-11-10 18:34:41 +13:00
parent 577c1eecf8
commit b4f6f1c936
2 changed files with 45 additions and 3 deletions

View file

@ -8,6 +8,7 @@
#include "HeaderGuardCheck.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/Path.h"
namespace clang {
namespace tidy {
@ -21,6 +22,10 @@ std::string LLVMHeaderGuardCheck::getHeaderGuard(StringRef Filename,
StringRef OldGuard) {
std::string Guard = tooling::getAbsolutePath(Filename);
// When running under Windows, need to convert the path separators from
// `\` to `/`.
Guard = llvm::sys::path::convert_to_slash(Guard);
// Sanitize the path. There are some rules for compatibility with the historic
// style in include/llvm and include/clang which we want to preserve.

View file

@ -9,8 +9,6 @@ namespace clang {
namespace tidy {
namespace test {
// FIXME: It seems this might be incompatible to dos path. Investigating.
#if !defined(_WIN32)
static std::string runHeaderGuardCheck(StringRef Code, const Twine &Filename,
Optional<StringRef> ExpectedWarning) {
std::vector<ClangTidyError> Errors;
@ -220,8 +218,47 @@ TEST(LLVMHeaderGuardCheckTest, FixHeaderGuards) {
runHeaderGuardCheck(
"", "/llvm-project/clang-tools-extra/clangd/foo.h",
StringRef("header is missing header guard")));
}
#ifdef WIN32
// Check interaction with Windows-style path separators (\).
EXPECT_EQ(
"#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck("", "llvm-project\\clang-tools-extra\\clangd\\foo.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck(
"", "C:\\llvm-project\\clang-tools-extra\\clangd\\foo.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck(
"",
"\\\\SMBShare\\llvm-project\\clang-tools-extra\\clangd\\foo.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck(
"", "\\\\?\\C:\\llvm-project\\clang-tools-extra\\clangd\\foo.h",
StringRef("header is missing header guard")));
#endif
}
} // namespace test
} // namespace tidy