summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Format/SortIncludesTest.cpp
diff options
context:
space:
mode:
authorKrasimir Georgiev <krasimir@google.com>2017-11-27 13:23:45 +0000
committerKrasimir Georgiev <krasimir@google.com>2017-11-27 13:23:45 +0000
commit4c2c9c3620e3bed9cb6fa709ec3b3cc454eb5748 (patch)
treee12bf4b790ef7ae6324b31ce60e65548998ca680 /clang/unittests/Format/SortIncludesTest.cpp
parent6e39e68983045fe48fb955f24afe376b46eb3600 (diff)
downloadbcm5719-llvm-4c2c9c3620e3bed9cb6fa709ec3b3cc454eb5748.tar.gz
bcm5719-llvm-4c2c9c3620e3bed9cb6fa709ec3b3cc454eb5748.zip
[clang-format] Add option to group multiple #include blocks when sorting includes
Summary: This patch allows grouping multiple #include blocks together and sort all includes as one big block. Additionally, sorted includes can be regrouped after sorting based on configured categories. Contributed by @KrzysztofKapusta! Reviewers: krasimir Reviewed By: krasimir Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D40288 llvm-svn: 319024
Diffstat (limited to 'clang/unittests/Format/SortIncludesTest.cpp')
-rw-r--r--clang/unittests/Format/SortIncludesTest.cpp182
1 files changed, 182 insertions, 0 deletions
diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp
index ff3988776bd..09fc0703d42 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -77,6 +77,28 @@ TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
EXPECT_TRUE(sortIncludes(Style, Code, GetCodeRange(Code), "a.cc").empty());
}
+TEST_F(SortIncludesTest, SortedIncludesInMultipleBlocksAreMerged) {
+ Style.IncludeBlocks = FormatStyle::IBS_Merge;
+ EXPECT_EQ("#include \"a.h\"\n"
+ "#include \"b.h\"\n"
+ "#include \"c.h\"\n",
+ sort("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "\n"
+ "\n"
+ "#include \"b.h\"\n"));
+
+ Style.IncludeBlocks = FormatStyle::IBS_Regroup;
+ EXPECT_EQ("#include \"a.h\"\n"
+ "#include \"b.h\"\n"
+ "#include \"c.h\"\n",
+ sort("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "\n"
+ "\n"
+ "#include \"b.h\"\n"));
+}
+
TEST_F(SortIncludesTest, SupportClangFormatOff) {
EXPECT_EQ("#include <a>\n"
"#include <b>\n"
@@ -159,6 +181,48 @@ TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
"#include \"b.h\"\n"));
}
+TEST_F(SortIncludesTest, SortsAllBlocksWhenMerging) {
+ Style.IncludeBlocks = FormatStyle::IBS_Merge;
+ EXPECT_EQ("#include \"a.h\"\n"
+ "#include \"b.h\"\n"
+ "#include \"c.h\"\n",
+ sort("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "\n"
+ "#include \"b.h\"\n"));
+}
+
+TEST_F(SortIncludesTest, CommentsAlwaysSeparateGroups) {
+ EXPECT_EQ("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "// comment\n"
+ "#include \"b.h\"\n",
+ sort("#include \"c.h\"\n"
+ "#include \"a.h\"\n"
+ "// comment\n"
+ "#include \"b.h\"\n"));
+
+ Style.IncludeBlocks = FormatStyle::IBS_Merge;
+ EXPECT_EQ("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "// comment\n"
+ "#include \"b.h\"\n",
+ sort("#include \"c.h\"\n"
+ "#include \"a.h\"\n"
+ "// comment\n"
+ "#include \"b.h\"\n"));
+
+ Style.IncludeBlocks = FormatStyle::IBS_Regroup;
+ EXPECT_EQ("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "// comment\n"
+ "#include \"b.h\"\n",
+ sort("#include \"c.h\"\n"
+ "#include \"a.h\"\n"
+ "// comment\n"
+ "#include \"b.h\"\n"));
+}
+
TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
EXPECT_EQ("#include \"a.h\"\n"
"#include \"c.h\"\n"
@@ -180,6 +244,19 @@ TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
"#include \"a.h\"\n"));
}
+TEST_F(SortIncludesTest, RegroupsAngledIncludesInSeparateBlocks) {
+ Style.IncludeBlocks = FormatStyle::IBS_Regroup;
+ EXPECT_EQ("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "\n"
+ "#include <b.h>\n"
+ "#include <d.h>\n",
+ sort("#include <d.h>\n"
+ "#include <b.h>\n"
+ "#include \"c.h\"\n"
+ "#include \"a.h\"\n"));
+}
+
TEST_F(SortIncludesTest, HandlesMultilineIncludes) {
EXPECT_EQ("#include \"a.h\"\n"
"#include \"b.h\"\n"
@@ -266,6 +343,35 @@ TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
"a.cc"));
}
+TEST_F(SortIncludesTest, RecognizeMainHeaderInAllGroups) {
+ Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
+ Style.IncludeBlocks = FormatStyle::IBS_Merge;
+
+ EXPECT_EQ("#include \"c.h\"\n"
+ "#include \"a.h\"\n"
+ "#include \"b.h\"\n",
+ sort("#include \"b.h\"\n"
+ "\n"
+ "#include \"a.h\"\n"
+ "#include \"c.h\"\n",
+ "c.cc"));
+}
+
+TEST_F(SortIncludesTest, MainHeaderIsSeparatedWhenRegroupping) {
+ Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
+ Style.IncludeBlocks = FormatStyle::IBS_Regroup;
+
+ EXPECT_EQ("#include \"a.h\"\n"
+ "\n"
+ "#include \"b.h\"\n"
+ "#include \"c.h\"\n",
+ sort("#include \"b.h\"\n"
+ "\n"
+ "#include \"a.h\"\n"
+ "#include \"c.h\"\n",
+ "a.cc"));
+}
+
TEST_F(SortIncludesTest, SupportCaseInsensitiveMatching) {
// Setup an regex for main includes so we can cover those as well.
Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
@@ -309,6 +415,34 @@ TEST_F(SortIncludesTest, NegativePriorities) {
"c_main.cc"));
}
+TEST_F(SortIncludesTest, PriorityGroupsAreSeparatedWhenRegroupping) {
+ Style.IncludeCategories = {{".*important_os_header.*", -1}, {".*", 1}};
+ Style.IncludeBlocks = FormatStyle::IBS_Regroup;
+
+ EXPECT_EQ("#include \"important_os_header.h\"\n"
+ "\n"
+ "#include \"c_main.h\"\n"
+ "\n"
+ "#include \"a_other.h\"\n",
+ sort("#include \"c_main.h\"\n"
+ "#include \"a_other.h\"\n"
+ "#include \"important_os_header.h\"\n",
+ "c_main.cc"));
+
+ // check stable when re-run
+ EXPECT_EQ("#include \"important_os_header.h\"\n"
+ "\n"
+ "#include \"c_main.h\"\n"
+ "\n"
+ "#include \"a_other.h\"\n",
+ sort("#include \"important_os_header.h\"\n"
+ "\n"
+ "#include \"c_main.h\"\n"
+ "\n"
+ "#include \"a_other.h\"\n",
+ "c_main.cc"));
+}
+
TEST_F(SortIncludesTest, CalculatesCorrectCursorPosition) {
std::string Code = "#include <ccc>\n" // Start of line: 0
"#include <bbbbbb>\n" // Start of line: 15
@@ -332,6 +466,30 @@ TEST_F(SortIncludesTest, DeduplicateIncludes) {
"#include <b>\n"
"#include <b>\n"
"#include <c>\n"));
+
+ Style.IncludeBlocks = FormatStyle::IBS_Merge;
+ EXPECT_EQ("#include <a>\n"
+ "#include <b>\n"
+ "#include <c>\n",
+ sort("#include <a>\n"
+ "#include <b>\n"
+ "\n"
+ "#include <b>\n"
+ "\n"
+ "#include <b>\n"
+ "#include <c>\n"));
+
+ Style.IncludeBlocks = FormatStyle::IBS_Regroup;
+ EXPECT_EQ("#include <a>\n"
+ "#include <b>\n"
+ "#include <c>\n",
+ sort("#include <a>\n"
+ "#include <b>\n"
+ "\n"
+ "#include <b>\n"
+ "\n"
+ "#include <b>\n"
+ "#include <c>\n"));
}
TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) {
@@ -344,6 +502,30 @@ TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) {
"#include <b>\n"
"#include <c>\n"
"#include <b>\n"));
+
+ Style.IncludeBlocks = FormatStyle::IBS_Merge;
+ EXPECT_EQ("#include <a>\n"
+ "#include <b>\n"
+ "#include <c>\n",
+ sort("#include <b>\n"
+ "#include <a>\n"
+ "\n"
+ "#include <b>\n"
+ "\n"
+ "#include <c>\n"
+ "#include <b>\n"));
+
+ Style.IncludeBlocks = FormatStyle::IBS_Regroup;
+ EXPECT_EQ("#include <a>\n"
+ "#include <b>\n"
+ "#include <c>\n",
+ sort("#include <b>\n"
+ "#include <a>\n"
+ "\n"
+ "#include <b>\n"
+ "\n"
+ "#include <c>\n"
+ "#include <b>\n"));
}
TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionAfterDeduplicate) {
OpenPOWER on IntegriCloud