]> granicus.if.org Git - clang/commitdiff
clang-format: Only try to find the "main" include in the first block of
authorDaniel Jasper <djasper@google.com>
Mon, 21 Dec 2015 13:40:49 +0000 (13:40 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 21 Dec 2015 13:40:49 +0000 (13:40 +0000)
includes.

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

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

index c9058a587e6eb1f103d1fde429c4d125d4501788..9b9a1b3e41c5cc1c91b4bce644fa8d9e70034b8d 100644 (file)
@@ -1814,6 +1814,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
                   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 FormatStyle &Style, StringRef Code,
             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 FormatStyle &Style, StringRef Code,
         sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces,
                      Cursor);
         IncludesInBlock.clear();
+        FirstIncludeBlock = false;
       }
       Prev = Pos + 1;
     }
index ce83091b7154b3f2d424b4ad43b7ca19d9c09f19..d96227d13ee24f836b5126087ef5a18e60232aff 100644 (file)
@@ -191,6 +191,19 @@ TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
                  "#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) {