blob: 9d0683a285848e664cd9ba470bd5b5a81e569ad5 [file] [log] [blame]
Alex Lorenza844f392017-08-24 13:51:09 +00001//===--- ASTSelection.cpp - Clang refactoring library ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Tooling/Refactoring/ASTSelection.h"
11#include "clang/AST/LexicallyOrderedRecursiveASTVisitor.h"
12#include "clang/Lex/Lexer.h"
Alex Lorenz410ef382017-08-30 15:28:01 +000013#include "llvm/Support/SaveAndRestore.h"
Alex Lorenza844f392017-08-24 13:51:09 +000014
15using namespace clang;
16using namespace tooling;
17using ast_type_traits::DynTypedNode;
18
19namespace {
20
Alex Lorenz23654b52017-08-30 13:24:37 +000021CharSourceRange getLexicalDeclRange(Decl *D, const SourceManager &SM,
22 const LangOptions &LangOpts) {
23 if (!isa<ObjCImplDecl>(D))
24 return CharSourceRange::getTokenRange(D->getSourceRange());
25 // Objective-C implementation declarations end at the '@' instead of the 'end'
26 // keyword. Use the lexer to find the location right after 'end'.
27 SourceRange R = D->getSourceRange();
28 SourceLocation LocAfterEnd = Lexer::findLocationAfterToken(
29 R.getEnd(), tok::raw_identifier, SM, LangOpts,
30 /*SkipTrailingWhitespaceAndNewLine=*/false);
31 return LocAfterEnd.isValid()
32 ? CharSourceRange::getCharRange(R.getBegin(), LocAfterEnd)
33 : CharSourceRange::getTokenRange(R);
34}
35
Alex Lorenza844f392017-08-24 13:51:09 +000036/// Constructs the tree of selected AST nodes that either contain the location
37/// of the cursor or overlap with the selection range.
38class ASTSelectionFinder
39 : public LexicallyOrderedRecursiveASTVisitor<ASTSelectionFinder> {
40public:
41 ASTSelectionFinder(SourceRange Selection, FileID TargetFile,
42 const ASTContext &Context)
43 : LexicallyOrderedRecursiveASTVisitor(Context.getSourceManager()),
44 SelectionBegin(Selection.getBegin()),
45 SelectionEnd(Selection.getBegin() == Selection.getEnd()
46 ? SourceLocation()
47 : Selection.getEnd()),
48 TargetFile(TargetFile), Context(Context) {
49 // The TU decl is the root of the selected node tree.
50 SelectionStack.push_back(
51 SelectedASTNode(DynTypedNode::create(*Context.getTranslationUnitDecl()),
52 SourceSelectionKind::None));
53 }
54
55 Optional<SelectedASTNode> getSelectedASTNode() {
56 assert(SelectionStack.size() == 1 && "stack was not popped");
57 SelectedASTNode Result = std::move(SelectionStack.back());
58 SelectionStack.pop_back();
59 if (Result.Children.empty())
60 return None;
Alex Lorenz0eaa4832017-08-24 14:08:18 +000061 return std::move(Result);
Alex Lorenza844f392017-08-24 13:51:09 +000062 }
63
Alex Lorenz410ef382017-08-30 15:28:01 +000064 bool TraversePseudoObjectExpr(PseudoObjectExpr *E) {
65 // Avoid traversing the semantic expressions. They should be handled by
66 // looking through the appropriate opaque expressions in order to build
67 // a meaningful selection tree.
68 llvm::SaveAndRestore<bool> LookThrough(LookThroughOpaqueValueExprs, true);
69 return TraverseStmt(E->getSyntacticForm());
70 }
71
72 bool TraverseOpaqueValueExpr(OpaqueValueExpr *E) {
73 if (!LookThroughOpaqueValueExprs)
74 return true;
75 llvm::SaveAndRestore<bool> LookThrough(LookThroughOpaqueValueExprs, false);
76 return TraverseStmt(E->getSourceExpr());
77 }
78
Alex Lorenza844f392017-08-24 13:51:09 +000079 bool TraverseDecl(Decl *D) {
80 if (isa<TranslationUnitDecl>(D))
81 return LexicallyOrderedRecursiveASTVisitor::TraverseDecl(D);
82 if (D->isImplicit())
83 return true;
84
85 // Check if this declaration is written in the file of interest.
86 const SourceRange DeclRange = D->getSourceRange();
87 const SourceManager &SM = Context.getSourceManager();
88 SourceLocation FileLoc;
89 if (DeclRange.getBegin().isMacroID() && !DeclRange.getEnd().isMacroID())
90 FileLoc = DeclRange.getEnd();
91 else
92 FileLoc = SM.getSpellingLoc(DeclRange.getBegin());
93 if (SM.getFileID(FileLoc) != TargetFile)
94 return true;
95
Alex Lorenza844f392017-08-24 13:51:09 +000096 SourceSelectionKind SelectionKind =
Alex Lorenz23654b52017-08-30 13:24:37 +000097 selectionKindFor(getLexicalDeclRange(D, SM, Context.getLangOpts()));
Alex Lorenza844f392017-08-24 13:51:09 +000098 SelectionStack.push_back(
99 SelectedASTNode(DynTypedNode::create(*D), SelectionKind));
100 LexicallyOrderedRecursiveASTVisitor::TraverseDecl(D);
101 popAndAddToSelectionIfSelected(SelectionKind);
102
103 if (DeclRange.getEnd().isValid() &&
104 SM.isBeforeInTranslationUnit(SelectionEnd.isValid() ? SelectionEnd
105 : SelectionBegin,
106 DeclRange.getEnd())) {
107 // Stop early when we've reached a declaration after the selection.
108 return false;
109 }
110 return true;
111 }
112
113 bool TraverseStmt(Stmt *S) {
114 if (!S)
115 return true;
Alex Lorenz410ef382017-08-30 15:28:01 +0000116 if (auto *Opaque = dyn_cast<OpaqueValueExpr>(S))
117 return TraverseOpaqueValueExpr(Opaque);
Alex Lorenza844f392017-08-24 13:51:09 +0000118 // FIXME (Alex Lorenz): Improve handling for macro locations.
119 SourceSelectionKind SelectionKind =
120 selectionKindFor(CharSourceRange::getTokenRange(S->getSourceRange()));
121 SelectionStack.push_back(
122 SelectedASTNode(DynTypedNode::create(*S), SelectionKind));
123 LexicallyOrderedRecursiveASTVisitor::TraverseStmt(S);
124 popAndAddToSelectionIfSelected(SelectionKind);
125 return true;
126 }
127
128private:
129 void popAndAddToSelectionIfSelected(SourceSelectionKind SelectionKind) {
130 SelectedASTNode Node = std::move(SelectionStack.back());
131 SelectionStack.pop_back();
132 if (SelectionKind != SourceSelectionKind::None || !Node.Children.empty())
133 SelectionStack.back().Children.push_back(std::move(Node));
134 }
135
136 SourceSelectionKind selectionKindFor(CharSourceRange Range) {
137 SourceLocation End = Range.getEnd();
138 const SourceManager &SM = Context.getSourceManager();
139 if (Range.isTokenRange())
140 End = Lexer::getLocForEndOfToken(End, 0, SM, Context.getLangOpts());
141 if (!SourceLocation::isPairOfFileLocations(Range.getBegin(), End))
142 return SourceSelectionKind::None;
143 if (!SelectionEnd.isValid()) {
144 // Do a quick check when the selection is of length 0.
145 if (SM.isPointWithin(SelectionBegin, Range.getBegin(), End))
146 return SourceSelectionKind::ContainsSelection;
147 return SourceSelectionKind::None;
148 }
149 bool HasStart = SM.isPointWithin(SelectionBegin, Range.getBegin(), End);
150 bool HasEnd = SM.isPointWithin(SelectionEnd, Range.getBegin(), End);
151 if (HasStart && HasEnd)
152 return SourceSelectionKind::ContainsSelection;
153 if (SM.isPointWithin(Range.getBegin(), SelectionBegin, SelectionEnd) &&
154 SM.isPointWithin(End, SelectionBegin, SelectionEnd))
155 return SourceSelectionKind::InsideSelection;
156 // Ensure there's at least some overlap with the 'start'/'end' selection
157 // types.
158 if (HasStart && SelectionBegin != End)
159 return SourceSelectionKind::ContainsSelectionStart;
160 if (HasEnd && SelectionEnd != Range.getBegin())
161 return SourceSelectionKind::ContainsSelectionEnd;
162
163 return SourceSelectionKind::None;
164 }
165
166 const SourceLocation SelectionBegin, SelectionEnd;
167 FileID TargetFile;
168 const ASTContext &Context;
169 std::vector<SelectedASTNode> SelectionStack;
Alex Lorenz410ef382017-08-30 15:28:01 +0000170 /// Controls whether we can traverse through the OpaqueValueExpr. This is
171 /// typically enabled during the traversal of syntactic form for
172 /// PseudoObjectExprs.
173 bool LookThroughOpaqueValueExprs = false;
Alex Lorenza844f392017-08-24 13:51:09 +0000174};
175
176} // end anonymous namespace
177
178Optional<SelectedASTNode>
179clang::tooling::findSelectedASTNodes(const ASTContext &Context,
180 SourceRange SelectionRange) {
181 assert(SelectionRange.isValid() &&
182 SourceLocation::isPairOfFileLocations(SelectionRange.getBegin(),
183 SelectionRange.getEnd()) &&
184 "Expected a file range");
185 FileID TargetFile =
186 Context.getSourceManager().getFileID(SelectionRange.getBegin());
187 assert(Context.getSourceManager().getFileID(SelectionRange.getEnd()) ==
188 TargetFile &&
189 "selection range must span one file");
190
191 ASTSelectionFinder Visitor(SelectionRange, TargetFile, Context);
192 Visitor.TraverseDecl(Context.getTranslationUnitDecl());
193 return Visitor.getSelectedASTNode();
194}
195
196static const char *selectionKindToString(SourceSelectionKind Kind) {
197 switch (Kind) {
198 case SourceSelectionKind::None:
199 return "none";
200 case SourceSelectionKind::ContainsSelection:
201 return "contains-selection";
202 case SourceSelectionKind::ContainsSelectionStart:
203 return "contains-selection-start";
204 case SourceSelectionKind::ContainsSelectionEnd:
205 return "contains-selection-end";
206 case SourceSelectionKind::InsideSelection:
207 return "inside";
208 }
209 llvm_unreachable("invalid selection kind");
210}
211
212static void dump(const SelectedASTNode &Node, llvm::raw_ostream &OS,
213 unsigned Indent = 0) {
214 OS.indent(Indent * 2);
215 if (const Decl *D = Node.Node.get<Decl>()) {
216 OS << D->getDeclKindName() << "Decl";
217 if (const auto *ND = dyn_cast<NamedDecl>(D))
218 OS << " \"" << ND->getNameAsString() << '"';
219 } else if (const Stmt *S = Node.Node.get<Stmt>()) {
220 OS << S->getStmtClassName();
221 }
222 OS << ' ' << selectionKindToString(Node.SelectionKind) << "\n";
223 for (const auto &Child : Node.Children)
224 dump(Child, OS, Indent + 1);
225}
226
227void SelectedASTNode::dump(llvm::raw_ostream &OS) const { ::dump(*this, OS); }
Alex Lorenzcd6c7832017-10-18 18:48:58 +0000228
229/// Returns true if the given node has any direct children with the following
230/// selection kind.
231///
232/// Note: The direct children also include children of direct children with the
233/// "None" selection kind.
234static bool hasAnyDirectChildrenWithKind(const SelectedASTNode &Node,
235 SourceSelectionKind Kind) {
236 assert(Kind != SourceSelectionKind::None && "invalid predicate!");
237 for (const auto &Child : Node.Children) {
238 if (Child.SelectionKind == Kind)
239 return true;
240 if (Child.SelectionKind == SourceSelectionKind::None)
241 return hasAnyDirectChildrenWithKind(Child, Kind);
242 }
243 return false;
244}
245
246namespace {
247struct SelectedNodeWithParents {
248 SelectedNodeWithParents(SelectedNodeWithParents &&) = default;
249 SelectedNodeWithParents &operator=(SelectedNodeWithParents &&) = default;
250 SelectedASTNode::ReferenceType Node;
251 llvm::SmallVector<SelectedASTNode::ReferenceType, 8> Parents;
252};
253} // end anonymous namespace
254
255/// Finds the set of bottom-most selected AST nodes that are in the selection
256/// tree with the specified selection kind.
257///
258/// For example, given the following selection tree:
259///
260/// FunctionDecl "f" contains-selection
261/// CompoundStmt contains-selection [#1]
262/// CallExpr inside
263/// ImplicitCastExpr inside
264/// DeclRefExpr inside
265/// IntegerLiteral inside
266/// IntegerLiteral inside
267/// FunctionDecl "f2" contains-selection
268/// CompoundStmt contains-selection [#2]
269/// CallExpr inside
270/// ImplicitCastExpr inside
271/// DeclRefExpr inside
272/// IntegerLiteral inside
273/// IntegerLiteral inside
274///
275/// This function will find references to nodes #1 and #2 when searching for the
276/// \c ContainsSelection kind.
277static void findDeepestWithKind(
278 const SelectedASTNode &ASTSelection,
279 llvm::SmallVectorImpl<SelectedNodeWithParents> &MatchingNodes,
280 SourceSelectionKind Kind,
281 llvm::SmallVectorImpl<SelectedASTNode::ReferenceType> &ParentStack) {
282 if (!hasAnyDirectChildrenWithKind(ASTSelection, Kind)) {
283 // This node is the bottom-most.
284 MatchingNodes.push_back(SelectedNodeWithParents{
285 std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}});
286 return;
287 }
288 // Search in the children.
289 ParentStack.push_back(std::cref(ASTSelection));
290 for (const auto &Child : ASTSelection.Children)
291 findDeepestWithKind(Child, MatchingNodes, Kind, ParentStack);
292 ParentStack.pop_back();
293}
294
295static void findDeepestWithKind(
296 const SelectedASTNode &ASTSelection,
297 llvm::SmallVectorImpl<SelectedNodeWithParents> &MatchingNodes,
298 SourceSelectionKind Kind) {
299 llvm::SmallVector<SelectedASTNode::ReferenceType, 16> ParentStack;
300 findDeepestWithKind(ASTSelection, MatchingNodes, Kind, ParentStack);
301}
302
303Optional<CodeRangeASTSelection>
304CodeRangeASTSelection::create(SourceRange SelectionRange,
305 const SelectedASTNode &ASTSelection) {
306 // Code range is selected when the selection range is not empty.
307 if (SelectionRange.getBegin() == SelectionRange.getEnd())
308 return None;
309 llvm::SmallVector<SelectedNodeWithParents, 4> ContainSelection;
310 findDeepestWithKind(ASTSelection, ContainSelection,
311 SourceSelectionKind::ContainsSelection);
312 // We are looking for a selection in one body of code, so let's focus on
313 // one matching result.
314 if (ContainSelection.size() != 1)
315 return None;
316 SelectedNodeWithParents &Selected = ContainSelection[0];
317 if (!Selected.Node.get().Node.get<Stmt>())
318 return None;
319 const Stmt *CodeRangeStmt = Selected.Node.get().Node.get<Stmt>();
320 if (!isa<CompoundStmt>(CodeRangeStmt)) {
321 // FIXME (Alex L): Canonicalize.
322 return CodeRangeASTSelection(Selected.Node, Selected.Parents,
323 /*AreChildrenSelected=*/false);
324 }
Alex Lorenz7fe441b2017-10-24 17:18:45 +0000325 // FIXME (Alex L): First selected SwitchCase means that first case statement.
326 // is selected actually
327 // (See https://github.com/apple/swift-clang & CompoundStmtRange).
328
Alex Lorenzcd6c7832017-10-18 18:48:58 +0000329 // FIXME (Alex L): Tweak selection rules for compound statements, see:
330 // https://github.com/apple/swift-clang/blob/swift-4.1-branch/lib/Tooling/
331 // Refactor/ASTSlice.cpp#L513
332 // The user selected multiple statements in a compound statement.
333 Selected.Parents.push_back(Selected.Node);
334 return CodeRangeASTSelection(Selected.Node, Selected.Parents,
335 /*AreChildrenSelected=*/true);
336}
Alex Lorenz7fe441b2017-10-24 17:18:45 +0000337
338bool CodeRangeASTSelection::isInFunctionLikeBodyOfCode() const {
339 bool IsPrevCompound = false;
340 // Scan through the parents (bottom-to-top) and check if the selection is
341 // contained in a compound statement that's a body of a function/method
342 // declaration.
343 for (const auto &Parent : llvm::reverse(Parents)) {
344 const DynTypedNode &Node = Parent.get().Node;
345 if (const auto *D = Node.get<Decl>()) {
346 // FIXME (Alex L): Test for BlockDecl && ObjCMethodDecl.
347 if (isa<FunctionDecl>(D))
348 return IsPrevCompound;
349 // FIXME (Alex L): We should return false on top-level decls in functions
350 // e.g. we don't want to extract:
351 // function foo() { struct X {
352 // int m = /*selection:*/ 1 + 2 /*selection end*/; }; };
353 }
354 IsPrevCompound = Node.get<CompoundStmt>() != nullptr;
355 }
356 return false;
357}
358
359const Decl *CodeRangeASTSelection::getFunctionLikeNearestParent() const {
360 for (const auto &Parent : llvm::reverse(Parents)) {
361 const DynTypedNode &Node = Parent.get().Node;
362 if (const auto *D = Node.get<Decl>()) {
363 // FIXME (Alex L): Test for BlockDecl && ObjCMethodDecl.
364 if (isa<FunctionDecl>(D))
365 return D;
366 }
367 }
368 return nullptr;
369}