]> granicus.if.org Git - clang/commitdiff
clang-format: Teach --sort-includes to interleave #include and #import.
authorNico Weber <nicolasweber@gmx.de>
Wed, 21 Oct 2015 17:13:45 +0000 (17:13 +0000)
committerNico Weber <nicolasweber@gmx.de>
Wed, 21 Oct 2015 17:13:45 +0000 (17:13 +0000)
clang accepts both #include and #import for includes (the latter having an
implicit header guard).  Let clang-format interleave both types if
--sort-includes is passed.  #import is used frequently in Objective-C code.

http://reviews.llvm.org/D13853

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

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

index 2595a666699c51c3818a5ab58b8508fd02a3b4c8..365c22a8ec237f5deed5f28004935fe1a2e8112c 100644 (file)
@@ -1732,7 +1732,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
   unsigned Prev = 0;
   unsigned SearchFrom = 0;
   llvm::Regex IncludeRegex(
-      R"(^[\t\ ]*#[\t\ ]*include[^"<]*(["<][^">]*[">]))");
+      R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))");
   SmallVector<StringRef, 4> Matches;
   SmallVector<IncludeDirective, 16> IncludesInBlock;
 
@@ -1762,20 +1762,21 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
         Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
     if (!Line.endswith("\\")) {
       if (IncludeRegex.match(Line, &Matches)) {
+        StringRef IncludeName = Matches[2];
         unsigned Category;
-        if (LookForMainHeader && !Matches[1].startswith("<")) {
+        if (LookForMainHeader && !IncludeName.startswith("<")) {
           Category = 0;
         } else {
           Category = UINT_MAX;
           for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) {
-            if (CategoryRegexs[i].match(Matches[1])) {
+            if (CategoryRegexs[i].match(IncludeName)) {
               Category = Style.IncludeCategories[i].Priority;
               break;
             }
           }
         }
         LookForMainHeader = false;
-        IncludesInBlock.push_back({Matches[1], Line, Prev, Category});
+        IncludesInBlock.push_back({IncludeName, Line, Prev, Category});
       } else if (!IncludesInBlock.empty()) {
         sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
         IncludesInBlock.clear();
index 36c8351810e36467c1de405d5e19098fc9b4e95f..84bc554edd11aef1627f04db1d21c251534f1d8b 100644 (file)
@@ -40,6 +40,15 @@ TEST_F(SortIncludesTest, BasicSorting) {
                  "#include \"b.h\"\n"));
 }
 
+TEST_F(SortIncludesTest, MixIncludeAndImport) {
+  EXPECT_EQ("#include \"a.h\"\n"
+            "#import \"b.h\"\n"
+            "#include \"c.h\"\n",
+            sort("#include \"a.h\"\n"
+                 "#include \"c.h\"\n"
+                 "#import \"b.h\"\n"));
+}
+
 TEST_F(SortIncludesTest, FixTrailingComments) {
   EXPECT_EQ("#include \"a.h\"  // comment\n"
             "#include \"bb.h\" // comment\n"