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

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 4 12:37:48 PDT 2022


sammccall created this revision.
sammccall added a reviewer: hokein.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
sammccall requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124955

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


Index: clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
===================================================================
--- clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
+++ clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp
@@ -204,6 +204,14 @@
                 "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"
Index: clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
===================================================================
--- clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
+++ clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
@@ -145,13 +145,13 @@
         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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124955.427108.patch
Type: text/x-patch
Size: 1891 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220504/beeeb876/attachment.bin>


More information about the cfe-commits mailing list