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