ASTSelection.cpp 17 KB

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