diff options
Diffstat (limited to 'clang/Driver')
-rw-r--r-- | clang/Driver/ASTStreamers.cpp | 54 | ||||
-rw-r--r-- | clang/Driver/ASTStreamers.h | 3 | ||||
-rw-r--r-- | clang/Driver/clang.cpp | 6 |
3 files changed, 52 insertions, 11 deletions
diff --git a/clang/Driver/ASTStreamers.cpp b/clang/Driver/ASTStreamers.cpp index 19e12bd69e2..7f67cd405d7 100644 --- a/clang/Driver/ASTStreamers.cpp +++ b/clang/Driver/ASTStreamers.cpp @@ -15,6 +15,7 @@ #include "clang/AST/AST.h" #include "clang/Lex/Preprocessor.h" #include "clang/Sema/ASTStreamer.h" +using namespace clang; void clang::BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) { // collect global stats on Decls/Stmts (until we have a module streamer) @@ -40,7 +41,9 @@ void clang::BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) { ASTStreamer_Terminate(Streamer); } -void clang::PrintFunctionDecl(FunctionDecl *FD) { + + +static void PrintFunctionDeclStart(FunctionDecl *FD) { bool HasBody = FD->getBody(); std::string Proto = FD->getName(); @@ -70,16 +73,12 @@ void clang::PrintFunctionDecl(FunctionDecl *FD) { AFT->getResultType().getAsStringInternal(Proto); fprintf(stderr, "\n%s", Proto.c_str()); - if (FD->getBody()) { - fprintf(stderr, " "); - FD->getBody()->dump(); - fprintf(stderr, "\n"); - } else { + if (!FD->getBody()) fprintf(stderr, ";\n"); - } + // Doesn't print the body. } -void clang::PrintTypeDefDecl(TypedefDecl *TD) { +static void PrintTypeDefDecl(TypedefDecl *TD) { std::string S = TD->getName(); TD->getUnderlyingType().getAsStringInternal(S); fprintf(stderr, "typedef %s;\n", S.c_str()); @@ -91,7 +90,13 @@ void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) { while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) { if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { - PrintFunctionDecl(FD); + PrintFunctionDeclStart(FD); + + if (FD->getBody()) { + fprintf(stderr, " "); + FD->getBody()->dumpPretty(); + fprintf(stderr, "\n"); + } } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { PrintTypeDefDecl(TD); } else { @@ -107,3 +112,34 @@ void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) { ASTStreamer_Terminate(Streamer); } + +void clang::DumpASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) { + ASTContext Context(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(); + fprintf(stderr, "\n"); + } + } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { + PrintTypeDefDecl(TD); + } else { + fprintf(stderr, "Read top-level variable decl: '%s'\n", D->getName()); + } + } + + if (Stats) { + fprintf(stderr, "\nSTATISTICS:\n"); + ASTStreamer_PrintStats(Streamer); + Context.PrintStats(); + } + + ASTStreamer_Terminate(Streamer); +} + + diff --git a/clang/Driver/ASTStreamers.h b/clang/Driver/ASTStreamers.h index 2cce217ce6d..93749a46e74 100644 --- a/clang/Driver/ASTStreamers.h +++ b/clang/Driver/ASTStreamers.h @@ -22,8 +22,7 @@ class TypedefDecl; void BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats); void PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats); -void PrintFunctionDecl(FunctionDecl *FD); -void PrintTypeDefDecl(TypedefDecl *TD); +void DumpASTs(Preprocessor &PP, unsigned MainFileID, bool Stats); } // end clang namespace diff --git a/clang/Driver/clang.cpp b/clang/Driver/clang.cpp index 7d8a4590f12..2ee8a93a300 100644 --- a/clang/Driver/clang.cpp +++ b/clang/Driver/clang.cpp @@ -49,6 +49,7 @@ Stats("stats", llvm::cl::desc("Print performance metrics and statistics")); enum ProgActions { EmitLLVM, // Emit a .ll file. ParseASTPrint, // Parse ASTs and print them. + ParseASTDump, // Parse ASTs and dump them. ParseASTCheck, // Parse ASTs and check diagnostics. ParseAST, // Parse ASTs. ParsePrintCallbacks, // Parse and print each callback. @@ -79,6 +80,8 @@ ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, "Run parser and build ASTs"), clEnumValN(ParseASTPrint, "parse-ast-print", "Run parser, build ASTs, then print ASTs"), + clEnumValN(ParseASTDump, "parse-ast-dump", + "Run parser, build ASTs, then dump them"), clEnumValN(ParseASTCheck, "parse-ast-check", "Run parser, build ASTs, then check diagnostics"), clEnumValN(EmitLLVM, "emit-llvm", @@ -819,6 +822,9 @@ static void ProcessInputFile(Preprocessor &PP, unsigned MainFileID, case ParseASTPrint: PrintASTs(PP, MainFileID, Stats); break; + case ParseASTDump: + DumpASTs(PP, MainFileID, Stats); + break; case EmitLLVM: EmitLLVMFromASTs(PP, MainFileID, Stats); break; |