]> granicus.if.org Git - clang/commitdiff
Adds overlapsWith and contains predicates on tooling::Range.
authorManuel Klimek <klimek@google.com>
Fri, 19 Jul 2013 12:12:36 +0000 (12:12 +0000)
committerManuel Klimek <klimek@google.com>
Fri, 19 Jul 2013 12:12:36 +0000 (12:12 +0000)
Patch by Guillaume Papin.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@186670 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/Refactoring.h
unittests/Tooling/RefactoringTest.cpp

index 0eaef95d37bdaf923dc251dd1abe2fa8046c76c2..3b171058367295ef68fa52fb1c7ef4ce7565fbd4 100644 (file)
@@ -38,8 +38,27 @@ public:
   Range() : Offset(0), Length(0) {}
   Range(unsigned Offset, unsigned Length) : Offset(Offset), Length(Length) {}
 
+  /// \brief Accessors.
+  /// @{
   unsigned getOffset() const { return Offset; }
   unsigned getLength() const { return Length; }
+  /// @}
+
+  /// \name Range Predicates
+  /// @{
+  /// \brief Whether this range overlaps with \p RHS or not.
+  bool overlapsWith(Range RHS) const {
+    if ((Offset + Length) <= RHS.Offset || Offset >= (RHS.Offset + RHS.Length))
+      return false;
+    return true;
+  }
+
+  /// \brief Whether this range contains \p RHS or not.
+  bool contains(Range RHS) const {
+    return RHS.Offset >= Offset &&
+           (RHS.Offset + RHS.Length) <= (Offset + Length);
+  }
+  /// @}
 
 private:
   unsigned Offset;
index f8d10d286edae22184ec55c6f9f2787141be6049..5741e5d22279d9d6c8b8f976381b7e23b30e7ad5 100644 (file)
@@ -332,5 +332,20 @@ TEST(Replacement, TemplatedFunctionCall) {
   expectReplacementAt(CallToF.Replace, "input.cc", 43, 8);
 }
 
+TEST(Range, overlaps) {
+  EXPECT_TRUE(Range(10, 10).overlapsWith(Range(0, 11)));
+  EXPECT_TRUE(Range(0, 11).overlapsWith(Range(10, 10)));
+  EXPECT_FALSE(Range(10, 10).overlapsWith(Range(0, 10)));
+  EXPECT_FALSE(Range(0, 10).overlapsWith(Range(10, 10)));
+  EXPECT_TRUE(Range(0, 10).overlapsWith(Range(2, 6)));
+}
+
+TEST(Range, contains) {
+  EXPECT_TRUE(Range(0, 10).contains(Range(0, 10)));
+  EXPECT_TRUE(Range(0, 10).contains(Range(2, 6)));
+  EXPECT_FALSE(Range(2, 6).contains(Range(0, 10)));
+  EXPECT_FALSE(Range(0, 10).contains(Range(0, 11)));
+}
+
 } // end namespace tooling
 } // end namespace clang