summaryrefslogtreecommitdiffstats
path: root/clang/lib/Tooling/Refactoring/RangeSelector.cpp
diff options
context:
space:
mode:
authorYitzhak Mandelbaum <yitzhakm@google.com>2019-09-20 17:11:03 +0000
committerYitzhak Mandelbaum <yitzhakm@google.com>2019-09-20 17:11:03 +0000
commiteff88e42f7860ac38cccafd175f5feca4d89c7c3 (patch)
treee1258524ae2832ab6890abdabc2aca20afa175f3 /clang/lib/Tooling/Refactoring/RangeSelector.cpp
parent1b7b4b467f03322f37b20ccee5cdef0c9ecec5d4 (diff)
downloadbcm5719-llvm-eff88e42f7860ac38cccafd175f5feca4d89c7c3.tar.gz
bcm5719-llvm-eff88e42f7860ac38cccafd175f5feca4d89c7c3.zip
[libTooling] Add `ifBound`, `elseBranch` RangeSelector combinators.
Summary: Adds two new combinators and corresponding tests to the RangeSelector library. * `ifBound` -- conditional evaluation of range-selectors, based on whether a given node id is bound in the match. * `elseBranch` -- selects the source range of the else and its statement. Reviewers: gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D67621 llvm-svn: 372410
Diffstat (limited to 'clang/lib/Tooling/Refactoring/RangeSelector.cpp')
-rw-r--r--clang/lib/Tooling/Refactoring/RangeSelector.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/clang/lib/Tooling/Refactoring/RangeSelector.cpp b/clang/lib/Tooling/Refactoring/RangeSelector.cpp
index 768c02e2277..ae55698189c 100644
--- a/clang/lib/Tooling/Refactoring/RangeSelector.cpp
+++ b/clang/lib/Tooling/Refactoring/RangeSelector.cpp
@@ -219,6 +219,9 @@ RangeSelector tooling::name(std::string ID) {
}
namespace {
+// FIXME: make this available in the public API for users to easily create their
+// own selectors.
+
// Creates a selector from a range-selection function \p Func, which selects a
// range that is relative to a bound node id. \c T is the node type expected by
// \p Func.
@@ -286,6 +289,19 @@ RangeSelector tooling::initListElements(std::string ID) {
return RelativeSelector<InitListExpr, getElementsRange>(std::move(ID));
}
+namespace {
+// Returns the range of the else branch, including the `else` keyword.
+CharSourceRange getElseRange(const MatchResult &Result, const IfStmt &S) {
+ return maybeExtendRange(
+ CharSourceRange::getTokenRange(S.getElseLoc(), S.getEndLoc()),
+ tok::TokenKind::semi, *Result.Context);
+}
+} // namespace
+
+RangeSelector tooling::elseBranch(std::string ID) {
+ return RelativeSelector<IfStmt, getElseRange>(std::move(ID));
+}
+
RangeSelector tooling::expansion(RangeSelector S) {
return [S](const MatchResult &Result) -> Expected<CharSourceRange> {
Expected<CharSourceRange> SRange = S(Result);
@@ -294,3 +310,11 @@ RangeSelector tooling::expansion(RangeSelector S) {
return Result.SourceManager->getExpansionRange(*SRange);
};
}
+
+RangeSelector tooling::ifBound(std::string ID, RangeSelector TrueSelector,
+ RangeSelector FalseSelector) {
+ return [=](const MatchResult &Result) {
+ auto &Map = Result.Nodes.getMap();
+ return (Map.find(ID) != Map.end() ? TrueSelector : FalseSelector)(Result);
+ };
+}
OpenPOWER on IntegriCloud