diff options
author | Eric Liu <ioeric@google.com> | 2018-05-04 17:55:13 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2018-05-04 17:55:13 +0000 |
commit | bf4c41c7e18ddac1328f7873e2ff8d209a96bd50 (patch) | |
tree | a010a870615c13b429f6f209dd38c2d0f6269b56 /clang/unittests/Format/CleanupTest.cpp | |
parent | d7ffbc5c7e5d033ee3812fde2c13f1bb80044e70 (diff) | |
download | bcm5719-llvm-bf4c41c7e18ddac1328f7873e2ff8d209a96bd50.tar.gz bcm5719-llvm-bf4c41c7e18ddac1328f7873e2ff8d209a96bd50.zip |
[clang-format] Refactor #include insertion/deletion functionality into a class.
Summary:
The class will be moved into libToolingCore as followup.
The new behaviors in this patch:
- New #include is inserted in the right position in a #include block to
preserver sorted #includes. This is best effort - only works when the
block is already sorted.
- When inserting multiple #includes to the end of a file which doesn't
end with a "\n" character, a "\n" will be prepended to each #include.
This is a special and rare case that was previously handled. This is now
relaxed to avoid complexity as it's rare in practice.
Reviewers: ilya-biryukov
Reviewed By: ilya-biryukov
Subscribers: klimek, cfe-commits, djasper
Differential Revision: https://reviews.llvm.org/D46180
llvm-svn: 331544
Diffstat (limited to 'clang/unittests/Format/CleanupTest.cpp')
-rw-r--r-- | clang/unittests/Format/CleanupTest.cpp | 86 |
1 files changed, 74 insertions, 12 deletions
diff --git a/clang/unittests/Format/CleanupTest.cpp b/clang/unittests/Format/CleanupTest.cpp index 032cc441ca2..2b759cc332a 100644 --- a/clang/unittests/Format/CleanupTest.cpp +++ b/clang/unittests/Format/CleanupTest.cpp @@ -471,19 +471,77 @@ TEST_F(CleanUpReplacementsTest, InsertOneIncludeLLVMStyle) { EXPECT_EQ(Expected, apply(Code, Replaces)); } +TEST_F(CleanUpReplacementsTest, InsertIntoBlockSorted) { + std::string Code = "#include \"x/fix.h\"\n" + "#include \"a.h\"\n" + "#include \"c.h\"\n" + "#include <memory>\n"; + std::string Expected = "#include \"x/fix.h\"\n" + "#include \"a.h\"\n" + "#include \"b.h\"\n" + "#include \"c.h\"\n" + "#include <memory>\n"; + tooling::Replacements Replaces = + toReplacements({createInsertion("#include \"b.h\"")}); + EXPECT_EQ(Expected, apply(Code, Replaces)); +} + +TEST_F(CleanUpReplacementsTest, InsertIntoFirstBlockOfSameKind) { + std::string Code = "#include \"x/fix.h\"\n" + "#include \"c.h\"\n" + "#include \"e.h\"\n" + "#include \"f.h\"\n" + "#include <memory>\n" + "#include <vector>\n" + "#include \"m.h\"\n" + "#include \"n.h\"\n"; + std::string Expected = "#include \"x/fix.h\"\n" + "#include \"c.h\"\n" + "#include \"d.h\"\n" + "#include \"e.h\"\n" + "#include \"f.h\"\n" + "#include <memory>\n" + "#include <vector>\n" + "#include \"m.h\"\n" + "#include \"n.h\"\n"; + tooling::Replacements Replaces = + toReplacements({createInsertion("#include \"d.h\"")}); + EXPECT_EQ(Expected, apply(Code, Replaces)); +} + +TEST_F(CleanUpReplacementsTest, InsertIntoSystemBlockSorted) { + std::string Code = "#include \"x/fix.h\"\n" + "#include \"a.h\"\n" + "#include \"c.h\"\n" + "#include <a>\n" + "#include <z>\n"; + std::string Expected = "#include \"x/fix.h\"\n" + "#include \"a.h\"\n" + "#include \"c.h\"\n" + "#include <a>\n" + "#include <vector>\n" + "#include <z>\n"; + tooling::Replacements Replaces = + toReplacements({createInsertion("#include <vector>")}); + EXPECT_EQ(Expected, apply(Code, Replaces)); +} + + TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesLLVMStyle) { std::string Code = "#include \"x/fix.h\"\n" "#include \"a.h\"\n" "#include \"b.h\"\n" + "#include \"z.h\"\n" "#include \"clang/Format/Format.h\"\n" "#include <memory>\n"; std::string Expected = "#include \"x/fix.h\"\n" "#include \"a.h\"\n" "#include \"b.h\"\n" "#include \"new/new.h\"\n" + "#include \"z.h\"\n" "#include \"clang/Format/Format.h\"\n" - "#include <memory>\n" - "#include <list>\n"; + "#include <list>\n" + "#include <memory>\n"; tooling::Replacements Replaces = toReplacements({createInsertion("#include <list>"), createInsertion("#include \"new/new.h\"")}); @@ -517,12 +575,12 @@ TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesGoogleStyle) { "#include \"z/b.h\"\n"; std::string Expected = "#include \"x/fix.h\"\n" "\n" - "#include <vector>\n" "#include <list>\n" + "#include <vector>\n" "\n" + "#include \"x/x.h\"\n" "#include \"y/a.h\"\n" - "#include \"z/b.h\"\n" - "#include \"x/x.h\"\n"; + "#include \"z/b.h\"\n"; tooling::Replacements Replaces = toReplacements({createInsertion("#include <list>"), createInsertion("#include \"x/x.h\"")}); @@ -776,8 +834,10 @@ TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCode) { TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) { std::string Code = "#include <map>"; + // FIXME: a better behavior is to only append on newline to Code, but this + // case should be rare in practice. std::string Expected = - "#include <map>\n#include <string>\n#include <vector>\n"; + "#include <map>\n#include <string>\n\n#include <vector>\n"; tooling::Replacements Replaces = toReplacements({createInsertion("#include <string>"), createInsertion("#include <vector>")}); @@ -801,8 +861,8 @@ TEST_F(CleanUpReplacementsTest, AddIncludesWithDifferentForms) { // FIXME: this might not be the best behavior. std::string Expected = "#include \"a.h\"\n" "#include \"vector\"\n" - "#include <vector>\n" - "#include <a.h>\n"; + "#include <a.h>\n" + "#include <vector>\n"; tooling::Replacements Replaces = toReplacements({createInsertion("#include \"vector\""), createInsertion("#include <a.h>")}); @@ -825,16 +885,18 @@ TEST_F(CleanUpReplacementsTest, DeleteAllCode) { std::string Code = "#include \"xyz.h\"\n" "#include <xyz.h>"; std::string Expected = ""; - tooling::Replacements Replaces = toReplacements({createDeletion("xyz.h")}); + tooling::Replacements Replaces = + toReplacements({createDeletion("\"xyz.h\""), createDeletion("<xyz.h>")}); EXPECT_EQ(Expected, apply(Code, Replaces)); } -TEST_F(CleanUpReplacementsTest, DeleteAllIncludesWithSameNameIfNoType) { +TEST_F(CleanUpReplacementsTest, DeleteOnlyIncludesWithSameQuote) { std::string Code = "#include \"xyz.h\"\n" "#include \"xyz\"\n" "#include <xyz.h>\n"; - std::string Expected = "#include \"xyz\"\n"; - tooling::Replacements Replaces = toReplacements({createDeletion("xyz.h")}); + std::string Expected = "#include \"xyz.h\"\n" + "#include \"xyz\"\n"; + tooling::Replacements Replaces = toReplacements({createDeletion("<xyz.h>")}); EXPECT_EQ(Expected, apply(Code, Replaces)); } |