DeclReferenceMap.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "clang/Index/DeclReferenceMap.h"
  15. #include "clang/Index/ASTLocation.h"
  16. #include "ASTVisitor.h"
  17. using namespace clang;
  18. using namespace idx;
  19. namespace {
  20. class RefMapper : public ASTVisitor<RefMapper> {
  21. DeclReferenceMap::MapTy &Map;
  22. public:
  23. RefMapper(DeclReferenceMap::MapTy &map) : Map(map) { }
  24. void VisitDeclRefExpr(DeclRefExpr *Node);
  25. void VisitMemberExpr(MemberExpr *Node);
  26. void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
  27. void VisitTypedefTypeLoc(TypedefTypeLoc TL);
  28. void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
  29. };
  30. } // anonymous namespace
  31. //===----------------------------------------------------------------------===//
  32. // RefMapper Implementation
  33. //===----------------------------------------------------------------------===//
  34. void RefMapper::VisitDeclRefExpr(DeclRefExpr *Node) {
  35. NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getCanonicalDecl());
  36. Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node)));
  37. }
  38. void RefMapper::VisitMemberExpr(MemberExpr *Node) {
  39. NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getCanonicalDecl());
  40. Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node)));
  41. }
  42. void RefMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
  43. Map.insert(std::make_pair(Node->getDecl(), ASTLocation(CurrentDecl, Node)));
  44. }
  45. void RefMapper::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
  46. NamedDecl *ND = TL.getTypedefNameDecl();
  47. Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc())));
  48. }
  49. void RefMapper::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
  50. NamedDecl *ND = TL.getIFaceDecl();
  51. Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc())));
  52. }
  53. //===----------------------------------------------------------------------===//
  54. // DeclReferenceMap Implementation
  55. //===----------------------------------------------------------------------===//
  56. DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) {
  57. RefMapper(Map).Visit(Ctx.getTranslationUnitDecl());
  58. }
  59. DeclReferenceMap::astlocation_iterator
  60. DeclReferenceMap::refs_begin(NamedDecl *D) const {
  61. NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl());
  62. return astlocation_iterator(Map.lower_bound(Prim));
  63. }
  64. DeclReferenceMap::astlocation_iterator
  65. DeclReferenceMap::refs_end(NamedDecl *D) const {
  66. NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl());
  67. return astlocation_iterator(Map.upper_bound(Prim));
  68. }
  69. bool DeclReferenceMap::refs_empty(NamedDecl *D) const {
  70. NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl());
  71. return refs_begin(Prim) == refs_end(Prim);
  72. }