]> granicus.if.org Git - clang/commitdiff
Introduce ASTLocation::getReferencedDecl(), for getting the declaration that the...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 18 Jul 2009 21:17:58 +0000 (21:17 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 18 Jul 2009 21:17:58 +0000 (21:17 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76336 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Index/ASTLocation.h
lib/Index/ASTLocation.cpp
tools/index-test/index-test.cpp

index 303d95ecc2881be4ba27613e16fa5f2735cc3aaf..34ce9c37af62b322deba1f99261b6a0af47bb400 100644 (file)
@@ -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<ASTLocation*>(this)->getReferencedDecl();
+  }
+
   SourceRange getSourceRange() const;
 
   /// \brief Checks that D is the immediate Decl parent of Node.
index d83e0e33f35b4e7a9660fbbad34f56ae5fe38f6d..84d79363ef88459e71cf1999d9bfc05054b0efbe 100644 (file)
 using namespace clang;
 using namespace idx;
 
+static Decl *getDeclFromExpr(Stmt *E) {
+  if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
+    return RefExpr->getDecl();
+  if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
+    return ME->getMemberDecl();
+  if (CallExpr *CE = dyn_cast<CallExpr>(E))
+    return getDeclFromExpr(CE->getCallee());
+  if (CastExpr *CE = dyn_cast<CastExpr>(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");
   
index 10449ed0baa0e28c067624b5becfd27330d5a0c2..9f02c2284fdf02cbde38c034c8372dcba7b4b6bc 100644 (file)
@@ -141,30 +141,12 @@ static void ProcessDecl(Decl *D) {
   }
 }
 
-static Decl *getDeclFromExpr(Stmt *E) {
-  if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
-    return RefExpr->getDecl();
-  if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
-    return ME->getMemberDecl();
-  if (CallExpr *CE = dyn_cast<CallExpr>(E))
-    return getDeclFromExpr(CE->getCallee());
-  if (CastExpr *CE = dyn_cast<CastExpr>(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;
   }