Skip to content

Commit

Permalink
[libTooling] Add a between range-selector combinator.
Browse files Browse the repository at this point in the history
Adds the `between` combinator and registers it with the parser. As a driveby, updates some deprecated names to their current versions.

Reviewed By: gribozavr2

Differential Revision: https://reviews.llvm.org/D84315
  • Loading branch information
ymand committed Jul 28, 2020
1 parent 4887495 commit 04a2131
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions clang/include/clang/Tooling/Transformer/RangeSelector.h
Expand Up @@ -56,6 +56,11 @@ RangeSelector before(RangeSelector Selector);
/// * the TokenRange [B,E'] where the token at E' spans the range [E',E).
RangeSelector after(RangeSelector Selector);

/// Selects the range between `R1` and `R2.
inline RangeSelector between(RangeSelector R1, RangeSelector R2) {
return enclose(after(std::move(R1)), before(std::move(R2)));
}

/// Selects a node, including trailing semicolon (for non-expression
/// statements). \p ID is the node's binding in the match result.
RangeSelector node(std::string ID);
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Tooling/Transformer/Parsing.cpp
Expand Up @@ -109,14 +109,14 @@ getUnaryRangeSelectors() {
static const llvm::StringMap<RangeSelectorOp<std::string, std::string>> &
getBinaryStringSelectors() {
static const llvm::StringMap<RangeSelectorOp<std::string, std::string>> M = {
{"encloseNodes", range}};
{"encloseNodes", encloseNodes}};
return M;
}

static const llvm::StringMap<RangeSelectorOp<RangeSelector, RangeSelector>> &
getBinaryRangeSelectors() {
static const llvm::StringMap<RangeSelectorOp<RangeSelector, RangeSelector>>
M = {{"enclose", range}};
M = {{"enclose", enclose}, {"between", between}};
return M;
}

Expand Down
33 changes: 29 additions & 4 deletions clang/unittests/Tooling/RangeSelectorTest.cpp
Expand Up @@ -193,8 +193,33 @@ TEST(RangeSelectorTest, AfterOp) {
HasValue(EqualsCharSourceRange(ExpectedAfter)));
}

TEST(RangeSelectorTest, BetweenOp) {
StringRef Code = R"cc(
int f(int x, int y, int z) { return 3; }
int g() { return f(3, /* comment */ 7 /* comment */, 9); }
)cc";
auto Matcher = callExpr(hasArgument(0, expr().bind("a0")),
hasArgument(1, expr().bind("a1")));
RangeSelector R = between(node("a0"), node("a1"));
TestMatch Match = matchCode(Code, Matcher);
EXPECT_THAT_EXPECTED(select(R, Match), HasValue(", /* comment */ "));
}

TEST(RangeSelectorTest, BetweenOpParsed) {
StringRef Code = R"cc(
int f(int x, int y, int z) { return 3; }
int g() { return f(3, /* comment */ 7 /* comment */, 9); }
)cc";
auto Matcher = callExpr(hasArgument(0, expr().bind("a0")),
hasArgument(1, expr().bind("a1")));
auto R = parseRangeSelector(R"rs(between(node("a0"), node("a1")))rs");
ASSERT_THAT_EXPECTED(R, llvm::Succeeded());
TestMatch Match = matchCode(Code, Matcher);
EXPECT_THAT_EXPECTED(select(*R, Match), HasValue(", /* comment */ "));
}

// Node-id specific version.
TEST(RangeSelectorTest, RangeOpNodes) {
TEST(RangeSelectorTest, EncloseOpNodes) {
StringRef Code = R"cc(
int f(int x, int y, int z) { return 3; }
int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
Expand All @@ -206,7 +231,7 @@ TEST(RangeSelectorTest, RangeOpNodes) {
EXPECT_THAT_EXPECTED(select(R, Match), HasValue("3, 7"));
}

TEST(RangeSelectorTest, RangeOpGeneral) {
TEST(RangeSelectorTest, EncloseOpGeneral) {
StringRef Code = R"cc(
int f(int x, int y, int z) { return 3; }
int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
Expand All @@ -218,7 +243,7 @@ TEST(RangeSelectorTest, RangeOpGeneral) {
EXPECT_THAT_EXPECTED(select(R, Match), HasValue("3, 7"));
}

TEST(RangeSelectorTest, RangeOpNodesParsed) {
TEST(RangeSelectorTest, EncloseOpNodesParsed) {
StringRef Code = R"cc(
int f(int x, int y, int z) { return 3; }
int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
Expand All @@ -231,7 +256,7 @@ TEST(RangeSelectorTest, RangeOpNodesParsed) {
EXPECT_THAT_EXPECTED(select(*R, Match), HasValue("3, 7"));
}

TEST(RangeSelectorTest, RangeOpGeneralParsed) {
TEST(RangeSelectorTest, EncloseOpGeneralParsed) {
StringRef Code = R"cc(
int f(int x, int y, int z) { return 3; }
int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
Expand Down

0 comments on commit 04a2131

Please sign in to comment.