r253772 - clang-format: Make sorting includes respect // clang-format off

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 21 01:17:08 PST 2015


Author: djasper
Date: Sat Nov 21 03:17:08 2015
New Revision: 253772

URL: http://llvm.org/viewvc/llvm-project?rev=253772&view=rev
Log:
clang-format: Make sorting includes respect // clang-format off

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/SortIncludesTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=253772&r1=253771&r2=253772&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Sat Nov 21 03:17:08 2015
@@ -1780,11 +1780,20 @@ tooling::Replacements sortIncludes(const
   for (const auto &Category : Style.IncludeCategories)
     CategoryRegexs.emplace_back(Category.Regex);
 
+  bool FormattingOff = false;
+
   for (;;) {
     auto Pos = Code.find('\n', SearchFrom);
     StringRef Line =
         Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
-    if (!Line.endswith("\\")) {
+
+    StringRef Trimmed = Line.trim();
+    if (Trimmed == "// clang-format off")
+      FormattingOff = true;
+    else if (Trimmed == "// clang-format on")
+      FormattingOff = false;
+
+    if (!FormattingOff && !Line.endswith("\\")) {
       if (IncludeRegex.match(Line, &Matches)) {
         StringRef IncludeName = Matches[2];
         unsigned Category;

Modified: cfe/trunk/unittests/Format/SortIncludesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/SortIncludesTest.cpp?rev=253772&r1=253771&r2=253772&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/SortIncludesTest.cpp (original)
+++ cfe/trunk/unittests/Format/SortIncludesTest.cpp Sat Nov 21 03:17:08 2015
@@ -40,6 +40,25 @@ TEST_F(SortIncludesTest, BasicSorting) {
                  "#include \"b.h\"\n"));
 }
 
+TEST_F(SortIncludesTest, SupportClangFormatOff) {
+  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"));
+}
+
 TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
   Style.SortIncludes = false;
   EXPECT_EQ("#include \"a.h\"\n"




More information about the cfe-commits mailing list