1 //===--- RangeSelector.h - Source-selection library ---------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// Defines a combinator library supporting the definition of _selectors_, 11 /// which select source ranges based on (bound) AST nodes. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_TOOLING_REFACTOR_RANGE_SELECTOR_H_ 16 #define LLVM_CLANG_TOOLING_REFACTOR_RANGE_SELECTOR_H_ 17 18 #include "clang/ASTMatchers/ASTMatchFinder.h" 19 #include "clang/Basic/SourceLocation.h" 20 #include "clang/Tooling/Transformer/MatchConsumer.h" 21 #include "llvm/Support/Error.h" 22 #include <functional> 23 #include <string> 24 25 namespace clang { 26 namespace transformer { 27 using RangeSelector = MatchConsumer<CharSourceRange>; 28 29 inline RangeSelector charRange(CharSourceRange R) { 30 return [R](const ast_matchers::MatchFinder::MatchResult &) 31 -> Expected<CharSourceRange> { return R; }; 32 } 33 34 /// Selects from the start of \p Begin and to the end of \p End. 35 RangeSelector enclose(RangeSelector Begin, RangeSelector End); 36 37 /// Convenience version of \c range where end-points are bound nodes. 38 RangeSelector encloseNodes(std::string BeginID, std::string EndID); 39 40 /// DEPRECATED. Use `enclose`. 41 inline RangeSelector range(RangeSelector Begin, RangeSelector End) { 42 return enclose(std::move(Begin), std::move(End)); 43 } 44 45 /// DEPRECATED. Use `encloseNodes`. 46 inline RangeSelector range(std::string BeginID, std::string EndID) { 47 return encloseNodes(std::move(BeginID), std::move(EndID)); 48 } 49 50 /// Selects the (empty) range [B,B) when \p Selector selects the range [B,E). 51 RangeSelector before(RangeSelector Selector); 52 53 /// Selects the the point immediately following \p Selector. That is, the 54 /// (empty) range [E,E), when \p Selector selects either 55 /// * the CharRange [B,E) or 56 /// * the TokenRange [B,E'] where the token at E' spans the range [E',E). 57 RangeSelector after(RangeSelector Selector); 58 59 /// Selects a node, including trailing semicolon (for non-expression 60 /// statements). \p ID is the node's binding in the match result. 61 RangeSelector node(std::string ID); 62 63 /// Selects a node, including trailing semicolon (always). Useful for selecting 64 /// expression statements. \p ID is the node's binding in the match result. 65 RangeSelector statement(std::string ID); 66 67 /// Given a \c MemberExpr, selects the member token. \p ID is the node's 68 /// binding in the match result. 69 RangeSelector member(std::string ID); 70 71 /// Given a node with a "name", (like \c NamedDecl, \c DeclRefExpr or \c 72 /// CxxCtorInitializer) selects the name's token. Only selects the final 73 /// identifier of a qualified name, but not any qualifiers or template 74 /// arguments. For example, for `::foo::bar::baz` and `::foo::bar::baz<int>`, 75 /// it selects only `baz`. 76 /// 77 /// \param ID is the node's binding in the match result. 78 RangeSelector name(std::string ID); 79 80 // Given a \c CallExpr (bound to \p ID), selects the arguments' source text (all 81 // source between the call's parentheses). 82 RangeSelector callArgs(std::string ID); 83 84 // Given a \c CompoundStmt (bound to \p ID), selects the source of the 85 // statements (all source between the braces). 86 RangeSelector statements(std::string ID); 87 88 // Given a \c InitListExpr (bound to \p ID), selects the range of the elements 89 // (all source between the braces). 90 RangeSelector initListElements(std::string ID); 91 92 /// Given an \IfStmt (bound to \p ID), selects the range of the else branch, 93 /// starting from the \c else keyword. 94 RangeSelector elseBranch(std::string ID); 95 96 /// Selects the range from which `S` was expanded (possibly along with other 97 /// source), if `S` is an expansion, and `S` itself, otherwise. Corresponds to 98 /// `SourceManager::getExpansionRange`. 99 RangeSelector expansion(RangeSelector S); 100 } // namespace transformer 101 102 namespace tooling { 103 // DEPRECATED: These are temporary aliases supporting client migration to the 104 // `transformer` namespace. 105 using RangeSelector = transformer::RangeSelector; 106 107 using transformer::after; 108 using transformer::before; 109 using transformer::callArgs; 110 using transformer::charRange; 111 using transformer::elseBranch; 112 using transformer::expansion; 113 using transformer::initListElements; 114 using transformer::member; 115 using transformer::name; 116 using transformer::node; 117 using transformer::range; 118 using transformer::statement; 119 using transformer::statements; 120 } // namespace tooling 121 } // namespace clang 122 123 #endif // LLVM_CLANG_TOOLING_REFACTOR_RANGE_SELECTOR_H_ 124