r250909 - clang-format: Teach --sort-includes to interleave #include and #import.

Nico Weber via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 21 10:13:45 PDT 2015


Author: nico
Date: Wed Oct 21 12:13:45 2015
New Revision: 250909

URL: http://llvm.org/viewvc/llvm-project?rev=250909&view=rev
Log:
clang-format: Teach --sort-includes to interleave #include and #import.

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

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=250909&r1=250908&r2=250909&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Oct 21 12:13:45 2015
@@ -1732,7 +1732,7 @@ tooling::Replacements sortIncludes(const
   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
         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();

Modified: cfe/trunk/unittests/Format/SortIncludesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/SortIncludesTest.cpp?rev=250909&r1=250908&r2=250909&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/SortIncludesTest.cpp (original)
+++ cfe/trunk/unittests/Format/SortIncludesTest.cpp Wed Oct 21 12:13:45 2015
@@ -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"




More information about the cfe-commits mailing list