blob: 2aa7c651b20081b372b195143aead7baef05c741 [file] [log] [blame]
Chris Lattner97e8b6f2007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner97e8b6f2007-10-07 06:04:32 +000010// AST Consumer Implementations.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner97e8b6f2007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Ted Kremenekad99dbf2008-11-03 22:31:48 +000015#include "clang/Driver/PathDiagnosticClients.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Nico Weberdae86962008-08-09 18:32:11 +000017#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000022#include "clang/CodeGen/ModuleBuilder.h"
23#include "llvm/Module.h"
Daniel Dunbard46075f2008-10-21 23:54:00 +000024#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000025#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000027
Chris Lattner6000dac2007-08-08 22:51:59 +000028using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000029
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000030//===----------------------------------------------------------------------===//
31/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033namespace {
34 class DeclPrinter {
35 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +000036 llvm::raw_ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000037
Ted Kremeneka95d3752008-09-13 05:16:45 +000038 DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()) {}
39 DeclPrinter() : Out(llvm::errs()) {}
40 virtual ~DeclPrinter();
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000041
Chris Lattneref5a85d2008-01-02 21:04:16 +000042 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000043 void PrintFunctionDeclStart(FunctionDecl *FD);
44 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000045 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000046 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
47 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
48 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
49 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
50 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
51 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
52 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +000053 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +000054 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000055 };
56} // end anonymous namespace
57
Ted Kremeneka95d3752008-09-13 05:16:45 +000058DeclPrinter::~DeclPrinter() {
59 Out.flush();
60}
61
Chris Lattneref5a85d2008-01-02 21:04:16 +000062void DeclPrinter:: PrintDecl(Decl *D) {
63 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
64 PrintFunctionDeclStart(FD);
65
66 if (FD->getBody()) {
67 Out << ' ';
68 FD->getBody()->printPretty(Out);
69 Out << '\n';
70 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000071 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000072 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000073 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000074 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
75 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
77 PrintObjCInterfaceDecl(OID);
78 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
79 PrintObjCProtocolDecl(PID);
80 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000081 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000082 Out << "@protocol ";
83 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000084 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000085 if (i) Out << ", ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000086 Out << D->getNameAsString();
Chris Lattneref5a85d2008-01-02 21:04:16 +000087 }
88 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000089 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000090 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 PrintObjCImplementationDecl(OID);
92 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000093 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000094 PrintObjCCategoryImplDecl(OID);
95 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000096 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 PrintObjCCategoryDecl(OID);
98 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000099 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000101 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
102 Out << "@class ";
103 ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
104 for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
105 const ObjCInterfaceDecl *D = ForwardDecls[i];
106 if (i) Out << ", ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000107 Out << D->getNameAsString();
Chris Lattner23a0e452008-06-21 21:40:20 +0000108 }
109 Out << ";\n";
Douglas Gregor45579f52008-12-17 02:04:30 +0000110 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
111 Out << "enum " << ED->getNameAsString() << " {\n";
112 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(),
113 EEnd = ED->enumerator_end();
114 E != EEnd; ++E)
115 Out << " " << (*E)->getNameAsString() << ",\n";
116 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000117 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000118 Out << "Read top-level tag decl: '" << TD->getNameAsString() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000119 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
120 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000121 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
122 Out << "asm(";
123 AD->getAsmString()->printPretty(Out);
124 Out << ")\n";
Douglas Gregor074149e2009-01-05 19:45:36 +0000125 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
126 Out << "Read top-level variable decl: '" << SD->getNameAsString() << "'\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000127 } else {
128 assert(0 && "Unknown decl type!");
129 }
130}
131
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000132void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 bool HasBody = FD->getBody();
134
Ted Kremenekea75c552007-11-28 21:32:21 +0000135 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000136
137 switch (FD->getStorageClass()) {
138 default: assert(0 && "Unknown storage class");
139 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000140 case FunctionDecl::Extern: Out << "extern "; break;
141 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000142 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000143 }
144
145 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000146 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000147
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000148 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000149 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000150
Chris Lattner0d6ca112007-12-03 21:43:25 +0000151 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 Proto += "(";
153 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
154 if (i) Proto += ", ";
155 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000156 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000157
158 FT->getArgType(i).getAsStringInternal(ParamStr);
159 Proto += ParamStr;
160 }
161
162 if (FT->isVariadic()) {
163 if (FD->getNumParams()) Proto += ", ";
164 Proto += "...";
165 }
166 Proto += ")";
167 } else {
168 assert(isa<FunctionTypeNoProto>(AFT));
169 Proto += "()";
170 }
171
172 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000173 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000174
Chris Lattner6000dac2007-08-08 22:51:59 +0000175 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000176 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000177 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000178}
179
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000180void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000181 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000183 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000184}
185
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000186void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
187 const char *l;
188 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
189 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000190 else {
191 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
192 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000193 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000194 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000195
196 Out << "extern \"" << l << "\" ";
197 if (LS->hasBraces())
198 Out << "{\n";
199
Douglas Gregor074149e2009-01-05 19:45:36 +0000200 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(),
201 DEnd = LS->decls_end();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000202 D != DEnd; ++D)
203 PrintDecl(*D);
204
205 if (LS->hasBraces())
206 Out << "}";
207 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000208}
209
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000210void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000211 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000212 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000213 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000214 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000215 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000216 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000217
Chris Lattner077bf5e2008-11-24 03:33:13 +0000218 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000219 std::string::size_type pos, lastPos = 0;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000220 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000221 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000222 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000223 pos = name.find_first_of(":", lastPos);
224 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000225 Out << ":(" << PDecl->getType().getAsString() << ")"
226 << PDecl->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000227 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000228 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000229
230 if (OMD->getNumParams() == 0)
231 Out << " " << name;
232
233 if (OMD->isVariadic())
234 Out << ", ...";
235
236 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000237}
238
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000239void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000240 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000241 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000242
243 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000244 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000245 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000246 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000247
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000248 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000249 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000250 ObjCMethodDecl *OMD = *I;
251 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000252 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000253 Out << ' ';
254 OMD->getBody()->printPretty(Out);
255 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000256 }
257 }
258
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000259 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000260 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000261 ObjCMethodDecl *OMD = *I;
262 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000263 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000264 Out << ' ';
265 OMD->getBody()->printPretty(Out);
266 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000267 }
268 }
269
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000270 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
271 E = OID->propimpl_end(); I != E; ++I)
272 PrintObjCPropertyImplDecl(*I);
273
Ted Kremenekea75c552007-11-28 21:32:21 +0000274 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000275}
276
277
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000278void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000279 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000280 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000281
282 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000283 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000284 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000285 Out << "@interface " << I;
286
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000287 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000288 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
289 if (!Protocols.empty()) {
290 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
291 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000292 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000293 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000294
Chris Lattner3db6cae2008-07-21 18:19:38 +0000295 if (!Protocols.empty())
296 Out << ">";
297 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000298
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000299 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000300 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000301 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000302 E = OID->ivar_end(); I != E; ++I) {
303 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000304 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000305 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000306 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000307 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000308
Steve Naroff09c47192009-01-09 15:36:25 +0000309 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(),
310 E = OID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000311 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000312 bool eol_needed = false;
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000313 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
314 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000315 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000316
317 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
318 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000319 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000320
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000321 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000322 // FIXME: implement the rest...
323}
324
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000325void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000326 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000327
Steve Naroff09c47192009-01-09 15:36:25 +0000328 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(),
329 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000330 PrintObjCPropertyDecl(*I);
331 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000332 // FIXME: implement the rest...
333}
334
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000335void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000336 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000337 << PID->getClassInterface()->getNameAsString()
338 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000339 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
340 E = PID->propimpl_end(); I != E; ++I)
341 PrintObjCPropertyImplDecl(*I);
342 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000343 // FIXME: implement the rest...
344}
345
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000346void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000347 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000348 << PID->getClassInterface()->getNameAsString()
349 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000350 // Output property declarations.
Steve Naroff09c47192009-01-09 15:36:25 +0000351 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(),
352 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000353 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000354 Out << "@end\n";
355
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000356 // FIXME: implement the rest...
357}
358
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000359void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000360 Out << "@compatibility_alias " << AID->getNameAsString()
361 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000362}
363
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000364/// PrintObjCPropertyDecl - print a property declaration.
365///
366void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000367 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
368 Out << "@required\n";
369 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
370 Out << "@optional\n";
371
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000372 Out << "@property";
373 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
374 bool first = true;
375 Out << " (";
376 if (PDecl->getPropertyAttributes() &
377 ObjCPropertyDecl::OBJC_PR_readonly) {
378 Out << (first ? ' ' : ',') << "readonly";
379 first = false;
380 }
381
382 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
383 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000384 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000385 first = false;
386 }
387 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
388 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000389 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000390 first = false;
391 }
392
393 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
394 Out << (first ? ' ' : ',') << "assign";
395 first = false;
396 }
397
398 if (PDecl->getPropertyAttributes() &
399 ObjCPropertyDecl::OBJC_PR_readwrite) {
400 Out << (first ? ' ' : ',') << "readwrite";
401 first = false;
402 }
403
404 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
405 Out << (first ? ' ' : ',') << "retain";
406 first = false;
407 }
408
409 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
410 Out << (first ? ' ' : ',') << "copy";
411 first = false;
412 }
413
414 if (PDecl->getPropertyAttributes() &
415 ObjCPropertyDecl::OBJC_PR_nonatomic) {
416 Out << (first ? ' ' : ',') << "nonatomic";
417 first = false;
418 }
419 Out << " )";
420 }
421 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000422 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000423
424 Out << ";\n";
425}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000426
427/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
428/// declaration syntax.
429///
430void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000431 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000432 Out << "\n@synthesize ";
433 else
434 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000435 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000436 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000437 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000438 Out << ";\n";
439}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000440//===----------------------------------------------------------------------===//
441/// ASTPrinter - Pretty-printer of ASTs
442
Chris Lattner3d4997d2007-09-15 23:02:28 +0000443namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000444 class ASTPrinter : public ASTConsumer, public DeclPrinter {
445 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000446 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000447
Chris Lattner3d4997d2007-09-15 23:02:28 +0000448 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000449 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000451 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000452}
Chris Lattner6000dac2007-08-08 22:51:59 +0000453
Ted Kremeneka95d3752008-09-13 05:16:45 +0000454ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000455 return new ASTPrinter(out);
456}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000457
458//===----------------------------------------------------------------------===//
459/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000460
461namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000462 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000463 SourceManager *SM;
464 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000465 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000466
Ted Kremenek95041a22007-12-19 22:51:13 +0000467 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000468 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000469 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000470
471 virtual void HandleTopLevelDecl(Decl *D) {
472 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
473 PrintFunctionDeclStart(FD);
474
475 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000476 Out << '\n';
477 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000478 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000479 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000480 }
481 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
482 PrintTypeDefDecl(TD);
483 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000484 Out << "Read top-level variable decl: '" << SD->getNameAsString()
485 << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000486 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000487 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000488 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000489 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000490 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000491 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000492 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000493 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000494 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000495 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000496 } else if (isa<FileScopeAsmDecl>(D)) {
497 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000498 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000499 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
Ted Kremenek63bbe532008-03-14 17:31:00 +0000500 << "'\n";
Steve Naroff1a2b90d2008-05-23 18:50:58 +0000501 if (MD->getBody()) {
502 // FIXME: convert dumper to use std::ostream?
503 MD->getBody()->dumpAll(*SM);
504 Out << '\n';
505 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000506 } else if (isa<ObjCImplementationDecl>(D)) {
507 Out << "Read objc implementation decl\n";
Daniel Dunbar539ced12008-10-05 00:31:15 +0000508 } else if (isa<ObjCCategoryImplDecl>(D)) {
509 Out << "Read objc category implementation decl\n";
Eli Friedmanf595eb02008-12-16 22:14:15 +0000510 } else if (isa<LinkageSpecDecl>(D)) {
511 Out << "Read linkage spec decl\n";
512 } else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000513 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000514 }
515 }
516 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000517}
518
Chris Lattner3d4997d2007-09-15 23:02:28 +0000519ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
520
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000521//===----------------------------------------------------------------------===//
522/// ASTViewer - AST Visualization
523
Ted Kremenek80de08f2007-09-19 21:29:43 +0000524namespace {
525 class ASTViewer : public ASTConsumer {
526 SourceManager *SM;
527 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000528 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000529 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000530 }
531
532 virtual void HandleTopLevelDecl(Decl *D) {
533 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000534 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000535
536 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000537 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000538 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000539 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000540 }
541 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000542 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
543 DeclPrinter().PrintObjCMethodDecl(MD);
544
545 if (MD->getBody()) {
546 llvm::cerr << '\n';
547 MD->getBody()->viewAST();
548 llvm::cerr << '\n';
549 }
550 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000551 }
552 };
553}
554
555ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
556
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000557//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000558/// DeclContextPrinter - Decl and DeclContext Visualization
559
560namespace {
561
562class DeclContextPrinter : public ASTConsumer {
563 llvm::raw_ostream& Out;
564public:
565 DeclContextPrinter() : Out(llvm::errs()) {}
566
567 void HandleTranslationUnit(TranslationUnit& TU) {
568 TranslationUnitDecl* TUD = TU.getContext().getTranslationUnitDecl();
569 PrintDeclContext(TUD, 4);
570 }
571
572 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
573};
574
575void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
576 unsigned Indentation) {
577 // Print DeclContext name.
578 Decl::Kind DK = DeclContext::KindTrait<DeclContext>::getKind(DC);
579 switch (DK) {
580 case Decl::TranslationUnit:
581 Out << "[translation unit] " << DC;
582 break;
583 case Decl::Namespace: {
584 Out << "[namespace] ";
585 NamespaceDecl* ND = NamespaceDecl::castFromDeclContext(DC);
586 Out << ND->getNameAsString();
587 break;
588 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000589 case Decl::Enum: {
590 EnumDecl* ED = EnumDecl::castFromDeclContext(DC);
591 if (ED->isDefinition())
592 Out << "[enum] ";
593 else
594 Out << "<enum> ";
595 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000596 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000597 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000598 case Decl::Record: {
599 RecordDecl* RD = RecordDecl::castFromDeclContext(DC);
600 if (RD->isDefinition())
601 Out << "[struct] ";
602 else
603 Out << "<struct> ";
604 Out << RD->getNameAsString();
605 break;
606 }
607 case Decl::CXXRecord: {
608 CXXRecordDecl* RD = CXXRecordDecl::castFromDeclContext(DC);
609 if (RD->isDefinition())
610 Out << "[class] ";
611 else
612 Out << "<class> ";
613 Out << RD->getNameAsString() << " " << DC;
614 break;
615 }
616 case Decl::ObjCMethod:
617 Out << "[objc method]";
618 break;
619 case Decl::ObjCInterface:
620 Out << "[objc interface]";
621 break;
622 case Decl::ObjCCategory:
623 Out << "[objc category]";
624 break;
625 case Decl::ObjCProtocol:
626 Out << "[objc protocol]";
627 break;
628 case Decl::ObjCImplementation:
629 Out << "[objc implementation]";
630 break;
631 case Decl::ObjCCategoryImpl:
632 Out << "[objc categoryimpl]";
633 break;
634 case Decl::LinkageSpec:
635 Out << "[linkage spec]";
636 break;
637 case Decl::Block:
638 Out << "[block]";
639 break;
640 case Decl::Function: {
641 FunctionDecl* FD = FunctionDecl::castFromDeclContext(DC);
642 if (FD->isThisDeclarationADefinition())
643 Out << "[function] ";
644 else
645 Out << "<function> ";
646 Out << FD->getNameAsString();
647 break;
648 }
649 case Decl::CXXMethod: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000650 CXXMethodDecl* D = CXXMethodDecl::castFromDeclContext(DC);
651 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000652 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000653 else if (D->isImplicit())
654 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000655 else
656 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000657 Out << D->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000658
659 // Check the semantic DeclContext.
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000660 DeclContext* SemaDC = D->getDeclContext();
661 DeclContext* LexicalDC = D->getLexicalDeclContext();
662 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000663 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000664
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000665 break;
666 }
667 case Decl::CXXConstructor: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000668 CXXConstructorDecl* D = CXXConstructorDecl::castFromDeclContext(DC);
669 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000670 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000671 else if (D->isImplicit())
672 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000673 else
674 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000675 Out << D->getNameAsString();
676 // Check the semantic DC.
677 DeclContext* SemaDC = D->getDeclContext();
678 DeclContext* LexicalDC = D->getLexicalDeclContext();
679 if (SemaDC != LexicalDC)
680 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000681 break;
682 }
683 case Decl::CXXDestructor: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000684 CXXDestructorDecl* D = CXXDestructorDecl::castFromDeclContext(DC);
685 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000686 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000687 else if (D->isImplicit())
688 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000689 else
690 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000691 Out << D->getNameAsString();
692 // Check the semantic DC.
693 DeclContext* SemaDC = D->getDeclContext();
694 DeclContext* LexicalDC = D->getLexicalDeclContext();
695 if (SemaDC != LexicalDC)
696 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000697 break;
698 }
699 case Decl::CXXConversion: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000700 CXXConversionDecl* D = CXXConversionDecl::castFromDeclContext(DC);
701 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000702 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000703 else if (D->isImplicit())
704 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000705 else
706 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000707 Out << D->getNameAsString();
708 // Check the semantic DC.
709 DeclContext* SemaDC = D->getDeclContext();
710 DeclContext* LexicalDC = D->getLexicalDeclContext();
711 if (SemaDC != LexicalDC)
712 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000713 break;
714 }
715
716 default:
717 assert(0 && "a decl that inherits DeclContext isn't handled");
718 }
719
720 Out << "\n";
721
722 // Print decls in the DeclContext.
723 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
724 I != E; ++I) {
725 for (unsigned i = 0; i < Indentation; ++i)
726 Out << " ";
727
728 Decl::Kind DK = I->getKind();
729 switch (DK) {
730 case Decl::Namespace:
731 case Decl::Enum:
732 case Decl::Record:
733 case Decl::CXXRecord:
734 case Decl::ObjCMethod:
735 case Decl::ObjCInterface:
736 case Decl::ObjCCategory:
737 case Decl::ObjCProtocol:
738 case Decl::ObjCImplementation:
739 case Decl::ObjCCategoryImpl:
740 case Decl::LinkageSpec:
741 case Decl::Block:
742 case Decl::Function:
743 case Decl::CXXMethod:
744 case Decl::CXXConstructor:
745 case Decl::CXXDestructor:
746 case Decl::CXXConversion:
747 {
748 DeclContext* DC = Decl::castToDeclContext(*I);
749 PrintDeclContext(DC, Indentation+4);
750 break;
751 }
752 case Decl::Field: {
753 FieldDecl* FD = cast<FieldDecl>(*I);
754 Out << "<field> " << FD->getNameAsString() << "\n";
755 break;
756 }
757 case Decl::Typedef: {
758 TypedefDecl* TD = cast<TypedefDecl>(*I);
759 Out << "<typedef> " << TD->getNameAsString() << "\n";
760 break;
761 }
762 case Decl::EnumConstant: {
763 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
764 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
765 break;
766 }
767 case Decl::Var: {
768 VarDecl* VD = cast<VarDecl>(*I);
769 Out << "<var> " << VD->getNameAsString() << "\n";
770 break;
771 }
772 case Decl::ImplicitParam: {
773 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
774 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
775 break;
776 }
777 case Decl::CXXClassVar: {
778 CXXClassVarDecl* CVD = cast<CXXClassVarDecl>(*I);
779 Out << "<static member var> " << CVD->getNameAsString() << "\n";
780 break;
781 }
782 case Decl::ParmVar: {
783 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
784 Out << "<parameter> " << PVD->getNameAsString() << "\n";
785 break;
786 }
787 case Decl::ObjCProperty: {
788 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
789 Out << "<objc property> " << OPD->getNameAsString() << "\n";
790 break;
791 }
792 default:
793 fprintf(stderr, "DeclKind: %d\n", DK);
794 assert(0 && "decl unhandled");
795 }
796 }
797}
798
799}
800
801ASTConsumer *clang::CreateDeclContextPrinter() {
802 return new DeclContextPrinter();
803}
804
805//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000806/// InheritanceViewer - C++ Inheritance Visualization
807
808namespace {
809class InheritanceViewer : public ASTConsumer {
810 const std::string clsname;
811public:
812 InheritanceViewer(const std::string& cname) : clsname(cname) {}
813
814 void HandleTranslationUnit(TranslationUnit& TU) {
815 ASTContext& C = TU.getContext();
816 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
817 if (CXXRecordType *T = dyn_cast<CXXRecordType>(*I)) {
818 CXXRecordDecl* D = T->getDecl();
819 // FIXME: This lookup needs to be generalized to handle namespaces and
820 // (when we support them) templates.
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000821 if (D->getNameAsString() == clsname) {
Douglas Gregor1f812302008-10-24 19:53:54 +0000822 D->viewInheritance(C);
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000823 }
824 }
825 }
826};
827}
828
829ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
830 return new InheritanceViewer(clsname);
831}
832
833//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000834// AST Serializer
835
836namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000837
838class ASTSerializer : public ASTConsumer {
839protected:
Nico Weberdae86962008-08-09 18:32:11 +0000840 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000841
Ted Kremenekf06c9282007-12-19 23:49:37 +0000842public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000843 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000844};
Eli Friedmand8a65c12008-06-09 20:02:51 +0000845
Ted Kremenekf06c9282007-12-19 23:49:37 +0000846class SingleFileSerializer : public ASTSerializer {
847 const llvm::sys::Path FName;
848public:
Nico Weberdae86962008-08-09 18:32:11 +0000849 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
850 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000851
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000852 virtual void HandleTranslationUnit(TranslationUnit& TU) {
Nico Weberdae86962008-08-09 18:32:11 +0000853 if (Diags.hasErrorOccurred())
854 return;
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000855 EmitASTBitcodeFile(&TU, FName);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000856 }
857};
858
859class BuildSerializer : public ASTSerializer {
860 llvm::sys::Path EmitDir;
861public:
Nico Weberdae86962008-08-09 18:32:11 +0000862 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
863 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000864
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000865 virtual void HandleTranslationUnit(TranslationUnit& TU) {
866 if (Diags.hasErrorOccurred())
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000867 return;
868
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000869 SourceManager& SourceMgr = TU.getContext().getSourceManager();
Ted Kremenek54117722007-12-20 00:34:58 +0000870 unsigned ID = SourceMgr.getMainFileID();
871 assert (ID && "MainFileID not set!");
872 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
873 assert (FE && "No FileEntry for main file.");
874
875 // FIXME: This is not portable to Windows.
876 // FIXME: This logic should probably be moved elsewhere later.
877
Ted Kremenekee533642007-12-20 19:47:16 +0000878 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000879
880 std::vector<char> buf;
881 buf.reserve(strlen(FE->getName())+100);
882
883 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000884 FName.appendComponent(&buf[0]);
885 FName.createDirectoryOnDisk(true);
886 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000887 assert (false && "Could not create 'device' serialization directory.");
888 return;
889 }
Ted Kremenekee533642007-12-20 19:47:16 +0000890
Ted Kremenek54117722007-12-20 00:34:58 +0000891 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000892 FName.appendComponent(&buf[0]);
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000893 EmitASTBitcodeFile(&TU, FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000894
Ted Kremenekee533642007-12-20 19:47:16 +0000895 // Now emit the sources.
896
Ted Kremenek54117722007-12-20 00:34:58 +0000897 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000898};
899
900
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000901} // end anonymous namespace
902
903
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000904ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000905 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000906 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000907
Ted Kremenekf06c9282007-12-19 23:49:37 +0000908 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000909 if (InFile == "-") {
910 llvm::cerr <<
911 "error: Cannot use --serialize with -o for source read from STDIN.\n";
912 return NULL;
913 }
914
Ted Kremenekf06c9282007-12-19 23:49:37 +0000915 // The user specified an AST-emission directory. Determine if the path
916 // is absolute.
917 llvm::sys::Path EmitDir(OutputFile);
918
919 if (!EmitDir.isAbsolute()) {
920 llvm::cerr <<
921 "error: Output directory for --serialize must be an absolute path.\n";
922
923 return NULL;
924 }
925
926 // Create the directory if it does not exist.
927 EmitDir.createDirectoryOnDisk(true);
928 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
929 llvm::cerr <<
930 "error: Could not create output directory for --serialize.\n";
931
932 return NULL;
933 }
934
Ted Kremenek54117722007-12-20 00:34:58 +0000935 // FIXME: We should probably only allow using BuildSerializer when
936 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +0000937 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000938 }
939
940 // The user did not specify an output directory for serialized ASTs.
941 // Serialize the translation to a single file whose name is the same
942 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000943
Ted Kremenekf06c9282007-12-19 23:49:37 +0000944 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000945 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +0000946 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000947}