diff options
| author | Daniel Jasper <djasper@google.com> | 2015-09-29 07:53:08 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2015-09-29 07:53:08 +0000 |
| commit | 85c472dccdf536fb1853139f456a0763311a8a42 (patch) | |
| tree | eb2b58077b2ac928af88a1e962f22eb8838f8dae /clang/unittests/Format | |
| parent | 98b3ee50ffd76e4df6e9e83c7466a4410d81ea6d (diff) | |
| download | bcm5719-llvm-85c472dccdf536fb1853139f456a0763311a8a42.tar.gz bcm5719-llvm-85c472dccdf536fb1853139f456a0763311a8a42.zip | |
clang-format: Extend #include sorting functionality
Recognize the main module header as well as different #include categories.
This should now mimic the behavior of llvm/utils/sort_includes.py as
well as clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp very
closely.
llvm-svn: 248782
Diffstat (limited to 'clang/unittests/Format')
| -rw-r--r-- | clang/unittests/Format/SortIncludesTest.cpp | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp index cbbc2baad97..8908eb92a56 100644 --- a/clang/unittests/Format/SortIncludesTest.cpp +++ b/clang/unittests/Format/SortIncludesTest.cpp @@ -20,13 +20,15 @@ namespace { class SortIncludesTest : public ::testing::Test { protected: - std::string sort(llvm::StringRef Code) { + std::string sort(llvm::StringRef Code, StringRef FileName = "input.cpp") { std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); - std::string Sorted = applyAllReplacements( - Code, sortIncludes(getLLVMStyle(), Code, Ranges, "input.cpp")); - return applyAllReplacements( - Sorted, reformat(getLLVMStyle(), Sorted, Ranges, "input.cpp")); + std::string Sorted = + applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName)); + return applyAllReplacements(Sorted, + reformat(Style, Sorted, Ranges, FileName)); } + + FormatStyle Style = getLLVMStyle(); }; TEST_F(SortIncludesTest, BasicSorting) { @@ -76,13 +78,23 @@ TEST_F(SortIncludesTest, SortsLocallyInEachBlock) { "#include \"c.h\"\n" "\n" "#include \"b.h\"\n", - sort("#include \"c.h\"\n" - "#include \"a.h\"\n" + sort("#include \"a.h\"\n" + "#include \"c.h\"\n" "\n" "#include \"b.h\"\n")); } TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) { + EXPECT_EQ("#include \"a.h\"\n" + "#include \"c.h\"\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")); + + Style = getGoogleStyle(FormatStyle::LK_Cpp); EXPECT_EQ("#include <b.h>\n" "#include <d.h>\n" "#include \"a.h\"\n" @@ -103,6 +115,24 @@ TEST_F(SortIncludesTest, HandlesMultilineIncludes) { "#include \"b.h\"\n")); } +TEST_F(SortIncludesTest, LeavesMainHeaderFirst) { + EXPECT_EQ("#include \"llvm/a.h\"\n" + "#include \"b.h\"\n" + "#include \"c.h\"\n", + sort("#include \"llvm/a.h\"\n" + "#include \"c.h\"\n" + "#include \"b.h\"\n")); + + // Don't do this in headers. + EXPECT_EQ("#include \"b.h\"\n" + "#include \"c.h\"\n" + "#include \"llvm/a.h\"\n", + sort("#include \"llvm/a.h\"\n" + "#include \"c.h\"\n" + "#include \"b.h\"\n", + "some_header.h")); +} + } // end namespace } // end namespace format } // end namespace clang |

