clang  13.0.0git
ASTSelection.h
Go to the documentation of this file.
1 //===--- ASTSelection.h - Clang refactoring library -----------------------===//
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 #ifndef LLVM_CLANG_TOOLING_REFACTOR_AST_SELECTION_H
10 #define LLVM_CLANG_TOOLING_REFACTOR_AST_SELECTION_H
11 
13 #include "clang/AST/Stmt.h"
14 #include "clang/Basic/LLVM.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <vector>
18 
19 namespace clang {
20 
21 class ASTContext;
22 
23 namespace tooling {
24 
25 enum class SourceSelectionKind {
26  /// A node that's not selected.
27  None,
28 
29  /// A node that's considered to be selected because the whole selection range
30  /// is inside of its source range.
32  /// A node that's considered to be selected because the start of the selection
33  /// range is inside its source range.
35  /// A node that's considered to be selected because the end of the selection
36  /// range is inside its source range.
38 
39  /// A node that's considered to be selected because the node is entirely in
40  /// the selection range.
42 };
43 
44 /// Represents a selected AST node.
45 ///
46 /// AST selection is represented using a tree of \c SelectedASTNode. The tree
47 /// follows the top-down shape of the actual AST. Each selected node has
48 /// a selection kind. The kind might be none as the node itself might not
49 /// actually be selected, e.g. a statement in macro whose child is in a macro
50 /// argument.
54  std::vector<SelectedASTNode> Children;
55 
58  SelectedASTNode(SelectedASTNode &&) = default;
60 
61  void dump(llvm::raw_ostream &OS = llvm::errs()) const;
62 
63  using ReferenceType = std::reference_wrapper<const SelectedASTNode>;
64 };
65 
66 /// Traverses the given ASTContext and creates a tree of selected AST nodes.
67 ///
68 /// \returns None if no nodes are selected in the AST, or a selected AST node
69 /// that corresponds to the TranslationUnitDecl otherwise.
71  SourceRange SelectionRange);
72 
73 /// An AST selection value that corresponds to a selection of a set of
74 /// statements that belong to one body of code (like one function).
75 ///
76 /// For example, the following selection in the source.
77 ///
78 /// \code
79 /// void function() {
80 /// // selection begin:
81 /// int x = 0;
82 /// {
83 /// // selection end
84 /// x = 1;
85 /// }
86 /// x = 2;
87 /// }
88 /// \endcode
89 ///
90 /// Would correspond to a code range selection of statements "int x = 0"
91 /// and the entire compound statement that follows it.
92 ///
93 /// A \c CodeRangeASTSelection value stores references to the full
94 /// \c SelectedASTNode tree and should not outlive it.
96 public:
99 
100  /// Returns the parent hierarchy (top to bottom) for the selected nodes.
102 
103  /// Returns the number of selected statements.
104  size_t size() const {
105  if (!AreChildrenSelected)
106  return 1;
107  return SelectedNode.get().Children.size();
108  }
109 
110  const Stmt *operator[](size_t I) const {
111  if (!AreChildrenSelected) {
112  assert(I == 0 && "Invalid index");
113  return SelectedNode.get().Node.get<Stmt>();
114  }
115  return SelectedNode.get().Children[I].Node.get<Stmt>();
116  }
117 
118  /// Returns true when a selected code range is in a function-like body
119  /// of code, like a function, method or a block.
120  ///
121  /// This function can be used to test against selected expressions that are
122  /// located outside of a function, e.g. global variable initializers, default
123  /// argument values, or even template arguments.
124  ///
125  /// Use the \c getFunctionLikeNearestParent to get the function-like parent
126  /// declaration.
127  bool isInFunctionLikeBodyOfCode() const;
128 
129  /// Returns the nearest function-like parent declaration or null if such
130  /// declaration doesn't exist.
131  const Decl *getFunctionLikeNearestParent() const;
132 
134  create(SourceRange SelectionRange, const SelectedASTNode &ASTSelection);
135 
136 private:
139  bool AreChildrenSelected)
140  : SelectedNode(SelectedNode), Parents(Parents.begin(), Parents.end()),
141  AreChildrenSelected(AreChildrenSelected) {}
142 
143  /// The reference to the selected node (or reference to the selected
144  /// child nodes).
145  SelectedASTNode::ReferenceType SelectedNode;
146  /// The parent hierarchy (top to bottom) for the selected noe.
148  /// True only when the children of the selected node are actually selected.
149  bool AreChildrenSelected;
150 };
151 
152 } // end namespace tooling
153 } // end namespace clang
154 
155 #endif // LLVM_CLANG_TOOLING_REFACTOR_AST_SELECTION_H
clang::tooling::CodeRangeASTSelection::getParents
ArrayRef< SelectedASTNode::ReferenceType > getParents()
Returns the parent hierarchy (top to bottom) for the selected nodes.
Definition: ASTSelection.h:101
clang::tooling::CodeRangeASTSelection::size
size_t size() const
Returns the number of selected statements.
Definition: ASTSelection.h:104
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:212
llvm::SmallVector< SelectedASTNode::ReferenceType, 8 >
clang::tooling::SelectedASTNode::Node
DynTypedNode Node
Definition: ASTSelection.h:52
clang::tooling::CodeRangeASTSelection::operator=
CodeRangeASTSelection & operator=(CodeRangeASTSelection &&)=default
clang::tooling::SelectedASTNode::SelectionKind
SourceSelectionKind SelectionKind
Definition: ASTSelection.h:53
llvm::Optional
Definition: LLVM.h:40
clang::tooling::SelectedASTNode::ReferenceType
std::reference_wrapper< const SelectedASTNode > ReferenceType
Definition: ASTSelection.h:63
clang::tooling::SourceSelectionKind::ContainsSelectionStart
@ ContainsSelectionStart
A node that's considered to be selected because the start of the selection range is inside its source...
clang::tooling::SourceSelectionKind::InsideSelection
@ InsideSelection
A node that's considered to be selected because the node is entirely in the selection range.
clang::tooling::CodeRangeASTSelection::CodeRangeASTSelection
CodeRangeASTSelection(CodeRangeASTSelection &&)=default
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
clang::tooling::SelectedASTNode::dump
void dump(llvm::raw_ostream &OS=llvm::errs()) const
Definition: ASTSelection.cpp:230
clang::tooling::CodeRangeASTSelection::operator[]
const Stmt * operator[](size_t I) const
Definition: ASTSelection.h:110
clang::tooling::SelectedASTNode::Children
std::vector< SelectedASTNode > Children
Definition: ASTSelection.h:54
clang::tooling::findSelectedASTNodes
Optional< SelectedASTNode > findSelectedASTNodes(const ASTContext &Context, SourceRange SelectionRange)
Traverses the given ASTContext and creates a tree of selected AST nodes.
Definition: ASTSelection.cpp:182
SourceLocation.h
clang::tooling::CodeRangeASTSelection::isInFunctionLikeBodyOfCode
bool isInFunctionLikeBodyOfCode() const
Returns true when a selected code range is in a function-like body of code, like a function,...
Definition: ASTSelection.cpp:418
clang::tooling::CodeRangeASTSelection
An AST selection value that corresponds to a selection of a set of statements that belong to one body...
Definition: ASTSelection.h:95
llvm::ArrayRef
Definition: LLVM.h:34
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::tooling::CodeRangeASTSelection::create
static Optional< CodeRangeASTSelection > create(SourceRange SelectionRange, const SelectedASTNode &ASTSelection)
Definition: ASTSelection.cpp:379
LLVM.h
clang::tooling::SelectedASTNode::SelectedASTNode
SelectedASTNode(const DynTypedNode &Node, SourceSelectionKind SelectionKind)
Definition: ASTSelection.h:56
ASTTypeTraits.h
clang::tooling::SourceSelectionKind
SourceSelectionKind
Definition: ASTSelection.h:25
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::tooling::SelectedASTNode
Represents a selected AST node.
Definition: ASTSelection.h:51
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:68
clang::tooling::SelectedASTNode::operator=
SelectedASTNode & operator=(SelectedASTNode &&)=default
clang::tooling::CodeRangeASTSelection::getFunctionLikeNearestParent
const Decl * getFunctionLikeNearestParent() const
Returns the nearest function-like parent declaration or null if such declaration doesn't exist.
Definition: ASTSelection.cpp:440
clang::DynTypedNode
A dynamically typed AST node container.
Definition: ASTTypeTraits.h:233
Stmt.h
clang::tooling::SourceSelectionKind::ContainsSelectionEnd
@ ContainsSelectionEnd
A node that's considered to be selected because the end of the selection range is inside its source r...
clang::tooling::SourceSelectionKind::None
@ None
A node that's not selected.
clang::tooling::SourceSelectionKind::ContainsSelection
@ ContainsSelection
A node that's considered to be selected because the whole selection range is inside of its source ran...