--- /dev/null
+//===--- ASTVisitor.h - Visitor for an ASTContext ---------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the ASTVisitor interface.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_INDEX_ASTVISITOR_H
+#define LLVM_CLANG_INDEX_ASTVISITOR_H
+
+#include "clang/AST/DeclVisitor.h"
+#include "clang/AST/StmtVisitor.h"
+
+namespace clang {
+
+namespace idx {
+
+/// \brief Traverses the full AST, both Decls and Stmts.
+template<typename ImplClass>
+class ASTVisitor : public DeclVisitor<ImplClass>,
+ public StmtVisitor<ImplClass> {
+public:
+ ASTVisitor() : CurrentDecl(0) { }
+
+ Decl *CurrentDecl;
+
+ typedef ASTVisitor<ImplClass> Base;
+ typedef DeclVisitor<ImplClass> BaseDeclVisitor;
+ typedef StmtVisitor<ImplClass> BaseStmtVisitor;
+
+ using BaseStmtVisitor::Visit;
+
+ //===--------------------------------------------------------------------===//
+ // DeclVisitor
+ //===--------------------------------------------------------------------===//
+
+ void Visit(Decl *D) {
+ Decl *PrevDecl = CurrentDecl;
+ CurrentDecl = D;
+ BaseDeclVisitor::Visit(D);
+ CurrentDecl = PrevDecl;
+ }
+
+ void VisitFunctionDecl(FunctionDecl *D) {
+ BaseDeclVisitor::VisitValueDecl(D);
+ if (D->isThisDeclarationADefinition())
+ Visit(D->getBody());
+ }
+
+ void VisitObjCMethodDecl(ObjCMethodDecl *D) {
+ BaseDeclVisitor::VisitNamedDecl(D);
+ if (D->getBody())
+ Visit(D->getBody());
+ }
+
+ void VisitBlockDecl(BlockDecl *D) {
+ BaseDeclVisitor::VisitDecl(D);
+ Visit(D->getBody());
+ }
+
+ void VisitVarDecl(VarDecl *D) {
+ BaseDeclVisitor::VisitValueDecl(D);
+ if (Expr *Init = D->getInit())
+ Visit(Init);
+ }
+
+ void VisitDecl(Decl *D) {
+ if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D))
+ return;
+
+ if (DeclContext *DC = dyn_cast<DeclContext>(D))
+ static_cast<ImplClass*>(this)->VisitDeclContext(DC);
+ }
+
+ void VisitDeclContext(DeclContext *DC) {
+ for (DeclContext::decl_iterator
+ I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
+ Visit(*I);
+ }
+
+ //===--------------------------------------------------------------------===//
+ // StmtVisitor
+ //===--------------------------------------------------------------------===//
+
+ void VisitDeclStmt(DeclStmt *Node) {
+ for (DeclStmt::decl_iterator
+ I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I)
+ Visit(*I);
+ }
+
+ void VisitStmt(Stmt *Node) {
+ for (Stmt::child_iterator
+ I = Node->child_begin(), E = Node->child_end(); I != E; ++I)
+ if (*I)
+ Visit(*I);
+ }
+};
+
+} // namespace idx
+
+} // namespace clang
+
+#endif
#include "clang/Index/DeclReferenceMap.h"
#include "clang/Index/ASTLocation.h"
-#include "clang/AST/Decl.h"
-#include "clang/AST/Stmt.h"
-#include "clang/AST/DeclVisitor.h"
-#include "clang/AST/StmtVisitor.h"
+#include "ASTVisitor.h"
#include "llvm/Support/Compiler.h"
using namespace clang;
using namespace idx;
namespace {
-class VISIBILITY_HIDDEN StmtMapper : public StmtVisitor<StmtMapper> {
+class VISIBILITY_HIDDEN RefMapper : public ASTVisitor<RefMapper> {
DeclReferenceMap::MapTy ⤅
- Decl *Parent;
public:
- StmtMapper(DeclReferenceMap::MapTy &map, Decl *parent)
- : Map(map), Parent(parent) { }
+ RefMapper(DeclReferenceMap::MapTy &map) : Map(map) { }
- void VisitDeclStmt(DeclStmt *Node);
void VisitDeclRefExpr(DeclRefExpr *Node);
void VisitMemberExpr(MemberExpr *Node);
void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
- void VisitStmt(Stmt *Node);
-};
-
-class VISIBILITY_HIDDEN DeclMapper : public DeclVisitor<DeclMapper> {
- DeclReferenceMap::MapTy ⤅
-
-public:
- DeclMapper(DeclReferenceMap::MapTy &map)
- : Map(map) { }
-
- void VisitDeclContext(DeclContext *DC);
- void VisitVarDecl(VarDecl *D);
- void VisitFunctionDecl(FunctionDecl *D);
- void VisitObjCMethodDecl(ObjCMethodDecl *D);
- void VisitBlockDecl(BlockDecl *D);
- void VisitDecl(Decl *D);
};
} // anonymous namespace
//===----------------------------------------------------------------------===//
-// StmtMapper Implementation
+// RefMapper Implementation
//===----------------------------------------------------------------------===//
-void StmtMapper::VisitDeclStmt(DeclStmt *Node) {
- DeclMapper Mapper(Map);
- for (DeclStmt::decl_iterator
- I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I)
- Mapper.Visit(*I);
-}
-
-void StmtMapper::VisitDeclRefExpr(DeclRefExpr *Node) {
+void RefMapper::VisitDeclRefExpr(DeclRefExpr *Node) {
NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getCanonicalDecl());
- Map.insert(std::make_pair(PrimD, ASTLocation(Parent, Node)));
+ Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node)));
}
-void StmtMapper::VisitMemberExpr(MemberExpr *Node) {
+void RefMapper::VisitMemberExpr(MemberExpr *Node) {
NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getCanonicalDecl());
- Map.insert(std::make_pair(PrimD, ASTLocation(Parent, Node)));
-}
-
-void StmtMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
- Map.insert(std::make_pair(Node->getDecl(), ASTLocation(Parent, Node)));
-}
-
-void StmtMapper::VisitStmt(Stmt *Node) {
- for (Stmt::child_iterator
- I = Node->child_begin(), E = Node->child_end(); I != E; ++I)
- Visit(*I);
-}
-
-//===----------------------------------------------------------------------===//
-// DeclMapper Implementation
-//===----------------------------------------------------------------------===//
-
-void DeclMapper::VisitDeclContext(DeclContext *DC) {
- for (DeclContext::decl_iterator
- I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
- Visit(*I);
-}
-
-void DeclMapper::VisitFunctionDecl(FunctionDecl *D) {
- if (D->isThisDeclarationADefinition())
- StmtMapper(Map, D).Visit(D->getBody());
-}
-
-void DeclMapper::VisitObjCMethodDecl(ObjCMethodDecl *D) {
- if (D->getBody())
- StmtMapper(Map, D).Visit(D->getBody());
-}
-
-void DeclMapper::VisitBlockDecl(BlockDecl *D) {
- StmtMapper(Map, D).Visit(D->getBody());
-}
-
-void DeclMapper::VisitVarDecl(VarDecl *D) {
- if (Expr *Init = D->getInit())
- StmtMapper(Map, D).Visit(Init);
+ Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node)));
}
-void DeclMapper::VisitDecl(Decl *D) {
- if (DeclContext *DC = dyn_cast<DeclContext>(D))
- VisitDeclContext(DC);
+void RefMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
+ Map.insert(std::make_pair(Node->getDecl(), ASTLocation(CurrentDecl, Node)));
}
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) {
- DeclMapper(Map).Visit(Ctx.getTranslationUnitDecl());
+ RefMapper(Map).Visit(Ctx.getTranslationUnitDecl());
}
DeclReferenceMap::astlocation_iterator