diff options
Diffstat (limited to 'lib/Index/ASTLocation.cpp')
-rw-r--r-- | lib/Index/ASTLocation.cpp | 141 |
1 files changed, 51 insertions, 90 deletions
diff --git a/lib/Index/ASTLocation.cpp b/lib/Index/ASTLocation.cpp index 7bad129d6c..6294d699a8 100644 --- a/lib/Index/ASTLocation.cpp +++ b/lib/Index/ASTLocation.cpp @@ -39,94 +39,40 @@ static Decl *getDeclFromExpr(Stmt *E) { Decl *ASTLocation::getReferencedDecl() { if (isInvalid()) return 0; - if (isDecl()) - return getDecl(); - assert(getStmt()); - return getDeclFromExpr(getStmt()); -} - - -static bool isContainedInStatement(const Stmt *Node, const Stmt *Parent) { - assert(Node && Parent && "Passed null Node or Parent"); - - if (Node == Parent) - return true; - - for (Stmt::const_child_iterator - I = Parent->child_begin(), E = Parent->child_end(); I != E; ++I) { - if (*I) - if (isContainedInStatement(Node, *I)) - return true; - } - - return false; -} - -const Decl *ASTLocation::FindImmediateParent(const Decl *D, const Stmt *Node) { - assert(D && Node && "Passed null Decl or null Stmt"); - - if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { - const Expr *Init = VD->getInit(); - if (Init == 0) - return 0; - return isContainedInStatement(Node, Init) ? D : 0; - } - - if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { - if (!FD->isThisDeclarationADefinition()) - return 0; - - for (DeclContext::decl_iterator - I = FD->decls_begin(), E = FD->decls_end(); I != E; ++I) { - const Decl *Child = FindImmediateParent(*I, Node); - if (Child) - return Child; - } - - assert(FD->getBody() && "If not definition we should have exited already"); - return isContainedInStatement(Node, FD->getBody()) ? D : 0; - } - - if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { - if (!MD->getBody()) - return 0; - - for (DeclContext::decl_iterator - I = MD->decls_begin(), E = MD->decls_end(); I != E; ++I) { - const Decl *Child = FindImmediateParent(*I, Node); - if (Child) - return Child; - } - - assert(MD->getBody() && "If not definition we should have exited already"); - return isContainedInStatement(Node, MD->getBody()) ? D : 0; - } - - if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { - for (DeclContext::decl_iterator - I = BD->decls_begin(), E = BD->decls_end(); I != E; ++I) { - const Decl *Child = FindImmediateParent(*I, Node); - if (Child) - return Child; - } - - assert(BD->getBody() && "BlockDecl without body ?"); - return isContainedInStatement(Node, BD->getBody()) ? D : 0; + switch (getKind()) { + default: assert(0 && "Invalid Kind"); + case N_Type: + return 0; + case N_Decl: + return D; + case N_NamedRef: + return NDRef.ND; + case N_Stmt: + return getDeclFromExpr(Stm); } - + return 0; } -bool ASTLocation::isImmediateParent(const Decl *D, const Stmt *Node) { - assert(D && Node && "Passed null Decl or null Stmt"); - return D == FindImmediateParent(D, Node); -} - SourceRange ASTLocation::getSourceRange() const { if (isInvalid()) return SourceRange(); - return isDecl() ? getDecl()->getSourceRange() : getStmt()->getSourceRange(); + + switch (getKind()) { + default: assert(0 && "Invalid Kind"); + return SourceRange(); + case N_Decl: + return D->getSourceRange(); + case N_Stmt: + return Stm->getSourceRange(); + case N_NamedRef: + return SourceRange(AsNamedRef().Loc, AsNamedRef().Loc); + case N_Type: + return AsTypeLoc().getSourceRange(); + } + + return SourceRange(); } void ASTLocation::print(llvm::raw_ostream &OS) const { @@ -134,21 +80,36 @@ void ASTLocation::print(llvm::raw_ostream &OS) const { OS << "<< Invalid ASTLocation >>\n"; return; } - - OS << "[Decl: " << getDecl()->getDeclKindName() << " "; - if (const NamedDecl *ND = dyn_cast<NamedDecl>(getDecl())) - OS << ND->getNameAsString(); - - if (getStmt()) { - ASTContext &Ctx = getDecl()->getASTContext(); - OS << " | Stmt: " << getStmt()->getStmtClassName() << " "; - getStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); + + ASTContext &Ctx = getParentDecl()->getASTContext(); + + switch (getKind()) { + case N_Decl: + OS << "[Decl: " << AsDecl()->getDeclKindName() << " "; + if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl())) + OS << ND->getNameAsString(); + break; + + case N_Stmt: + OS << "[Stmt: " << AsStmt()->getStmtClassName() << " "; + AsStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); + break; + + case N_NamedRef: + OS << "[NamedRef: " << AsNamedRef().ND->getDeclKindName() << " "; + OS << AsNamedRef().ND->getNameAsString(); + break; + + case N_Type: { + QualType T = AsTypeLoc().getSourceType(); + OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString(); + } } OS << "] <"; SourceRange Range = getSourceRange(); - SourceManager &SourceMgr = getDecl()->getASTContext().getSourceManager(); + SourceManager &SourceMgr = Ctx.getSourceManager(); Range.getBegin().print(OS, SourceMgr); OS << ", "; Range.getEnd().print(OS, SourceMgr); |