blob: c35f4c9e200bc2a60060d7478cc5b36ffa01a4e9 [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
Eli Friedman39d7c4d2009-05-18 22:50:54 +000014#include "clang/Frontend/ASTConsumers.h"
Douglas Gregoree75c052009-05-21 20:55:50 +000015#include "clang/Frontend/DocumentXML.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000016#include "clang/Frontend/PathDiagnosticClients.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"
Chris Lattner557c5b12009-03-28 04:27:18 +000022#include "clang/AST/ASTContext.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000024#include "clang/CodeGen/ModuleBuilder.h"
25#include "llvm/Module.h"
Daniel Dunbard46075f2008-10-21 23:54:00 +000026#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000027#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000029#include "llvm/System/Path.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000030using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000031
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000032//===----------------------------------------------------------------------===//
33/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000034
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000035namespace {
36 class DeclPrinter {
37 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +000038 llvm::raw_ostream& Out;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000039 PrintingPolicy Policy;
Mike Stump071e4da2009-02-10 20:16:46 +000040 unsigned Indentation;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000041
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000042 DeclPrinter(llvm::raw_ostream* out,
43 const PrintingPolicy &Policy = PrintingPolicy())
44 : Out(out ? *out : llvm::errs()), Policy(Policy),
45 Indentation(0) {}
Mike Stump071e4da2009-02-10 20:16:46 +000046 DeclPrinter() : Out(llvm::errs()), Indentation(0) {}
Ted Kremeneka95d3752008-09-13 05:16:45 +000047 virtual ~DeclPrinter();
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000048
Mike Stump071e4da2009-02-10 20:16:46 +000049 void ChangeIndent(int I) {
50 Indentation += I;
51 }
52
53 llvm::raw_ostream& Indent() {
54 for (unsigned i = 0; i < Indentation; ++i)
55 Out << " ";
56 return Out;
57 }
58
Chris Lattneref5a85d2008-01-02 21:04:16 +000059 void PrintDecl(Decl *D);
Mike Stump071e4da2009-02-10 20:16:46 +000060 void Print(NamedDecl *ND);
61 void Print(NamespaceDecl *NS);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000062 void PrintFunctionDeclStart(FunctionDecl *FD);
63 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000064 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000065 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
66 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
67 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
68 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
69 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
70 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
71 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +000072 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +000073 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Douglas Gregoraaba5e32009-02-04 19:02:06 +000074
75 void PrintTemplateDecl(TemplateDecl *TD);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000076 };
77} // end anonymous namespace
78
Ted Kremeneka95d3752008-09-13 05:16:45 +000079DeclPrinter::~DeclPrinter() {
80 Out.flush();
81}
82
Chris Lattneref5a85d2008-01-02 21:04:16 +000083void DeclPrinter:: PrintDecl(Decl *D) {
Mike Stump071e4da2009-02-10 20:16:46 +000084 Indent();
Chris Lattneref5a85d2008-01-02 21:04:16 +000085 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
86 PrintFunctionDeclStart(FD);
87
Douglas Gregor72971342009-04-18 00:02:19 +000088 // FIXME: Pass a context here so we can use getBody()
89 if (FD->getBodyIfAvailable()) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000090 Out << ' ';
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000091 FD->getBodyIfAvailable()->printPretty(Out, 0, Policy);
Chris Lattneref5a85d2008-01-02 21:04:16 +000092 Out << '\n';
93 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000094 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000095 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000096 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000097 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
98 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000099 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
100 PrintObjCInterfaceDecl(OID);
101 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
102 PrintObjCProtocolDecl(PID);
103 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000104 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000105 Out << "@protocol ";
Steve Naroff30833f82009-04-21 15:12:33 +0000106 for (ObjCForwardProtocolDecl::protocol_iterator I = OFPD->protocol_begin(),
107 E = OFPD->protocol_end();
Chris Lattner07fa7742009-02-20 18:10:37 +0000108 I != E; ++I) {
Steve Naroff30833f82009-04-21 15:12:33 +0000109 if (I != OFPD->protocol_begin()) Out << ", ";
Chris Lattner07fa7742009-02-20 18:10:37 +0000110 Out << (*I)->getNameAsString();
Chris Lattneref5a85d2008-01-02 21:04:16 +0000111 }
112 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000113 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000114 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000115 PrintObjCImplementationDecl(OID);
116 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000117 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000118 PrintObjCCategoryImplDecl(OID);
119 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000120 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000121 PrintObjCCategoryDecl(OID);
122 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000123 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000124 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000125 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
126 Out << "@class ";
Chris Lattner67956052009-02-20 18:04:31 +0000127 for (ObjCClassDecl::iterator I = OFCD->begin(), E = OFCD->end();
128 I != E; ++I) {
129 if (I != OFCD->begin()) Out << ", ";
130 Out << (*I)->getNameAsString();
Chris Lattner23a0e452008-06-21 21:40:20 +0000131 }
132 Out << ";\n";
Douglas Gregor45579f52008-12-17 02:04:30 +0000133 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
134 Out << "enum " << ED->getNameAsString() << " {\n";
Douglas Gregor6ab35242009-04-09 21:40:53 +0000135 // FIXME: Shouldn't pass a NULL context
136 ASTContext *Context = 0;
137 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(*Context),
138 EEnd = ED->enumerator_end(*Context);
Douglas Gregor45579f52008-12-17 02:04:30 +0000139 E != EEnd; ++E)
140 Out << " " << (*E)->getNameAsString() << ",\n";
141 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000142 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000143 // print a free standing tag decl (e.g. "struct x;").
144 Out << TD->getKindName();
145 Out << " ";
146 if (const IdentifierInfo *II = TD->getIdentifier())
147 Out << II->getName();
148
Eli Friedmanb3e22962009-05-17 01:05:34 +0000149 if (TD->isDefinition()) {
150 Out << " {\n";
151 ChangeIndent(1);
152 // FIXME: Shouldn't pass a NULL context
153 ASTContext *Context = 0;
154 for (DeclContext::decl_iterator i = TD->decls_begin(*Context);
155 i != TD->decls_end(*Context);
156 ++i)
157 PrintDecl(*i);
158 ChangeIndent(-1);
159 Indent();
160 Out << "}";
161 }
162 Out << ";\n";
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000163 } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) {
164 PrintTemplateDecl(TempD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000165 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
166 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000167 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
168 Out << "asm(";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000169 AD->getAsmString()->printPretty(Out, 0, Policy);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000170 Out << ")\n";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000171 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000172 Print(ND);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000173 } else {
174 assert(0 && "Unknown decl type!");
175 }
176}
177
Mike Stump071e4da2009-02-10 20:16:46 +0000178void DeclPrinter::Print(NamedDecl *ND) {
179 switch (ND->getKind()) {
180 default:
181 // FIXME: Handle the rest of the NamedDecls.
182 Out << "### NamedDecl " << ND->getNameAsString() << "\n";
183 break;
184 case Decl::Field:
185 case Decl::Var: {
186 // Emit storage class for vardecls.
187 if (VarDecl *V = dyn_cast<VarDecl>(ND)) {
188 switch (V->getStorageClass()) {
189 default: assert(0 && "Unknown storage class!");
Mike Stumpc5840c02009-02-10 23:49:50 +0000190 case VarDecl::None: break;
191 case VarDecl::Auto: Out << "auto "; break;
192 case VarDecl::Register: Out << "register "; break;
193 case VarDecl::Extern: Out << "extern "; break;
194 case VarDecl::Static: Out << "static "; break;
Daniel Dunbar7ab41f72009-02-13 22:49:34 +0000195 case VarDecl::PrivateExtern: Out << "__private_extern__ "; break;
Mike Stump071e4da2009-02-10 20:16:46 +0000196 }
197 }
198 std::string Name = ND->getNameAsString();
199 // This forms: "int a".
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000200 dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name, Policy);
Douglas Gregor087fd532009-04-14 23:32:43 +0000201 Out << Name;
202 if (VarDecl *Var = dyn_cast<VarDecl>(ND)) {
203 if (Var->getInit()) {
204 Out << " = ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000205 Var->getInit()->printPretty(Out, 0, Policy);
Douglas Gregor087fd532009-04-14 23:32:43 +0000206 }
207 }
208 Out << ";\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000209 break;
210 }
211 case Decl::Namespace:
212 Print(dyn_cast<NamespaceDecl>(ND));
213 break;
214 }
215}
216
217void DeclPrinter::Print(NamespaceDecl *NS) {
218 Out << "namespace " << NS->getNameAsString() << " {\n";
219 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000220 // FIXME: Shouldn't pass a NULL context
221 ASTContext *Context = 0;
222 for (DeclContext::decl_iterator i = NS->decls_begin(*Context);
223 i != NS->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000224 ++i)
225 PrintDecl(*i);
226 ChangeIndent(-1);
227 Indent();
228 Out << "}\n";
229}
230
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000231void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Douglas Gregor72971342009-04-18 00:02:19 +0000232 // FIXME: pass a context so that we can use getBody.
233 bool HasBody = FD->getBodyIfAvailable();
Reid Spencer5f016e22007-07-11 17:01:13 +0000234
Ted Kremenekea75c552007-11-28 21:32:21 +0000235 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000236
Mike Stump071e4da2009-02-10 20:16:46 +0000237 Indent();
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000238 switch (FD->getStorageClass()) {
239 default: assert(0 && "Unknown storage class");
240 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000241 case FunctionDecl::Extern: Out << "extern "; break;
242 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000243 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000244 }
245
246 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000247 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000248
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000249 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000250 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000251
Douglas Gregor72564e72009-02-26 23:50:07 +0000252 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 Proto += "(";
254 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
255 if (i) Proto += ", ";
256 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000257 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000258
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000259 FT->getArgType(i).getAsStringInternal(ParamStr, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 Proto += ParamStr;
261 }
262
263 if (FT->isVariadic()) {
264 if (FD->getNumParams()) Proto += ", ";
265 Proto += "...";
266 }
267 Proto += ")";
268 } else {
Douglas Gregor72564e72009-02-26 23:50:07 +0000269 assert(isa<FunctionNoProtoType>(AFT));
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 Proto += "()";
271 }
272
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000273 AFT->getResultType().getAsStringInternal(Proto, Policy);
Ted Kremenekea75c552007-11-28 21:32:21 +0000274 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000275
Douglas Gregor72971342009-04-18 00:02:19 +0000276 if (!FD->getBodyIfAvailable())
Ted Kremenekea75c552007-11-28 21:32:21 +0000277 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000278 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000279}
280
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000281void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000282 std::string S = TD->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000283 TD->getUnderlyingType().getAsStringInternal(S, Policy);
Ted Kremenekea75c552007-11-28 21:32:21 +0000284 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000285}
286
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000287void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
288 const char *l;
289 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
290 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000291 else {
292 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
293 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000294 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000295 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000296
297 Out << "extern \"" << l << "\" ";
Mike Stump071e4da2009-02-10 20:16:46 +0000298 if (LS->hasBraces()) {
Douglas Gregorf44515a2008-12-16 22:23:02 +0000299 Out << "{\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000300 ChangeIndent(1);
301 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000302
Douglas Gregor6ab35242009-04-09 21:40:53 +0000303 // FIXME: Should not use a NULL DeclContext!
304 ASTContext *Context = 0;
305 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context),
306 DEnd = LS->decls_end(*Context);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000307 D != DEnd; ++D)
308 PrintDecl(*D);
309
Mike Stump071e4da2009-02-10 20:16:46 +0000310 if (LS->hasBraces()) {
311 ChangeIndent(-1);
312 Indent() << "}";
313 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000314 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000315}
316
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000317void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000318 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000319 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000320 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000321 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000322 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000323 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000324
Chris Lattner077bf5e2008-11-24 03:33:13 +0000325 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000326 std::string::size_type pos, lastPos = 0;
Chris Lattner89951a82009-02-20 18:43:26 +0000327 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
328 E = OMD->param_end(); PI != E; ++PI) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000329 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000330 pos = name.find_first_of(":", lastPos);
331 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattner89951a82009-02-20 18:43:26 +0000332 Out << ":(" << (*PI)->getType().getAsString() << ")"
333 << (*PI)->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000334 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000335 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000336
Chris Lattner89951a82009-02-20 18:43:26 +0000337 if (OMD->param_begin() == OMD->param_end())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000338 Out << " " << name;
339
340 if (OMD->isVariadic())
341 Out << ", ...";
342
343 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000344}
345
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000346void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000347 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000348 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000349
350 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000351 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000352 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000353 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000354
Douglas Gregor653f1b12009-04-23 01:02:12 +0000355 // FIXME: Don't use a NULL context
356 ASTContext *Context = 0;
357 for (ObjCImplementationDecl::instmeth_iterator
358 I = OID->instmeth_begin(*Context),
359 E = OID->instmeth_end(*Context);
360 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000361 ObjCMethodDecl *OMD = *I;
362 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000363 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000364 Out << ' ';
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000365 OMD->getBody()->printPretty(Out, 0, Policy);
Ted Kremenekea75c552007-11-28 21:32:21 +0000366 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000367 }
368 }
369
Douglas Gregor653f1b12009-04-23 01:02:12 +0000370 for (ObjCImplementationDecl::classmeth_iterator
371 I = OID->classmeth_begin(*Context),
372 E = OID->classmeth_end(*Context);
373 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000374 ObjCMethodDecl *OMD = *I;
375 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000376 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000377 Out << ' ';
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000378 OMD->getBody()->printPretty(Out, 0, Policy);
Ted Kremenekea75c552007-11-28 21:32:21 +0000379 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000380 }
381 }
382
Douglas Gregor653f1b12009-04-23 01:02:12 +0000383 for (ObjCImplementationDecl::propimpl_iterator
384 I = OID->propimpl_begin(*Context),
385 E = OID->propimpl_end(*Context); I != E; ++I)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000386 PrintObjCPropertyImplDecl(*I);
387
Ted Kremenekea75c552007-11-28 21:32:21 +0000388 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000389}
390
391
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000392void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000393 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000394 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000395
396 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000397 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000398 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000399 Out << "@interface " << I;
400
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000401 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000402 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
403 if (!Protocols.empty()) {
404 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
405 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000406 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000407 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000408
Chris Lattner3db6cae2008-07-21 18:19:38 +0000409 if (!Protocols.empty())
410 Out << ">";
411 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000412
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000413 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000414 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000415 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000416 E = OID->ivar_end(); I != E; ++I) {
417 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000418 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000419 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000420 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000421 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000422
Douglas Gregor6ab35242009-04-09 21:40:53 +0000423 // FIXME: Should not use a NULL DeclContext!
424 ASTContext *Context = 0;
425 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context),
426 E = OID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000427 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000428 bool eol_needed = false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000429 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context),
430 E = OID->classmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000431 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000432
Douglas Gregor6ab35242009-04-09 21:40:53 +0000433 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context),
434 E = OID->instmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000435 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000436
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000437 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000438 // FIXME: implement the rest...
439}
440
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000441void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000442 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000443
Douglas Gregor6ab35242009-04-09 21:40:53 +0000444 // FIXME: Should not use a NULL DeclContext!
445 ASTContext *Context = 0;
446 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context),
447 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000448 PrintObjCPropertyDecl(*I);
449 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000450 // FIXME: implement the rest...
451}
452
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000453void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000454 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000455 << PID->getClassInterface()->getNameAsString()
456 << '(' << PID->getNameAsString() << ");\n";
Douglas Gregor653f1b12009-04-23 01:02:12 +0000457
458 // FIXME: Don't use a NULL context here
459 ASTContext *Context = 0;
460 for (ObjCCategoryImplDecl::propimpl_iterator
461 I = PID->propimpl_begin(*Context),
462 E = PID->propimpl_end(*Context); I != E; ++I)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000463 PrintObjCPropertyImplDecl(*I);
464 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000465 // FIXME: implement the rest...
466}
467
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000468void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000469 // FIXME: Should not use a NULL DeclContext!
470 ASTContext *Context = 0;
Ted Kremenekea75c552007-11-28 21:32:21 +0000471 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000472 << PID->getClassInterface()->getNameAsString()
473 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000474 // Output property declarations.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000475 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context),
476 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000477 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000478 Out << "@end\n";
479
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000480 // FIXME: implement the rest...
481}
482
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000483void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000484 Out << "@compatibility_alias " << AID->getNameAsString()
485 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000486}
487
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000488/// PrintObjCPropertyDecl - print a property declaration.
489///
490void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000491 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
492 Out << "@required\n";
493 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
494 Out << "@optional\n";
495
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000496 Out << "@property";
497 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
498 bool first = true;
499 Out << " (";
500 if (PDecl->getPropertyAttributes() &
501 ObjCPropertyDecl::OBJC_PR_readonly) {
502 Out << (first ? ' ' : ',') << "readonly";
503 first = false;
504 }
505
506 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
507 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000508 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000509 first = false;
510 }
511 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
512 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000513 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000514 first = false;
515 }
516
517 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
518 Out << (first ? ' ' : ',') << "assign";
519 first = false;
520 }
521
522 if (PDecl->getPropertyAttributes() &
523 ObjCPropertyDecl::OBJC_PR_readwrite) {
524 Out << (first ? ' ' : ',') << "readwrite";
525 first = false;
526 }
527
528 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
529 Out << (first ? ' ' : ',') << "retain";
530 first = false;
531 }
532
533 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
534 Out << (first ? ' ' : ',') << "copy";
535 first = false;
536 }
537
538 if (PDecl->getPropertyAttributes() &
539 ObjCPropertyDecl::OBJC_PR_nonatomic) {
540 Out << (first ? ' ' : ',') << "nonatomic";
541 first = false;
542 }
543 Out << " )";
544 }
545 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000546 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000547
548 Out << ";\n";
549}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000550
551/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
552/// declaration syntax.
553///
554void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000555 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000556 Out << "\n@synthesize ";
557 else
558 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000559 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000560 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000561 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000562 Out << ";\n";
563}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000564
565/// PrintTemplateParams - Print a template parameter list and recursively print
566/// it's underlying top-level definition.
567void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
568 // TODO: Write template parameters.
569 Out << "template <...> ";
570 PrintDecl(TD->getTemplatedDecl());
571}
572
573
574
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000575//===----------------------------------------------------------------------===//
576/// ASTPrinter - Pretty-printer of ASTs
577
Chris Lattner3d4997d2007-09-15 23:02:28 +0000578namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000579 class ASTPrinter : public ASTConsumer, public DeclPrinter {
580 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000581 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000582
Chris Lattner682bf922009-03-29 16:50:03 +0000583 virtual void HandleTopLevelDecl(DeclGroupRef D) {
584 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
585 PrintDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000587 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000588} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +0000589
Ted Kremeneka95d3752008-09-13 05:16:45 +0000590ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000591 return new ASTPrinter(out);
592}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000593
594//===----------------------------------------------------------------------===//
Douglas Gregoree75c052009-05-21 20:55:50 +0000595/// ASTPrinterXML - XML-printer of ASTs
596
597namespace {
598 class ASTPrinterXML : public ASTConsumer {
599 DocumentXML Doc;
600
601 public:
602 ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {}
603
604 void Initialize(ASTContext &Context) {
605 Doc.initialize(Context);
606 }
607
608 virtual void HandleTranslationUnit(ASTContext &Ctx) {
609 Doc.addSubNode("TranslationUnit");
610 for (DeclContext::decl_iterator
611 D = Ctx.getTranslationUnitDecl()->decls_begin(Ctx),
612 DEnd = Ctx.getTranslationUnitDecl()->decls_end(Ctx);
613 D != DEnd;
614 ++D)
615 {
616 Doc.PrintDecl(*D);
617 }
618 Doc.toParent();
619 Doc.finalize();
620 }
621 };
622} // end anonymous namespace
623
624
625ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) {
626 return new ASTPrinterXML(out ? *out : llvm::outs());
627}
628
629//===----------------------------------------------------------------------===//
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000630/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000631
632namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000633 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerc50a2802009-04-25 23:31:28 +0000634 ASTContext *Ctx;
Douglas Gregor609e72f2009-04-26 02:02:08 +0000635 bool FullDump;
636
Chris Lattner3d4997d2007-09-15 23:02:28 +0000637 public:
Douglas Gregor609e72f2009-04-26 02:02:08 +0000638 explicit ASTDumper(bool FullDump) : DeclPrinter(), FullDump(FullDump) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000639
Ted Kremenek95041a22007-12-19 22:51:13 +0000640 void Initialize(ASTContext &Context) {
Chris Lattnerc50a2802009-04-25 23:31:28 +0000641 Ctx = &Context;
Chris Lattner6000dac2007-08-08 22:51:59 +0000642 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000643
Chris Lattner682bf922009-03-29 16:50:03 +0000644 virtual void HandleTopLevelDecl(DeclGroupRef D) {
Douglas Gregor609e72f2009-04-26 02:02:08 +0000645 if (FullDump)
646 return;
Chris Lattner682bf922009-03-29 16:50:03 +0000647 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
648 HandleTopLevelSingleDecl(*I);
649 }
650 void HandleTopLevelSingleDecl(Decl *D);
Douglas Gregor609e72f2009-04-26 02:02:08 +0000651
652 virtual void HandleTranslationUnit(ASTContext &Ctx) {
653 if (!FullDump)
654 return;
655
656 for (DeclContext::decl_iterator
657 D = Ctx.getTranslationUnitDecl()->decls_begin(Ctx),
658 DEnd = Ctx.getTranslationUnitDecl()->decls_end(Ctx);
659 D != DEnd;
660 ++D)
661 HandleTopLevelSingleDecl(*D);
662 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000663 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000664} // end anonymous namespace
665
Chris Lattner682bf922009-03-29 16:50:03 +0000666void ASTDumper::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000667 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
668 PrintFunctionDeclStart(FD);
669
Chris Lattnerc50a2802009-04-25 23:31:28 +0000670 if (Stmt *Body = FD->getBody(*Ctx)) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000671 Out << '\n';
Chris Lattnerc50a2802009-04-25 23:31:28 +0000672 // FIXME: convert dumper to use raw_ostream.
673 Body->dumpAll(Ctx->getSourceManager());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000674 Out << '\n';
675 }
676 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
677 PrintTypeDefDecl(TD);
678 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
679 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
680 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
681 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
682 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
683 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
684 } else if (isa<ObjCForwardProtocolDecl>(D)) {
685 Out << "Read objc fwd protocol decl\n";
686 } else if (isa<ObjCClassDecl>(D)) {
687 Out << "Read objc fwd class decl\n";
688 } else if (isa<FileScopeAsmDecl>(D)) {
689 Out << "Read file scope asm decl\n";
690 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
691 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
692 << "'\n";
Chris Lattnerc50a2802009-04-25 23:31:28 +0000693 if (Stmt *S = MD->getBody()) {
694 // FIXME: convert dumper to use raw_ostream.
695 S->dumpAll(Ctx->getSourceManager());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000696 Out << '\n';
697 }
698 } else if (isa<ObjCImplementationDecl>(D)) {
699 Out << "Read objc implementation decl\n";
700 } else if (isa<ObjCCategoryImplDecl>(D)) {
701 Out << "Read objc category implementation decl\n";
702 } else if (isa<LinkageSpecDecl>(D)) {
703 Out << "Read linkage spec decl\n";
704 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
705 Out << "Read top-level variable decl: '" << ND->getNameAsString()
706 << "'\n";
707 } else {
708 assert(0 && "Unknown decl type!");
709 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000710}
711
Douglas Gregor609e72f2009-04-26 02:02:08 +0000712ASTConsumer *clang::CreateASTDumper(bool FullDump) {
713 return new ASTDumper(FullDump);
714}
Chris Lattner3d4997d2007-09-15 23:02:28 +0000715
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000716//===----------------------------------------------------------------------===//
717/// ASTViewer - AST Visualization
718
Ted Kremenek80de08f2007-09-19 21:29:43 +0000719namespace {
720 class ASTViewer : public ASTConsumer {
721 SourceManager *SM;
722 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000723 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000724 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000725 }
Chris Lattner682bf922009-03-29 16:50:03 +0000726
727 virtual void HandleTopLevelDecl(DeclGroupRef D) {
728 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
729 HandleTopLevelSingleDecl(*I);
730 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000731
Chris Lattner682bf922009-03-29 16:50:03 +0000732 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000733 };
734}
735
Chris Lattner682bf922009-03-29 16:50:03 +0000736void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000737 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
738 DeclPrinter().PrintFunctionDeclStart(FD);
739
Douglas Gregor72971342009-04-18 00:02:19 +0000740 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000741 llvm::cerr << '\n';
Douglas Gregor72971342009-04-18 00:02:19 +0000742 FD->getBodyIfAvailable()->viewAST();
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000743 llvm::cerr << '\n';
744 }
745 return;
746 }
747
748 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
749 DeclPrinter().PrintObjCMethodDecl(MD);
750
751 if (MD->getBody()) {
752 llvm::cerr << '\n';
753 MD->getBody()->viewAST();
754 llvm::cerr << '\n';
755 }
756 }
757}
758
759
Ted Kremenek80de08f2007-09-19 21:29:43 +0000760ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
761
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000762//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000763/// DeclContextPrinter - Decl and DeclContext Visualization
764
765namespace {
766
767class DeclContextPrinter : public ASTConsumer {
768 llvm::raw_ostream& Out;
769public:
770 DeclContextPrinter() : Out(llvm::errs()) {}
771
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000772 void HandleTranslationUnit(ASTContext &C) {
773 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000774 }
775
776 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
777};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000778} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000779
780void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
781 unsigned Indentation) {
782 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000783 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000784 case Decl::TranslationUnit:
785 Out << "[translation unit] " << DC;
786 break;
787 case Decl::Namespace: {
788 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000789 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000790 Out << ND->getNameAsString();
791 break;
792 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000793 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000794 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000795 if (ED->isDefinition())
796 Out << "[enum] ";
797 else
798 Out << "<enum> ";
799 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000800 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000801 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000802 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000803 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000804 if (RD->isDefinition())
805 Out << "[struct] ";
806 else
807 Out << "<struct> ";
808 Out << RD->getNameAsString();
809 break;
810 }
811 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000812 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000813 if (RD->isDefinition())
814 Out << "[class] ";
815 else
816 Out << "<class> ";
817 Out << RD->getNameAsString() << " " << DC;
818 break;
819 }
820 case Decl::ObjCMethod:
821 Out << "[objc method]";
822 break;
823 case Decl::ObjCInterface:
824 Out << "[objc interface]";
825 break;
826 case Decl::ObjCCategory:
827 Out << "[objc category]";
828 break;
829 case Decl::ObjCProtocol:
830 Out << "[objc protocol]";
831 break;
832 case Decl::ObjCImplementation:
833 Out << "[objc implementation]";
834 break;
835 case Decl::ObjCCategoryImpl:
836 Out << "[objc categoryimpl]";
837 break;
838 case Decl::LinkageSpec:
839 Out << "[linkage spec]";
840 break;
841 case Decl::Block:
842 Out << "[block]";
843 break;
844 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000845 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000846 if (FD->isThisDeclarationADefinition())
847 Out << "[function] ";
848 else
849 Out << "<function> ";
850 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000851 // Print the parameters.
852 Out << "(";
853 bool PrintComma = false;
854 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
855 E = FD->param_end(); I != E; ++I) {
856 if (PrintComma)
857 Out << ", ";
858 else
859 PrintComma = true;
860 Out << (*I)->getNameAsString();
861 }
862 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000863 break;
864 }
865 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000866 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000867 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000868 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000869 else if (D->isImplicit())
870 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000871 else
872 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000873 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000874 // Print the parameters.
875 Out << "(";
876 bool PrintComma = false;
877 for (FunctionDecl::param_const_iterator I = D->param_begin(),
878 E = D->param_end(); I != E; ++I) {
879 if (PrintComma)
880 Out << ", ";
881 else
882 PrintComma = true;
883 Out << (*I)->getNameAsString();
884 }
885 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000886
887 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000888 const DeclContext* SemaDC = D->getDeclContext();
889 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000890 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000891 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000892
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000893 break;
894 }
895 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000896 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000897 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000898 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000899 else if (D->isImplicit())
900 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000901 else
902 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000903 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000904 // Print the parameters.
905 Out << "(";
906 bool PrintComma = false;
907 for (FunctionDecl::param_const_iterator I = D->param_begin(),
908 E = D->param_end(); I != E; ++I) {
909 if (PrintComma)
910 Out << ", ";
911 else
912 PrintComma = true;
913 Out << (*I)->getNameAsString();
914 }
915 Out << ")";
916
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000917 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000918 const DeclContext* SemaDC = D->getDeclContext();
919 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000920 if (SemaDC != LexicalDC)
921 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000922 break;
923 }
924 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000925 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000926 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000927 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000928 else if (D->isImplicit())
929 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000930 else
931 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000932 Out << D->getNameAsString();
933 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000934 const DeclContext* SemaDC = D->getDeclContext();
935 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000936 if (SemaDC != LexicalDC)
937 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000938 break;
939 }
940 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000941 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000942 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000943 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000944 else if (D->isImplicit())
945 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000946 else
947 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000948 Out << D->getNameAsString();
949 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000950 const DeclContext* SemaDC = D->getDeclContext();
951 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000952 if (SemaDC != LexicalDC)
953 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000954 break;
955 }
956
957 default:
958 assert(0 && "a decl that inherits DeclContext isn't handled");
959 }
960
961 Out << "\n";
962
963 // Print decls in the DeclContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000964 // FIXME: Should not use a NULL DeclContext!
965 ASTContext *Context = 0;
966 for (DeclContext::decl_iterator I = DC->decls_begin(*Context),
967 E = DC->decls_end(*Context);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000968 I != E; ++I) {
969 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000970 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000971
972 Decl::Kind DK = I->getKind();
973 switch (DK) {
974 case Decl::Namespace:
975 case Decl::Enum:
976 case Decl::Record:
977 case Decl::CXXRecord:
978 case Decl::ObjCMethod:
979 case Decl::ObjCInterface:
980 case Decl::ObjCCategory:
981 case Decl::ObjCProtocol:
982 case Decl::ObjCImplementation:
983 case Decl::ObjCCategoryImpl:
984 case Decl::LinkageSpec:
985 case Decl::Block:
986 case Decl::Function:
987 case Decl::CXXMethod:
988 case Decl::CXXConstructor:
989 case Decl::CXXDestructor:
990 case Decl::CXXConversion:
991 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000992 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000993 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000994 break;
995 }
996 case Decl::Field: {
997 FieldDecl* FD = cast<FieldDecl>(*I);
998 Out << "<field> " << FD->getNameAsString() << "\n";
999 break;
1000 }
1001 case Decl::Typedef: {
1002 TypedefDecl* TD = cast<TypedefDecl>(*I);
1003 Out << "<typedef> " << TD->getNameAsString() << "\n";
1004 break;
1005 }
1006 case Decl::EnumConstant: {
1007 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
1008 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
1009 break;
1010 }
1011 case Decl::Var: {
1012 VarDecl* VD = cast<VarDecl>(*I);
1013 Out << "<var> " << VD->getNameAsString() << "\n";
1014 break;
1015 }
1016 case Decl::ImplicitParam: {
1017 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
1018 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
1019 break;
1020 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +00001021 case Decl::ParmVar: {
1022 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
1023 Out << "<parameter> " << PVD->getNameAsString() << "\n";
1024 break;
1025 }
Zhongxing Xuba16be92009-04-05 02:04:38 +00001026 case Decl::OriginalParmVar: {
1027 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
1028 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
1029 break;
1030 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +00001031 case Decl::ObjCProperty: {
1032 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
1033 Out << "<objc property> " << OPD->getNameAsString() << "\n";
1034 break;
1035 }
1036 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +00001037 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +00001038 assert(0 && "decl unhandled");
1039 }
1040 }
1041}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +00001042ASTConsumer *clang::CreateDeclContextPrinter() {
1043 return new DeclContextPrinter();
1044}
1045
1046//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001047/// InheritanceViewer - C++ Inheritance Visualization
1048
1049namespace {
1050class InheritanceViewer : public ASTConsumer {
1051 const std::string clsname;
1052public:
1053 InheritanceViewer(const std::string& cname) : clsname(cname) {}
1054
Chris Lattnerdacbc5d2009-03-28 04:11:33 +00001055 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001056 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001057 if (RecordType *T = dyn_cast<RecordType>(*I)) {
1058 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
1059 // FIXME: This lookup needs to be generalized to handle namespaces and
1060 // (when we support them) templates.
1061 if (D->getNameAsString() == clsname) {
1062 D->viewInheritance(C);
1063 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001064 }
1065 }
1066 }
1067};
1068}
1069
1070ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
1071 return new InheritanceViewer(clsname);
1072}