convert ast printer and dumper ocver to ASTConsumer interface,

genericizing them and eliminating boilerplate code.

llvm-svn: 41992
This commit is contained in:
Chris Lattner 2007-09-15 23:02:28 +00:00
parent 75e0c8cf4c
commit 09c39db0c4
3 changed files with 56 additions and 61 deletions

View File

@ -13,6 +13,7 @@
#include "ASTStreamers.h" #include "ASTStreamers.h"
#include "clang/AST/AST.h" #include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/CFG.h" #include "clang/AST/CFG.h"
#include "clang/Analysis/LiveVariables.h" #include "clang/Analysis/LiveVariables.h"
#include "clang/Analysis/LocalCheckers.h" #include "clang/Analysis/LocalCheckers.h"
@ -80,12 +81,9 @@ static void PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) {
// FIXME: implement the rest... // FIXME: implement the rest...
} }
void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) { namespace {
ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(), class ASTPrinter : public ASTConsumer {
PP.getIdentifierTable()); virtual void HandleTopLevelDecl(Decl *D) {
ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
PrintFunctionDeclStart(FD); PrintFunctionDeclStart(FD);
@ -102,28 +100,26 @@ void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName()); 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) { ASTConsumer *clang::CreateASTPrinter() { return new ASTPrinter(); }
ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
PP.getIdentifierTable());
ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) { 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)) { if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
PrintFunctionDeclStart(FD); PrintFunctionDeclStart(FD);
if (FD->getBody()) { if (FD->getBody()) {
fprintf(stderr, "\n"); fprintf(stderr, "\n");
FD->getBody()->dumpAll(PP.getSourceManager()); FD->getBody()->dumpAll(*SM);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
} else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
@ -132,16 +128,11 @@ void clang::DumpASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName()); 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 // CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
// the CFGs for all function definitions. // the CFGs for all function definitions.

View File

@ -21,8 +21,8 @@ class FunctionDecl;
class TypedefDecl; class TypedefDecl;
class ASTConsumer; class ASTConsumer;
void PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats); ASTConsumer *CreateASTPrinter();
void DumpASTs(Preprocessor &PP, unsigned MainFileID, bool Stats); ASTConsumer *CreateASTDumper();
void DumpCFGs(Preprocessor &PP, unsigned MainFileID, void DumpCFGs(Preprocessor &PP, unsigned MainFileID,
bool Stats, bool use_graphviz = false); bool Stats, bool use_graphviz = false);

View File

@ -844,12 +844,16 @@ static void ProcessInputFile(Preprocessor &PP, unsigned MainFileID,
ParseAST(PP, MainFileID, NullConsumer, Stats); ParseAST(PP, MainFileID, NullConsumer, Stats);
break; break;
} }
case ParseASTPrint: case ParseASTPrint: {
PrintASTs(PP, MainFileID, Stats); std::auto_ptr<ASTConsumer> C(CreateASTPrinter());
ParseAST(PP, MainFileID, *C.get(), Stats);
break; break;
case ParseASTDump: }
DumpASTs(PP, MainFileID, Stats); case ParseASTDump: {
std::auto_ptr<ASTConsumer> C(CreateASTDumper());
ParseAST(PP, MainFileID, *C.get(), Stats);
break; break;
}
case ParseCFGDump: case ParseCFGDump:
DumpCFGs(PP, MainFileID, Stats); DumpCFGs(PP, MainFileID, Stats);
break; break;