r256153 - clang-format: Only try to find the "main" include in the first block of

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 21 05:40:49 PST 2015


Author: djasper
Date: Mon Dec 21 07:40:49 2015
New Revision: 256153

URL: http://llvm.org/viewvc/llvm-project?rev=256153&view=rev
Log:
clang-format: Only try to find the "main" include in the first block of
includes.

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=256153&r1=256152&r2=256153&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Dec 21 07:40:49 2015
@@ -1814,6 +1814,7 @@ tooling::Replacements sortIncludes(const
                   FileName.endswith(".cxx") || FileName.endswith(".m") ||
                   FileName.endswith(".mm");
   StringRef FileStem = llvm::sys::path::stem(FileName);
+  bool FirstIncludeBlock = true;
 
   // Create pre-compiled regular expressions for the #include categories.
   SmallVector<llvm::Regex, 4> CategoryRegexs;
@@ -1843,7 +1844,8 @@ tooling::Replacements sortIncludes(const
             break;
           }
         }
-        if (IsSource && Category > 0 && IncludeName.startswith("\"")) {
+        if (IsSource && Category > 0 && FirstIncludeBlock &&
+            IncludeName.startswith("\"")) {
           StringRef HeaderStem =
               llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
           if (FileStem.startswith(HeaderStem))
@@ -1854,6 +1856,7 @@ tooling::Replacements sortIncludes(const
         sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces,
                      Cursor);
         IncludesInBlock.clear();
+        FirstIncludeBlock = false;
       }
       Prev = Pos + 1;
     }

Modified: cfe/trunk/unittests/Format/SortIncludesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/SortIncludesTest.cpp?rev=256153&r1=256152&r2=256153&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/SortIncludesTest.cpp (original)
+++ cfe/trunk/unittests/Format/SortIncludesTest.cpp Mon Dec 21 07:40:49 2015
@@ -191,6 +191,19 @@ TEST_F(SortIncludesTest, LeavesMainHeade
                  "#include \"c.h\"\n"
                  "#include \"b.h\"\n",
                  "a.h"));
+
+  // Only do this in the first #include block.
+  EXPECT_EQ("#include <a>\n"
+            "\n"
+            "#include \"b.h\"\n"
+            "#include \"c.h\"\n"
+            "#include \"llvm/a.h\"\n",
+            sort("#include <a>\n"
+                 "\n"
+                 "#include \"llvm/a.h\"\n"
+                 "#include \"c.h\"\n"
+                 "#include \"b.h\"\n",
+                 "a.cc"));
 }
 
 TEST_F(SortIncludesTest, NegativePriorities) {




More information about the cfe-commits mailing list