r278004 - Fixes calculateRangesAfterReplacements crash when Replacements is empty.

Eric Liu via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 8 06:37:39 PDT 2016


Author: ioeric
Date: Mon Aug  8 08:37:39 2016
New Revision: 278004

URL: http://llvm.org/viewvc/llvm-project?rev=278004&view=rev
Log:
Fixes calculateRangesAfterReplacements crash when Replacements is empty.

Reviewers: klimek, djasper

Subscribers: cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D23264

Modified:
    cfe/trunk/lib/Tooling/Core/Replacement.cpp
    cfe/trunk/unittests/Tooling/RefactoringTest.cpp

Modified: cfe/trunk/lib/Tooling/Core/Replacement.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Core/Replacement.cpp?rev=278004&r1=278003&r2=278004&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Core/Replacement.cpp (original)
+++ cfe/trunk/lib/Tooling/Core/Replacement.cpp Mon Aug  8 08:37:39 2016
@@ -361,6 +361,8 @@ calculateRangesAfterReplacements(const R
   //   - Merge with \p Replaces.
   //   - The new ranges will be the affected ranges of the merged replacements.
   auto MergedRanges = combineAndSortRanges(Ranges);
+  if (Replaces.empty())
+    return MergedRanges;
   tooling::Replacements FakeReplaces;
   for (const auto &R : MergedRanges) {
     auto Err = FakeReplaces.add(Replacement(Replaces.begin()->getFilePath(),

Modified: cfe/trunk/unittests/Tooling/RefactoringTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RefactoringTest.cpp?rev=278004&r1=278003&r2=278004&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/RefactoringTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/RefactoringTest.cpp Mon Aug  8 08:37:39 2016
@@ -506,6 +506,13 @@ TEST(Range, CalculateRangesOfReplacement
   EXPECT_TRUE(Ranges[1].getLength() == 22);
 }
 
+TEST(Range, RangesAfterEmptyReplacements) {
+  std::vector<Range> Ranges = {Range(5, 6), Range(10, 5)};
+  Replacements Replaces;
+  std::vector<Range> Expected = {Range(5, 10)};
+  EXPECT_EQ(Expected, calculateRangesAfterReplacements(Replaces, Ranges));
+}
+
 TEST(Range, RangesAfterReplacements) {
   std::vector<Range> Ranges = {Range(5, 2), Range(10, 5)};
   Replacements Replaces = toReplacements({Replacement("foo", 0, 2, "1234")});




More information about the cfe-commits mailing list