Argiris Kirtzidis | 75abd9c | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 1 | //===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===// |
Argiris Kirtzidis | 970cfe1 | 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 | // |
Argiris Kirtzidis | 2fcbc1e | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 10 | // ASTLocation is Decl or a Stmt and its immediate Decl parent. |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argiris Kirtzidis | 75abd9c | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 14 | #include "clang/Index/ASTLocation.h" |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Argiris Kirtzidis | b57d7e7 | 2009-07-18 00:33:46 +0000 | [diff] [blame^] | 16 | #include "clang/AST/DeclObjC.h" |
Argiris Kirtzidis | 970cfe1 | 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; |
Argiris Kirtzidis | 75abd9c | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 20 | using namespace idx; |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 21 | |
| 22 | static bool isContainedInStatement(Stmt *Node, Stmt *Parent) { |
| 23 | assert(Node && Parent && "Passed null Node or Parent"); |
| 24 | |
| 25 | if (Node == Parent) |
| 26 | return true; |
| 27 | |
| 28 | for (Stmt::child_iterator |
| 29 | I = Parent->child_begin(), E = Parent->child_end(); I != E; ++I) { |
| 30 | if (isContainedInStatement(Node, *I)) |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | return false; |
| 35 | } |
| 36 | |
Zhongxing Xu | 95ed5d0 | 2009-07-17 07:36:20 +0000 | [diff] [blame] | 37 | Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) { |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 38 | assert(D && Node && "Passed null Decl or null Stmt"); |
| 39 | |
| 40 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 41 | Expr *Init = VD->getInit(); |
| 42 | if (Init == 0) |
| 43 | return 0; |
| 44 | return isContainedInStatement(Node, Init) ? D : 0; |
| 45 | } |
Argiris Kirtzidis | b57d7e7 | 2009-07-18 00:33:46 +0000 | [diff] [blame^] | 46 | |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 47 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 48 | if (!FD->isThisDeclarationADefinition()) |
| 49 | return 0; |
| 50 | |
| 51 | for (DeclContext::decl_iterator |
| 52 | I = FD->decls_begin(), E = FD->decls_end(); I != E; ++I) { |
| 53 | Decl *Child = FindImmediateParent(*I, Node); |
| 54 | if (Child) |
| 55 | return Child; |
| 56 | } |
Argiris Kirtzidis | b57d7e7 | 2009-07-18 00:33:46 +0000 | [diff] [blame^] | 57 | |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 58 | assert(FD->getBody() && "If not definition we should have exited already"); |
| 59 | return isContainedInStatement(Node, FD->getBody()) ? D : 0; |
| 60 | } |
Argiris Kirtzidis | b57d7e7 | 2009-07-18 00:33:46 +0000 | [diff] [blame^] | 61 | |
| 62 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 63 | if (!MD->getBody()) |
| 64 | return 0; |
| 65 | |
| 66 | for (DeclContext::decl_iterator |
| 67 | I = MD->decls_begin(), E = MD->decls_end(); I != E; ++I) { |
| 68 | Decl *Child = FindImmediateParent(*I, Node); |
| 69 | if (Child) |
| 70 | return Child; |
| 71 | } |
| 72 | |
| 73 | assert(MD->getBody() && "If not definition we should have exited already"); |
| 74 | return isContainedInStatement(Node, MD->getBody()) ? D : 0; |
| 75 | } |
| 76 | |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Argiris Kirtzidis | 2fcbc1e | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 80 | bool ASTLocation::isImmediateParent(Decl *D, Stmt *Node) { |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 81 | assert(D && Node && "Passed null Decl or null Stmt"); |
| 82 | return D == FindImmediateParent(D, Node); |
| 83 | } |
| 84 | |
Argiris Kirtzidis | d45d112 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 85 | SourceRange ASTLocation::getSourceRange() const { |
| 86 | return isDecl() ? getDecl()->getSourceRange() : getStmt()->getSourceRange(); |
| 87 | } |
| 88 | |
Argiris Kirtzidis | 2fcbc1e | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 89 | void ASTLocation::print(llvm::raw_ostream &OS) { |
| 90 | assert(isValid() && "ASTLocation is not valid"); |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 91 | |
| 92 | OS << "[Decl: " << getDecl()->getDeclKindName() << " "; |
| 93 | if (NamedDecl *ND = dyn_cast<NamedDecl>(getDecl())) |
| 94 | OS << ND->getNameAsString(); |
| 95 | |
| 96 | if (getStmt()) { |
| 97 | ASTContext &Ctx = getDecl()->getASTContext(); |
| 98 | OS << " | Stmt: " << getStmt()->getStmtClassName() << " "; |
| 99 | getStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); |
| 100 | } |
| 101 | |
| 102 | OS << "] <"; |
| 103 | |
Argiris Kirtzidis | d45d112 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 104 | SourceRange Range = getSourceRange(); |
Argiris Kirtzidis | 970cfe1 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 105 | SourceManager &SourceMgr = getDecl()->getASTContext().getSourceManager(); |
| 106 | Range.getBegin().print(OS, SourceMgr); |
| 107 | OS << ", "; |
| 108 | Range.getEnd().print(OS, SourceMgr); |
| 109 | OS << ">\n"; |
| 110 | } |