1//===--- DeclReferenceMap.cpp - Map Decls to their references -------------===// 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// 10// DeclReferenceMap creates a mapping from Decls to the ASTLocations that 11// reference them. 12// 13//===----------------------------------------------------------------------===// 14 15#include "clang/Index/DeclReferenceMap.h" 16#include "clang/Index/ASTLocation.h" 17#include "ASTVisitor.h" 18using namespace clang; 19using namespace idx; 20 21namespace { 22 23class RefMapper : public ASTVisitor<RefMapper> { 24 DeclReferenceMap::MapTy ⤅ 25 26public: 27 RefMapper(DeclReferenceMap::MapTy &map) : Map(map) { } 28 29 void VisitDeclRefExpr(DeclRefExpr *Node); 30 void VisitMemberExpr(MemberExpr *Node); 31 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node); 32 33 void VisitTypedefTypeLoc(TypedefTypeLoc TL); 34 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); 35}; 36 37} // anonymous namespace 38 39//===----------------------------------------------------------------------===// 40// RefMapper Implementation 41//===----------------------------------------------------------------------===// 42 43void RefMapper::VisitDeclRefExpr(DeclRefExpr *Node) { 44 NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getCanonicalDecl()); 45 Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node))); 46} 47 48void RefMapper::VisitMemberExpr(MemberExpr *Node) { 49 NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getCanonicalDecl()); 50 Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node))); 51} 52 53void RefMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 54 Map.insert(std::make_pair(Node->getDecl(), ASTLocation(CurrentDecl, Node))); 55} 56 57void RefMapper::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 58 NamedDecl *ND = TL.getTypedefNameDecl(); 59 Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc()))); 60} 61 62void RefMapper::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 63 NamedDecl *ND = TL.getIFaceDecl(); 64 Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc()))); 65} 66 67//===----------------------------------------------------------------------===// 68// DeclReferenceMap Implementation 69//===----------------------------------------------------------------------===// 70 71DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) { 72 RefMapper(Map).Visit(Ctx.getTranslationUnitDecl()); 73} 74 75DeclReferenceMap::astlocation_iterator 76DeclReferenceMap::refs_begin(NamedDecl *D) const { 77 NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); 78 return astlocation_iterator(Map.lower_bound(Prim)); 79} 80 81DeclReferenceMap::astlocation_iterator 82DeclReferenceMap::refs_end(NamedDecl *D) const { 83 NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); 84 return astlocation_iterator(Map.upper_bound(Prim)); 85} 86 87bool DeclReferenceMap::refs_empty(NamedDecl *D) const { 88 NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); 89 return refs_begin(Prim) == refs_end(Prim); 90} 91