ASTStreamers.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "ASTStreamers.h"
  14. #include "clang/AST/AST.h"
  15. #include "clang/Lex/Preprocessor.h"
  16. #include "clang/Sema/ASTStreamer.h"
  17. void clang::BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
  18. // collect global stats on Decls/Stmts (until we have a module streamer)
  19. if (Stats) {
  20. Decl::CollectingStats(true);
  21. Stmt::CollectingStats(true);
  22. }
  23. ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
  24. ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
  25. while (ASTStreamer_ReadTopLevelDecl(Streamer))
  26. /* keep reading */;
  27. if (Stats) {
  28. fprintf(stderr, "\nSTATISTICS:\n");
  29. ASTStreamer_PrintStats(Streamer);
  30. Context.PrintStats();
  31. Decl::PrintStats();
  32. Stmt::PrintStats();
  33. }
  34. ASTStreamer_Terminate(Streamer);
  35. }
  36. void clang::PrintFunctionDecl(FunctionDecl *FD) {
  37. bool HasBody = FD->getBody();
  38. std::string Proto = FD->getName();
  39. FunctionType *AFT = cast<FunctionType>(FD->getType());
  40. if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
  41. Proto += "(";
  42. for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
  43. if (i) Proto += ", ";
  44. std::string ParamStr;
  45. if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
  46. FT->getArgType(i).getAsStringInternal(ParamStr);
  47. Proto += ParamStr;
  48. }
  49. if (FT->isVariadic()) {
  50. if (FD->getNumParams()) Proto += ", ";
  51. Proto += "...";
  52. }
  53. Proto += ")";
  54. } else {
  55. assert(isa<FunctionTypeNoProto>(AFT));
  56. Proto += "()";
  57. }
  58. AFT->getResultType().getAsStringInternal(Proto);
  59. fprintf(stderr, "\n%s", Proto.c_str());
  60. if (FD->getBody()) {
  61. fprintf(stderr, " ");
  62. FD->getBody()->dump();
  63. fprintf(stderr, "\n");
  64. } else {
  65. fprintf(stderr, ";\n");
  66. }
  67. }
  68. void clang::PrintTypeDefDecl(TypedefDecl *TD) {
  69. std::string S = TD->getName();
  70. TD->getUnderlyingType().getAsStringInternal(S);
  71. fprintf(stderr, "typedef %s;\n", S.c_str());
  72. }
  73. void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
  74. ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
  75. ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
  76. while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
  77. if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  78. PrintFunctionDecl(FD);
  79. } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
  80. PrintTypeDefDecl(TD);
  81. } else {
  82. fprintf(stderr, "Read top-level variable decl: '%s'\n", D->getName());
  83. }
  84. }
  85. if (Stats) {
  86. fprintf(stderr, "\nSTATISTICS:\n");
  87. ASTStreamer_PrintStats(Streamer);
  88. Context.PrintStats();
  89. }
  90. ASTStreamer_Terminate(Streamer);
  91. }