blob: 48bf85b4dccab16a9fa5f241ef04cdb82fc10806 [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
Eli Friedmanb3e22962009-05-17 01:05:34 +0000144 if (TD->isDefinition()) {
145 Out << " {\n";
146 ChangeIndent(1);
147 // FIXME: Shouldn't pass a NULL context
148 ASTContext *Context = 0;
149 for (DeclContext::decl_iterator i = TD->decls_begin(*Context);
150 i != TD->decls_end(*Context);
151 ++i)
152 PrintDecl(*i);
153 ChangeIndent(-1);
154 Indent();
155 Out << "}";
156 }
157 Out << ";\n";
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000158 } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) {
159 PrintTemplateDecl(TempD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000160 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
161 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000162 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
163 Out << "asm(";
164 AD->getAsmString()->printPretty(Out);
165 Out << ")\n";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000166 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000167 Print(ND);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000168 } else {
169 assert(0 && "Unknown decl type!");
170 }
171}
172
Mike Stump071e4da2009-02-10 20:16:46 +0000173void DeclPrinter::Print(NamedDecl *ND) {
174 switch (ND->getKind()) {
175 default:
176 // FIXME: Handle the rest of the NamedDecls.
177 Out << "### NamedDecl " << ND->getNameAsString() << "\n";
178 break;
179 case Decl::Field:
180 case Decl::Var: {
181 // Emit storage class for vardecls.
182 if (VarDecl *V = dyn_cast<VarDecl>(ND)) {
183 switch (V->getStorageClass()) {
184 default: assert(0 && "Unknown storage class!");
Mike Stumpc5840c02009-02-10 23:49:50 +0000185 case VarDecl::None: break;
186 case VarDecl::Auto: Out << "auto "; break;
187 case VarDecl::Register: Out << "register "; break;
188 case VarDecl::Extern: Out << "extern "; break;
189 case VarDecl::Static: Out << "static "; break;
Daniel Dunbar7ab41f72009-02-13 22:49:34 +0000190 case VarDecl::PrivateExtern: Out << "__private_extern__ "; break;
Mike Stump071e4da2009-02-10 20:16:46 +0000191 }
192 }
193 std::string Name = ND->getNameAsString();
194 // This forms: "int a".
195 dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name);
Douglas Gregor087fd532009-04-14 23:32:43 +0000196 Out << Name;
197 if (VarDecl *Var = dyn_cast<VarDecl>(ND)) {
198 if (Var->getInit()) {
199 Out << " = ";
200 Var->getInit()->printPretty(Out);
201 }
202 }
203 Out << ";\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000204 break;
205 }
206 case Decl::Namespace:
207 Print(dyn_cast<NamespaceDecl>(ND));
208 break;
209 }
210}
211
212void DeclPrinter::Print(NamespaceDecl *NS) {
213 Out << "namespace " << NS->getNameAsString() << " {\n";
214 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000215 // FIXME: Shouldn't pass a NULL context
216 ASTContext *Context = 0;
217 for (DeclContext::decl_iterator i = NS->decls_begin(*Context);
218 i != NS->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000219 ++i)
220 PrintDecl(*i);
221 ChangeIndent(-1);
222 Indent();
223 Out << "}\n";
224}
225
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000226void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Douglas Gregor72971342009-04-18 00:02:19 +0000227 // FIXME: pass a context so that we can use getBody.
228 bool HasBody = FD->getBodyIfAvailable();
Reid Spencer5f016e22007-07-11 17:01:13 +0000229
Ted Kremenekea75c552007-11-28 21:32:21 +0000230 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000231
Mike Stump071e4da2009-02-10 20:16:46 +0000232 Indent();
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000233 switch (FD->getStorageClass()) {
234 default: assert(0 && "Unknown storage class");
235 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000236 case FunctionDecl::Extern: Out << "extern "; break;
237 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000238 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000239 }
240
241 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000242 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000243
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000244 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000245 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000246
Douglas Gregor72564e72009-02-26 23:50:07 +0000247 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 Proto += "(";
249 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
250 if (i) Proto += ", ";
251 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000252 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000253
254 FT->getArgType(i).getAsStringInternal(ParamStr);
255 Proto += ParamStr;
256 }
257
258 if (FT->isVariadic()) {
259 if (FD->getNumParams()) Proto += ", ";
260 Proto += "...";
261 }
262 Proto += ")";
263 } else {
Douglas Gregor72564e72009-02-26 23:50:07 +0000264 assert(isa<FunctionNoProtoType>(AFT));
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 Proto += "()";
266 }
267
268 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000269 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000270
Douglas Gregor72971342009-04-18 00:02:19 +0000271 if (!FD->getBodyIfAvailable())
Ted Kremenekea75c552007-11-28 21:32:21 +0000272 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000273 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000274}
275
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000276void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000277 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000279 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000282void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
283 const char *l;
284 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
285 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000286 else {
287 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
288 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000290 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000291
292 Out << "extern \"" << l << "\" ";
Mike Stump071e4da2009-02-10 20:16:46 +0000293 if (LS->hasBraces()) {
Douglas Gregorf44515a2008-12-16 22:23:02 +0000294 Out << "{\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000295 ChangeIndent(1);
296 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000297
Douglas Gregor6ab35242009-04-09 21:40:53 +0000298 // FIXME: Should not use a NULL DeclContext!
299 ASTContext *Context = 0;
300 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context),
301 DEnd = LS->decls_end(*Context);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000302 D != DEnd; ++D)
303 PrintDecl(*D);
304
Mike Stump071e4da2009-02-10 20:16:46 +0000305 if (LS->hasBraces()) {
306 ChangeIndent(-1);
307 Indent() << "}";
308 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000309 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000310}
311
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000312void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000313 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000314 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000315 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000316 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000317 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000318 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000319
Chris Lattner077bf5e2008-11-24 03:33:13 +0000320 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000321 std::string::size_type pos, lastPos = 0;
Chris Lattner89951a82009-02-20 18:43:26 +0000322 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
323 E = OMD->param_end(); PI != E; ++PI) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000324 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000325 pos = name.find_first_of(":", lastPos);
326 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattner89951a82009-02-20 18:43:26 +0000327 Out << ":(" << (*PI)->getType().getAsString() << ")"
328 << (*PI)->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000329 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000330 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000331
Chris Lattner89951a82009-02-20 18:43:26 +0000332 if (OMD->param_begin() == OMD->param_end())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000333 Out << " " << name;
334
335 if (OMD->isVariadic())
336 Out << ", ...";
337
338 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000339}
340
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000341void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000342 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000343 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000344
345 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000346 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000347 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000348 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000349
Douglas Gregor653f1b12009-04-23 01:02:12 +0000350 // FIXME: Don't use a NULL context
351 ASTContext *Context = 0;
352 for (ObjCImplementationDecl::instmeth_iterator
353 I = OID->instmeth_begin(*Context),
354 E = OID->instmeth_end(*Context);
355 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000356 ObjCMethodDecl *OMD = *I;
357 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000358 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000359 Out << ' ';
360 OMD->getBody()->printPretty(Out);
361 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000362 }
363 }
364
Douglas Gregor653f1b12009-04-23 01:02:12 +0000365 for (ObjCImplementationDecl::classmeth_iterator
366 I = OID->classmeth_begin(*Context),
367 E = OID->classmeth_end(*Context);
368 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000369 ObjCMethodDecl *OMD = *I;
370 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000371 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000372 Out << ' ';
373 OMD->getBody()->printPretty(Out);
374 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000375 }
376 }
377
Douglas Gregor653f1b12009-04-23 01:02:12 +0000378 for (ObjCImplementationDecl::propimpl_iterator
379 I = OID->propimpl_begin(*Context),
380 E = OID->propimpl_end(*Context); I != E; ++I)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000381 PrintObjCPropertyImplDecl(*I);
382
Ted Kremenekea75c552007-11-28 21:32:21 +0000383 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000384}
385
386
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000387void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000388 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000389 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000390
391 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000392 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000393 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000394 Out << "@interface " << I;
395
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000396 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000397 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
398 if (!Protocols.empty()) {
399 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
400 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000401 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000402 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000403
Chris Lattner3db6cae2008-07-21 18:19:38 +0000404 if (!Protocols.empty())
405 Out << ">";
406 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000407
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000408 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000409 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000410 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000411 E = OID->ivar_end(); I != E; ++I) {
412 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000413 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000414 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000415 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000416 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000417
Douglas Gregor6ab35242009-04-09 21:40:53 +0000418 // FIXME: Should not use a NULL DeclContext!
419 ASTContext *Context = 0;
420 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context),
421 E = OID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000422 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000423 bool eol_needed = false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000424 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context),
425 E = OID->classmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000426 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000427
Douglas Gregor6ab35242009-04-09 21:40:53 +0000428 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context),
429 E = OID->instmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000430 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000431
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000432 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000433 // FIXME: implement the rest...
434}
435
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000436void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000437 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000438
Douglas Gregor6ab35242009-04-09 21:40:53 +0000439 // FIXME: Should not use a NULL DeclContext!
440 ASTContext *Context = 0;
441 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context),
442 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000443 PrintObjCPropertyDecl(*I);
444 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000445 // FIXME: implement the rest...
446}
447
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000448void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000449 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000450 << PID->getClassInterface()->getNameAsString()
451 << '(' << PID->getNameAsString() << ");\n";
Douglas Gregor653f1b12009-04-23 01:02:12 +0000452
453 // FIXME: Don't use a NULL context here
454 ASTContext *Context = 0;
455 for (ObjCCategoryImplDecl::propimpl_iterator
456 I = PID->propimpl_begin(*Context),
457 E = PID->propimpl_end(*Context); I != E; ++I)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000458 PrintObjCPropertyImplDecl(*I);
459 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000460 // FIXME: implement the rest...
461}
462
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000463void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000464 // FIXME: Should not use a NULL DeclContext!
465 ASTContext *Context = 0;
Ted Kremenekea75c552007-11-28 21:32:21 +0000466 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000467 << PID->getClassInterface()->getNameAsString()
468 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000469 // Output property declarations.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000470 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context),
471 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000472 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000473 Out << "@end\n";
474
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000475 // FIXME: implement the rest...
476}
477
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000478void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000479 Out << "@compatibility_alias " << AID->getNameAsString()
480 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000481}
482
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000483/// PrintObjCPropertyDecl - print a property declaration.
484///
485void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000486 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
487 Out << "@required\n";
488 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
489 Out << "@optional\n";
490
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000491 Out << "@property";
492 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
493 bool first = true;
494 Out << " (";
495 if (PDecl->getPropertyAttributes() &
496 ObjCPropertyDecl::OBJC_PR_readonly) {
497 Out << (first ? ' ' : ',') << "readonly";
498 first = false;
499 }
500
501 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
502 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000503 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000504 first = false;
505 }
506 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
507 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000508 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000509 first = false;
510 }
511
512 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
513 Out << (first ? ' ' : ',') << "assign";
514 first = false;
515 }
516
517 if (PDecl->getPropertyAttributes() &
518 ObjCPropertyDecl::OBJC_PR_readwrite) {
519 Out << (first ? ' ' : ',') << "readwrite";
520 first = false;
521 }
522
523 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
524 Out << (first ? ' ' : ',') << "retain";
525 first = false;
526 }
527
528 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
529 Out << (first ? ' ' : ',') << "copy";
530 first = false;
531 }
532
533 if (PDecl->getPropertyAttributes() &
534 ObjCPropertyDecl::OBJC_PR_nonatomic) {
535 Out << (first ? ' ' : ',') << "nonatomic";
536 first = false;
537 }
538 Out << " )";
539 }
540 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000541 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000542
543 Out << ";\n";
544}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000545
546/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
547/// declaration syntax.
548///
549void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000550 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000551 Out << "\n@synthesize ";
552 else
553 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000554 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000555 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000556 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000557 Out << ";\n";
558}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000559
560/// PrintTemplateParams - Print a template parameter list and recursively print
561/// it's underlying top-level definition.
562void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
563 // TODO: Write template parameters.
564 Out << "template <...> ";
565 PrintDecl(TD->getTemplatedDecl());
566}
567
568
569
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000570//===----------------------------------------------------------------------===//
571/// ASTPrinter - Pretty-printer of ASTs
572
Chris Lattner3d4997d2007-09-15 23:02:28 +0000573namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000574 class ASTPrinter : public ASTConsumer, public DeclPrinter {
575 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000576 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000577
Chris Lattner682bf922009-03-29 16:50:03 +0000578 virtual void HandleTopLevelDecl(DeclGroupRef D) {
579 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
580 PrintDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000582 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000583} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +0000584
Ted Kremeneka95d3752008-09-13 05:16:45 +0000585ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000586 return new ASTPrinter(out);
587}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000588
589//===----------------------------------------------------------------------===//
590/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000591
592namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000593 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerc50a2802009-04-25 23:31:28 +0000594 ASTContext *Ctx;
Douglas Gregor609e72f2009-04-26 02:02:08 +0000595 bool FullDump;
596
Chris Lattner3d4997d2007-09-15 23:02:28 +0000597 public:
Douglas Gregor609e72f2009-04-26 02:02:08 +0000598 explicit ASTDumper(bool FullDump) : DeclPrinter(), FullDump(FullDump) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000599
Ted Kremenek95041a22007-12-19 22:51:13 +0000600 void Initialize(ASTContext &Context) {
Chris Lattnerc50a2802009-04-25 23:31:28 +0000601 Ctx = &Context;
Chris Lattner6000dac2007-08-08 22:51:59 +0000602 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000603
Chris Lattner682bf922009-03-29 16:50:03 +0000604 virtual void HandleTopLevelDecl(DeclGroupRef D) {
Douglas Gregor609e72f2009-04-26 02:02:08 +0000605 if (FullDump)
606 return;
Chris Lattner682bf922009-03-29 16:50:03 +0000607 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
608 HandleTopLevelSingleDecl(*I);
609 }
610 void HandleTopLevelSingleDecl(Decl *D);
Douglas Gregor609e72f2009-04-26 02:02:08 +0000611
612 virtual void HandleTranslationUnit(ASTContext &Ctx) {
613 if (!FullDump)
614 return;
615
616 for (DeclContext::decl_iterator
617 D = Ctx.getTranslationUnitDecl()->decls_begin(Ctx),
618 DEnd = Ctx.getTranslationUnitDecl()->decls_end(Ctx);
619 D != DEnd;
620 ++D)
621 HandleTopLevelSingleDecl(*D);
622 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000623 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000624} // end anonymous namespace
625
Chris Lattner682bf922009-03-29 16:50:03 +0000626void ASTDumper::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000627 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
628 PrintFunctionDeclStart(FD);
629
Chris Lattnerc50a2802009-04-25 23:31:28 +0000630 if (Stmt *Body = FD->getBody(*Ctx)) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000631 Out << '\n';
Chris Lattnerc50a2802009-04-25 23:31:28 +0000632 // FIXME: convert dumper to use raw_ostream.
633 Body->dumpAll(Ctx->getSourceManager());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000634 Out << '\n';
635 }
636 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
637 PrintTypeDefDecl(TD);
638 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
639 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
640 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
641 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
642 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
643 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
644 } else if (isa<ObjCForwardProtocolDecl>(D)) {
645 Out << "Read objc fwd protocol decl\n";
646 } else if (isa<ObjCClassDecl>(D)) {
647 Out << "Read objc fwd class decl\n";
648 } else if (isa<FileScopeAsmDecl>(D)) {
649 Out << "Read file scope asm decl\n";
650 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
651 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
652 << "'\n";
Chris Lattnerc50a2802009-04-25 23:31:28 +0000653 if (Stmt *S = MD->getBody()) {
654 // FIXME: convert dumper to use raw_ostream.
655 S->dumpAll(Ctx->getSourceManager());
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000656 Out << '\n';
657 }
658 } else if (isa<ObjCImplementationDecl>(D)) {
659 Out << "Read objc implementation decl\n";
660 } else if (isa<ObjCCategoryImplDecl>(D)) {
661 Out << "Read objc category implementation decl\n";
662 } else if (isa<LinkageSpecDecl>(D)) {
663 Out << "Read linkage spec decl\n";
664 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
665 Out << "Read top-level variable decl: '" << ND->getNameAsString()
666 << "'\n";
667 } else {
668 assert(0 && "Unknown decl type!");
669 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000670}
671
Douglas Gregor609e72f2009-04-26 02:02:08 +0000672ASTConsumer *clang::CreateASTDumper(bool FullDump) {
673 return new ASTDumper(FullDump);
674}
Chris Lattner3d4997d2007-09-15 23:02:28 +0000675
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000676//===----------------------------------------------------------------------===//
677/// ASTViewer - AST Visualization
678
Ted Kremenek80de08f2007-09-19 21:29:43 +0000679namespace {
680 class ASTViewer : public ASTConsumer {
681 SourceManager *SM;
682 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000683 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000684 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000685 }
Chris Lattner682bf922009-03-29 16:50:03 +0000686
687 virtual void HandleTopLevelDecl(DeclGroupRef D) {
688 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
689 HandleTopLevelSingleDecl(*I);
690 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000691
Chris Lattner682bf922009-03-29 16:50:03 +0000692 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000693 };
694}
695
Chris Lattner682bf922009-03-29 16:50:03 +0000696void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000697 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
698 DeclPrinter().PrintFunctionDeclStart(FD);
699
Douglas Gregor72971342009-04-18 00:02:19 +0000700 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000701 llvm::cerr << '\n';
Douglas Gregor72971342009-04-18 00:02:19 +0000702 FD->getBodyIfAvailable()->viewAST();
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000703 llvm::cerr << '\n';
704 }
705 return;
706 }
707
708 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
709 DeclPrinter().PrintObjCMethodDecl(MD);
710
711 if (MD->getBody()) {
712 llvm::cerr << '\n';
713 MD->getBody()->viewAST();
714 llvm::cerr << '\n';
715 }
716 }
717}
718
719
Ted Kremenek80de08f2007-09-19 21:29:43 +0000720ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
721
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000722//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000723/// DeclContextPrinter - Decl and DeclContext Visualization
724
725namespace {
726
727class DeclContextPrinter : public ASTConsumer {
728 llvm::raw_ostream& Out;
729public:
730 DeclContextPrinter() : Out(llvm::errs()) {}
731
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000732 void HandleTranslationUnit(ASTContext &C) {
733 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000734 }
735
736 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
737};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000738} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000739
740void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
741 unsigned Indentation) {
742 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000743 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000744 case Decl::TranslationUnit:
745 Out << "[translation unit] " << DC;
746 break;
747 case Decl::Namespace: {
748 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000749 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000750 Out << ND->getNameAsString();
751 break;
752 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000753 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000754 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000755 if (ED->isDefinition())
756 Out << "[enum] ";
757 else
758 Out << "<enum> ";
759 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000760 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000761 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000762 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000763 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000764 if (RD->isDefinition())
765 Out << "[struct] ";
766 else
767 Out << "<struct> ";
768 Out << RD->getNameAsString();
769 break;
770 }
771 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000772 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000773 if (RD->isDefinition())
774 Out << "[class] ";
775 else
776 Out << "<class> ";
777 Out << RD->getNameAsString() << " " << DC;
778 break;
779 }
780 case Decl::ObjCMethod:
781 Out << "[objc method]";
782 break;
783 case Decl::ObjCInterface:
784 Out << "[objc interface]";
785 break;
786 case Decl::ObjCCategory:
787 Out << "[objc category]";
788 break;
789 case Decl::ObjCProtocol:
790 Out << "[objc protocol]";
791 break;
792 case Decl::ObjCImplementation:
793 Out << "[objc implementation]";
794 break;
795 case Decl::ObjCCategoryImpl:
796 Out << "[objc categoryimpl]";
797 break;
798 case Decl::LinkageSpec:
799 Out << "[linkage spec]";
800 break;
801 case Decl::Block:
802 Out << "[block]";
803 break;
804 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000805 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000806 if (FD->isThisDeclarationADefinition())
807 Out << "[function] ";
808 else
809 Out << "<function> ";
810 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000811 // Print the parameters.
812 Out << "(";
813 bool PrintComma = false;
814 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
815 E = FD->param_end(); I != E; ++I) {
816 if (PrintComma)
817 Out << ", ";
818 else
819 PrintComma = true;
820 Out << (*I)->getNameAsString();
821 }
822 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000823 break;
824 }
825 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000826 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000827 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000828 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000829 else if (D->isImplicit())
830 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000831 else
832 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000833 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000834 // Print the parameters.
835 Out << "(";
836 bool PrintComma = false;
837 for (FunctionDecl::param_const_iterator I = D->param_begin(),
838 E = D->param_end(); I != E; ++I) {
839 if (PrintComma)
840 Out << ", ";
841 else
842 PrintComma = true;
843 Out << (*I)->getNameAsString();
844 }
845 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000846
847 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000848 const DeclContext* SemaDC = D->getDeclContext();
849 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000850 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000851 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000852
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000853 break;
854 }
855 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000856 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000857 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000858 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000859 else if (D->isImplicit())
860 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000861 else
862 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000863 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000864 // Print the parameters.
865 Out << "(";
866 bool PrintComma = false;
867 for (FunctionDecl::param_const_iterator I = D->param_begin(),
868 E = D->param_end(); I != E; ++I) {
869 if (PrintComma)
870 Out << ", ";
871 else
872 PrintComma = true;
873 Out << (*I)->getNameAsString();
874 }
875 Out << ")";
876
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000877 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000878 const DeclContext* SemaDC = D->getDeclContext();
879 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000880 if (SemaDC != LexicalDC)
881 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000882 break;
883 }
884 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000885 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000886 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000887 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000888 else if (D->isImplicit())
889 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000890 else
891 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000892 Out << D->getNameAsString();
893 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000894 const DeclContext* SemaDC = D->getDeclContext();
895 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000896 if (SemaDC != LexicalDC)
897 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000898 break;
899 }
900 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000901 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000902 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000903 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000904 else if (D->isImplicit())
905 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000906 else
907 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000908 Out << D->getNameAsString();
909 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000910 const DeclContext* SemaDC = D->getDeclContext();
911 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000912 if (SemaDC != LexicalDC)
913 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000914 break;
915 }
916
917 default:
918 assert(0 && "a decl that inherits DeclContext isn't handled");
919 }
920
921 Out << "\n";
922
923 // Print decls in the DeclContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000924 // FIXME: Should not use a NULL DeclContext!
925 ASTContext *Context = 0;
926 for (DeclContext::decl_iterator I = DC->decls_begin(*Context),
927 E = DC->decls_end(*Context);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000928 I != E; ++I) {
929 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000930 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000931
932 Decl::Kind DK = I->getKind();
933 switch (DK) {
934 case Decl::Namespace:
935 case Decl::Enum:
936 case Decl::Record:
937 case Decl::CXXRecord:
938 case Decl::ObjCMethod:
939 case Decl::ObjCInterface:
940 case Decl::ObjCCategory:
941 case Decl::ObjCProtocol:
942 case Decl::ObjCImplementation:
943 case Decl::ObjCCategoryImpl:
944 case Decl::LinkageSpec:
945 case Decl::Block:
946 case Decl::Function:
947 case Decl::CXXMethod:
948 case Decl::CXXConstructor:
949 case Decl::CXXDestructor:
950 case Decl::CXXConversion:
951 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000952 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000953 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000954 break;
955 }
956 case Decl::Field: {
957 FieldDecl* FD = cast<FieldDecl>(*I);
958 Out << "<field> " << FD->getNameAsString() << "\n";
959 break;
960 }
961 case Decl::Typedef: {
962 TypedefDecl* TD = cast<TypedefDecl>(*I);
963 Out << "<typedef> " << TD->getNameAsString() << "\n";
964 break;
965 }
966 case Decl::EnumConstant: {
967 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
968 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
969 break;
970 }
971 case Decl::Var: {
972 VarDecl* VD = cast<VarDecl>(*I);
973 Out << "<var> " << VD->getNameAsString() << "\n";
974 break;
975 }
976 case Decl::ImplicitParam: {
977 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
978 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
979 break;
980 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000981 case Decl::ParmVar: {
982 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
983 Out << "<parameter> " << PVD->getNameAsString() << "\n";
984 break;
985 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000986 case Decl::OriginalParmVar: {
987 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
988 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
989 break;
990 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000991 case Decl::ObjCProperty: {
992 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
993 Out << "<objc property> " << OPD->getNameAsString() << "\n";
994 break;
995 }
996 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000997 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000998 assert(0 && "decl unhandled");
999 }
1000 }
1001}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +00001002ASTConsumer *clang::CreateDeclContextPrinter() {
1003 return new DeclContextPrinter();
1004}
1005
1006//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001007/// InheritanceViewer - C++ Inheritance Visualization
1008
1009namespace {
1010class InheritanceViewer : public ASTConsumer {
1011 const std::string clsname;
1012public:
1013 InheritanceViewer(const std::string& cname) : clsname(cname) {}
1014
Chris Lattnerdacbc5d2009-03-28 04:11:33 +00001015 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001016 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001017 if (RecordType *T = dyn_cast<RecordType>(*I)) {
1018 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
1019 // FIXME: This lookup needs to be generalized to handle namespaces and
1020 // (when we support them) templates.
1021 if (D->getNameAsString() == clsname) {
1022 D->viewInheritance(C);
1023 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001024 }
1025 }
1026 }
1027};
1028}
1029
1030ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
1031 return new InheritanceViewer(clsname);
1032}