]> granicus.if.org Git - clang/commitdiff
[clang-format] clang-format off/on not respected when using C Style comments
authorPaul Hoad <mydeveloperday@gmail.com>
Sat, 2 Mar 2019 09:08:51 +0000 (09:08 +0000)
committerPaul Hoad <mydeveloperday@gmail.com>
Sat, 2 Mar 2019 09:08:51 +0000 (09:08 +0000)
Summary:
If the clang-format on/off is in a /* comment */ then the sorting of headers is not ignored

PR40901 - https://bugs.llvm.org/show_bug.cgi?id=40901

Reviewers: djasper, klimek, JonasToth, krasimir, alexfh

Reviewed By: alexfh

Subscribers: alexfh, cfe-commits, llvm-commits

Tags: #clang, #clang-tools-extra

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@355266 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/SortIncludesTest.cpp

index a5023ec301976691d8c08e2e7e350aa847a3d0f3..a56fed9dee1eff1008eed5ad0b35b1ab610e5554 100644 (file)
@@ -1786,9 +1786,10 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
         Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
 
     StringRef Trimmed = Line.trim();
-    if (Trimmed == "// clang-format off")
+    if (Trimmed == "// clang-format off" || Trimmed == "/* clang-format off */")
       FormattingOff = true;
-    else if (Trimmed == "// clang-format on")
+    else if (Trimmed == "// clang-format on" ||
+             Trimmed == "/* clang-format on */")
       FormattingOff = false;
 
     const bool EmptyLineSkipped =
index 3f49ace75f7874c3541a677be6eff1a6f9602e5f..75f4156f3d8715a203fa567f8e30c5371e41c92c 100644 (file)
@@ -117,6 +117,43 @@ TEST_F(SortIncludesTest, SupportClangFormatOff) {
                  "// clang-format on\n"));
 }
 
+TEST_F(SortIncludesTest, SupportClangFormatOffCStyle) {
+  EXPECT_EQ("#include <a>\n"
+            "#include <b>\n"
+            "#include <c>\n"
+            "/* clang-format off */\n"
+            "#include <b>\n"
+            "#include <a>\n"
+            "#include <c>\n"
+            "/* clang-format on */\n",
+            sort("#include <b>\n"
+                 "#include <a>\n"
+                 "#include <c>\n"
+                 "/* clang-format off */\n"
+                 "#include <b>\n"
+                 "#include <a>\n"
+                 "#include <c>\n"
+                 "/* clang-format on */\n"));
+
+  // Not really turning it off
+  EXPECT_EQ("#include <a>\n"
+            "#include <b>\n"
+            "#include <c>\n"
+            "/* clang-format offically */\n"
+            "#include <a>\n"
+            "#include <b>\n"
+            "#include <c>\n"
+            "/* clang-format onwards */\n",
+            sort("#include <b>\n"
+                 "#include <a>\n"
+                 "#include <c>\n"
+                 "/* clang-format offically */\n"
+                 "#include <b>\n"
+                 "#include <a>\n"
+                 "#include <c>\n"
+                 "/* clang-format onwards */\n"));
+}
+
 TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
   FmtStyle.SortIncludes = false;
   EXPECT_EQ("#include \"a.h\"\n"