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"
18 using namespace clang;
19 using namespace idx;
20
21 namespace {
22
23 class RefMapper : public ASTVisitor<RefMapper> {
24 DeclReferenceMap::MapTy ⤅
25
26 public:
RefMapper(DeclReferenceMap::MapTy & map)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
VisitDeclRefExpr(DeclRefExpr * Node)43 void RefMapper::VisitDeclRefExpr(DeclRefExpr *Node) {
44 NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getCanonicalDecl());
45 Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node)));
46 }
47
VisitMemberExpr(MemberExpr * Node)48 void RefMapper::VisitMemberExpr(MemberExpr *Node) {
49 NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getCanonicalDecl());
50 Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node)));
51 }
52
VisitObjCIvarRefExpr(ObjCIvarRefExpr * Node)53 void RefMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
54 Map.insert(std::make_pair(Node->getDecl(), ASTLocation(CurrentDecl, Node)));
55 }
56
VisitTypedefTypeLoc(TypedefTypeLoc TL)57 void RefMapper::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
58 NamedDecl *ND = TL.getTypedefNameDecl();
59 Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc())));
60 }
61
VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL)62 void 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
DeclReferenceMap(ASTContext & Ctx)71 DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) {
72 RefMapper(Map).Visit(Ctx.getTranslationUnitDecl());
73 }
74
75 DeclReferenceMap::astlocation_iterator
refs_begin(NamedDecl * D) const76 DeclReferenceMap::refs_begin(NamedDecl *D) const {
77 NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl());
78 return astlocation_iterator(Map.lower_bound(Prim));
79 }
80
81 DeclReferenceMap::astlocation_iterator
refs_end(NamedDecl * D) const82 DeclReferenceMap::refs_end(NamedDecl *D) const {
83 NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl());
84 return astlocation_iterator(Map.upper_bound(Prim));
85 }
86
refs_empty(NamedDecl * D) const87 bool DeclReferenceMap::refs_empty(NamedDecl *D) const {
88 NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl());
89 return refs_begin(Prim) == refs_end(Prim);
90 }
91