[PATCH] Add contains() and overlaps() predicates to tooling::Range.
Guillaume Papin
guillaume.papin at epitech.eu
Fri Jul 19 05:09:41 PDT 2013
Rename overlaps() to overlapsWith().
Hi klimek,
http://llvm-reviews.chandlerc.com/D1184
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D1184?vs=2906&id=2908#toc
BRANCH
range-predicates
ARCANIST PROJECT
clang
Files:
include/clang/Tooling/Refactoring.h
unittests/Tooling/RefactoringTest.cpp
Index: include/clang/Tooling/Refactoring.h
===================================================================
--- include/clang/Tooling/Refactoring.h
+++ include/clang/Tooling/Refactoring.h
@@ -38,8 +38,27 @@
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: unittests/Tooling/RefactoringTest.cpp
===================================================================
--- unittests/Tooling/RefactoringTest.cpp
+++ unittests/Tooling/RefactoringTest.cpp
@@ -332,5 +332,20 @@
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1184.3.patch
Type: text/x-patch
Size: 1877 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130719/614f6c39/attachment.bin>
More information about the cfe-commits
mailing list