2009-07-13 06:33:12 +08:00
|
|
|
//===--- DeclReferenceMap.cpp - Map Decls to their references -------------===//
|
2009-07-06 06:22:06 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-07-07 05:34:20 +08:00
|
|
|
// DeclReferenceMap creates a mapping from Decls to the ASTLocations that
|
|
|
|
// reference them.
|
2009-07-06 06:22:06 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-07 05:34:47 +08:00
|
|
|
#include "clang/Index/DeclReferenceMap.h"
|
|
|
|
#include "clang/Index/ASTLocation.h"
|
2009-07-30 07:40:39 +08:00
|
|
|
#include "ASTVisitor.h"
|
2009-07-06 06:22:06 +08:00
|
|
|
using namespace clang;
|
2009-07-07 05:34:47 +08:00
|
|
|
using namespace idx;
|
2009-07-06 06:22:06 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-11-29 03:45:26 +08:00
|
|
|
class RefMapper : public ASTVisitor<RefMapper> {
|
2009-07-06 06:22:06 +08:00
|
|
|
DeclReferenceMap::MapTy ⤅
|
|
|
|
|
|
|
|
public:
|
2009-07-30 07:40:39 +08:00
|
|
|
RefMapper(DeclReferenceMap::MapTy &map) : Map(map) { }
|
2009-07-06 06:22:06 +08:00
|
|
|
|
|
|
|
void VisitDeclRefExpr(DeclRefExpr *Node);
|
2009-07-14 11:18:09 +08:00
|
|
|
void VisitMemberExpr(MemberExpr *Node);
|
2009-07-21 08:05:38 +08:00
|
|
|
void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
|
2009-09-30 05:26:53 +08:00
|
|
|
|
2009-10-18 09:05:36 +08:00
|
|
|
void VisitTypedefTypeLoc(TypedefTypeLoc TL);
|
|
|
|
void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
|
2009-07-06 06:22:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-07-30 07:40:39 +08:00
|
|
|
// RefMapper Implementation
|
2009-07-06 06:22:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-30 07:40:39 +08:00
|
|
|
void RefMapper::VisitDeclRefExpr(DeclRefExpr *Node) {
|
2009-07-18 08:34:07 +08:00
|
|
|
NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getCanonicalDecl());
|
2009-07-30 07:40:39 +08:00
|
|
|
Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node)));
|
2009-07-06 06:22:06 +08:00
|
|
|
}
|
|
|
|
|
2009-07-30 07:40:39 +08:00
|
|
|
void RefMapper::VisitMemberExpr(MemberExpr *Node) {
|
2009-07-18 08:34:07 +08:00
|
|
|
NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getCanonicalDecl());
|
2009-07-30 07:40:39 +08:00
|
|
|
Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node)));
|
2009-07-06 06:22:06 +08:00
|
|
|
}
|
|
|
|
|
2009-07-30 07:40:39 +08:00
|
|
|
void RefMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
|
|
|
|
Map.insert(std::make_pair(Node->getDecl(), ASTLocation(CurrentDecl, Node)));
|
2009-07-06 06:22:06 +08:00
|
|
|
}
|
|
|
|
|
2009-10-18 09:05:36 +08:00
|
|
|
void RefMapper::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
|
2011-04-15 22:24:37 +08:00
|
|
|
NamedDecl *ND = TL.getTypedefNameDecl();
|
2009-09-30 05:26:53 +08:00
|
|
|
Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc())));
|
|
|
|
}
|
|
|
|
|
2009-10-18 09:05:36 +08:00
|
|
|
void RefMapper::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
|
2009-09-30 05:26:53 +08:00
|
|
|
NamedDecl *ND = TL.getIFaceDecl();
|
|
|
|
Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc())));
|
|
|
|
}
|
|
|
|
|
2009-07-06 06:22:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DeclReferenceMap Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) {
|
2009-07-30 07:40:39 +08:00
|
|
|
RefMapper(Map).Visit(Ctx.getTranslationUnitDecl());
|
2009-07-06 06:22:06 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 05:34:20 +08:00
|
|
|
DeclReferenceMap::astlocation_iterator
|
2009-07-06 06:22:06 +08:00
|
|
|
DeclReferenceMap::refs_begin(NamedDecl *D) const {
|
2009-07-18 08:34:07 +08:00
|
|
|
NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
return astlocation_iterator(Map.lower_bound(Prim));
|
2009-07-06 06:22:06 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 05:34:20 +08:00
|
|
|
DeclReferenceMap::astlocation_iterator
|
2009-07-06 06:22:06 +08:00
|
|
|
DeclReferenceMap::refs_end(NamedDecl *D) const {
|
2009-07-18 08:34:07 +08:00
|
|
|
NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
return astlocation_iterator(Map.upper_bound(Prim));
|
2009-07-06 06:22:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclReferenceMap::refs_empty(NamedDecl *D) const {
|
2009-07-18 08:34:07 +08:00
|
|
|
NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
return refs_begin(Prim) == refs_end(Prim);
|
2009-07-06 06:22:06 +08:00
|
|
|
}
|