1 //= CFGRecStmtDeclVisitor - Recursive visitor of CFG stmts/decls -*- C++ --*-=// 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 // This file implements the template class CFGRecStmtDeclVisitor, which extends 11 // CFGRecStmtVisitor by implementing (typed) visitation of decls. 12 // 13 // FIXME: This may not be fully complete. We currently explore only subtypes 14 // of ScopedDecl. 15 //===----------------------------------------------------------------------===// 16 17 #ifndef LLVM_CLANG_ANALYSIS_CFG_REC_STMT_DECL_VISITOR_H 18 #define LLVM_CLANG_ANALYSIS_CFG_REC_STMT_DECL_VISITOR_H 19 20 #include "clang/Analysis/Visitors/CFGRecStmtVisitor.h" 21 #include "clang/AST/Decl.h" 22 #include "clang/AST/DeclObjC.h" 23 #include "clang/AST/DeclCXX.h" 24 25 #define DISPATCH_CASE(CLASS) \ 26 case Decl::CLASS: \ 27 static_cast<ImplClass*>(this)->Visit##CLASS##Decl( \ 28 static_cast<CLASS##Decl*>(D)); \ 29 break; 30 31 #define DEFAULT_DISPATCH(CLASS) void Visit##CLASS##Decl(CLASS##Decl *D) {} 32 #define DEFAULT_DISPATCH_VARDECL(CLASS) void Visit##CLASS##Decl(CLASS##Decl *D)\ 33 { static_cast<ImplClass*>(this)->VisitVarDecl(D); } 34 35 36 namespace clang { 37 template <typename ImplClass> 38 class CFGRecStmtDeclVisitor : public CFGRecStmtVisitor<ImplClass> { 39 public: 40 VisitDeclRefExpr(DeclRefExpr * DR)41 void VisitDeclRefExpr(DeclRefExpr *DR) { 42 static_cast<ImplClass*>(this)->VisitDecl(DR->getDecl()); 43 } 44 VisitDeclStmt(DeclStmt * DS)45 void VisitDeclStmt(DeclStmt *DS) { 46 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); 47 DI != DE; ++DI) { 48 Decl *D = *DI; 49 static_cast<ImplClass*>(this)->VisitDecl(D); 50 // Visit the initializer. 51 if (VarDecl *VD = dyn_cast<VarDecl>(D)) 52 if (Expr *I = VD->getInit()) 53 static_cast<ImplClass*>(this)->Visit(I); 54 } 55 } 56 VisitDecl(Decl * D)57 void VisitDecl(Decl *D) { 58 switch (D->getKind()) { 59 DISPATCH_CASE(Function) 60 DISPATCH_CASE(CXXMethod) 61 DISPATCH_CASE(Var) 62 DISPATCH_CASE(ParmVar) // FIXME: (same) 63 DISPATCH_CASE(ImplicitParam) 64 DISPATCH_CASE(EnumConstant) 65 DISPATCH_CASE(Typedef) 66 DISPATCH_CASE(Record) // FIXME: Refine. VisitStructDecl? 67 DISPATCH_CASE(CXXRecord) 68 DISPATCH_CASE(Enum) 69 DISPATCH_CASE(Field) 70 DISPATCH_CASE(UsingDirective) 71 DISPATCH_CASE(Using) 72 default: 73 llvm_unreachable("Subtype of ScopedDecl not handled."); 74 } 75 } 76 77 DEFAULT_DISPATCH(Var) DEFAULT_DISPATCH(Function)78 DEFAULT_DISPATCH(Function) 79 DEFAULT_DISPATCH(CXXMethod) 80 DEFAULT_DISPATCH_VARDECL(ParmVar) 81 DEFAULT_DISPATCH(ImplicitParam) 82 DEFAULT_DISPATCH(EnumConstant) 83 DEFAULT_DISPATCH(Typedef) 84 DEFAULT_DISPATCH(Record) 85 DEFAULT_DISPATCH(Enum) 86 DEFAULT_DISPATCH(Field) 87 DEFAULT_DISPATCH(ObjCInterface) 88 DEFAULT_DISPATCH(ObjCMethod) 89 DEFAULT_DISPATCH(ObjCProtocol) 90 DEFAULT_DISPATCH(ObjCCategory) 91 DEFAULT_DISPATCH(UsingDirective) 92 DEFAULT_DISPATCH(Using) 93 94 void VisitCXXRecordDecl(CXXRecordDecl *D) { 95 static_cast<ImplClass*>(this)->VisitRecordDecl(D); 96 } 97 }; 98 99 } // end namespace clang 100 101 #undef DISPATCH_CASE 102 #undef DEFAULT_DISPATCH 103 #endif 104