Adds overlapsWith and contains predicates on tooling::Range.

Patch by Guillaume Papin.

llvm-svn: 186670
This commit is contained in:
Manuel Klimek 2013-07-19 12:12:36 +00:00
parent ba46fc01a8
commit dce2347f6d
2 changed files with 34 additions and 0 deletions

View 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;

View 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