ioeric created this revision. ioeric added a reviewer: klimek. ioeric added a subscriber: cfe-commits. Herald added a subscriber: klimek.
Merge branch 'master' of http://llvm.org/git/clang http://reviews.llvm.org/D17704 Files: include/clang/Tooling/Core/Replacement.h lib/Tooling/Core/Replacement.cpp unittests/Tooling/CMakeLists.txt unittests/Tooling/RefactoringTest.cpp
Index: unittests/Tooling/RefactoringTest.cpp =================================================================== --- unittests/Tooling/RefactoringTest.cpp +++ unittests/Tooling/RefactoringTest.cpp @@ -18,6 +18,7 @@ #include "clang/Basic/FileManager.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceManager.h" +#include "clang/Format/Format.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/TextDiagnosticPrinter.h" @@ -166,6 +167,37 @@ EXPECT_EQ("z", Context.getRewrittenText(IDz)); } +TEST_F(ReplacementTest, FormatCodeAfterReplacements) { + std::string Code = "MyType012345678901234567890123456789 *a =\n" + " new MyType012345678901234567890123456789();\n" + "g(iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii, 0, " + "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj,\n" + " 0, kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk, 0, " + "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm);\n" + "int bad = format ;"; + std::string Expected = + "auto a = new MyType012345678901234567890123456789();\n" + "g(iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii, nullptr,\n" + " jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj, nullptr,\n" + " kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk, nullptr,\n" + " mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm);\n" + "int bad = format ;"; + FileID ID = Context.createInMemoryFile("format.cpp", Code); + Replacements Replaces; + Replaces.insert( + Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 38, "auto ")); + Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 3, 40), + 1, "nullptr")); + Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, + "nullptr")); + Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 4, 43), + 1, "nullptr")); + + EXPECT_TRUE(applyAllReplacementsAndFormat(format::getLLVMStyle(), Replaces, + Context.Rewrite)); + EXPECT_EQ(Expected, Context.getRewrittenText(ID)); +} + TEST(ShiftedCodePositionTest, FindsNewCodePosition) { Replacements Replaces; Replaces.insert(Replacement("", 0, 1, "")); @@ -418,6 +450,23 @@ 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 = calculateChangedRanges(Replaces); + EXPECT_EQ(3, 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 ")); Index: unittests/Tooling/CMakeLists.txt =================================================================== --- unittests/Tooling/CMakeLists.txt +++ unittests/Tooling/CMakeLists.txt @@ -24,6 +24,7 @@ clangAST clangASTMatchers clangBasic + clangFormat clangFrontend clangLex clangRewrite Index: lib/Tooling/Core/Replacement.cpp =================================================================== --- lib/Tooling/Core/Replacement.cpp +++ lib/Tooling/Core/Replacement.cpp @@ -11,14 +11,16 @@ // //===----------------------------------------------------------------------===// +#include "clang/Tooling/Core/Replacement.h" +#include <iterator> #include "clang/Basic/Diagnostic.h" #include "clang/Basic/DiagnosticIDs.h" #include "clang/Basic/DiagnosticOptions.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" +#include "clang/Format/Format.h" #include "clang/Lex/Lexer.h" #include "clang/Rewrite/Core/Rewriter.h" -#include "clang/Tooling/Core/Replacement.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_os_ostream.h" @@ -281,6 +283,63 @@ return Result; } +tooling::Replacements +formatReplacements(const format::FormatStyle &Style, StringRef Code, + const tooling::Replacements &Replaces) { + std::string NewCode = applyAllReplacements(Code, Replaces); + + std::vector<tooling::Range> ChangedRanges = calculateChangedRanges(Replaces); + StringRef FileName = Replaces.begin()->getFilePath(); + tooling::Replacements FormatReplaces = + format::reformat(Style, NewCode, ChangedRanges, FileName); + + tooling::Replacements MergedReplacements = + mergeReplacements(Replaces, FormatReplaces); + return MergedReplacements; +} + +std::vector<Range> calculateChangedRanges(const Replacements &Replaces) { + std::vector<Range> ChangedRanges; + + // Generate the new ranges from the replacements. + int Shift = 0; + for (const tooling::Replacement &R : Replaces) { + unsigned Offset = R.getOffset() + Shift; + unsigned Length = R.getReplacementText().size(); + Shift += Length - R.getLength(); + ChangedRanges.push_back(tooling::Range(Offset, Length)); + } + return ChangedRanges; +} + +bool applyAllReplacementsAndFormat(const format::FormatStyle &Style, + const Replacements &Replaces, + Rewriter &Rewrite) { + SourceManager &SM = Rewrite.getSourceMgr(); + FileManager &Files = SM.getFileManager(); + StringRef FileName = Replaces.begin()->getFilePath(); + const clang::FileEntry *Entry = Files.getFile(FileName); + assert(Entry && "Expected an existing file"); + SourceLocation Location = SM.translateFileLineCol(Entry, 1, 1); + FileID ID = Location.isValid() + ? SM.getFileID(Location) + : SM.createFileID(Entry, SourceLocation(), SrcMgr::C_User); + assert(ID.isValid() && "Expected a valid FileID"); + + StringRef Code = SM.getBufferData(ID); + Replacements NewReplacements = formatReplacements(Style, Code, Replaces); + return applyAllReplacements(NewReplacements, Rewrite); +} + +std::string applyAllReplacementsAndFormat(const format::FormatStyle &Style, + StringRef Code, + const Replacements &Replaces) { + if (Replaces.empty()) return Code; + + Replacements NewReplacements = formatReplacements(Style, Code, Replaces); + return applyAllReplacements(Code, NewReplacements); +} + namespace { // Represents a merged replacement, i.e. a replacement consisting of multiple // overlapping replacements from 'First' and 'Second' in mergeReplacements. @@ -314,7 +373,7 @@ // Merges the next element 'R' into this merged element. As we always merge // from 'First' into 'Second' or vice versa, the MergedReplacement knows what - // set the next element is coming from. + // set the next element is coming from. void merge(const Replacement &R) { if (MergeSecond) { unsigned REnd = R.getOffset() + Delta + R.getLength(); Index: include/clang/Tooling/Core/Replacement.h =================================================================== --- include/clang/Tooling/Core/Replacement.h +++ include/clang/Tooling/Core/Replacement.h @@ -30,6 +30,10 @@ class Rewriter; +namespace format { +struct FormatStyle; +} // namespace format + namespace tooling { /// \brief A source range independent of the \c SourceManager. @@ -220,6 +224,51 @@ /// replacements cannot be applied, this returns an empty \c string. std::string applyAllReplacements(StringRef Code, const Replacements &Replaces); +/// \brief Applies all replacements in \p Replaces to \p Code. +/// +/// This completely ignores the path stored in each replacement. If one or more +/// replacements cannot be applied, this returns an empty \c string. +std::string applyAllReplacements(StringRef Code, + const std::vector<Replacements> &Replaces); + +/// \brief Calculate the ranges that are affected by the Replacements. +std::vector<tooling::Range> +calculateChangedRanges(const tooling::Replacements &Replaces); + +/// \brief Return replacements that are merged from orginal replacements +/// and the replacements for formatting the code after applying the orginal +/// replacements. +tooling::Replacements formatReplacements(const format::FormatStyle &Style, + StringRef Code, + const tooling::Replacements &Replaces); + +/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite and +/// format the code after replacements. +/// +/// \pre Replacements must be for the same file and conflict-free. +/// +/// Replacement applications happen independently of the success of +/// other applications. +/// +/// \returns true if all replacements apply and code is fixed. false otherwise. +bool applyAllReplacementsAndFormat(const format::FormatStyle &Style, + const Replacements &Replaces, + Rewriter &Rewrite); + +/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite and +/// format the code after replacements. +/// +/// \pre Replacements must be for the same file and conflict-free. +/// +/// Replacement applications happen independently of the success of +/// other applications. +/// +/// \returns the changed code if all replacements apply and code is fixed. +/// empty string otherwise. +std::string applyAllReplacementsAndFormat(const format::FormatStyle &Style, + StringRef Code, + const Replacements &Replaces); + /// \brief Merges two sets of replacements with the second set referring to the /// code after applying the first set. Within both 'First' and 'Second', /// replacements must not overlap.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits