[cfe-commits] r41992 - in /cfe/trunk/Driver: ASTStreamers.cpp ASTStreamers.h clang.cpp
Chris Lattner
sabre at nondot.org
Sat Sep 15 16:02:29 PDT 2007
Author: lattner
Date: Sat Sep 15 18:02:28 2007
New Revision: 41992
URL: http://llvm.org/viewvc/llvm-project?rev=41992&view=rev
Log:
convert ast printer and dumper ocver to ASTConsumer interface,
genericizing them and eliminating boilerplate code.
Modified:
cfe/trunk/Driver/ASTStreamers.cpp
cfe/trunk/Driver/ASTStreamers.h
cfe/trunk/Driver/clang.cpp
Modified: cfe/trunk/Driver/ASTStreamers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTStreamers.cpp?rev=41992&r1=41991&r2=41992&view=diff
==============================================================================
--- cfe/trunk/Driver/ASTStreamers.cpp (original)
+++ cfe/trunk/Driver/ASTStreamers.cpp Sat Sep 15 18:02:28 2007
@@ -13,6 +13,7 @@
#include "ASTStreamers.h"
#include "clang/AST/AST.h"
+#include "clang/AST/ASTConsumer.h"
#include "clang/AST/CFG.h"
#include "clang/Analysis/LiveVariables.h"
#include "clang/Analysis/LocalCheckers.h"
@@ -80,68 +81,58 @@
// FIXME: implement the rest...
}
-void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
- ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
- PP.getIdentifierTable());
- ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
-
- while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
- if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
- PrintFunctionDeclStart(FD);
-
- if (FD->getBody()) {
- fprintf(stderr, " ");
- FD->getBody()->dumpPretty();
- fprintf(stderr, "\n");
+namespace {
+ class ASTPrinter : public ASTConsumer {
+ virtual void HandleTopLevelDecl(Decl *D) {
+ if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+ PrintFunctionDeclStart(FD);
+
+ if (FD->getBody()) {
+ fprintf(stderr, " ");
+ FD->getBody()->dumpPretty();
+ fprintf(stderr, "\n");
+ }
+ } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
+ PrintTypeDefDecl(TD);
+ } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
+ PrintObjcInterfaceDecl(OID);
+ } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
+ fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
}
- } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
- PrintTypeDefDecl(TD);
- } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
- PrintObjcInterfaceDecl(OID);
- } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
- fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
}
- }
-
- if (Stats) {
- fprintf(stderr, "\nSTATISTICS:\n");
- ASTStreamer_PrintStats(Streamer);
- Context.PrintStats();
- }
-
- ASTStreamer_Terminate(Streamer);
+ };
}
-void clang::DumpASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
- ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
- PP.getIdentifierTable());
- ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
-
- while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
- if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
- PrintFunctionDeclStart(FD);
-
- if (FD->getBody()) {
- fprintf(stderr, "\n");
- FD->getBody()->dumpAll(PP.getSourceManager());
- fprintf(stderr, "\n");
+ASTConsumer *clang::CreateASTPrinter() { return new ASTPrinter(); }
+
+namespace {
+ class ASTDumper : public ASTConsumer {
+ SourceManager *SM;
+ public:
+ void Initialize(ASTContext &Context, unsigned MainFileID) {
+ SM = &Context.SourceMgr;
+ }
+
+ virtual void HandleTopLevelDecl(Decl *D) {
+ if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+ PrintFunctionDeclStart(FD);
+
+ if (FD->getBody()) {
+ fprintf(stderr, "\n");
+ FD->getBody()->dumpAll(*SM);
+ fprintf(stderr, "\n");
+ }
+ } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
+ PrintTypeDefDecl(TD);
+ } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
+ fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
}
- } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
- PrintTypeDefDecl(TD);
- } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
- fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
}
- }
-
- if (Stats) {
- fprintf(stderr, "\nSTATISTICS:\n");
- ASTStreamer_PrintStats(Streamer);
- Context.PrintStats();
- }
-
- ASTStreamer_Terminate(Streamer);
+ };
}
+ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
+
//===----------------------------------------------------------------------===//
// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
// the CFGs for all function definitions.
Modified: cfe/trunk/Driver/ASTStreamers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTStreamers.h?rev=41992&r1=41991&r2=41992&view=diff
==============================================================================
--- cfe/trunk/Driver/ASTStreamers.h (original)
+++ cfe/trunk/Driver/ASTStreamers.h Sat Sep 15 18:02:28 2007
@@ -21,8 +21,8 @@
class TypedefDecl;
class ASTConsumer;
-void PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats);
-void DumpASTs(Preprocessor &PP, unsigned MainFileID, bool Stats);
+ASTConsumer *CreateASTPrinter();
+ASTConsumer *CreateASTDumper();
void DumpCFGs(Preprocessor &PP, unsigned MainFileID,
bool Stats, bool use_graphviz = false);
Modified: cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/clang.cpp?rev=41992&r1=41991&r2=41992&view=diff
==============================================================================
--- cfe/trunk/Driver/clang.cpp (original)
+++ cfe/trunk/Driver/clang.cpp Sat Sep 15 18:02:28 2007
@@ -844,12 +844,16 @@
ParseAST(PP, MainFileID, NullConsumer, Stats);
break;
}
- case ParseASTPrint:
- PrintASTs(PP, MainFileID, Stats);
+ case ParseASTPrint: {
+ std::auto_ptr<ASTConsumer> C(CreateASTPrinter());
+ ParseAST(PP, MainFileID, *C.get(), Stats);
break;
- case ParseASTDump:
- DumpASTs(PP, MainFileID, Stats);
+ }
+ case ParseASTDump: {
+ std::auto_ptr<ASTConsumer> C(CreateASTDumper());
+ ParseAST(PP, MainFileID, *C.get(), Stats);
break;
+ }
case ParseCFGDump:
DumpCFGs(PP, MainFileID, Stats);
break;
More information about the cfe-commits
mailing list