Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 1 | //===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===// |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 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 | // |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 10 | // ASTLocation is Decl or a Stmt and its immediate Decl parent. |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 14 | #include "clang/Index/ASTLocation.h" |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Argyrios Kyrtzidis | cc1ccb7 | 2009-07-18 00:33:46 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 17 | #include "clang/AST/Stmt.h" |
| 18 | #include "clang/AST/Expr.h" |
| 19 | using namespace clang; |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 20 | using namespace idx; |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 21 | |
Argyrios Kyrtzidis | dc50c64 | 2009-07-18 21:17:58 +0000 | [diff] [blame] | 22 | static Decl *getDeclFromExpr(Stmt *E) { |
| 23 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 24 | return RefExpr->getDecl(); |
| 25 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 26 | return ME->getMemberDecl(); |
| 27 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 28 | return getDeclFromExpr(CE->getCallee()); |
| 29 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 30 | return getDeclFromExpr(CE->getSubExpr()); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | Decl *ASTLocation::getReferencedDecl() { |
| 36 | if (isInvalid()) |
| 37 | return 0; |
| 38 | if (isDecl()) |
| 39 | return getDecl(); |
| 40 | |
| 41 | assert(getStmt()); |
| 42 | return getDeclFromExpr(getStmt()); |
| 43 | } |
| 44 | |
| 45 | |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 46 | static bool isContainedInStatement(Stmt *Node, Stmt *Parent) { |
| 47 | assert(Node && Parent && "Passed null Node or Parent"); |
| 48 | |
| 49 | if (Node == Parent) |
| 50 | return true; |
| 51 | |
| 52 | for (Stmt::child_iterator |
| 53 | I = Parent->child_begin(), E = Parent->child_end(); I != E; ++I) { |
Zhongxing Xu | 89021b0 | 2009-07-20 08:28:49 +0000 | [diff] [blame^] | 54 | if (*I) |
| 55 | if (isContainedInStatement(Node, *I)) |
| 56 | return true; |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | return false; |
| 60 | } |
| 61 | |
Zhongxing Xu | 7f66bd2 | 2009-07-17 07:36:20 +0000 | [diff] [blame] | 62 | Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) { |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 63 | assert(D && Node && "Passed null Decl or null Stmt"); |
| 64 | |
| 65 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 66 | Expr *Init = VD->getInit(); |
| 67 | if (Init == 0) |
| 68 | return 0; |
| 69 | return isContainedInStatement(Node, Init) ? D : 0; |
| 70 | } |
Argyrios Kyrtzidis | cc1ccb7 | 2009-07-18 00:33:46 +0000 | [diff] [blame] | 71 | |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 72 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 73 | if (!FD->isThisDeclarationADefinition()) |
| 74 | return 0; |
| 75 | |
| 76 | for (DeclContext::decl_iterator |
| 77 | I = FD->decls_begin(), E = FD->decls_end(); I != E; ++I) { |
| 78 | Decl *Child = FindImmediateParent(*I, Node); |
| 79 | if (Child) |
| 80 | return Child; |
| 81 | } |
Argyrios Kyrtzidis | cc1ccb7 | 2009-07-18 00:33:46 +0000 | [diff] [blame] | 82 | |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 83 | assert(FD->getBody() && "If not definition we should have exited already"); |
| 84 | return isContainedInStatement(Node, FD->getBody()) ? D : 0; |
| 85 | } |
Argyrios Kyrtzidis | cc1ccb7 | 2009-07-18 00:33:46 +0000 | [diff] [blame] | 86 | |
| 87 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 88 | if (!MD->getBody()) |
| 89 | return 0; |
| 90 | |
| 91 | for (DeclContext::decl_iterator |
| 92 | I = MD->decls_begin(), E = MD->decls_end(); I != E; ++I) { |
| 93 | Decl *Child = FindImmediateParent(*I, Node); |
| 94 | if (Child) |
| 95 | return Child; |
| 96 | } |
| 97 | |
| 98 | assert(MD->getBody() && "If not definition we should have exited already"); |
| 99 | return isContainedInStatement(Node, MD->getBody()) ? D : 0; |
| 100 | } |
| 101 | |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 105 | bool ASTLocation::isImmediateParent(Decl *D, Stmt *Node) { |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 106 | assert(D && Node && "Passed null Decl or null Stmt"); |
| 107 | return D == FindImmediateParent(D, Node); |
| 108 | } |
| 109 | |
Argyrios Kyrtzidis | 755c6b4 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 110 | SourceRange ASTLocation::getSourceRange() const { |
Argyrios Kyrtzidis | 9b9685d | 2009-07-18 21:17:43 +0000 | [diff] [blame] | 111 | if (isInvalid()) |
| 112 | return SourceRange(); |
Argyrios Kyrtzidis | 755c6b4 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 113 | return isDecl() ? getDecl()->getSourceRange() : getStmt()->getSourceRange(); |
| 114 | } |
| 115 | |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 116 | void ASTLocation::print(llvm::raw_ostream &OS) { |
Argyrios Kyrtzidis | 9b9685d | 2009-07-18 21:17:43 +0000 | [diff] [blame] | 117 | if (isInvalid()) { |
| 118 | OS << "<< Invalid ASTLocation >>\n"; |
| 119 | return; |
| 120 | } |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 121 | |
| 122 | OS << "[Decl: " << getDecl()->getDeclKindName() << " "; |
| 123 | if (NamedDecl *ND = dyn_cast<NamedDecl>(getDecl())) |
| 124 | OS << ND->getNameAsString(); |
| 125 | |
| 126 | if (getStmt()) { |
| 127 | ASTContext &Ctx = getDecl()->getASTContext(); |
| 128 | OS << " | Stmt: " << getStmt()->getStmtClassName() << " "; |
| 129 | getStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); |
| 130 | } |
| 131 | |
| 132 | OS << "] <"; |
| 133 | |
Argyrios Kyrtzidis | 755c6b4 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 134 | SourceRange Range = getSourceRange(); |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 135 | SourceManager &SourceMgr = getDecl()->getASTContext().getSourceManager(); |
| 136 | Range.getBegin().print(OS, SourceMgr); |
| 137 | OS << ", "; |
| 138 | Range.getEnd().print(OS, SourceMgr); |
| 139 | OS << ">\n"; |
| 140 | } |