RangeSelector.h revision 356998
1169691Skan//===--- RangeSelector.h - Source-selection library ---------*- C++ -*-===// 2169691Skan// 3169691Skan// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4169691Skan// See https://llvm.org/LICENSE.txt for license information. 5169691Skan// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6169691Skan// 7169691Skan//===----------------------------------------------------------------------===// 8169691Skan/// 9169691Skan/// \file 10169691Skan/// Defines a combinator library supporting the definition of _selectors_, 11169691Skan/// which select source ranges based on (bound) AST nodes. 12169691Skan/// 13169691Skan//===----------------------------------------------------------------------===// 14169691Skan 15169691Skan#ifndef LLVM_CLANG_TOOLING_REFACTOR_RANGE_SELECTOR_H_ 16169691Skan#define LLVM_CLANG_TOOLING_REFACTOR_RANGE_SELECTOR_H_ 17169691Skan 18169691Skan#include "clang/ASTMatchers/ASTMatchFinder.h" 19169691Skan#include "clang/Basic/SourceLocation.h" 20169691Skan#include "clang/Tooling/Transformer/MatchConsumer.h" 21169691Skan#include "llvm/Support/Error.h" 22169691Skan#include <functional> 23169691Skan#include <string> 24169691Skan 25169691Skannamespace clang { 26169691Skannamespace transformer { 27169691Skanusing RangeSelector = MatchConsumer<CharSourceRange>; 28169691Skan 29169691Skaninline RangeSelector charRange(CharSourceRange R) { 30169691Skan return [R](const ast_matchers::MatchFinder::MatchResult &) 31169691Skan -> Expected<CharSourceRange> { return R; }; 32169691Skan} 33169691Skan 34169691Skan/// Selects from the start of \p Begin and to the end of \p End. 35169691SkanRangeSelector range(RangeSelector Begin, RangeSelector End); 36169691Skan 37169691Skan/// Convenience version of \c range where end-points are bound nodes. 38169691SkanRangeSelector range(std::string BeginID, std::string EndID); 39169691Skan 40169691Skan/// Selects the (empty) range [B,B) when \p Selector selects the range [B,E). 41169691SkanRangeSelector before(RangeSelector Selector); 42169691Skan 43169691Skan/// Selects the the point immediately following \p Selector. That is, the 44169691Skan/// (empty) range [E,E), when \p Selector selects either 45169691Skan/// * the CharRange [B,E) or 46169691Skan/// * the TokenRange [B,E'] where the token at E' spans the range [E,E'). 47169691SkanRangeSelector after(RangeSelector Selector); 48169691Skan 49169691Skan/// Selects a node, including trailing semicolon (for non-expression 50169691Skan/// statements). \p ID is the node's binding in the match result. 51169691SkanRangeSelector node(std::string ID); 52169691Skan 53169691Skan/// Selects a node, including trailing semicolon (always). Useful for selecting 54169691Skan/// expression statements. \p ID is the node's binding in the match result. 55169691SkanRangeSelector statement(std::string ID); 56169691Skan 57169691Skan/// Given a \c MemberExpr, selects the member token. \p ID is the node's 58169691Skan/// binding in the match result. 59169691SkanRangeSelector member(std::string ID); 60169691Skan 61169691Skan/// Given a node with a "name", (like \c NamedDecl, \c DeclRefExpr or \c 62169691Skan/// CxxCtorInitializer) selects the name's token. Only selects the final 63169691Skan/// identifier of a qualified name, but not any qualifiers or template 64169691Skan/// arguments. For example, for `::foo::bar::baz` and `::foo::bar::baz<int>`, 65169691Skan/// it selects only `baz`. 66169691Skan/// 67169691Skan/// \param ID is the node's binding in the match result. 68169691SkanRangeSelector name(std::string ID); 69169691Skan 70169691Skan// Given a \c CallExpr (bound to \p ID), selects the arguments' source text (all 71169691Skan// source between the call's parentheses). 72169691SkanRangeSelector callArgs(std::string ID); 73169691Skan 74169691Skan// Given a \c CompoundStmt (bound to \p ID), selects the source of the 75169691Skan// statements (all source between the braces). 76169691SkanRangeSelector statements(std::string ID); 77169691Skan 78169691Skan// Given a \c InitListExpr (bound to \p ID), selects the range of the elements 79169691Skan// (all source between the braces). 80169691SkanRangeSelector initListElements(std::string ID); 81169691Skan 82169691Skan/// Given an \IfStmt (bound to \p ID), selects the range of the else branch, 83169691Skan/// starting from the \c else keyword. 84169691SkanRangeSelector elseBranch(std::string ID); 85169691Skan 86169691Skan/// Selects the range from which `S` was expanded (possibly along with other 87169691Skan/// source), if `S` is an expansion, and `S` itself, otherwise. Corresponds to 88169691Skan/// `SourceManager::getExpansionRange`. 89169691SkanRangeSelector expansion(RangeSelector S); 90169691Skan} // namespace transformer 91169691Skan 92169691Skannamespace tooling { 93169691Skan// DEPRECATED: These are temporary aliases supporting client migration to the 94169691Skan// `transformer` namespace. 95169691Skanusing RangeSelector = transformer::RangeSelector; 96169691Skan 97169691Skanusing transformer::after; 98169691Skanusing transformer::before; 99169691Skanusing transformer::callArgs; 100169691Skanusing transformer::charRange; 101169691Skanusing transformer::elseBranch; 102169691Skanusing transformer::expansion; 103169691Skanusing transformer::initListElements; 104169691Skanusing transformer::member; 105169691Skanusing transformer::name; 106169691Skanusing transformer::node; 107169691Skanusing transformer::range; 108169691Skanusing transformer::statement; 109169691Skanusing transformer::statements; 110169691Skan} // namespace tooling 111169691Skan} // namespace clang 112169691Skan 113169691Skan#endif // LLVM_CLANG_TOOLING_REFACTOR_RANGE_SELECTOR_H_ 114169691Skan