ASTSelection.cpp 17 KB

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