Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame^] | 1 | //===--- ASTStreamers.cpp - ASTStreamer Drivers ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Bill Wendling and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // ASTStreamer drivers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ASTStreamers.h" |
| 15 | #include "clang/AST/AST.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "clang/Sema/ASTStreamer.h" |
| 18 | |
| 19 | void clang::BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) { |
| 20 | // collect global stats on Decls/Stmts (until we have a module streamer) |
| 21 | if (Stats) { |
| 22 | Decl::CollectingStats(true); |
| 23 | Stmt::CollectingStats(true); |
| 24 | } |
| 25 | |
| 26 | ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable()); |
| 27 | ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID); |
| 28 | |
| 29 | while (ASTStreamer_ReadTopLevelDecl(Streamer)) |
| 30 | /* keep reading */; |
| 31 | |
| 32 | if (Stats) { |
| 33 | fprintf(stderr, "\nSTATISTICS:\n"); |
| 34 | ASTStreamer_PrintStats(Streamer); |
| 35 | Context.PrintStats(); |
| 36 | Decl::PrintStats(); |
| 37 | Stmt::PrintStats(); |
| 38 | } |
| 39 | |
| 40 | ASTStreamer_Terminate(Streamer); |
| 41 | } |
| 42 | |
| 43 | void clang::PrintFunctionDecl(FunctionDecl *FD) { |
| 44 | bool HasBody = FD->getBody(); |
| 45 | |
| 46 | std::string Proto = FD->getName(); |
| 47 | FunctionType *AFT = cast<FunctionType>(FD->getType()); |
| 48 | |
| 49 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) { |
| 50 | Proto += "("; |
| 51 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 52 | if (i) Proto += ", "; |
| 53 | std::string ParamStr; |
| 54 | if (HasBody) ParamStr = FD->getParamDecl(i)->getName(); |
| 55 | |
| 56 | FT->getArgType(i).getAsStringInternal(ParamStr); |
| 57 | Proto += ParamStr; |
| 58 | } |
| 59 | |
| 60 | if (FT->isVariadic()) { |
| 61 | if (FD->getNumParams()) Proto += ", "; |
| 62 | Proto += "..."; |
| 63 | } |
| 64 | Proto += ")"; |
| 65 | } else { |
| 66 | assert(isa<FunctionTypeNoProto>(AFT)); |
| 67 | Proto += "()"; |
| 68 | } |
| 69 | |
| 70 | AFT->getResultType().getAsStringInternal(Proto); |
| 71 | fprintf(stderr, "\n%s", Proto.c_str()); |
| 72 | |
| 73 | if (FD->getBody()) { |
| 74 | fprintf(stderr, " "); |
| 75 | FD->getBody()->dump(); |
| 76 | fprintf(stderr, "\n"); |
| 77 | } else { |
| 78 | fprintf(stderr, ";\n"); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void clang::PrintTypeDefDecl(TypedefDecl *TD) { |
| 83 | std::string S = TD->getName(); |
| 84 | TD->getUnderlyingType().getAsStringInternal(S); |
| 85 | fprintf(stderr, "typedef %s;\n", S.c_str()); |
| 86 | } |
| 87 | |
| 88 | void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) { |
| 89 | ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable()); |
| 90 | ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID); |
| 91 | |
| 92 | while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) { |
| 93 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 94 | PrintFunctionDecl(FD); |
| 95 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 96 | PrintTypeDefDecl(TD); |
| 97 | } else { |
| 98 | fprintf(stderr, "Read top-level variable decl: '%s'\n", D->getName()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (Stats) { |
| 103 | fprintf(stderr, "\nSTATISTICS:\n"); |
| 104 | ASTStreamer_PrintStats(Streamer); |
| 105 | Context.PrintStats(); |
| 106 | } |
| 107 | |
| 108 | ASTStreamer_Terminate(Streamer); |
| 109 | } |