]> granicus.if.org Git - clang/commitdiff
[clang-format] Regroup #includes into blocks for Google style
authorEric Liu <ioeric@google.com>
Wed, 3 Apr 2019 09:25:16 +0000 (09:25 +0000)
committerEric Liu <ioeric@google.com>
Wed, 3 Apr 2019 09:25:16 +0000 (09:25 +0000)
Summary:
Regrouping #includes in blocks separated by blank lines when sorting C++ #include
headers was implemented recently, and it has been preferred in Google's C++ style guide:
https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes

Reviewers: sammccall, klimek

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D60116

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

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

index 119be9fae678990895630293767317a44f2e5f7f..4dee599fe01f42a73c5db6ffcafccd804e907ac9 100644 (file)
@@ -784,6 +784,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
   GoogleStyle.IncludeStyle.IncludeCategories = {
       {"^<ext/.*\\.h>", 2}, {"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}};
   GoogleStyle.IncludeStyle.IncludeIsMainRegex = "([-_](test|unittest))?$";
+  GoogleStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
   GoogleStyle.IndentCaseLabels = true;
   GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
   GoogleStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
index 6afa56ec10a2897431760faa6dc68ba3b39a766e..0628c38a2bd6b41bcc0dba43c1e11093ee1a3d8e 100644 (file)
@@ -420,8 +420,10 @@ TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortLLVM) {
 TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortGoogle) {
   std::string Code = "\nint x;";
   std::string Expected = "\n#include \"fix.h\"\n"
+                         "\n"
                          "#include <list>\n"
                          "#include <vector>\n"
+                         "\n"
                          "#include \"a.h\"\n"
                          "#include \"b.h\"\n"
                          "#include \"c.h\"\n"
index 75f4156f3d8715a203fa567f8e30c5371e41c92c..836d4c366cc400409900a09b8515b272c1c40dfd 100644 (file)
@@ -262,9 +262,13 @@ TEST_F(SortIncludesTest, CommentsAlwaysSeparateGroups) {
 TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
   EXPECT_EQ("#include \"a.h\"\n"
             "#include \"c.h\"\n"
+            "#include <array>\n"
             "#include <b.h>\n"
-            "#include <d.h>\n",
-            sort("#include <d.h>\n"
+            "#include <d.h>\n"
+            "#include <vector>\n",
+            sort("#include <vector>\n"
+                 "#include <d.h>\n"
+                 "#include <array>\n"
                  "#include <b.h>\n"
                  "#include \"c.h\"\n"
                  "#include \"a.h\"\n"));
@@ -272,9 +276,15 @@ TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
   FmtStyle = getGoogleStyle(FormatStyle::LK_Cpp);
   EXPECT_EQ("#include <b.h>\n"
             "#include <d.h>\n"
+            "\n"
+            "#include <array>\n"
+            "#include <vector>\n"
+            "\n"
             "#include \"a.h\"\n"
             "#include \"c.h\"\n",
-            sort("#include <d.h>\n"
+            sort("#include <vector>\n"
+                 "#include <d.h>\n"
+                 "#include <array>\n"
                  "#include <b.h>\n"
                  "#include \"c.h\"\n"
                  "#include \"a.h\"\n"));