1207753Smm//===--- RangeSelector.h - Source-selection library ---------*- C++ -*-===//
2207753Smm//
3207753Smm// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4207753Smm// See https://llvm.org/LICENSE.txt for license information.
5207753Smm// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6207753Smm//
7207753Smm//===----------------------------------------------------------------------===//
8207753Smm///
9207753Smm///  \file
10207753Smm///  Defines a combinator library supporting the definition of _selectors_,
11207753Smm///  which select source ranges based on (bound) AST nodes.
12207753Smm///
13207753Smm//===----------------------------------------------------------------------===//
14207753Smm
15207753Smm#ifndef LLVM_CLANG_TOOLING_TRANSFORMER_RANGESELECTOR_H
16207753Smm#define LLVM_CLANG_TOOLING_TRANSFORMER_RANGESELECTOR_H
17207753Smm
18207753Smm#include "clang/ASTMatchers/ASTMatchFinder.h"
19207753Smm#include "clang/Basic/SourceLocation.h"
20207753Smm#include "clang/Tooling/Transformer/MatchConsumer.h"
21207753Smm#include "llvm/Support/Error.h"
22207753Smm#include <functional>
23207753Smm#include <string>
24207753Smm
25207753Smmnamespace clang {
26207753Smmnamespace transformer {
27207753Smmusing RangeSelector = MatchConsumer<CharSourceRange>;
28207753Smm
29207753Smminline RangeSelector charRange(CharSourceRange R) {
30207753Smm  return [R](const ast_matchers::MatchFinder::MatchResult &)
31207753Smm             -> Expected<CharSourceRange> { return R; };
32207753Smm}
33207753Smm
34207753Smm/// Selects from the start of \p Begin and to the end of \p End.
35207753SmmRangeSelector enclose(RangeSelector Begin, RangeSelector End);
36207753Smm
37207753Smm/// Convenience version of \c range where end-points are bound nodes.
38207753SmmRangeSelector encloseNodes(std::string BeginID, std::string EndID);
39207753Smm
40207753Smm/// DEPRECATED. Use `enclose`.
41207753Smminline RangeSelector range(RangeSelector Begin, RangeSelector End) {
42207753Smm  return enclose(std::move(Begin), std::move(End));
43207753Smm}
44207753Smm
45207753Smm/// DEPRECATED. Use `encloseNodes`.
46223935Smminline RangeSelector range(std::string BeginID, std::string EndID) {
47207753Smm  return encloseNodes(std::move(BeginID), std::move(EndID));
48207753Smm}
49207753Smm
50207753Smm/// Selects the (empty) range [B,B) when \p Selector selects the range [B,E).
51207753SmmRangeSelector before(RangeSelector Selector);
52207753Smm
53207753Smm/// Selects the point immediately following \p Selector. That is, the
54207753Smm/// (empty) range [E,E), when \p Selector selects either
55223935Smm/// * the CharRange [B,E) or
56207753Smm/// * the TokenRange [B,E'] where the token at E' spans the range [E',E).
57207753SmmRangeSelector after(RangeSelector Selector);
58207753Smm
59207753Smm/// Selects the range between `R1` and `R2.
60207753Smminline RangeSelector between(RangeSelector R1, RangeSelector R2) {
61207753Smm  return enclose(after(std::move(R1)), before(std::move(R2)));
62207753Smm}
63207753Smm
64207753Smm/// Selects a node, including trailing semicolon, if any (for declarations and
65207753Smm/// non-expression statements). \p ID is the node's binding in the match result.
66207753SmmRangeSelector node(std::string ID);
67207753Smm
68207753Smm/// Selects a node, including trailing semicolon (always). Useful for selecting
69207753Smm/// expression statements. \p ID is the node's binding in the match result.
70207753SmmRangeSelector statement(std::string ID);
71207753Smm
72207753Smm/// Given a \c MemberExpr, selects the member token. \p ID is the node's
73223935Smm/// binding in the match result.
74207753SmmRangeSelector member(std::string ID);
75207753Smm
76207753Smm/// Given a node with a "name", (like \c NamedDecl, \c DeclRefExpr, \c
77207753Smm/// CxxCtorInitializer, and \c TypeLoc) selects the name's token.  Only selects
78207753Smm/// the final identifier of a qualified name, but not any qualifiers or template
79207753Smm/// arguments.  For example, for `::foo::bar::baz` and `::foo::bar::baz<int>`,
80207753Smm/// it selects only `baz`.
81207753Smm///
82207753Smm/// \param ID is the node's binding in the match result.
83207753SmmRangeSelector name(std::string ID);
84207753Smm
85207753Smm// Given a \c CallExpr (bound to \p ID), selects the arguments' source text (all
86207753Smm// source between the call's parentheses).
87207753SmmRangeSelector callArgs(std::string ID);
88207753Smm
89207753Smm// Given a \c CompoundStmt (bound to \p ID), selects the source of the
90207753Smm// statements (all source between the braces).
91207753SmmRangeSelector statements(std::string ID);
92207753Smm
93207753Smm// Given a \c InitListExpr (bound to \p ID), selects the range of the elements
94207753Smm// (all source between the braces).
95207753SmmRangeSelector initListElements(std::string ID);
96207753Smm
97207753Smm/// Given an \IfStmt (bound to \p ID), selects the range of the else branch,
98207753Smm/// starting from the \c else keyword.
99207753SmmRangeSelector elseBranch(std::string ID);
100207753Smm
101207753Smm/// Selects the range from which `S` was expanded (possibly along with other
102207753Smm/// source), if `S` is an expansion, and `S` itself, otherwise.  Corresponds to
103207753Smm/// `SourceManager::getExpansionRange`.
104207753SmmRangeSelector expansion(RangeSelector S);
105207753Smm} // namespace transformer
106207753Smm} // namespace clang
107207753Smm
108207753Smm#endif // LLVM_CLANG_TOOLING_TRANSFORMER_RANGESELECTOR_H
109207753Smm