From: Edwin Vane Date: Tue, 13 Aug 2013 17:38:19 +0000 (+0000) Subject: Adding a vector version of tooling::applyAllReplacements X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b58cfd93c49727c1d471ce71901502df824556c7;p=clang Adding a vector version of tooling::applyAllReplacements One day soon, tooling::Replacements will be changed from being implemented as an std::set to being implemented as an std::vector. Until then, some new code using vectors of Replacements would enjoy having a version of applyAllReplacements that takes a vector. Differential Revision: http://llvm-reviews.chandlerc.com/D1380 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@188295 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Tooling/Refactoring.h b/include/clang/Tooling/Refactoring.h index a976146a77..efac016b19 100644 --- a/include/clang/Tooling/Refactoring.h +++ b/include/clang/Tooling/Refactoring.h @@ -147,6 +147,15 @@ typedef std::set Replacements; /// \returns true if all replacements apply. false otherwise. bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite); +/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite. +/// +/// Replacement applications happen independently of the success of +/// other applications. +/// +/// \returns true if all replacements apply. false otherwise. +bool applyAllReplacements(const std::vector &Replaces, + Rewriter &Rewrite); + /// \brief Applies all replacements in \p Replaces to \p Code. /// /// This completely ignores the path stored in each replacement. If one or more diff --git a/lib/Tooling/Refactoring.cpp b/lib/Tooling/Refactoring.cpp index 52daaebfd2..bc94a2b2c1 100644 --- a/lib/Tooling/Refactoring.cpp +++ b/lib/Tooling/Refactoring.cpp @@ -143,6 +143,23 @@ bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) { return Result; } +// FIXME: Remove this function when Replacements is implemented as std::vector +// instead of std::set. +bool applyAllReplacements(const std::vector &Replaces, + Rewriter &Rewrite) { + bool Result = true; + for (std::vector::const_iterator I = Replaces.begin(), + E = Replaces.end(); + I != E; ++I) { + if (I->isApplicable()) { + Result = I->apply(Rewrite) && Result; + } else { + Result = false; + } + } + return Result; +} + std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) { FileManager Files((FileSystemOptions())); DiagnosticsEngine Diagnostics( diff --git a/unittests/Tooling/RefactoringTest.cpp b/unittests/Tooling/RefactoringTest.cpp index d9f023d965..81f9f040ae 100644 --- a/unittests/Tooling/RefactoringTest.cpp +++ b/unittests/Tooling/RefactoringTest.cpp @@ -120,6 +120,21 @@ TEST_F(ReplacementTest, CanApplyReplacements) { EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID)); } +// FIXME: Remove this test case when Replacements is implemented as std::vector +// instead of std::set. The other ReplacementTest tests will need to be updated +// at that point as well. +TEST_F(ReplacementTest, VectorCanApplyReplacements) { + FileID ID = Context.createInMemoryFile("input.cpp", + "line1\nline2\nline3\nline4"); + std::vector Replaces; + Replaces.push_back(Replacement(Context.Sources, Context.getLocation(ID, 2, 1), + 5, "replaced")); + Replaces.push_back( + Replacement(Context.Sources, Context.getLocation(ID, 3, 1), 5, "other")); + EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite)); + EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID)); +} + TEST_F(ReplacementTest, SkipsDuplicateReplacements) { FileID ID = Context.createInMemoryFile("input.cpp", "line1\nline2\nline3\nline4");