diff --git a/tools/libclang/IndexBody.cpp b/tools/libclang/IndexBody.cpp
index 769a1c7ae81e125127a392d84551182d9fc73943..ece1ed429ca99e50e37868fbd9ab371f2070e67a 100644
--- a/tools/libclang/IndexBody.cpp
+++ b/tools/libclang/IndexBody.cpp
@@ -32,35 +32,18 @@ public:
   }
 
   bool VisitDeclRefExpr(DeclRefExpr *E) {
-    const NamedDecl *D = E->getDecl();
-    if (!D)
-      return true;
-    if (D->getParentFunctionOrMethod())
-      return true;
-    
-    IndexCtx.handleReference(D, E->getLocation(), 0, ParentDC, E);
+    IndexCtx.handleReference(E->getDecl(), E->getLocation(), 0, ParentDC, E);
     return true;
   }
 
   bool VisitMemberExpr(MemberExpr *E) {
-    const NamedDecl *D = E->getMemberDecl();
-    if (!D)
-      return true;
-    if (D->getParentFunctionOrMethod())
-      return true;
-    
-    IndexCtx.handleReference(D, E->getMemberLoc(), 0, ParentDC, E);
+    IndexCtx.handleReference(E->getMemberDecl(), E->getMemberLoc(), 0, ParentDC,
+                             E);
     return true;
   }
 
   bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
-    const NamedDecl *D = E->getDecl();
-    if (!D)
-      return true;
-    if (D->getParentFunctionOrMethod())
-      return true;
-    
-    IndexCtx.handleReference(D, E->getLocation(), 0, ParentDC, E);
+    IndexCtx.handleReference(E->getDecl(), E->getLocation(), 0, ParentDC, E);
     return true;
   }
 
diff --git a/tools/libclang/IndexTypeSourceInfo.cpp b/tools/libclang/IndexTypeSourceInfo.cpp
index b63ebddfb0243817bcd32d4cdeaa9c9a59a77ee1..5aeee0d26340113ca83be1ccdb5d9a0606f1e4e4 100644
--- a/tools/libclang/IndexTypeSourceInfo.cpp
+++ b/tools/libclang/IndexTypeSourceInfo.cpp
@@ -36,6 +36,8 @@ public:
 
   bool VisitTagTypeLoc(TagTypeLoc TL) {
     TagDecl *D = TL.getDecl();
+    if (D->getParentFunctionOrMethod())
+      return true;
 
     if (TL.isDefinition()) {
       IndexCtx.indexTagDecl(D);
diff --git a/tools/libclang/IndexingContext.cpp b/tools/libclang/IndexingContext.cpp
index 15fcde174de2b5531b7ef4565f4736eb0ee2ac33..3ecc560d35013fdbf552ef57ceb76276c61fc721 100644
--- a/tools/libclang/IndexingContext.cpp
+++ b/tools/libclang/IndexingContext.cpp
@@ -283,6 +283,10 @@ void IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
                                       const DeclContext *DC,
                                       const Expr *E,
                                       CXIdxEntityRefKind Kind) {
+  if (!D)
+    return;
+  if (D->getParentFunctionOrMethod())
+    return;
   if (Loc.isInvalid())
     return;
   if (!CB.indexEntityReference)