summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorPaul Hoad <mydeveloperday@gmail.com>2019-09-25 20:33:01 +0000
committerPaul Hoad <mydeveloperday@gmail.com>2019-09-25 20:33:01 +0000
commit258784db36173d3966a4cf860344018d7eb84648 (patch)
treee171789789aaebf04d18ce85bd82aa18e93a16a4 /unittests
parent135af9b01b370a941da35aa6fa07f26438b55f91 (diff)
downloadclang-258784db36173d3966a4cf860344018d7eb84648.tar.gz
[clang-format] Modified SortIncludes and IncludeCategories to priority for sorting #includes within the Group Category.
Summary: This new Style rule is made as a part of adding support for NetBSD KNF in clang-format. NetBSD have it's own priority of includes which should be followed while formatting NetBSD code. This style sorts the Cpp Includes according to the priorities of NetBSD, as mentioned in the [Style Guide](http://cvsweb.netbsd.org/bsdweb.cgi/src/share/misc/style?rev=HEAD&content-type=text/x-cvsweb-markup) The working of this Style rule shown below: **Configuration:** This revision introduces a new field under IncludeCategories named `SortPriority` which defines the priority of ordering the `#includes` and the `Priority` will define the categories for grouping the `#include blocks`. Reviewers: cfe-commits, mgorny, christos, MyDeveloperDay Reviewed By: MyDeveloperDay Subscribers: lebedev.ri, rdwampler, christos, mgorny, krytarowski Patch By: Manikishan Tags: #clang, #clang-format Differential Revision: https://reviews.llvm.org/D64695 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@372919 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/Format/SortIncludesTest.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/unittests/Format/SortIncludesTest.cpp b/unittests/Format/SortIncludesTest.cpp
index c00d3cb747..ef67586f1f 100644
--- a/unittests/Format/SortIncludesTest.cpp
+++ b/unittests/Format/SortIncludesTest.cpp
@@ -70,6 +70,77 @@ TEST_F(SortIncludesTest, BasicSorting) {
{tooling::Range(25, 1)}));
}
+TEST_F(SortIncludesTest, SortedIncludesUsingSortPriorityAttribute) {
+ FmtStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
+ FmtStyle.IncludeStyle.IncludeCategories = {
+ {"^<sys/param\\.h>", 1, 0},
+ {"^<sys/types\\.h>", 1, 1},
+ {"^<sys.*/", 1, 2},
+ {"^<uvm/", 2, 3},
+ {"^<machine/", 3, 4},
+ {"^<dev/", 4, 5},
+ {"^<net.*/", 5, 6},
+ {"^<protocols/", 5, 7},
+ {"^<(fs|miscfs|msdosfs|nfs|ntfs|ufs)/", 6, 8},
+ {"^<(x86|amd64|i386|xen)/", 7, 8},
+ {"<path", 9, 11},
+ {"^<[^/].*\\.h>", 8, 10},
+ {"^\".*\\.h\"", 10, 12}};
+ EXPECT_EQ("#include <sys/param.h>\n"
+ "#include <sys/types.h>\n"
+ "#include <sys/ioctl.h>\n"
+ "#include <sys/socket.h>\n"
+ "#include <sys/stat.h>\n"
+ "#include <sys/wait.h>\n"
+ "\n"
+ "#include <net/if.h>\n"
+ "#include <net/if_dl.h>\n"
+ "#include <net/route.h>\n"
+ "#include <netinet/in.h>\n"
+ "#include <protocols/rwhod.h>\n"
+ "\n"
+ "#include <assert.h>\n"
+ "#include <errno.h>\n"
+ "#include <inttypes.h>\n"
+ "#include <stdio.h>\n"
+ "#include <stdlib.h>\n"
+ "\n"
+ "#include <paths.h>\n"
+ "\n"
+ "#include \"pathnames.h\"\n",
+ sort("#include <sys/param.h>\n"
+ "#include <sys/types.h>\n"
+ "#include <sys/ioctl.h>\n"
+ "#include <net/if_dl.h>\n"
+ "#include <net/route.h>\n"
+ "#include <netinet/in.h>\n"
+ "#include <sys/socket.h>\n"
+ "#include <sys/stat.h>\n"
+ "#include <sys/wait.h>\n"
+ "#include <net/if.h>\n"
+ "#include <protocols/rwhod.h>\n"
+ "#include <assert.h>\n"
+ "#include <paths.h>\n"
+ "#include \"pathnames.h\"\n"
+ "#include <errno.h>\n"
+ "#include <inttypes.h>\n"
+ "#include <stdio.h>\n"
+ "#include <stdlib.h>\n"));
+}
+TEST_F(SortIncludesTest, SortPriorityNotDefined) {
+ FmtStyle = getLLVMStyle();
+ EXPECT_EQ("#include \"FormatTestUtils.h\"\n"
+ "#include \"clang/Format/Format.h\"\n"
+ "#include \"llvm/ADT/None.h\"\n"
+ "#include \"llvm/Support/Debug.h\"\n"
+ "#include \"gtest/gtest.h\"\n",
+ sort("#include \"clang/Format/Format.h\"\n"
+ "#include \"llvm/ADT/None.h\"\n"
+ "#include \"FormatTestUtils.h\"\n"
+ "#include \"gtest/gtest.h\"\n"
+ "#include \"llvm/Support/Debug.h\"\n"));
+}
+
TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
// Identical #includes have led to a failure with an unstable sort.
std::string Code = "#include <a>\n"