clang API Documentation
00001 //===--- DeclReferenceMap.cpp - Map Decls to their references -------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // DeclReferenceMap creates a mapping from Decls to the ASTLocations that 00011 // reference them. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "clang/Index/DeclReferenceMap.h" 00016 #include "clang/Index/ASTLocation.h" 00017 #include "ASTVisitor.h" 00018 using namespace clang; 00019 using namespace idx; 00020 00021 namespace { 00022 00023 class RefMapper : public ASTVisitor<RefMapper> { 00024 DeclReferenceMap::MapTy ⤅ 00025 00026 public: 00027 RefMapper(DeclReferenceMap::MapTy &map) : Map(map) { } 00028 00029 void VisitDeclRefExpr(DeclRefExpr *Node); 00030 void VisitMemberExpr(MemberExpr *Node); 00031 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node); 00032 00033 void VisitTypedefTypeLoc(TypedefTypeLoc TL); 00034 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); 00035 }; 00036 00037 } // anonymous namespace 00038 00039 //===----------------------------------------------------------------------===// 00040 // RefMapper Implementation 00041 //===----------------------------------------------------------------------===// 00042 00043 void RefMapper::VisitDeclRefExpr(DeclRefExpr *Node) { 00044 NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getCanonicalDecl()); 00045 Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node))); 00046 } 00047 00048 void RefMapper::VisitMemberExpr(MemberExpr *Node) { 00049 NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getCanonicalDecl()); 00050 Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node))); 00051 } 00052 00053 void RefMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 00054 Map.insert(std::make_pair(Node->getDecl(), ASTLocation(CurrentDecl, Node))); 00055 } 00056 00057 void RefMapper::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 00058 NamedDecl *ND = TL.getTypedefNameDecl(); 00059 Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc()))); 00060 } 00061 00062 void RefMapper::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 00063 NamedDecl *ND = TL.getIFaceDecl(); 00064 Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc()))); 00065 } 00066 00067 //===----------------------------------------------------------------------===// 00068 // DeclReferenceMap Implementation 00069 //===----------------------------------------------------------------------===// 00070 00071 DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) { 00072 RefMapper(Map).Visit(Ctx.getTranslationUnitDecl()); 00073 } 00074 00075 DeclReferenceMap::astlocation_iterator 00076 DeclReferenceMap::refs_begin(NamedDecl *D) const { 00077 NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); 00078 return astlocation_iterator(Map.lower_bound(Prim)); 00079 } 00080 00081 DeclReferenceMap::astlocation_iterator 00082 DeclReferenceMap::refs_end(NamedDecl *D) const { 00083 NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); 00084 return astlocation_iterator(Map.upper_bound(Prim)); 00085 } 00086 00087 bool DeclReferenceMap::refs_empty(NamedDecl *D) const { 00088 NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); 00089 return refs_begin(Prim) == refs_end(Prim); 00090 }