From: Chris Lattner Date: Wed, 2 Jan 2008 21:04:16 +0000 (+0000) Subject: Refactor the decl printer, patch by Mike Stump! X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ef5a85d364108fc26631f5e7c569bc4544bdb108;p=clang Refactor the decl printer, patch by Mike Stump! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45497 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp index a2d634ffe8..2a391986f4 100644 --- a/Driver/ASTConsumers.cpp +++ b/Driver/ASTConsumers.cpp @@ -36,6 +36,7 @@ namespace { DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {} DeclPrinter() : Out(*llvm::cerr.stream()) {} + void PrintDecl(Decl *D); void PrintFunctionDeclStart(FunctionDecl *FD); void PrintTypeDefDecl(TypedefDecl *TD); void PrintObjcMethodDecl(ObjcMethodDecl *OMD); @@ -48,6 +49,56 @@ namespace { }; } // end anonymous namespace +void DeclPrinter:: PrintDecl(Decl *D) { + if (FunctionDecl *FD = dyn_cast(D)) { + PrintFunctionDeclStart(FD); + + if (FD->getBody()) { + Out << ' '; + FD->getBody()->printPretty(Out); + Out << '\n'; + } + } else if (isa(D)) { + // Do nothing, methods definitions are printed in + // PrintObjcImplementationDecl. + } else if (TypedefDecl *TD = dyn_cast(D)) { + PrintTypeDefDecl(TD); + } else if (ObjcInterfaceDecl *OID = dyn_cast(D)) { + PrintObjcInterfaceDecl(OID); + } else if (ObjcProtocolDecl *PID = dyn_cast(D)) { + PrintObjcProtocolDecl(PID); + } else if (ObjcForwardProtocolDecl *OFPD = + dyn_cast(D)) { + Out << "@protocol "; + for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) { + const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i); + if (i) Out << ", "; + Out << D->getName(); + } + Out << ";\n"; + } else if (ObjcImplementationDecl *OID = + dyn_cast(D)) { + PrintObjcImplementationDecl(OID); + } else if (ObjcCategoryImplDecl *OID = + dyn_cast(D)) { + PrintObjcCategoryImplDecl(OID); + } else if (ObjcCategoryDecl *OID = + dyn_cast(D)) { + PrintObjcCategoryDecl(OID); + } else if (ObjcCompatibleAliasDecl *OID = + dyn_cast(D)) { + PrintObjcCompatibleAliasDecl(OID); + } else if (isa(D)) { + Out << "@class [printing todo]\n"; + } else if (TagDecl *TD = dyn_cast(D)) { + Out << "Read top-level tag decl: '" << TD->getName() << "'\n"; + } else if (ScopedDecl *SD = dyn_cast(D)) { + Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; + } else { + assert(0 && "Unknown decl type!"); + } +} + void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { bool HasBody = FD->getBody(); @@ -295,53 +346,7 @@ namespace { ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {} virtual void HandleTopLevelDecl(Decl *D) { - if (FunctionDecl *FD = dyn_cast(D)) { - PrintFunctionDeclStart(FD); - - if (FD->getBody()) { - Out << ' '; - FD->getBody()->printPretty(Out); - Out << '\n'; - } - } else if (isa(D)) { - // Do nothing, methods definitions are printed in - // PrintObjcImplementationDecl. - } else if (TypedefDecl *TD = dyn_cast(D)) { - PrintTypeDefDecl(TD); - } else if (ObjcInterfaceDecl *OID = dyn_cast(D)) { - PrintObjcInterfaceDecl(OID); - } else if (ObjcProtocolDecl *PID = dyn_cast(D)) { - PrintObjcProtocolDecl(PID); - } else if (ObjcForwardProtocolDecl *OFPD = - dyn_cast(D)) { - Out << "@protocol "; - for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) { - const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i); - if (i) Out << ", "; - Out << D->getName(); - } - Out << ";\n"; - } else if (ObjcImplementationDecl *OID = - dyn_cast(D)) { - PrintObjcImplementationDecl(OID); - } else if (ObjcCategoryImplDecl *OID = - dyn_cast(D)) { - PrintObjcCategoryImplDecl(OID); - } else if (ObjcCategoryDecl *OID = - dyn_cast(D)) { - PrintObjcCategoryDecl(OID); - } else if (ObjcCompatibleAliasDecl *OID = - dyn_cast(D)) { - PrintObjcCompatibleAliasDecl(OID); - } else if (isa(D)) { - Out << "@class [printing todo]\n"; - } else if (TagDecl *TD = dyn_cast(D)) { - Out << "Read top-level tag decl: '" << TD->getName() << "'\n"; - } else if (ScopedDecl *SD = dyn_cast(D)) { - Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; - } else { - assert(0 && "Unknown decl type!"); - } + PrintDecl(D); } }; }