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

RafaƂ Jelonek via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 6 17:01:09 PST 2021


rjelonek created this revision.
rjelonek added reviewers: rsmith, rnk.
rjelonek created this object with visibility "All Users".
rjelonek created this object with edit policy "Members of Project: clang".
rjelonek added a project: clang-format.
rjelonek requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Formatting is not active after "clang-format on" due to merging lines while formatting is off. Also, use trimmed line. Behaviour with LF is different than with CRLF.


Repository:
  rG LLVM Github Monorepo

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
@@ -879,6 +879,58 @@
                  "#include \"a.h\""));
 }
 
+TEST_F(SortIncludesTest, DetectClangFormatOn) {
+  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, MergeLinesWhenCodeContainsLF) {
+  Style.IncludeBlocks = Style.IBS_Merge;
+  std::string Code = "#include \"c.h\"\n"
+                     "#include \"b\\\n"
+                     ".h\"\n"
+                     "#include \"a.h\"\n";
+
+  std::string Expected = "#include \"a.h\"\n"
+                         "#include \"b\\\n"
+                         ".h\"\n"
+                         "#include \"c.h\"\n";
+
+  EXPECT_EQ(Expected, sort(Code, "a.cpp", 1));
+}
+
+TEST_F(SortIncludesTest, MergeLinesWhenCodeContainsCRLF) {
+  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
@@ -2289,7 +2289,8 @@
          Style.IncludeStyle.IncludeBlocks ==
              tooling::IncludeStyle::IBS_Regroup);
 
-    if (!FormattingOff && !Line.endswith("\\")) {
+    bool MergeNextLine = Trimmed.endswith("\\");
+    if (!FormattingOff && !MergeNextLine) {
       if (IncludeRegex.match(Line, &Matches)) {
         StringRef IncludeName = Matches[2];
         int Category = Categories.getIncludePriority(
@@ -2307,10 +2308,12 @@
         IncludesInBlock.clear();
         FirstIncludeBlock = false;
       }
-      Prev = Pos + 1;
     }
     if (Pos == StringRef::npos || Pos + 1 == Code.size())
       break;
+
+    if (!MergeNextLine)
+      Prev = Pos + 1;
     SearchFrom = Pos + 1;
   }
   if (!IncludesInBlock.empty()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94206.315026.patch
Type: text/x-patch
Size: 3138 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210107/6948d3e4/attachment.bin>


More information about the cfe-commits mailing list