blob: 7f67cd405d7065f75463a8b986810f51ffa622ea [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- 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"
Chris Lattner95578782007-08-08 22:51:59 +000018using namespace clang;
Chris Lattner4b009652007-07-25 00:24:17 +000019
20void clang::BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
21 // collect global stats on Decls/Stmts (until we have a module streamer)
22 if (Stats) {
23 Decl::CollectingStats(true);
24 Stmt::CollectingStats(true);
25 }
26
27 ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
28 ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
29
30 while (ASTStreamer_ReadTopLevelDecl(Streamer))
31 /* keep reading */;
32
33 if (Stats) {
34 fprintf(stderr, "\nSTATISTICS:\n");
35 ASTStreamer_PrintStats(Streamer);
36 Context.PrintStats();
37 Decl::PrintStats();
38 Stmt::PrintStats();
39 }
40
41 ASTStreamer_Terminate(Streamer);
42}
43
Chris Lattner95578782007-08-08 22:51:59 +000044
45
46static void PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +000047 bool HasBody = FD->getBody();
48
49 std::string Proto = FD->getName();
50 FunctionType *AFT = cast<FunctionType>(FD->getType());
51
52 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
53 Proto += "(";
54 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
55 if (i) Proto += ", ";
56 std::string ParamStr;
57 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
58
59 FT->getArgType(i).getAsStringInternal(ParamStr);
60 Proto += ParamStr;
61 }
62
63 if (FT->isVariadic()) {
64 if (FD->getNumParams()) Proto += ", ";
65 Proto += "...";
66 }
67 Proto += ")";
68 } else {
69 assert(isa<FunctionTypeNoProto>(AFT));
70 Proto += "()";
71 }
72
73 AFT->getResultType().getAsStringInternal(Proto);
74 fprintf(stderr, "\n%s", Proto.c_str());
75
Chris Lattner95578782007-08-08 22:51:59 +000076 if (!FD->getBody())
Chris Lattner4b009652007-07-25 00:24:17 +000077 fprintf(stderr, ";\n");
Chris Lattner95578782007-08-08 22:51:59 +000078 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +000079}
80
Chris Lattner95578782007-08-08 22:51:59 +000081static void PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +000082 std::string S = TD->getName();
83 TD->getUnderlyingType().getAsStringInternal(S);
84 fprintf(stderr, "typedef %s;\n", S.c_str());
85}
86
87void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
88 ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
89 ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
90
91 while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
92 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Chris Lattner95578782007-08-08 22:51:59 +000093 PrintFunctionDeclStart(FD);
94
95 if (FD->getBody()) {
96 fprintf(stderr, " ");
97 FD->getBody()->dumpPretty();
98 fprintf(stderr, "\n");
99 }
Chris Lattner4b009652007-07-25 00:24:17 +0000100 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
101 PrintTypeDefDecl(TD);
102 } else {
103 fprintf(stderr, "Read top-level variable decl: '%s'\n", D->getName());
104 }
105 }
106
107 if (Stats) {
108 fprintf(stderr, "\nSTATISTICS:\n");
109 ASTStreamer_PrintStats(Streamer);
110 Context.PrintStats();
111 }
112
113 ASTStreamer_Terminate(Streamer);
114}
Chris Lattner95578782007-08-08 22:51:59 +0000115
116void clang::DumpASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
117 ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
118 ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
119
120 while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
121 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
122 PrintFunctionDeclStart(FD);
123
124 if (FD->getBody()) {
125 fprintf(stderr, "\n");
126 FD->getBody()->dumpAll();
127 fprintf(stderr, "\n");
128 }
129 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
130 PrintTypeDefDecl(TD);
131 } else {
132 fprintf(stderr, "Read top-level variable decl: '%s'\n", D->getName());
133 }
134 }
135
136 if (Stats) {
137 fprintf(stderr, "\nSTATISTICS:\n");
138 ASTStreamer_PrintStats(Streamer);
139 Context.PrintStats();
140 }
141
142 ASTStreamer_Terminate(Streamer);
143}
144
145