1e5dd7070Spatrick //===--- ASTSelection.cpp - Clang refactoring library ---------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick
9e5dd7070Spatrick #include "clang/Tooling/Refactoring/ASTSelection.h"
10e5dd7070Spatrick #include "clang/AST/LexicallyOrderedRecursiveASTVisitor.h"
11e5dd7070Spatrick #include "clang/Lex/Lexer.h"
12e5dd7070Spatrick #include "llvm/Support/SaveAndRestore.h"
13*12c85518Srobert #include <optional>
14e5dd7070Spatrick
15e5dd7070Spatrick using namespace clang;
16e5dd7070Spatrick using namespace tooling;
17e5dd7070Spatrick
18e5dd7070Spatrick namespace {
19e5dd7070Spatrick
getLexicalDeclRange(Decl * D,const SourceManager & SM,const LangOptions & LangOpts)20e5dd7070Spatrick CharSourceRange getLexicalDeclRange(Decl *D, const SourceManager &SM,
21e5dd7070Spatrick const LangOptions &LangOpts) {
22e5dd7070Spatrick if (!isa<ObjCImplDecl>(D))
23e5dd7070Spatrick return CharSourceRange::getTokenRange(D->getSourceRange());
24e5dd7070Spatrick // Objective-C implementation declarations end at the '@' instead of the 'end'
25e5dd7070Spatrick // keyword. Use the lexer to find the location right after 'end'.
26e5dd7070Spatrick SourceRange R = D->getSourceRange();
27e5dd7070Spatrick SourceLocation LocAfterEnd = Lexer::findLocationAfterToken(
28e5dd7070Spatrick R.getEnd(), tok::raw_identifier, SM, LangOpts,
29e5dd7070Spatrick /*SkipTrailingWhitespaceAndNewLine=*/false);
30e5dd7070Spatrick return LocAfterEnd.isValid()
31e5dd7070Spatrick ? CharSourceRange::getCharRange(R.getBegin(), LocAfterEnd)
32e5dd7070Spatrick : CharSourceRange::getTokenRange(R);
33e5dd7070Spatrick }
34e5dd7070Spatrick
35e5dd7070Spatrick /// Constructs the tree of selected AST nodes that either contain the location
36e5dd7070Spatrick /// of the cursor or overlap with the selection range.
37e5dd7070Spatrick class ASTSelectionFinder
38e5dd7070Spatrick : public LexicallyOrderedRecursiveASTVisitor<ASTSelectionFinder> {
39e5dd7070Spatrick public:
ASTSelectionFinder(SourceRange Selection,FileID TargetFile,const ASTContext & Context)40e5dd7070Spatrick ASTSelectionFinder(SourceRange Selection, FileID TargetFile,
41e5dd7070Spatrick const ASTContext &Context)
42e5dd7070Spatrick : LexicallyOrderedRecursiveASTVisitor(Context.getSourceManager()),
43e5dd7070Spatrick SelectionBegin(Selection.getBegin()),
44e5dd7070Spatrick SelectionEnd(Selection.getBegin() == Selection.getEnd()
45e5dd7070Spatrick ? SourceLocation()
46e5dd7070Spatrick : Selection.getEnd()),
47e5dd7070Spatrick TargetFile(TargetFile), Context(Context) {
48e5dd7070Spatrick // The TU decl is the root of the selected node tree.
49e5dd7070Spatrick SelectionStack.push_back(
50e5dd7070Spatrick SelectedASTNode(DynTypedNode::create(*Context.getTranslationUnitDecl()),
51e5dd7070Spatrick SourceSelectionKind::None));
52e5dd7070Spatrick }
53e5dd7070Spatrick
getSelectedASTNode()54*12c85518Srobert std::optional<SelectedASTNode> getSelectedASTNode() {
55e5dd7070Spatrick assert(SelectionStack.size() == 1 && "stack was not popped");
56e5dd7070Spatrick SelectedASTNode Result = std::move(SelectionStack.back());
57e5dd7070Spatrick SelectionStack.pop_back();
58e5dd7070Spatrick if (Result.Children.empty())
59*12c85518Srobert return std::nullopt;
60e5dd7070Spatrick return std::move(Result);
61e5dd7070Spatrick }
62e5dd7070Spatrick
TraversePseudoObjectExpr(PseudoObjectExpr * E)63e5dd7070Spatrick bool TraversePseudoObjectExpr(PseudoObjectExpr *E) {
64e5dd7070Spatrick // Avoid traversing the semantic expressions. They should be handled by
65e5dd7070Spatrick // looking through the appropriate opaque expressions in order to build
66e5dd7070Spatrick // a meaningful selection tree.
67*12c85518Srobert llvm::SaveAndRestore LookThrough(LookThroughOpaqueValueExprs, true);
68e5dd7070Spatrick return TraverseStmt(E->getSyntacticForm());
69e5dd7070Spatrick }
70e5dd7070Spatrick
TraverseOpaqueValueExpr(OpaqueValueExpr * E)71e5dd7070Spatrick bool TraverseOpaqueValueExpr(OpaqueValueExpr *E) {
72e5dd7070Spatrick if (!LookThroughOpaqueValueExprs)
73e5dd7070Spatrick return true;
74*12c85518Srobert llvm::SaveAndRestore LookThrough(LookThroughOpaqueValueExprs, false);
75e5dd7070Spatrick return TraverseStmt(E->getSourceExpr());
76e5dd7070Spatrick }
77e5dd7070Spatrick
TraverseDecl(Decl * D)78e5dd7070Spatrick bool TraverseDecl(Decl *D) {
79e5dd7070Spatrick if (isa<TranslationUnitDecl>(D))
80e5dd7070Spatrick return LexicallyOrderedRecursiveASTVisitor::TraverseDecl(D);
81e5dd7070Spatrick if (D->isImplicit())
82e5dd7070Spatrick return true;
83e5dd7070Spatrick
84e5dd7070Spatrick // Check if this declaration is written in the file of interest.
85e5dd7070Spatrick const SourceRange DeclRange = D->getSourceRange();
86e5dd7070Spatrick const SourceManager &SM = Context.getSourceManager();
87e5dd7070Spatrick SourceLocation FileLoc;
88e5dd7070Spatrick if (DeclRange.getBegin().isMacroID() && !DeclRange.getEnd().isMacroID())
89e5dd7070Spatrick FileLoc = DeclRange.getEnd();
90e5dd7070Spatrick else
91e5dd7070Spatrick FileLoc = SM.getSpellingLoc(DeclRange.getBegin());
92e5dd7070Spatrick if (SM.getFileID(FileLoc) != TargetFile)
93e5dd7070Spatrick return true;
94e5dd7070Spatrick
95e5dd7070Spatrick SourceSelectionKind SelectionKind =
96e5dd7070Spatrick selectionKindFor(getLexicalDeclRange(D, SM, Context.getLangOpts()));
97e5dd7070Spatrick SelectionStack.push_back(
98e5dd7070Spatrick SelectedASTNode(DynTypedNode::create(*D), SelectionKind));
99e5dd7070Spatrick LexicallyOrderedRecursiveASTVisitor::TraverseDecl(D);
100e5dd7070Spatrick popAndAddToSelectionIfSelected(SelectionKind);
101e5dd7070Spatrick
102e5dd7070Spatrick if (DeclRange.getEnd().isValid() &&
103e5dd7070Spatrick SM.isBeforeInTranslationUnit(SelectionEnd.isValid() ? SelectionEnd
104e5dd7070Spatrick : SelectionBegin,
105e5dd7070Spatrick DeclRange.getEnd())) {
106e5dd7070Spatrick // Stop early when we've reached a declaration after the selection.
107e5dd7070Spatrick return false;
108e5dd7070Spatrick }
109e5dd7070Spatrick return true;
110e5dd7070Spatrick }
111e5dd7070Spatrick
TraverseStmt(Stmt * S)112e5dd7070Spatrick bool TraverseStmt(Stmt *S) {
113e5dd7070Spatrick if (!S)
114e5dd7070Spatrick return true;
115e5dd7070Spatrick if (auto *Opaque = dyn_cast<OpaqueValueExpr>(S))
116e5dd7070Spatrick return TraverseOpaqueValueExpr(Opaque);
117e5dd7070Spatrick // Avoid selecting implicit 'this' expressions.
118e5dd7070Spatrick if (auto *TE = dyn_cast<CXXThisExpr>(S)) {
119e5dd7070Spatrick if (TE->isImplicit())
120e5dd7070Spatrick return true;
121e5dd7070Spatrick }
122e5dd7070Spatrick // FIXME (Alex Lorenz): Improve handling for macro locations.
123e5dd7070Spatrick SourceSelectionKind SelectionKind =
124e5dd7070Spatrick selectionKindFor(CharSourceRange::getTokenRange(S->getSourceRange()));
125e5dd7070Spatrick SelectionStack.push_back(
126e5dd7070Spatrick SelectedASTNode(DynTypedNode::create(*S), SelectionKind));
127e5dd7070Spatrick LexicallyOrderedRecursiveASTVisitor::TraverseStmt(S);
128e5dd7070Spatrick popAndAddToSelectionIfSelected(SelectionKind);
129e5dd7070Spatrick return true;
130e5dd7070Spatrick }
131e5dd7070Spatrick
132e5dd7070Spatrick private:
popAndAddToSelectionIfSelected(SourceSelectionKind SelectionKind)133e5dd7070Spatrick void popAndAddToSelectionIfSelected(SourceSelectionKind SelectionKind) {
134e5dd7070Spatrick SelectedASTNode Node = std::move(SelectionStack.back());
135e5dd7070Spatrick SelectionStack.pop_back();
136e5dd7070Spatrick if (SelectionKind != SourceSelectionKind::None || !Node.Children.empty())
137e5dd7070Spatrick SelectionStack.back().Children.push_back(std::move(Node));
138e5dd7070Spatrick }
139e5dd7070Spatrick
selectionKindFor(CharSourceRange Range)140e5dd7070Spatrick SourceSelectionKind selectionKindFor(CharSourceRange Range) {
141e5dd7070Spatrick SourceLocation End = Range.getEnd();
142e5dd7070Spatrick const SourceManager &SM = Context.getSourceManager();
143e5dd7070Spatrick if (Range.isTokenRange())
144e5dd7070Spatrick End = Lexer::getLocForEndOfToken(End, 0, SM, Context.getLangOpts());
145e5dd7070Spatrick if (!SourceLocation::isPairOfFileLocations(Range.getBegin(), End))
146e5dd7070Spatrick return SourceSelectionKind::None;
147e5dd7070Spatrick if (!SelectionEnd.isValid()) {
148e5dd7070Spatrick // Do a quick check when the selection is of length 0.
149e5dd7070Spatrick if (SM.isPointWithin(SelectionBegin, Range.getBegin(), End))
150e5dd7070Spatrick return SourceSelectionKind::ContainsSelection;
151e5dd7070Spatrick return SourceSelectionKind::None;
152e5dd7070Spatrick }
153e5dd7070Spatrick bool HasStart = SM.isPointWithin(SelectionBegin, Range.getBegin(), End);
154e5dd7070Spatrick bool HasEnd = SM.isPointWithin(SelectionEnd, Range.getBegin(), End);
155e5dd7070Spatrick if (HasStart && HasEnd)
156e5dd7070Spatrick return SourceSelectionKind::ContainsSelection;
157e5dd7070Spatrick if (SM.isPointWithin(Range.getBegin(), SelectionBegin, SelectionEnd) &&
158e5dd7070Spatrick SM.isPointWithin(End, SelectionBegin, SelectionEnd))
159e5dd7070Spatrick return SourceSelectionKind::InsideSelection;
160e5dd7070Spatrick // Ensure there's at least some overlap with the 'start'/'end' selection
161e5dd7070Spatrick // types.
162e5dd7070Spatrick if (HasStart && SelectionBegin != End)
163e5dd7070Spatrick return SourceSelectionKind::ContainsSelectionStart;
164e5dd7070Spatrick if (HasEnd && SelectionEnd != Range.getBegin())
165e5dd7070Spatrick return SourceSelectionKind::ContainsSelectionEnd;
166e5dd7070Spatrick
167e5dd7070Spatrick return SourceSelectionKind::None;
168e5dd7070Spatrick }
169e5dd7070Spatrick
170e5dd7070Spatrick const SourceLocation SelectionBegin, SelectionEnd;
171e5dd7070Spatrick FileID TargetFile;
172e5dd7070Spatrick const ASTContext &Context;
173e5dd7070Spatrick std::vector<SelectedASTNode> SelectionStack;
174e5dd7070Spatrick /// Controls whether we can traverse through the OpaqueValueExpr. This is
175e5dd7070Spatrick /// typically enabled during the traversal of syntactic form for
176e5dd7070Spatrick /// PseudoObjectExprs.
177e5dd7070Spatrick bool LookThroughOpaqueValueExprs = false;
178e5dd7070Spatrick };
179e5dd7070Spatrick
180e5dd7070Spatrick } // end anonymous namespace
181e5dd7070Spatrick
182*12c85518Srobert std::optional<SelectedASTNode>
findSelectedASTNodes(const ASTContext & Context,SourceRange SelectionRange)183e5dd7070Spatrick clang::tooling::findSelectedASTNodes(const ASTContext &Context,
184e5dd7070Spatrick SourceRange SelectionRange) {
185e5dd7070Spatrick assert(SelectionRange.isValid() &&
186e5dd7070Spatrick SourceLocation::isPairOfFileLocations(SelectionRange.getBegin(),
187e5dd7070Spatrick SelectionRange.getEnd()) &&
188e5dd7070Spatrick "Expected a file range");
189e5dd7070Spatrick FileID TargetFile =
190e5dd7070Spatrick Context.getSourceManager().getFileID(SelectionRange.getBegin());
191e5dd7070Spatrick assert(Context.getSourceManager().getFileID(SelectionRange.getEnd()) ==
192e5dd7070Spatrick TargetFile &&
193e5dd7070Spatrick "selection range must span one file");
194e5dd7070Spatrick
195e5dd7070Spatrick ASTSelectionFinder Visitor(SelectionRange, TargetFile, Context);
196e5dd7070Spatrick Visitor.TraverseDecl(Context.getTranslationUnitDecl());
197e5dd7070Spatrick return Visitor.getSelectedASTNode();
198e5dd7070Spatrick }
199e5dd7070Spatrick
selectionKindToString(SourceSelectionKind Kind)200e5dd7070Spatrick static const char *selectionKindToString(SourceSelectionKind Kind) {
201e5dd7070Spatrick switch (Kind) {
202e5dd7070Spatrick case SourceSelectionKind::None:
203e5dd7070Spatrick return "none";
204e5dd7070Spatrick case SourceSelectionKind::ContainsSelection:
205e5dd7070Spatrick return "contains-selection";
206e5dd7070Spatrick case SourceSelectionKind::ContainsSelectionStart:
207e5dd7070Spatrick return "contains-selection-start";
208e5dd7070Spatrick case SourceSelectionKind::ContainsSelectionEnd:
209e5dd7070Spatrick return "contains-selection-end";
210e5dd7070Spatrick case SourceSelectionKind::InsideSelection:
211e5dd7070Spatrick return "inside";
212e5dd7070Spatrick }
213e5dd7070Spatrick llvm_unreachable("invalid selection kind");
214e5dd7070Spatrick }
215e5dd7070Spatrick
dump(const SelectedASTNode & Node,llvm::raw_ostream & OS,unsigned Indent=0)216e5dd7070Spatrick static void dump(const SelectedASTNode &Node, llvm::raw_ostream &OS,
217e5dd7070Spatrick unsigned Indent = 0) {
218e5dd7070Spatrick OS.indent(Indent * 2);
219e5dd7070Spatrick if (const Decl *D = Node.Node.get<Decl>()) {
220e5dd7070Spatrick OS << D->getDeclKindName() << "Decl";
221e5dd7070Spatrick if (const auto *ND = dyn_cast<NamedDecl>(D))
222a9ac8606Spatrick OS << " \"" << ND->getDeclName() << '"';
223e5dd7070Spatrick } else if (const Stmt *S = Node.Node.get<Stmt>()) {
224e5dd7070Spatrick OS << S->getStmtClassName();
225e5dd7070Spatrick }
226e5dd7070Spatrick OS << ' ' << selectionKindToString(Node.SelectionKind) << "\n";
227e5dd7070Spatrick for (const auto &Child : Node.Children)
228e5dd7070Spatrick dump(Child, OS, Indent + 1);
229e5dd7070Spatrick }
230e5dd7070Spatrick
dump(llvm::raw_ostream & OS) const231e5dd7070Spatrick void SelectedASTNode::dump(llvm::raw_ostream &OS) const { ::dump(*this, OS); }
232e5dd7070Spatrick
233e5dd7070Spatrick /// Returns true if the given node has any direct children with the following
234e5dd7070Spatrick /// selection kind.
235e5dd7070Spatrick ///
236e5dd7070Spatrick /// Note: The direct children also include children of direct children with the
237e5dd7070Spatrick /// "None" selection kind.
hasAnyDirectChildrenWithKind(const SelectedASTNode & Node,SourceSelectionKind Kind)238e5dd7070Spatrick static bool hasAnyDirectChildrenWithKind(const SelectedASTNode &Node,
239e5dd7070Spatrick SourceSelectionKind Kind) {
240e5dd7070Spatrick assert(Kind != SourceSelectionKind::None && "invalid predicate!");
241e5dd7070Spatrick for (const auto &Child : Node.Children) {
242e5dd7070Spatrick if (Child.SelectionKind == Kind)
243e5dd7070Spatrick return true;
244e5dd7070Spatrick if (Child.SelectionKind == SourceSelectionKind::None)
245e5dd7070Spatrick return hasAnyDirectChildrenWithKind(Child, Kind);
246e5dd7070Spatrick }
247e5dd7070Spatrick return false;
248e5dd7070Spatrick }
249e5dd7070Spatrick
250e5dd7070Spatrick namespace {
251e5dd7070Spatrick struct SelectedNodeWithParents {
252e5dd7070Spatrick SelectedASTNode::ReferenceType Node;
253e5dd7070Spatrick llvm::SmallVector<SelectedASTNode::ReferenceType, 8> Parents;
254e5dd7070Spatrick
255e5dd7070Spatrick /// Canonicalizes the given selection by selecting different related AST nodes
256e5dd7070Spatrick /// when it makes sense to do so.
257e5dd7070Spatrick void canonicalize();
258e5dd7070Spatrick };
259e5dd7070Spatrick
260e5dd7070Spatrick enum SelectionCanonicalizationAction { KeepSelection, SelectParent };
261e5dd7070Spatrick
262e5dd7070Spatrick /// Returns the canonicalization action which should be applied to the
263e5dd7070Spatrick /// selected statement.
264e5dd7070Spatrick SelectionCanonicalizationAction
getSelectionCanonizalizationAction(const Stmt * S,const Stmt * Parent)265e5dd7070Spatrick getSelectionCanonizalizationAction(const Stmt *S, const Stmt *Parent) {
266e5dd7070Spatrick // Select the parent expression when:
267e5dd7070Spatrick // - The string literal in ObjC string literal is selected, e.g.:
268e5dd7070Spatrick // @"test" becomes @"test"
269e5dd7070Spatrick // ~~~~~~ ~~~~~~~
270e5dd7070Spatrick if (isa<StringLiteral>(S) && isa<ObjCStringLiteral>(Parent))
271e5dd7070Spatrick return SelectParent;
272e5dd7070Spatrick // The entire call should be selected when just the member expression
273e5dd7070Spatrick // that refers to the method or the decl ref that refers to the function
274e5dd7070Spatrick // is selected.
275e5dd7070Spatrick // f.call(args) becomes f.call(args)
276e5dd7070Spatrick // ~~~~ ~~~~~~~~~~~~
277e5dd7070Spatrick // func(args) becomes func(args)
278e5dd7070Spatrick // ~~~~ ~~~~~~~~~~
279e5dd7070Spatrick else if (const auto *CE = dyn_cast<CallExpr>(Parent)) {
280e5dd7070Spatrick if ((isa<MemberExpr>(S) || isa<DeclRefExpr>(S)) &&
281e5dd7070Spatrick CE->getCallee()->IgnoreImpCasts() == S)
282e5dd7070Spatrick return SelectParent;
283e5dd7070Spatrick }
284e5dd7070Spatrick // FIXME: Syntactic form -> Entire pseudo-object expr.
285e5dd7070Spatrick return KeepSelection;
286e5dd7070Spatrick }
287e5dd7070Spatrick
288e5dd7070Spatrick } // end anonymous namespace
289e5dd7070Spatrick
canonicalize()290e5dd7070Spatrick void SelectedNodeWithParents::canonicalize() {
291e5dd7070Spatrick const Stmt *S = Node.get().Node.get<Stmt>();
292e5dd7070Spatrick assert(S && "non statement selection!");
293e5dd7070Spatrick const Stmt *Parent = Parents[Parents.size() - 1].get().Node.get<Stmt>();
294e5dd7070Spatrick if (!Parent)
295e5dd7070Spatrick return;
296e5dd7070Spatrick
297e5dd7070Spatrick // Look through the implicit casts in the parents.
298e5dd7070Spatrick unsigned ParentIndex = 1;
299e5dd7070Spatrick for (; (ParentIndex + 1) <= Parents.size() && isa<ImplicitCastExpr>(Parent);
300e5dd7070Spatrick ++ParentIndex) {
301e5dd7070Spatrick const Stmt *NewParent =
302e5dd7070Spatrick Parents[Parents.size() - ParentIndex - 1].get().Node.get<Stmt>();
303e5dd7070Spatrick if (!NewParent)
304e5dd7070Spatrick break;
305e5dd7070Spatrick Parent = NewParent;
306e5dd7070Spatrick }
307e5dd7070Spatrick
308e5dd7070Spatrick switch (getSelectionCanonizalizationAction(S, Parent)) {
309e5dd7070Spatrick case SelectParent:
310e5dd7070Spatrick Node = Parents[Parents.size() - ParentIndex];
311e5dd7070Spatrick for (; ParentIndex != 0; --ParentIndex)
312e5dd7070Spatrick Parents.pop_back();
313e5dd7070Spatrick break;
314e5dd7070Spatrick case KeepSelection:
315e5dd7070Spatrick break;
316e5dd7070Spatrick }
317e5dd7070Spatrick }
318e5dd7070Spatrick
319e5dd7070Spatrick /// Finds the set of bottom-most selected AST nodes that are in the selection
320e5dd7070Spatrick /// tree with the specified selection kind.
321e5dd7070Spatrick ///
322e5dd7070Spatrick /// For example, given the following selection tree:
323e5dd7070Spatrick ///
324e5dd7070Spatrick /// FunctionDecl "f" contains-selection
325e5dd7070Spatrick /// CompoundStmt contains-selection [#1]
326e5dd7070Spatrick /// CallExpr inside
327e5dd7070Spatrick /// ImplicitCastExpr inside
328e5dd7070Spatrick /// DeclRefExpr inside
329e5dd7070Spatrick /// IntegerLiteral inside
330e5dd7070Spatrick /// IntegerLiteral inside
331e5dd7070Spatrick /// FunctionDecl "f2" contains-selection
332e5dd7070Spatrick /// CompoundStmt contains-selection [#2]
333e5dd7070Spatrick /// CallExpr inside
334e5dd7070Spatrick /// ImplicitCastExpr inside
335e5dd7070Spatrick /// DeclRefExpr inside
336e5dd7070Spatrick /// IntegerLiteral inside
337e5dd7070Spatrick /// IntegerLiteral inside
338e5dd7070Spatrick ///
339e5dd7070Spatrick /// This function will find references to nodes #1 and #2 when searching for the
340e5dd7070Spatrick /// \c ContainsSelection kind.
findDeepestWithKind(const SelectedASTNode & ASTSelection,llvm::SmallVectorImpl<SelectedNodeWithParents> & MatchingNodes,SourceSelectionKind Kind,llvm::SmallVectorImpl<SelectedASTNode::ReferenceType> & ParentStack)341e5dd7070Spatrick static void findDeepestWithKind(
342e5dd7070Spatrick const SelectedASTNode &ASTSelection,
343e5dd7070Spatrick llvm::SmallVectorImpl<SelectedNodeWithParents> &MatchingNodes,
344e5dd7070Spatrick SourceSelectionKind Kind,
345e5dd7070Spatrick llvm::SmallVectorImpl<SelectedASTNode::ReferenceType> &ParentStack) {
346e5dd7070Spatrick if (ASTSelection.Node.get<DeclStmt>()) {
347e5dd7070Spatrick // Select the entire decl stmt when any of its child declarations is the
348e5dd7070Spatrick // bottom-most.
349e5dd7070Spatrick for (const auto &Child : ASTSelection.Children) {
350e5dd7070Spatrick if (!hasAnyDirectChildrenWithKind(Child, Kind)) {
351e5dd7070Spatrick MatchingNodes.push_back(SelectedNodeWithParents{
352e5dd7070Spatrick std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}});
353e5dd7070Spatrick return;
354e5dd7070Spatrick }
355e5dd7070Spatrick }
356e5dd7070Spatrick } else {
357e5dd7070Spatrick if (!hasAnyDirectChildrenWithKind(ASTSelection, Kind)) {
358e5dd7070Spatrick // This node is the bottom-most.
359e5dd7070Spatrick MatchingNodes.push_back(SelectedNodeWithParents{
360e5dd7070Spatrick std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}});
361e5dd7070Spatrick return;
362e5dd7070Spatrick }
363e5dd7070Spatrick }
364e5dd7070Spatrick // Search in the children.
365e5dd7070Spatrick ParentStack.push_back(std::cref(ASTSelection));
366e5dd7070Spatrick for (const auto &Child : ASTSelection.Children)
367e5dd7070Spatrick findDeepestWithKind(Child, MatchingNodes, Kind, ParentStack);
368e5dd7070Spatrick ParentStack.pop_back();
369e5dd7070Spatrick }
370e5dd7070Spatrick
findDeepestWithKind(const SelectedASTNode & ASTSelection,llvm::SmallVectorImpl<SelectedNodeWithParents> & MatchingNodes,SourceSelectionKind Kind)371e5dd7070Spatrick static void findDeepestWithKind(
372e5dd7070Spatrick const SelectedASTNode &ASTSelection,
373e5dd7070Spatrick llvm::SmallVectorImpl<SelectedNodeWithParents> &MatchingNodes,
374e5dd7070Spatrick SourceSelectionKind Kind) {
375e5dd7070Spatrick llvm::SmallVector<SelectedASTNode::ReferenceType, 16> ParentStack;
376e5dd7070Spatrick findDeepestWithKind(ASTSelection, MatchingNodes, Kind, ParentStack);
377e5dd7070Spatrick }
378e5dd7070Spatrick
379*12c85518Srobert std::optional<CodeRangeASTSelection>
create(SourceRange SelectionRange,const SelectedASTNode & ASTSelection)380e5dd7070Spatrick CodeRangeASTSelection::create(SourceRange SelectionRange,
381e5dd7070Spatrick const SelectedASTNode &ASTSelection) {
382e5dd7070Spatrick // Code range is selected when the selection range is not empty.
383e5dd7070Spatrick if (SelectionRange.getBegin() == SelectionRange.getEnd())
384*12c85518Srobert return std::nullopt;
385e5dd7070Spatrick llvm::SmallVector<SelectedNodeWithParents, 4> ContainSelection;
386e5dd7070Spatrick findDeepestWithKind(ASTSelection, ContainSelection,
387e5dd7070Spatrick SourceSelectionKind::ContainsSelection);
388e5dd7070Spatrick // We are looking for a selection in one body of code, so let's focus on
389e5dd7070Spatrick // one matching result.
390e5dd7070Spatrick if (ContainSelection.size() != 1)
391*12c85518Srobert return std::nullopt;
392e5dd7070Spatrick SelectedNodeWithParents &Selected = ContainSelection[0];
393e5dd7070Spatrick if (!Selected.Node.get().Node.get<Stmt>())
394*12c85518Srobert return std::nullopt;
395e5dd7070Spatrick const Stmt *CodeRangeStmt = Selected.Node.get().Node.get<Stmt>();
396e5dd7070Spatrick if (!isa<CompoundStmt>(CodeRangeStmt)) {
397e5dd7070Spatrick Selected.canonicalize();
398e5dd7070Spatrick return CodeRangeASTSelection(Selected.Node, Selected.Parents,
399e5dd7070Spatrick /*AreChildrenSelected=*/false);
400e5dd7070Spatrick }
401e5dd7070Spatrick // FIXME (Alex L): First selected SwitchCase means that first case statement.
402e5dd7070Spatrick // is selected actually
403e5dd7070Spatrick // (See https://github.com/apple/swift-clang & CompoundStmtRange).
404e5dd7070Spatrick
405e5dd7070Spatrick // FIXME (Alex L): Tweak selection rules for compound statements, see:
406e5dd7070Spatrick // https://github.com/apple/swift-clang/blob/swift-4.1-branch/lib/Tooling/
407e5dd7070Spatrick // Refactor/ASTSlice.cpp#L513
408e5dd7070Spatrick // The user selected multiple statements in a compound statement.
409e5dd7070Spatrick Selected.Parents.push_back(Selected.Node);
410e5dd7070Spatrick return CodeRangeASTSelection(Selected.Node, Selected.Parents,
411e5dd7070Spatrick /*AreChildrenSelected=*/true);
412e5dd7070Spatrick }
413e5dd7070Spatrick
isFunctionLikeDeclaration(const Decl * D)414e5dd7070Spatrick static bool isFunctionLikeDeclaration(const Decl *D) {
415e5dd7070Spatrick // FIXME (Alex L): Test for BlockDecl.
416e5dd7070Spatrick return isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D);
417e5dd7070Spatrick }
418e5dd7070Spatrick
isInFunctionLikeBodyOfCode() const419e5dd7070Spatrick bool CodeRangeASTSelection::isInFunctionLikeBodyOfCode() const {
420e5dd7070Spatrick bool IsPrevCompound = false;
421e5dd7070Spatrick // Scan through the parents (bottom-to-top) and check if the selection is
422e5dd7070Spatrick // contained in a compound statement that's a body of a function/method
423e5dd7070Spatrick // declaration.
424e5dd7070Spatrick for (const auto &Parent : llvm::reverse(Parents)) {
425e5dd7070Spatrick const DynTypedNode &Node = Parent.get().Node;
426e5dd7070Spatrick if (const auto *D = Node.get<Decl>()) {
427e5dd7070Spatrick if (isFunctionLikeDeclaration(D))
428e5dd7070Spatrick return IsPrevCompound;
429e5dd7070Spatrick // Stop the search at any type declaration to avoid returning true for
430e5dd7070Spatrick // expressions in type declarations in functions, like:
431e5dd7070Spatrick // function foo() { struct X {
432e5dd7070Spatrick // int m = /*selection:*/ 1 + 2 /*selection end*/; }; };
433e5dd7070Spatrick if (isa<TypeDecl>(D))
434e5dd7070Spatrick return false;
435e5dd7070Spatrick }
436e5dd7070Spatrick IsPrevCompound = Node.get<CompoundStmt>() != nullptr;
437e5dd7070Spatrick }
438e5dd7070Spatrick return false;
439e5dd7070Spatrick }
440e5dd7070Spatrick
getFunctionLikeNearestParent() const441e5dd7070Spatrick const Decl *CodeRangeASTSelection::getFunctionLikeNearestParent() const {
442e5dd7070Spatrick for (const auto &Parent : llvm::reverse(Parents)) {
443e5dd7070Spatrick const DynTypedNode &Node = Parent.get().Node;
444e5dd7070Spatrick if (const auto *D = Node.get<Decl>()) {
445e5dd7070Spatrick if (isFunctionLikeDeclaration(D))
446e5dd7070Spatrick return D;
447e5dd7070Spatrick }
448e5dd7070Spatrick }
449e5dd7070Spatrick return nullptr;
450e5dd7070Spatrick }
451