diff options
| author | Manuel Klimek <klimek@google.com> | 2016-03-01 12:37:30 +0000 |
|---|---|---|
| committer | Manuel Klimek <klimek@google.com> | 2016-03-01 12:37:30 +0000 |
| commit | b12e5a5ccd8e5ca71a3722a4b5103a794ca5515f (patch) | |
| tree | 2be713f424b906e73a5320edca75855f6b37226e /clang/unittests | |
| parent | 109702ccb16f33a5b0305a1d1287413c9a88adca (diff) | |
| download | bcm5719-llvm-b12e5a5ccd8e5ca71a3722a4b5103a794ca5515f.tar.gz bcm5719-llvm-b12e5a5ccd8e5ca71a3722a4b5103a794ca5515f.zip | |
Add functions to apply replacements and reformat them.
This is a commonly useful feature to have, and we have implemented it
multiple times with different kinds of bugs. This implementation
centralizes the idea in a set of functions that we can then use from the various
tools.
Reverts r262234, which is a revert of r262232, and puts the functions
into FOrmat.h, as they are closely coupled to clang-format, and we
otherwise introduce a cyclic dependency between libFormat and
libTooling.
Patch by Eric Liu.
llvm-svn: 262323
Diffstat (limited to 'clang/unittests')
| -rw-r--r-- | clang/unittests/Format/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 46 | ||||
| -rw-r--r-- | clang/unittests/Tooling/RefactoringTest.cpp | 19 |
3 files changed, 65 insertions, 1 deletions
diff --git a/clang/unittests/Format/CMakeLists.txt b/clang/unittests/Format/CMakeLists.txt index 01af435fffc..de26a4704a8 100644 --- a/clang/unittests/Format/CMakeLists.txt +++ b/clang/unittests/Format/CMakeLists.txt @@ -13,5 +13,6 @@ add_clang_unittest(FormatTests target_link_libraries(FormatTests clangFormat + clangFrontend clangToolingCore ) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 164909f8688..4872565e04a 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -7,8 +7,12 @@ // //===----------------------------------------------------------------------===// -#include "FormatTestUtils.h" #include "clang/Format/Format.h" + +#include "../Tooling/RewriterTestContext.h" +#include "FormatTestUtils.h" + +#include "clang/Frontend/TextDiagnosticPrinter.h" #include "llvm/Support/Debug.h" #include "gtest/gtest.h" @@ -11171,6 +11175,46 @@ TEST_F(FormatTest, FormatsTableGenCode) { verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); } +class ReplacementTest : public ::testing::Test { +protected: + tooling::Replacement createReplacement(SourceLocation Start, unsigned Length, + llvm::StringRef ReplacementText) { + return tooling::Replacement(Context.Sources, Start, Length, + ReplacementText); + } + + RewriterTestContext Context; +}; + +TEST_F(ReplacementTest, FormatCodeAfterReplacements) { + // Column limit is 20. + std::string Code = "Type *a =\n" + " new Type();\n" + "g(iiiii, 0, jjjjj,\n" + " 0, kkkkk, 0, mm);\n" + "int bad = format ;"; + std::string Expected = "auto a = new Type();\n" + "g(iiiii, nullptr,\n" + " jjjjj, nullptr,\n" + " kkkkk, nullptr,\n" + " mm);\n" + "int bad = format ;"; + FileID ID = Context.createInMemoryFile("format.cpp", Code); + tooling::Replacements Replaces; + Replaces.insert(tooling::Replacement( + Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto ")); + Replaces.insert(tooling::Replacement( + Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr")); + Replaces.insert(tooling::Replacement( + Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr")); + Replaces.insert(tooling::Replacement( + Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr")); + + format::FormatStyle Style = format::getLLVMStyle(); + Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. + EXPECT_EQ(Expected, applyAllReplacementsAndFormat(Code, Replaces, Style)); +} + } // end namespace } // end namespace format } // end namespace clang diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp index ff11aeae117..73da5a82cb2 100644 --- a/clang/unittests/Tooling/RefactoringTest.cpp +++ b/clang/unittests/Tooling/RefactoringTest.cpp @@ -418,6 +418,25 @@ TEST(Range, contains) { EXPECT_FALSE(Range(0, 10).contains(Range(0, 11))); } +TEST(Range, CalculateRangesOfReplacements) { + // Before: aaaabbbbbbz + // After : bbbbbbzzzzzzoooooooooooooooo + Replacements Replaces; + Replaces.insert(Replacement("foo", 0, 4, "")); + Replaces.insert(Replacement("foo", 10, 1, "zzzzzz")); + Replaces.insert(Replacement("foo", 11, 0, "oooooooooooooooo")); + + std::vector<Range> Ranges = calculateChangedRangesInFile(Replaces); + + EXPECT_EQ(3ul, Ranges.size()); + EXPECT_TRUE(Ranges[0].getOffset() == 0); + EXPECT_TRUE(Ranges[0].getLength() == 0); + EXPECT_TRUE(Ranges[1].getOffset() == 6); + EXPECT_TRUE(Ranges[1].getLength() == 6); + EXPECT_TRUE(Ranges[2].getOffset() == 12); + EXPECT_TRUE(Ranges[2].getLength() == 16); +} + TEST(DeduplicateTest, removesDuplicates) { std::vector<Replacement> Input; Input.push_back(Replacement("fileA", 50, 0, " foo ")); |

