| Alex Lorenz | a844f39 | 2017-08-24 13:51:09 +0000 | [diff] [blame] | 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" |
| Alex Lorenz | 410ef38 | 2017-08-30 15:28:01 +0000 | [diff] [blame] | 13 | #include "llvm/Support/SaveAndRestore.h" |
| Alex Lorenz | a844f39 | 2017-08-24 13:51:09 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace clang; |
| 16 | using namespace tooling; |
| 17 | using ast_type_traits::DynTypedNode; |
| 18 | |
| 19 | namespace { |
| 20 | |
| Alex Lorenz | 23654b5 | 2017-08-30 13:24:37 +0000 | [diff] [blame] | 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 | |
| Alex Lorenz | a844f39 | 2017-08-24 13:51:09 +0000 | [diff] [blame] | 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: |
| 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 Lorenz | 0eaa483 | 2017-08-24 14:08:18 +0000 | [diff] [blame] | 61 | return std::move(Result); |
| Alex Lorenz | a844f39 | 2017-08-24 13:51:09 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| Alex Lorenz | 410ef38 | 2017-08-30 15:28:01 +0000 | [diff] [blame] | 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 | |
| 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 Lorenz | a844f39 | 2017-08-24 13:51:09 +0000 | [diff] [blame] | 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 | |
| Alex Lorenz | a844f39 | 2017-08-24 13:51:09 +0000 | [diff] [blame] | 96 | SourceSelectionKind SelectionKind = |
| Alex Lorenz | 23654b5 | 2017-08-30 13:24:37 +0000 | [diff] [blame] | 97 | selectionKindFor(getLexicalDeclRange(D, SM, Context.getLangOpts())); |
| Alex Lorenz | a844f39 | 2017-08-24 13:51:09 +0000 | [diff] [blame] | 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 | |
| 113 | bool TraverseStmt(Stmt *S) { |
| 114 | if (!S) |
| 115 | return true; |
| Alex Lorenz | 410ef38 | 2017-08-30 15:28:01 +0000 | [diff] [blame] | 116 | if (auto *Opaque = dyn_cast<OpaqueValueExpr>(S)) |
| 117 | return TraverseOpaqueValueExpr(Opaque); |
| Alex Lorenz | a844f39 | 2017-08-24 13:51:09 +0000 | [diff] [blame] | 118 | // 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 | |
| 128 | private: |
| 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 Lorenz | 410ef38 | 2017-08-30 15:28:01 +0000 | [diff] [blame] | 170 | /// 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 Lorenz | a844f39 | 2017-08-24 13:51:09 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | } // end anonymous namespace |
| 177 | |
| 178 | Optional<SelectedASTNode> |
| 179 | clang::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 | |
| 196 | static 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 | |
| 212 | static 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 | |
| 227 | void SelectedASTNode::dump(llvm::raw_ostream &OS) const { ::dump(*this, OS); } |
| Alex Lorenz | cd6c783 | 2017-10-18 18:48:58 +0000 | [diff] [blame] | 228 | |
| 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. |
| 234 | static 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 | |
| 246 | namespace { |
| 247 | struct 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. |
| 277 | static void findDeepestWithKind( |
| 278 | const SelectedASTNode &ASTSelection, |
| 279 | llvm::SmallVectorImpl<SelectedNodeWithParents> &MatchingNodes, |
| 280 | SourceSelectionKind Kind, |
| 281 | llvm::SmallVectorImpl<SelectedASTNode::ReferenceType> &ParentStack) { |
| Alex Lorenz | b87b6bf | 2017-10-31 01:28:17 +0000 | [diff] [blame] | 282 | if (ASTSelection.Node.get<DeclStmt>()) { |
| 283 | // Select the entire decl stmt when any of its child declarations is the |
| 284 | // bottom-most. |
| 285 | for (const auto &Child : ASTSelection.Children) { |
| 286 | if (!hasAnyDirectChildrenWithKind(Child, Kind)) { |
| 287 | MatchingNodes.push_back(SelectedNodeWithParents{ |
| 288 | std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}}); |
| 289 | return; |
| 290 | } |
| 291 | } |
| 292 | } else { |
| 293 | if (!hasAnyDirectChildrenWithKind(ASTSelection, Kind)) { |
| 294 | // This node is the bottom-most. |
| 295 | MatchingNodes.push_back(SelectedNodeWithParents{ |
| 296 | std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}}); |
| 297 | return; |
| 298 | } |
| Alex Lorenz | cd6c783 | 2017-10-18 18:48:58 +0000 | [diff] [blame] | 299 | } |
| 300 | // Search in the children. |
| 301 | ParentStack.push_back(std::cref(ASTSelection)); |
| 302 | for (const auto &Child : ASTSelection.Children) |
| 303 | findDeepestWithKind(Child, MatchingNodes, Kind, ParentStack); |
| 304 | ParentStack.pop_back(); |
| 305 | } |
| 306 | |
| 307 | static void findDeepestWithKind( |
| 308 | const SelectedASTNode &ASTSelection, |
| 309 | llvm::SmallVectorImpl<SelectedNodeWithParents> &MatchingNodes, |
| 310 | SourceSelectionKind Kind) { |
| 311 | llvm::SmallVector<SelectedASTNode::ReferenceType, 16> ParentStack; |
| 312 | findDeepestWithKind(ASTSelection, MatchingNodes, Kind, ParentStack); |
| 313 | } |
| 314 | |
| 315 | Optional<CodeRangeASTSelection> |
| 316 | CodeRangeASTSelection::create(SourceRange SelectionRange, |
| 317 | const SelectedASTNode &ASTSelection) { |
| 318 | // Code range is selected when the selection range is not empty. |
| 319 | if (SelectionRange.getBegin() == SelectionRange.getEnd()) |
| 320 | return None; |
| 321 | llvm::SmallVector<SelectedNodeWithParents, 4> ContainSelection; |
| 322 | findDeepestWithKind(ASTSelection, ContainSelection, |
| 323 | SourceSelectionKind::ContainsSelection); |
| 324 | // We are looking for a selection in one body of code, so let's focus on |
| 325 | // one matching result. |
| 326 | if (ContainSelection.size() != 1) |
| 327 | return None; |
| 328 | SelectedNodeWithParents &Selected = ContainSelection[0]; |
| 329 | if (!Selected.Node.get().Node.get<Stmt>()) |
| 330 | return None; |
| 331 | const Stmt *CodeRangeStmt = Selected.Node.get().Node.get<Stmt>(); |
| 332 | if (!isa<CompoundStmt>(CodeRangeStmt)) { |
| 333 | // FIXME (Alex L): Canonicalize. |
| 334 | return CodeRangeASTSelection(Selected.Node, Selected.Parents, |
| 335 | /*AreChildrenSelected=*/false); |
| 336 | } |
| Alex Lorenz | 7fe441b | 2017-10-24 17:18:45 +0000 | [diff] [blame] | 337 | // FIXME (Alex L): First selected SwitchCase means that first case statement. |
| 338 | // is selected actually |
| 339 | // (See https://github.com/apple/swift-clang & CompoundStmtRange). |
| 340 | |
| Alex Lorenz | cd6c783 | 2017-10-18 18:48:58 +0000 | [diff] [blame] | 341 | // FIXME (Alex L): Tweak selection rules for compound statements, see: |
| 342 | // https://github.com/apple/swift-clang/blob/swift-4.1-branch/lib/Tooling/ |
| 343 | // Refactor/ASTSlice.cpp#L513 |
| 344 | // The user selected multiple statements in a compound statement. |
| 345 | Selected.Parents.push_back(Selected.Node); |
| 346 | return CodeRangeASTSelection(Selected.Node, Selected.Parents, |
| 347 | /*AreChildrenSelected=*/true); |
| 348 | } |
| Alex Lorenz | 7fe441b | 2017-10-24 17:18:45 +0000 | [diff] [blame] | 349 | |
| Alex Lorenz | 7cd48cd | 2017-11-01 00:07:12 +0000 | [diff] [blame] | 350 | static bool isFunctionLikeDeclaration(const Decl *D) { |
| 351 | // FIXME (Alex L): Test for BlockDecl. |
| 352 | return isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D); |
| 353 | } |
| 354 | |
| Alex Lorenz | 7fe441b | 2017-10-24 17:18:45 +0000 | [diff] [blame] | 355 | bool CodeRangeASTSelection::isInFunctionLikeBodyOfCode() const { |
| 356 | bool IsPrevCompound = false; |
| 357 | // Scan through the parents (bottom-to-top) and check if the selection is |
| 358 | // contained in a compound statement that's a body of a function/method |
| 359 | // declaration. |
| 360 | for (const auto &Parent : llvm::reverse(Parents)) { |
| 361 | const DynTypedNode &Node = Parent.get().Node; |
| 362 | if (const auto *D = Node.get<Decl>()) { |
| Alex Lorenz | 7cd48cd | 2017-11-01 00:07:12 +0000 | [diff] [blame] | 363 | if (isFunctionLikeDeclaration(D)) |
| Alex Lorenz | 7fe441b | 2017-10-24 17:18:45 +0000 | [diff] [blame] | 364 | return IsPrevCompound; |
| 365 | // FIXME (Alex L): We should return false on top-level decls in functions |
| 366 | // e.g. we don't want to extract: |
| 367 | // function foo() { struct X { |
| 368 | // int m = /*selection:*/ 1 + 2 /*selection end*/; }; }; |
| 369 | } |
| 370 | IsPrevCompound = Node.get<CompoundStmt>() != nullptr; |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | const Decl *CodeRangeASTSelection::getFunctionLikeNearestParent() const { |
| 376 | for (const auto &Parent : llvm::reverse(Parents)) { |
| 377 | const DynTypedNode &Node = Parent.get().Node; |
| 378 | if (const auto *D = Node.get<Decl>()) { |
| Alex Lorenz | 7cd48cd | 2017-11-01 00:07:12 +0000 | [diff] [blame] | 379 | if (isFunctionLikeDeclaration(D)) |
| Alex Lorenz | 7fe441b | 2017-10-24 17:18:45 +0000 | [diff] [blame] | 380 | return D; |
| 381 | } |
| 382 | } |
| 383 | return nullptr; |
| 384 | } |