[clang-tools-extra] b4f6f1c - [clang-tidy] Fix llvm-header-guard so that it works with Windows paths
Salman Javed via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 9 21:44:38 PST 2021
Author: Salman Javed
Date: 2021-11-10T18:35:57+13:00
New Revision: b4f6f1c9369ec4bb1c10852283a8c7e8c39e1a8d
URL: https://github.com/llvm/llvm-project/commit/b4f6f1c9369ec4bb1c10852283a8c7e8c39e1a8d
DIFF: https://github.com/llvm/llvm-project/commit/b4f6f1c9369ec4bb1c10852283a8c7e8c39e1a8d.diff
LOG: [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
Added:
Modified:
clang-tools-extra/clang-tidy/llvm/HeaderGuardCheck.cpp
clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/llvm/HeaderGuardCheck.cpp b/clang-tools-extra/clang-tidy/llvm/HeaderGuardCheck.cpp
index 8f8bd7a77ceb3..53c1e3c6b833f 100644
--- a/clang-tools-extra/clang-tidy/llvm/HeaderGuardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/llvm/HeaderGuardCheck.cpp
@@ -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.
diff --git a/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp b/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
index b86f8e63fb899..6b312413b870d 100644
--- a/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
@@ -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
More information about the cfe-commits
mailing list