[PATCH] D94206: [clang-format] turn on formatting after "clang-format on" while sorting includes

Marek Kurdej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 11 00:41:43 PST 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rG7473940bae0f: [clang-format] turn on formatting after "clang-format on" while sorting includes (authored by rjelonek, committed by curdeius).

Changed prior to commit:
  https://reviews.llvm.org/D94206?vs=315513&id=315715#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D94206/new/

https://reviews.llvm.org/D94206

Files:
  clang/lib/Format/Format.cpp
  clang/unittests/Format/SortIncludesTest.cpp


Index: clang/unittests/Format/SortIncludesTest.cpp
===================================================================
--- clang/unittests/Format/SortIncludesTest.cpp
+++ clang/unittests/Format/SortIncludesTest.cpp
@@ -207,6 +207,27 @@
                  "#include <a>\n"
                  "#include <c>\n"
                  "// clang-format on\n"));
+
+  Style.IncludeBlocks = Style.IBS_Merge;
+  std::string Code = "// clang-format off\r\n"
+                     "#include \"d.h\"\r\n"
+                     "#include \"b.h\"\r\n"
+                     "// clang-format on\r\n"
+                     "\r\n"
+                     "#include \"c.h\"\r\n"
+                     "#include \"a.h\"\r\n"
+                     "#include \"e.h\"\r\n";
+
+  std::string Expected = "// clang-format off\r\n"
+                         "#include \"d.h\"\r\n"
+                         "#include \"b.h\"\r\n"
+                         "// clang-format on\r\n"
+                         "\r\n"
+                         "#include \"e.h\"\r\n"
+                         "#include \"a.h\"\r\n"
+                         "#include \"c.h\"\r\n";
+
+  EXPECT_EQ(Expected, sort(Code, "e.cpp", 1));
 }
 
 TEST_F(SortIncludesTest, SupportClangFormatOffCStyle) {
@@ -915,6 +936,22 @@
 
   EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
 }
+
+TEST_F(SortIncludesTest, MergeLines) {
+  Style.IncludeBlocks = Style.IBS_Merge;
+  std::string Code = "#include \"c.h\"\r\n"
+                     "#include \"b\\\r\n"
+                     ".h\"\r\n"
+                     "#include \"a.h\"\r\n";
+
+  std::string Expected = "#include \"a.h\"\r\n"
+                         "#include \"b\\\r\n"
+                         ".h\"\r\n"
+                         "#include \"c.h\"\r\n";
+
+  EXPECT_EQ(Expected, sort(Code, "a.cpp", 1));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -2291,7 +2291,8 @@
          Style.IncludeStyle.IncludeBlocks ==
              tooling::IncludeStyle::IBS_Regroup);
 
-    if (!FormattingOff && !Line.endswith("\\")) {
+    bool MergeWithNextLine = Trimmed.endswith("\\");
+    if (!FormattingOff && !MergeWithNextLine) {
       if (IncludeRegex.match(Line, &Matches)) {
         StringRef IncludeName = Matches[2];
         int Category = Categories.getIncludePriority(
@@ -2309,10 +2310,12 @@
         IncludesInBlock.clear();
         FirstIncludeBlock = false;
       }
-      Prev = Pos + 1;
     }
     if (Pos == StringRef::npos || Pos + 1 == Code.size())
       break;
+
+    if (!MergeWithNextLine)
+      Prev = Pos + 1;
     SearchFrom = Pos + 1;
   }
   if (!IncludesInBlock.empty()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94206.315715.patch
Type: text/x-patch
Size: 2796 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210111/da9ef1e7/attachment.bin>


More information about the cfe-commits mailing list