]> granicus.if.org Git - clang/commitdiff
Refactor the decl printer, patch by Mike Stump!
authorChris Lattner <sabre@nondot.org>
Wed, 2 Jan 2008 21:04:16 +0000 (21:04 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 2 Jan 2008 21:04:16 +0000 (21:04 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45497 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/ASTConsumers.cpp

index a2d634ffe868d1d0c401d6faf140c62639c9427d..2a391986f421cd3e697a05195393f411f0814cad 100644 (file)
@@ -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<FunctionDecl>(D)) {
+    PrintFunctionDeclStart(FD);
+
+    if (FD->getBody()) {
+      Out << ' ';
+      FD->getBody()->printPretty(Out);
+      Out << '\n';
+    }
+  } else if (isa<ObjcMethodDecl>(D)) {
+    // Do nothing, methods definitions are printed in
+    // PrintObjcImplementationDecl.
+  } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
+    PrintTypeDefDecl(TD);
+  } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
+    PrintObjcInterfaceDecl(OID);
+  } else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) {
+    PrintObjcProtocolDecl(PID);
+  } else if (ObjcForwardProtocolDecl *OFPD = 
+            dyn_cast<ObjcForwardProtocolDecl>(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<ObjcImplementationDecl>(D)) {
+    PrintObjcImplementationDecl(OID);
+  } else if (ObjcCategoryImplDecl *OID = 
+            dyn_cast<ObjcCategoryImplDecl>(D)) {
+    PrintObjcCategoryImplDecl(OID);
+  } else if (ObjcCategoryDecl *OID = 
+            dyn_cast<ObjcCategoryDecl>(D)) {
+    PrintObjcCategoryDecl(OID);
+  } else if (ObjcCompatibleAliasDecl *OID = 
+            dyn_cast<ObjcCompatibleAliasDecl>(D)) {
+    PrintObjcCompatibleAliasDecl(OID);
+  } else if (isa<ObjcClassDecl>(D)) {
+    Out << "@class [printing todo]\n";
+  } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
+    Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
+  } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(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<FunctionDecl>(D)) {
-        PrintFunctionDeclStart(FD);
-        
-        if (FD->getBody()) {
-          Out << ' ';
-          FD->getBody()->printPretty(Out);
-          Out << '\n';
-        }
-      } else if (isa<ObjcMethodDecl>(D)) {
-           // Do nothing, methods definitions are printed in
-               // PrintObjcImplementationDecl.
-      } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
-        PrintTypeDefDecl(TD);
-      } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
-        PrintObjcInterfaceDecl(OID);
-      } else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) {
-        PrintObjcProtocolDecl(PID);
-      } else if (ObjcForwardProtocolDecl *OFPD = 
-                     dyn_cast<ObjcForwardProtocolDecl>(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<ObjcImplementationDecl>(D)) {
-        PrintObjcImplementationDecl(OID);
-      } else if (ObjcCategoryImplDecl *OID = 
-                 dyn_cast<ObjcCategoryImplDecl>(D)) {
-        PrintObjcCategoryImplDecl(OID);
-      } else if (ObjcCategoryDecl *OID = 
-                 dyn_cast<ObjcCategoryDecl>(D)) {
-        PrintObjcCategoryDecl(OID);
-      } else if (ObjcCompatibleAliasDecl *OID = 
-                 dyn_cast<ObjcCompatibleAliasDecl>(D)) {
-        PrintObjcCompatibleAliasDecl(OID);
-      } else if (isa<ObjcClassDecl>(D)) {
-        Out << "@class [printing todo]\n";
-      } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
-        Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
-      } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
-        Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
-      } else {
-        assert(0 && "Unknown decl type!");
-      }
+      PrintDecl(D);
     }
   };
 }