From 04a21318b55756d50836f6e40f2d209f18cce417 Mon Sep 17 00:00:00 2001 From: Yitzhak Mandelbaum Date: Tue, 28 Jul 2020 17:26:12 +0000 Subject: [PATCH] [libTooling] Add a `between` range-selector combinator. 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 --- .../clang/Tooling/Transformer/RangeSelector.h | 5 ++++ clang/lib/Tooling/Transformer/Parsing.cpp | 4 +-- clang/unittests/Tooling/RangeSelectorTest.cpp | 33 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Tooling/Transformer/RangeSelector.h b/clang/include/clang/Tooling/Transformer/RangeSelector.h index 2807037..e070c0e 100644 --- a/clang/include/clang/Tooling/Transformer/RangeSelector.h +++ b/clang/include/clang/Tooling/Transformer/RangeSelector.h @@ -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); diff --git a/clang/lib/Tooling/Transformer/Parsing.cpp b/clang/lib/Tooling/Transformer/Parsing.cpp index 1579115..fb5fd4a 100644 --- a/clang/lib/Tooling/Transformer/Parsing.cpp +++ b/clang/lib/Tooling/Transformer/Parsing.cpp @@ -109,14 +109,14 @@ getUnaryRangeSelectors() { static const llvm::StringMap> & getBinaryStringSelectors() { static const llvm::StringMap> M = { - {"encloseNodes", range}}; + {"encloseNodes", encloseNodes}}; return M; } static const llvm::StringMap> & getBinaryRangeSelectors() { static const llvm::StringMap> - M = {{"enclose", range}}; + M = {{"enclose", enclose}, {"between", between}}; return M; } diff --git a/clang/unittests/Tooling/RangeSelectorTest.cpp b/clang/unittests/Tooling/RangeSelectorTest.cpp index e2d7723..64ddee7 100644 --- a/clang/unittests/Tooling/RangeSelectorTest.cpp +++ b/clang/unittests/Tooling/RangeSelectorTest.cpp @@ -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); } @@ -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); } @@ -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); } @@ -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); } -- 2.7.4