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