[clang-tools-extra] 7cf97d6 - [clang-tidy] Make header-guard check a little looser on comment whitespace

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Thu May 5 08:42:47 PDT 2022


Author: Sam McCall
Date: 2022-05-05T17:42:35+02:00
New Revision: 7cf97d62f40990c2f86f75eb5a1d9cf2bd9de71e

URL: https://github.com/llvm/llvm-project/commit/7cf97d62f40990c2f86f75eb5a1d9cf2bd9de71e
DIFF: https://github.com/llvm/llvm-project/commit/7cf97d62f40990c2f86f75eb5a1d9cf2bd9de71e.diff

LOG: [clang-tidy] Make header-guard check a little looser on comment whitespace

Currently it rejects "//  FOO_BAR_H" as an endif comment due to the extra space.
A user complained that this is too picky, which seems fair enough.

Differential Revision: https://reviews.llvm.org/D124955

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
    clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
index 36bf64110a774..c5d5004fb4df1 100644
--- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
+++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
@@ -145,13 +145,13 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
         EndIfStr[FindEscapedNewline] == '\\')
       return false;
 
-    if (!Check->shouldSuggestEndifComment(FileName) &&
-        !(EndIfStr.startswith("//") ||
-          (EndIfStr.startswith("/*") && EndIfStr.endswith("*/"))))
-      return false;
+    bool IsLineComment =
+        EndIfStr.consume_front("//") ||
+        (EndIfStr.consume_front("/*") && EndIfStr.consume_back("*/"));
+    if (!IsLineComment)
+      return Check->shouldSuggestEndifComment(FileName);
 
-    return (EndIfStr != "// " + HeaderGuard.str()) &&
-           (EndIfStr != "/* " + HeaderGuard.str() + " */");
+    return EndIfStr.trim() != HeaderGuard;
   }
 
   /// Look for header guards that don't match the preferred style. Emit

diff  --git a/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp b/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
index 46a1600e2bd6d..ae9018faf6ae2 100644
--- a/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
@@ -204,6 +204,14 @@ TEST(LLVMHeaderGuardCheckTest, FixHeaderGuards) {
                 "include/llvm/ADT/foo.h",
                 StringRef("header guard does not follow preferred style")));
 
+  // An extra space inside the comment is OK.
+  llvm::StringRef WithExtraSpace = "#ifndef LLVM_ADT_FOO_H\n"
+                                   "#define LLVM_ADT_FOO_H\n"
+                                   "#endif //  LLVM_ADT_FOO_H\n";
+  EXPECT_EQ(WithExtraSpace,
+            runHeaderGuardCheckWithEndif(WithExtraSpace,
+                                         "include/llvm/ADT/foo.h", None));
+
   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
             "#define LLVM_ADT_FOO_H\n"
             "#endif \\ \n"


        


More information about the cfe-commits mailing list