blob: d68b61cc3d54f41d9b4b316d5382e00231568cf1 [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"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000015#include "clang/Frontend/PathDiagnosticClients.h"
Nico Weberdae86962008-08-09 18:32:11 +000016#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000020#include "clang/AST/ASTConsumer.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000021#include "clang/AST/ASTContext.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"
Chris Lattner557c5b12009-03-28 04:27:18 +000027#include "llvm/System/Path.h"
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;
Mike Stump071e4da2009-02-10 20:16:46 +000037 unsigned Indentation;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000038
Mike Stump071e4da2009-02-10 20:16:46 +000039 DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()),
40 Indentation(0) {}
41 DeclPrinter() : Out(llvm::errs()), Indentation(0) {}
Ted Kremeneka95d3752008-09-13 05:16:45 +000042 virtual ~DeclPrinter();
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000043
Mike Stump071e4da2009-02-10 20:16:46 +000044 void ChangeIndent(int I) {
45 Indentation += I;
46 }
47
48 llvm::raw_ostream& Indent() {
49 for (unsigned i = 0; i < Indentation; ++i)
50 Out << " ";
51 return Out;
52 }
53
Chris Lattneref5a85d2008-01-02 21:04:16 +000054 void PrintDecl(Decl *D);
Mike Stump071e4da2009-02-10 20:16:46 +000055 void Print(NamedDecl *ND);
56 void Print(NamespaceDecl *NS);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000057 void PrintFunctionDeclStart(FunctionDecl *FD);
58 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000059 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000060 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
61 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
62 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
63 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
64 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
65 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
66 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +000067 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +000068 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Douglas Gregoraaba5e32009-02-04 19:02:06 +000069
70 void PrintTemplateDecl(TemplateDecl *TD);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000071 };
72} // end anonymous namespace
73
Ted Kremeneka95d3752008-09-13 05:16:45 +000074DeclPrinter::~DeclPrinter() {
75 Out.flush();
76}
77
Chris Lattneref5a85d2008-01-02 21:04:16 +000078void DeclPrinter:: PrintDecl(Decl *D) {
Mike Stump071e4da2009-02-10 20:16:46 +000079 Indent();
Chris Lattneref5a85d2008-01-02 21:04:16 +000080 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
81 PrintFunctionDeclStart(FD);
82
Douglas Gregor72971342009-04-18 00:02:19 +000083 // FIXME: Pass a context here so we can use getBody()
84 if (FD->getBodyIfAvailable()) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000085 Out << ' ';
Douglas Gregor72971342009-04-18 00:02:19 +000086 FD->getBodyIfAvailable()->printPretty(Out, 0, Indentation, true);
Chris Lattneref5a85d2008-01-02 21:04:16 +000087 Out << '\n';
88 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000089 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000090 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000092 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
93 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000094 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
95 PrintObjCInterfaceDecl(OID);
96 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
97 PrintObjCProtocolDecl(PID);
98 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000099 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000100 Out << "@protocol ";
Steve Naroff30833f82009-04-21 15:12:33 +0000101 for (ObjCForwardProtocolDecl::protocol_iterator I = OFPD->protocol_begin(),
102 E = OFPD->protocol_end();
Chris Lattner07fa7742009-02-20 18:10:37 +0000103 I != E; ++I) {
Steve Naroff30833f82009-04-21 15:12:33 +0000104 if (I != OFPD->protocol_begin()) Out << ", ";
Chris Lattner07fa7742009-02-20 18:10:37 +0000105 Out << (*I)->getNameAsString();
Chris Lattneref5a85d2008-01-02 21:04:16 +0000106 }
107 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000108 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000109 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 PrintObjCImplementationDecl(OID);
111 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000112 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000113 PrintObjCCategoryImplDecl(OID);
114 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000115 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000116 PrintObjCCategoryDecl(OID);
117 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000118 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000119 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000120 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
121 Out << "@class ";
Chris Lattner67956052009-02-20 18:04:31 +0000122 for (ObjCClassDecl::iterator I = OFCD->begin(), E = OFCD->end();
123 I != E; ++I) {
124 if (I != OFCD->begin()) Out << ", ";
125 Out << (*I)->getNameAsString();
Chris Lattner23a0e452008-06-21 21:40:20 +0000126 }
127 Out << ";\n";
Douglas Gregor45579f52008-12-17 02:04:30 +0000128 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
129 Out << "enum " << ED->getNameAsString() << " {\n";
Douglas Gregor6ab35242009-04-09 21:40:53 +0000130 // FIXME: Shouldn't pass a NULL context
131 ASTContext *Context = 0;
132 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(*Context),
133 EEnd = ED->enumerator_end(*Context);
Douglas Gregor45579f52008-12-17 02:04:30 +0000134 E != EEnd; ++E)
135 Out << " " << (*E)->getNameAsString() << ",\n";
136 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000137 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000138 // print a free standing tag decl (e.g. "struct x;").
139 Out << TD->getKindName();
140 Out << " ";
141 if (const IdentifierInfo *II = TD->getIdentifier())
142 Out << II->getName();
143
144 Out << " {\n";
145 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000146 // FIXME: Shouldn't pass a NULL context
147 ASTContext *Context = 0;
148 for (DeclContext::decl_iterator i = TD->decls_begin(*Context);
149 i != TD->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000150 ++i)
151 PrintDecl(*i);
152 ChangeIndent(-1);
153 Indent();
154 Out << "}";
155
156 Out << "\n";
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000157 } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) {
158 PrintTemplateDecl(TempD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000159 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
160 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000161 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
162 Out << "asm(";
163 AD->getAsmString()->printPretty(Out);
164 Out << ")\n";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000165 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000166 Print(ND);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000167 } else {
168 assert(0 && "Unknown decl type!");
169 }
170}
171
Mike Stump071e4da2009-02-10 20:16:46 +0000172void DeclPrinter::Print(NamedDecl *ND) {
173 switch (ND->getKind()) {
174 default:
175 // FIXME: Handle the rest of the NamedDecls.
176 Out << "### NamedDecl " << ND->getNameAsString() << "\n";
177 break;
178 case Decl::Field:
179 case Decl::Var: {
180 // Emit storage class for vardecls.
181 if (VarDecl *V = dyn_cast<VarDecl>(ND)) {
182 switch (V->getStorageClass()) {
183 default: assert(0 && "Unknown storage class!");
Mike Stumpc5840c02009-02-10 23:49:50 +0000184 case VarDecl::None: break;
185 case VarDecl::Auto: Out << "auto "; break;
186 case VarDecl::Register: Out << "register "; break;
187 case VarDecl::Extern: Out << "extern "; break;
188 case VarDecl::Static: Out << "static "; break;
Daniel Dunbar7ab41f72009-02-13 22:49:34 +0000189 case VarDecl::PrivateExtern: Out << "__private_extern__ "; break;
Mike Stump071e4da2009-02-10 20:16:46 +0000190 }
191 }
192 std::string Name = ND->getNameAsString();
193 // This forms: "int a".
194 dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name);
Douglas Gregor087fd532009-04-14 23:32:43 +0000195 Out << Name;
196 if (VarDecl *Var = dyn_cast<VarDecl>(ND)) {
197 if (Var->getInit()) {
198 Out << " = ";
199 Var->getInit()->printPretty(Out);
200 }
201 }
202 Out << ";\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000203 break;
204 }
205 case Decl::Namespace:
206 Print(dyn_cast<NamespaceDecl>(ND));
207 break;
208 }
209}
210
211void DeclPrinter::Print(NamespaceDecl *NS) {
212 Out << "namespace " << NS->getNameAsString() << " {\n";
213 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000214 // FIXME: Shouldn't pass a NULL context
215 ASTContext *Context = 0;
216 for (DeclContext::decl_iterator i = NS->decls_begin(*Context);
217 i != NS->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000218 ++i)
219 PrintDecl(*i);
220 ChangeIndent(-1);
221 Indent();
222 Out << "}\n";
223}
224
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000225void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Douglas Gregor72971342009-04-18 00:02:19 +0000226 // FIXME: pass a context so that we can use getBody.
227 bool HasBody = FD->getBodyIfAvailable();
Reid Spencer5f016e22007-07-11 17:01:13 +0000228
Ted Kremenekea75c552007-11-28 21:32:21 +0000229 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000230
Mike Stump071e4da2009-02-10 20:16:46 +0000231 Indent();
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000232 switch (FD->getStorageClass()) {
233 default: assert(0 && "Unknown storage class");
234 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000235 case FunctionDecl::Extern: Out << "extern "; break;
236 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000237 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000238 }
239
240 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000241 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000242
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000243 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000244 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000245
Douglas Gregor72564e72009-02-26 23:50:07 +0000246 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 Proto += "(";
248 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
249 if (i) Proto += ", ";
250 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000251 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000252
253 FT->getArgType(i).getAsStringInternal(ParamStr);
254 Proto += ParamStr;
255 }
256
257 if (FT->isVariadic()) {
258 if (FD->getNumParams()) Proto += ", ";
259 Proto += "...";
260 }
261 Proto += ")";
262 } else {
Douglas Gregor72564e72009-02-26 23:50:07 +0000263 assert(isa<FunctionNoProtoType>(AFT));
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 Proto += "()";
265 }
266
267 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000268 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000269
Douglas Gregor72971342009-04-18 00:02:19 +0000270 if (!FD->getBodyIfAvailable())
Ted Kremenekea75c552007-11-28 21:32:21 +0000271 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000272 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000273}
274
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000275void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000276 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000278 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000279}
280
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000281void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
282 const char *l;
283 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
284 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000285 else {
286 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
287 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000289 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000290
291 Out << "extern \"" << l << "\" ";
Mike Stump071e4da2009-02-10 20:16:46 +0000292 if (LS->hasBraces()) {
Douglas Gregorf44515a2008-12-16 22:23:02 +0000293 Out << "{\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000294 ChangeIndent(1);
295 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000296
Douglas Gregor6ab35242009-04-09 21:40:53 +0000297 // FIXME: Should not use a NULL DeclContext!
298 ASTContext *Context = 0;
299 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context),
300 DEnd = LS->decls_end(*Context);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000301 D != DEnd; ++D)
302 PrintDecl(*D);
303
Mike Stump071e4da2009-02-10 20:16:46 +0000304 if (LS->hasBraces()) {
305 ChangeIndent(-1);
306 Indent() << "}";
307 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000308 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000309}
310
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000311void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000312 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000313 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000314 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000315 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000316 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000317 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000318
Chris Lattner077bf5e2008-11-24 03:33:13 +0000319 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000320 std::string::size_type pos, lastPos = 0;
Chris Lattner89951a82009-02-20 18:43:26 +0000321 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
322 E = OMD->param_end(); PI != E; ++PI) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000323 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000324 pos = name.find_first_of(":", lastPos);
325 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattner89951a82009-02-20 18:43:26 +0000326 Out << ":(" << (*PI)->getType().getAsString() << ")"
327 << (*PI)->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000328 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000329 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000330
Chris Lattner89951a82009-02-20 18:43:26 +0000331 if (OMD->param_begin() == OMD->param_end())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000332 Out << " " << name;
333
334 if (OMD->isVariadic())
335 Out << ", ...";
336
337 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000338}
339
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000340void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000341 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000342 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000343
344 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000345 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000346 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000347 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000348
Douglas Gregor653f1b12009-04-23 01:02:12 +0000349 // FIXME: Don't use a NULL context
350 ASTContext *Context = 0;
351 for (ObjCImplementationDecl::instmeth_iterator
352 I = OID->instmeth_begin(*Context),
353 E = OID->instmeth_end(*Context);
354 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000355 ObjCMethodDecl *OMD = *I;
356 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000357 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000358 Out << ' ';
359 OMD->getBody()->printPretty(Out);
360 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000361 }
362 }
363
Douglas Gregor653f1b12009-04-23 01:02:12 +0000364 for (ObjCImplementationDecl::classmeth_iterator
365 I = OID->classmeth_begin(*Context),
366 E = OID->classmeth_end(*Context);
367 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000368 ObjCMethodDecl *OMD = *I;
369 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000370 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000371 Out << ' ';
372 OMD->getBody()->printPretty(Out);
373 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000374 }
375 }
376
Douglas Gregor653f1b12009-04-23 01:02:12 +0000377 for (ObjCImplementationDecl::propimpl_iterator
378 I = OID->propimpl_begin(*Context),
379 E = OID->propimpl_end(*Context); I != E; ++I)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000380 PrintObjCPropertyImplDecl(*I);
381
Ted Kremenekea75c552007-11-28 21:32:21 +0000382 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000383}
384
385
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000386void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000387 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000388 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000389
390 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000391 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000392 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000393 Out << "@interface " << I;
394
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000395 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000396 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
397 if (!Protocols.empty()) {
398 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
399 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000400 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000401 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000402
Chris Lattner3db6cae2008-07-21 18:19:38 +0000403 if (!Protocols.empty())
404 Out << ">";
405 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000406
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000407 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000408 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000409 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000410 E = OID->ivar_end(); I != E; ++I) {
411 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000412 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000413 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000414 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000415 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000416
Douglas Gregor6ab35242009-04-09 21:40:53 +0000417 // FIXME: Should not use a NULL DeclContext!
418 ASTContext *Context = 0;
419 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context),
420 E = OID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000421 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000422 bool eol_needed = false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000423 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context),
424 E = OID->classmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000425 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000426
Douglas Gregor6ab35242009-04-09 21:40:53 +0000427 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context),
428 E = OID->instmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000429 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000430
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000431 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000432 // FIXME: implement the rest...
433}
434
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000435void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000436 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000437
Douglas Gregor6ab35242009-04-09 21:40:53 +0000438 // FIXME: Should not use a NULL DeclContext!
439 ASTContext *Context = 0;
440 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context),
441 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000442 PrintObjCPropertyDecl(*I);
443 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000444 // FIXME: implement the rest...
445}
446
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000447void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000448 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000449 << PID->getClassInterface()->getNameAsString()
450 << '(' << PID->getNameAsString() << ");\n";
Douglas Gregor653f1b12009-04-23 01:02:12 +0000451
452 // FIXME: Don't use a NULL context here
453 ASTContext *Context = 0;
454 for (ObjCCategoryImplDecl::propimpl_iterator
455 I = PID->propimpl_begin(*Context),
456 E = PID->propimpl_end(*Context); I != E; ++I)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000457 PrintObjCPropertyImplDecl(*I);
458 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000459 // FIXME: implement the rest...
460}
461
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000462void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000463 // FIXME: Should not use a NULL DeclContext!
464 ASTContext *Context = 0;
Ted Kremenekea75c552007-11-28 21:32:21 +0000465 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000466 << PID->getClassInterface()->getNameAsString()
467 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000468 // Output property declarations.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000469 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context),
470 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000471 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000472 Out << "@end\n";
473
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000474 // FIXME: implement the rest...
475}
476
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000477void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000478 Out << "@compatibility_alias " << AID->getNameAsString()
479 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000480}
481
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000482/// PrintObjCPropertyDecl - print a property declaration.
483///
484void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000485 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
486 Out << "@required\n";
487 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
488 Out << "@optional\n";
489
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000490 Out << "@property";
491 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
492 bool first = true;
493 Out << " (";
494 if (PDecl->getPropertyAttributes() &
495 ObjCPropertyDecl::OBJC_PR_readonly) {
496 Out << (first ? ' ' : ',') << "readonly";
497 first = false;
498 }
499
500 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
501 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000502 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000503 first = false;
504 }
505 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
506 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000507 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000508 first = false;
509 }
510
511 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
512 Out << (first ? ' ' : ',') << "assign";
513 first = false;
514 }
515
516 if (PDecl->getPropertyAttributes() &
517 ObjCPropertyDecl::OBJC_PR_readwrite) {
518 Out << (first ? ' ' : ',') << "readwrite";
519 first = false;
520 }
521
522 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
523 Out << (first ? ' ' : ',') << "retain";
524 first = false;
525 }
526
527 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
528 Out << (first ? ' ' : ',') << "copy";
529 first = false;
530 }
531
532 if (PDecl->getPropertyAttributes() &
533 ObjCPropertyDecl::OBJC_PR_nonatomic) {
534 Out << (first ? ' ' : ',') << "nonatomic";
535 first = false;
536 }
537 Out << " )";
538 }
539 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000540 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000541
542 Out << ";\n";
543}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000544
545/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
546/// declaration syntax.
547///
548void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000549 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000550 Out << "\n@synthesize ";
551 else
552 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000553 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000554 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000555 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000556 Out << ";\n";
557}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000558
559/// PrintTemplateParams - Print a template parameter list and recursively print
560/// it's underlying top-level definition.
561void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
562 // TODO: Write template parameters.
563 Out << "template <...> ";
564 PrintDecl(TD->getTemplatedDecl());
565}
566
567
568
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000569//===----------------------------------------------------------------------===//
570/// ASTPrinter - Pretty-printer of ASTs
571
Chris Lattner3d4997d2007-09-15 23:02:28 +0000572namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000573 class ASTPrinter : public ASTConsumer, public DeclPrinter {
574 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000575 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000576
Chris Lattner682bf922009-03-29 16:50:03 +0000577 virtual void HandleTopLevelDecl(DeclGroupRef D) {
578 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
579 PrintDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000581 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000582} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +0000583
Ted Kremeneka95d3752008-09-13 05:16:45 +0000584ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000585 return new ASTPrinter(out);
586}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000587
588//===----------------------------------------------------------------------===//
589/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000590
591namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000592 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerc50a2802009-04-25 23:31:28 +0000593 ASTContext *Ctx;
Douglas Gregor609e72f2009-04-26 02:02:08 +0000594 bool FullDump;
595
Chris Lattner3d4997d2007-09-15 23:02:28 +0000596 public:
Douglas Gregor609e72f2009-04-26 02:02:08 +0000597 explicit ASTDumper(bool FullDump) : DeclPrinter(), FullDump(FullDump) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000598
Ted Kremenek95041a22007-12-19 22:51:13 +0000599 void Initialize(ASTContext &Context) {
Chris Lattnerc50a2802009-04-25 23:31:28 +0000600 Ctx = &Context;
Chris Lattner6000dac2007-08-08 22:51:59 +0000601 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000602
Chris Lattner682bf922009-03-29 16:50:03 +0000603 virtual void HandleTopLevelDecl(DeclGroupRef D) {
Douglas Gregor609e72f2009-04-26 02:02:08 +0000604 if (FullDump)
605 return;
Chris Lattner682bf922009-03-29 16:50:03 +0000606 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
607 HandleTopLevelSingleDecl(*I);
608 }
609 void HandleTopLevelSingleDecl(Decl *D);
Douglas Gregor609e72f2009-04-26 02:02:08 +0000610
611 virtual void HandleTranslationUnit(ASTContext &Ctx) {
612 if (!FullDump)
613 return;
614
615 for (DeclContext::decl_iterator
616 D = Ctx.getTranslationUnitDecl()->decls_begin(Ctx),
617 DEnd = Ctx.getTranslationUnitDecl()->decls_end(Ctx);
618 D != DEnd;
619 ++D)
620 HandleTopLevelSingleDecl(*D);
621 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000622 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000623} // end anonymous namespace
624
Chris Lattner682bf922009-03-29 16:50:03 +0000625void ASTDumper::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000626 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
627 PrintFunctionDeclStart(FD);
628
Chris Lattnerc50a2802009-04-25 23:31:28 +0000629 if (Stmt *Body = FD->getBody(*Ctx)) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000630 Out << '\n';
Chris Lattnerc50a2802009-04-25 23:31:28 +0000631 // FIXME: convert dumper to use raw_ostream.
632 Body->dumpAll(Ctx->getSourceManager());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000633 Out << '\n';
634 }
635 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
636 PrintTypeDefDecl(TD);
637 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
638 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
639 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
640 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
641 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
642 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
643 } else if (isa<ObjCForwardProtocolDecl>(D)) {
644 Out << "Read objc fwd protocol decl\n";
645 } else if (isa<ObjCClassDecl>(D)) {
646 Out << "Read objc fwd class decl\n";
647 } else if (isa<FileScopeAsmDecl>(D)) {
648 Out << "Read file scope asm decl\n";
649 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
650 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
651 << "'\n";
Chris Lattnerc50a2802009-04-25 23:31:28 +0000652 if (Stmt *S = MD->getBody()) {
653 // FIXME: convert dumper to use raw_ostream.
654 S->dumpAll(Ctx->getSourceManager());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000655 Out << '\n';
656 }
657 } else if (isa<ObjCImplementationDecl>(D)) {
658 Out << "Read objc implementation decl\n";
659 } else if (isa<ObjCCategoryImplDecl>(D)) {
660 Out << "Read objc category implementation decl\n";
661 } else if (isa<LinkageSpecDecl>(D)) {
662 Out << "Read linkage spec decl\n";
663 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
664 Out << "Read top-level variable decl: '" << ND->getNameAsString()
665 << "'\n";
666 } else {
667 assert(0 && "Unknown decl type!");
668 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000669}
670
Douglas Gregor609e72f2009-04-26 02:02:08 +0000671ASTConsumer *clang::CreateASTDumper(bool FullDump) {
672 return new ASTDumper(FullDump);
673}
Chris Lattner3d4997d2007-09-15 23:02:28 +0000674
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000675//===----------------------------------------------------------------------===//
676/// ASTViewer - AST Visualization
677
Ted Kremenek80de08f2007-09-19 21:29:43 +0000678namespace {
679 class ASTViewer : public ASTConsumer {
680 SourceManager *SM;
681 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000682 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000683 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000684 }
Chris Lattner682bf922009-03-29 16:50:03 +0000685
686 virtual void HandleTopLevelDecl(DeclGroupRef D) {
687 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
688 HandleTopLevelSingleDecl(*I);
689 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000690
Chris Lattner682bf922009-03-29 16:50:03 +0000691 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000692 };
693}
694
Chris Lattner682bf922009-03-29 16:50:03 +0000695void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000696 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
697 DeclPrinter().PrintFunctionDeclStart(FD);
698
Douglas Gregor72971342009-04-18 00:02:19 +0000699 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000700 llvm::cerr << '\n';
Douglas Gregor72971342009-04-18 00:02:19 +0000701 FD->getBodyIfAvailable()->viewAST();
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000702 llvm::cerr << '\n';
703 }
704 return;
705 }
706
707 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
708 DeclPrinter().PrintObjCMethodDecl(MD);
709
710 if (MD->getBody()) {
711 llvm::cerr << '\n';
712 MD->getBody()->viewAST();
713 llvm::cerr << '\n';
714 }
715 }
716}
717
718
Ted Kremenek80de08f2007-09-19 21:29:43 +0000719ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
720
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000721//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000722/// DeclContextPrinter - Decl and DeclContext Visualization
723
724namespace {
725
726class DeclContextPrinter : public ASTConsumer {
727 llvm::raw_ostream& Out;
728public:
729 DeclContextPrinter() : Out(llvm::errs()) {}
730
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000731 void HandleTranslationUnit(ASTContext &C) {
732 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000733 }
734
735 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
736};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000737} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000738
739void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
740 unsigned Indentation) {
741 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000742 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000743 case Decl::TranslationUnit:
744 Out << "[translation unit] " << DC;
745 break;
746 case Decl::Namespace: {
747 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000748 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000749 Out << ND->getNameAsString();
750 break;
751 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000752 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000753 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000754 if (ED->isDefinition())
755 Out << "[enum] ";
756 else
757 Out << "<enum> ";
758 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000759 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000760 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000761 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000762 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000763 if (RD->isDefinition())
764 Out << "[struct] ";
765 else
766 Out << "<struct> ";
767 Out << RD->getNameAsString();
768 break;
769 }
770 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000771 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000772 if (RD->isDefinition())
773 Out << "[class] ";
774 else
775 Out << "<class> ";
776 Out << RD->getNameAsString() << " " << DC;
777 break;
778 }
779 case Decl::ObjCMethod:
780 Out << "[objc method]";
781 break;
782 case Decl::ObjCInterface:
783 Out << "[objc interface]";
784 break;
785 case Decl::ObjCCategory:
786 Out << "[objc category]";
787 break;
788 case Decl::ObjCProtocol:
789 Out << "[objc protocol]";
790 break;
791 case Decl::ObjCImplementation:
792 Out << "[objc implementation]";
793 break;
794 case Decl::ObjCCategoryImpl:
795 Out << "[objc categoryimpl]";
796 break;
797 case Decl::LinkageSpec:
798 Out << "[linkage spec]";
799 break;
800 case Decl::Block:
801 Out << "[block]";
802 break;
803 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000804 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000805 if (FD->isThisDeclarationADefinition())
806 Out << "[function] ";
807 else
808 Out << "<function> ";
809 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000810 // Print the parameters.
811 Out << "(";
812 bool PrintComma = false;
813 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
814 E = FD->param_end(); I != E; ++I) {
815 if (PrintComma)
816 Out << ", ";
817 else
818 PrintComma = true;
819 Out << (*I)->getNameAsString();
820 }
821 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000822 break;
823 }
824 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000825 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000826 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000827 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000828 else if (D->isImplicit())
829 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000830 else
831 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000832 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000833 // Print the parameters.
834 Out << "(";
835 bool PrintComma = false;
836 for (FunctionDecl::param_const_iterator I = D->param_begin(),
837 E = D->param_end(); I != E; ++I) {
838 if (PrintComma)
839 Out << ", ";
840 else
841 PrintComma = true;
842 Out << (*I)->getNameAsString();
843 }
844 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000845
846 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000847 const DeclContext* SemaDC = D->getDeclContext();
848 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000849 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000850 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000851
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000852 break;
853 }
854 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000855 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000856 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000857 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000858 else if (D->isImplicit())
859 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000860 else
861 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000862 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000863 // Print the parameters.
864 Out << "(";
865 bool PrintComma = false;
866 for (FunctionDecl::param_const_iterator I = D->param_begin(),
867 E = D->param_end(); I != E; ++I) {
868 if (PrintComma)
869 Out << ", ";
870 else
871 PrintComma = true;
872 Out << (*I)->getNameAsString();
873 }
874 Out << ")";
875
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000876 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000877 const DeclContext* SemaDC = D->getDeclContext();
878 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000879 if (SemaDC != LexicalDC)
880 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000881 break;
882 }
883 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000884 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000885 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000886 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000887 else if (D->isImplicit())
888 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000889 else
890 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000891 Out << D->getNameAsString();
892 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000893 const DeclContext* SemaDC = D->getDeclContext();
894 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000895 if (SemaDC != LexicalDC)
896 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000897 break;
898 }
899 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000900 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000901 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000902 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000903 else if (D->isImplicit())
904 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000905 else
906 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000907 Out << D->getNameAsString();
908 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000909 const DeclContext* SemaDC = D->getDeclContext();
910 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000911 if (SemaDC != LexicalDC)
912 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000913 break;
914 }
915
916 default:
917 assert(0 && "a decl that inherits DeclContext isn't handled");
918 }
919
920 Out << "\n";
921
922 // Print decls in the DeclContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000923 // FIXME: Should not use a NULL DeclContext!
924 ASTContext *Context = 0;
925 for (DeclContext::decl_iterator I = DC->decls_begin(*Context),
926 E = DC->decls_end(*Context);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000927 I != E; ++I) {
928 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000929 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000930
931 Decl::Kind DK = I->getKind();
932 switch (DK) {
933 case Decl::Namespace:
934 case Decl::Enum:
935 case Decl::Record:
936 case Decl::CXXRecord:
937 case Decl::ObjCMethod:
938 case Decl::ObjCInterface:
939 case Decl::ObjCCategory:
940 case Decl::ObjCProtocol:
941 case Decl::ObjCImplementation:
942 case Decl::ObjCCategoryImpl:
943 case Decl::LinkageSpec:
944 case Decl::Block:
945 case Decl::Function:
946 case Decl::CXXMethod:
947 case Decl::CXXConstructor:
948 case Decl::CXXDestructor:
949 case Decl::CXXConversion:
950 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000951 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000952 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000953 break;
954 }
955 case Decl::Field: {
956 FieldDecl* FD = cast<FieldDecl>(*I);
957 Out << "<field> " << FD->getNameAsString() << "\n";
958 break;
959 }
960 case Decl::Typedef: {
961 TypedefDecl* TD = cast<TypedefDecl>(*I);
962 Out << "<typedef> " << TD->getNameAsString() << "\n";
963 break;
964 }
965 case Decl::EnumConstant: {
966 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
967 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
968 break;
969 }
970 case Decl::Var: {
971 VarDecl* VD = cast<VarDecl>(*I);
972 Out << "<var> " << VD->getNameAsString() << "\n";
973 break;
974 }
975 case Decl::ImplicitParam: {
976 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
977 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
978 break;
979 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000980 case Decl::ParmVar: {
981 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
982 Out << "<parameter> " << PVD->getNameAsString() << "\n";
983 break;
984 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000985 case Decl::OriginalParmVar: {
986 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
987 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
988 break;
989 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000990 case Decl::ObjCProperty: {
991 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
992 Out << "<objc property> " << OPD->getNameAsString() << "\n";
993 break;
994 }
995 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000996 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000997 assert(0 && "decl unhandled");
998 }
999 }
1000}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +00001001ASTConsumer *clang::CreateDeclContextPrinter() {
1002 return new DeclContextPrinter();
1003}
1004
1005//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001006/// InheritanceViewer - C++ Inheritance Visualization
1007
1008namespace {
1009class InheritanceViewer : public ASTConsumer {
1010 const std::string clsname;
1011public:
1012 InheritanceViewer(const std::string& cname) : clsname(cname) {}
1013
Chris Lattnerdacbc5d2009-03-28 04:11:33 +00001014 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001015 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001016 if (RecordType *T = dyn_cast<RecordType>(*I)) {
1017 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
1018 // FIXME: This lookup needs to be generalized to handle namespaces and
1019 // (when we support them) templates.
1020 if (D->getNameAsString() == clsname) {
1021 D->viewInheritance(C);
1022 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001023 }
1024 }
1025 }
1026};
1027}
1028
1029ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
1030 return new InheritanceViewer(clsname);
1031}