From: Argyrios Kyrtzidis Date: Sat, 18 Jul 2009 21:17:58 +0000 (+0000) Subject: Introduce ASTLocation::getReferencedDecl(), for getting the declaration that the... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dc50c64c13322dd81b10263cbb023fb1f7eae1fd;p=clang Introduce ASTLocation::getReferencedDecl(), for getting the declaration that the ASTLocation references. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76336 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Index/ASTLocation.h b/include/clang/Index/ASTLocation.h index 303d95ecc2..34ce9c37af 100644 --- a/include/clang/Index/ASTLocation.h +++ b/include/clang/Index/ASTLocation.h @@ -58,6 +58,16 @@ public: bool isDecl() const { return isValid() && Stm == 0; } bool isStmt() const { return isValid() && Stm != 0; } + /// \brief Returns the declaration that this ASTLocation references. + /// + /// If this points to a Decl, that Decl is returned. + /// If this points to an Expr that references a Decl, that Decl is returned, + /// otherwise it returns NULL. + Decl *getReferencedDecl(); + const Decl *getReferencedDecl() const { + return const_cast(this)->getReferencedDecl(); + } + SourceRange getSourceRange() const; /// \brief Checks that D is the immediate Decl parent of Node. diff --git a/lib/Index/ASTLocation.cpp b/lib/Index/ASTLocation.cpp index d83e0e33f3..84d79363ef 100644 --- a/lib/Index/ASTLocation.cpp +++ b/lib/Index/ASTLocation.cpp @@ -19,6 +19,30 @@ using namespace clang; using namespace idx; +static Decl *getDeclFromExpr(Stmt *E) { + if (DeclRefExpr *RefExpr = dyn_cast(E)) + return RefExpr->getDecl(); + if (MemberExpr *ME = dyn_cast(E)) + return ME->getMemberDecl(); + if (CallExpr *CE = dyn_cast(E)) + return getDeclFromExpr(CE->getCallee()); + if (CastExpr *CE = dyn_cast(E)) + return getDeclFromExpr(CE->getSubExpr()); + + return 0; +} + +Decl *ASTLocation::getReferencedDecl() { + if (isInvalid()) + return 0; + if (isDecl()) + return getDecl(); + + assert(getStmt()); + return getDeclFromExpr(getStmt()); +} + + static bool isContainedInStatement(Stmt *Node, Stmt *Parent) { assert(Node && Parent && "Passed null Node or Parent"); diff --git a/tools/index-test/index-test.cpp b/tools/index-test/index-test.cpp index 10449ed0ba..9f02c2284f 100644 --- a/tools/index-test/index-test.cpp +++ b/tools/index-test/index-test.cpp @@ -141,30 +141,12 @@ static void ProcessDecl(Decl *D) { } } -static Decl *getDeclFromExpr(Stmt *E) { - if (DeclRefExpr *RefExpr = dyn_cast(E)) - return RefExpr->getDecl(); - if (MemberExpr *ME = dyn_cast(E)) - return ME->getMemberDecl(); - if (CallExpr *CE = dyn_cast(E)) - return getDeclFromExpr(CE->getCallee()); - if (CastExpr *CE = dyn_cast(E)) - return getDeclFromExpr(CE->getSubExpr()); - - return 0; -} - static void ProcessASTLocation(ASTLocation ASTLoc, IndexProvider &IdxProvider) { assert(ASTLoc.isValid()); - Decl *D = 0; - if (ASTLoc.isStmt()) - D = getDeclFromExpr(ASTLoc.getStmt()); - else - D = ASTLoc.getDecl(); - + Decl *D = ASTLoc.getReferencedDecl(); if (D == 0) { - llvm::errs() << "Error: Couldn't get a Decl out of the ASTLocation"; + llvm::errs() << "Error: Couldn't get a referenced Decl for the ASTLocation"; HadErrors = true; return; }