blob: 1469d5d3a23e63d5bb2791d68b38030b8db945b0 [file] [log] [blame]
Chris Lattner97e8b6f2007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner97e8b6f2007-10-07 06:04:32 +000010// AST Consumer Implementations.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner97e8b6f2007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Ted Kremenekad99dbf2008-11-03 22:31:48 +000015#include "clang/Driver/PathDiagnosticClients.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Nico Weberdae86962008-08-09 18:32:11 +000017#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000022#include "clang/CodeGen/ModuleBuilder.h"
23#include "llvm/Module.h"
Daniel Dunbard46075f2008-10-21 23:54:00 +000024#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000025#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000027
Chris Lattner6000dac2007-08-08 22:51:59 +000028using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000029
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000030//===----------------------------------------------------------------------===//
31/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033namespace {
34 class DeclPrinter {
35 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +000036 llvm::raw_ostream& Out;
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
83 if (FD->getBody()) {
84 Out << ' ';
Mike Stump071e4da2009-02-10 20:16:46 +000085 FD->getBody()->printPretty(Out, 0, Indentation, true);
Chris Lattneref5a85d2008-01-02 21:04:16 +000086 Out << '\n';
87 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000088 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000089 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000090 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000091 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
92 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000093 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
94 PrintObjCInterfaceDecl(OID);
95 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
96 PrintObjCProtocolDecl(PID);
97 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000098 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000099 Out << "@protocol ";
100 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000101 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000102 if (i) Out << ", ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000103 Out << D->getNameAsString();
Chris Lattneref5a85d2008-01-02 21:04:16 +0000104 }
105 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000107 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000108 PrintObjCImplementationDecl(OID);
109 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000110 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000111 PrintObjCCategoryImplDecl(OID);
112 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000113 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000114 PrintObjCCategoryDecl(OID);
115 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000116 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000117 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000118 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
119 Out << "@class ";
120 ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
121 for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
122 const ObjCInterfaceDecl *D = ForwardDecls[i];
123 if (i) Out << ", ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000124 Out << D->getNameAsString();
Chris Lattner23a0e452008-06-21 21:40:20 +0000125 }
126 Out << ";\n";
Douglas Gregor45579f52008-12-17 02:04:30 +0000127 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
128 Out << "enum " << ED->getNameAsString() << " {\n";
129 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(),
130 EEnd = ED->enumerator_end();
131 E != EEnd; ++E)
132 Out << " " << (*E)->getNameAsString() << ",\n";
133 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000134 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000135 // print a free standing tag decl (e.g. "struct x;").
136 Out << TD->getKindName();
137 Out << " ";
138 if (const IdentifierInfo *II = TD->getIdentifier())
139 Out << II->getName();
140
141 Out << " {\n";
142 ChangeIndent(1);
143 for (DeclContext::decl_iterator i = TD->decls_begin();
144 i != TD->decls_end();
145 ++i)
146 PrintDecl(*i);
147 ChangeIndent(-1);
148 Indent();
149 Out << "}";
150
151 Out << "\n";
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000152 } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) {
153 PrintTemplateDecl(TempD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000154 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
155 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000156 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
157 Out << "asm(";
158 AD->getAsmString()->printPretty(Out);
159 Out << ")\n";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000160 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000161 Print(ND);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000162 } else {
163 assert(0 && "Unknown decl type!");
164 }
165}
166
Mike Stump071e4da2009-02-10 20:16:46 +0000167void DeclPrinter::Print(NamedDecl *ND) {
168 switch (ND->getKind()) {
169 default:
170 // FIXME: Handle the rest of the NamedDecls.
171 Out << "### NamedDecl " << ND->getNameAsString() << "\n";
172 break;
173 case Decl::Field:
174 case Decl::Var: {
175 // Emit storage class for vardecls.
176 if (VarDecl *V = dyn_cast<VarDecl>(ND)) {
177 switch (V->getStorageClass()) {
178 default: assert(0 && "Unknown storage class!");
Mike Stumpc5840c02009-02-10 23:49:50 +0000179 case VarDecl::None: break;
180 case VarDecl::Auto: Out << "auto "; break;
181 case VarDecl::Register: Out << "register "; break;
182 case VarDecl::Extern: Out << "extern "; break;
183 case VarDecl::Static: Out << "static "; break;
Daniel Dunbar7ab41f72009-02-13 22:49:34 +0000184 case VarDecl::PrivateExtern: Out << "__private_extern__ "; break;
Mike Stump071e4da2009-02-10 20:16:46 +0000185 }
186 }
187 std::string Name = ND->getNameAsString();
188 // This forms: "int a".
189 dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name);
190 Out << Name << ";\n";
191 break;
192 }
193 case Decl::Namespace:
194 Print(dyn_cast<NamespaceDecl>(ND));
195 break;
196 }
197}
198
199void DeclPrinter::Print(NamespaceDecl *NS) {
200 Out << "namespace " << NS->getNameAsString() << " {\n";
201 ChangeIndent(1);
202 for (DeclContext::decl_iterator i = NS->decls_begin();
203 i != NS->decls_end();
204 ++i)
205 PrintDecl(*i);
206 ChangeIndent(-1);
207 Indent();
208 Out << "}\n";
209}
210
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000211void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 bool HasBody = FD->getBody();
213
Ted Kremenekea75c552007-11-28 21:32:21 +0000214 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000215
Mike Stump071e4da2009-02-10 20:16:46 +0000216 Indent();
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000217 switch (FD->getStorageClass()) {
218 default: assert(0 && "Unknown storage class");
219 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000220 case FunctionDecl::Extern: Out << "extern "; break;
221 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000222 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000223 }
224
225 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000226 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000227
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000228 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000229 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000230
Chris Lattner0d6ca112007-12-03 21:43:25 +0000231 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 Proto += "(";
233 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
234 if (i) Proto += ", ";
235 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000236 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000237
238 FT->getArgType(i).getAsStringInternal(ParamStr);
239 Proto += ParamStr;
240 }
241
242 if (FT->isVariadic()) {
243 if (FD->getNumParams()) Proto += ", ";
244 Proto += "...";
245 }
246 Proto += ")";
247 } else {
248 assert(isa<FunctionTypeNoProto>(AFT));
249 Proto += "()";
250 }
251
252 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000253 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000254
Chris Lattner6000dac2007-08-08 22:51:59 +0000255 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000256 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000257 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000260void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000261 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000263 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000264}
265
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000266void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
267 const char *l;
268 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
269 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000270 else {
271 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
272 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000273 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000274 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000275
276 Out << "extern \"" << l << "\" ";
Mike Stump071e4da2009-02-10 20:16:46 +0000277 if (LS->hasBraces()) {
Douglas Gregorf44515a2008-12-16 22:23:02 +0000278 Out << "{\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000279 ChangeIndent(1);
280 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000281
Douglas Gregor074149e2009-01-05 19:45:36 +0000282 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(),
283 DEnd = LS->decls_end();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000284 D != DEnd; ++D)
285 PrintDecl(*D);
286
Mike Stump071e4da2009-02-10 20:16:46 +0000287 if (LS->hasBraces()) {
288 ChangeIndent(-1);
289 Indent() << "}";
290 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000291 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000292}
293
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000294void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000295 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000296 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000297 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000298 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000299 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000300 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000301
Chris Lattner077bf5e2008-11-24 03:33:13 +0000302 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000303 std::string::size_type pos, lastPos = 0;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000304 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000305 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000306 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000307 pos = name.find_first_of(":", lastPos);
308 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000309 Out << ":(" << PDecl->getType().getAsString() << ")"
310 << PDecl->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000311 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000312 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000313
314 if (OMD->getNumParams() == 0)
315 Out << " " << name;
316
317 if (OMD->isVariadic())
318 Out << ", ...";
319
320 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000321}
322
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000323void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000324 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000325 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000326
327 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000328 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000329 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000330 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000331
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000332 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000333 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000334 ObjCMethodDecl *OMD = *I;
335 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000336 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000337 Out << ' ';
338 OMD->getBody()->printPretty(Out);
339 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000340 }
341 }
342
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000343 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000344 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000345 ObjCMethodDecl *OMD = *I;
346 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000347 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000348 Out << ' ';
349 OMD->getBody()->printPretty(Out);
350 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000351 }
352 }
353
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000354 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
355 E = OID->propimpl_end(); I != E; ++I)
356 PrintObjCPropertyImplDecl(*I);
357
Ted Kremenekea75c552007-11-28 21:32:21 +0000358 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000359}
360
361
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000362void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000363 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000364 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000365
366 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000367 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000368 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000369 Out << "@interface " << I;
370
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000371 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000372 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
373 if (!Protocols.empty()) {
374 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
375 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000376 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000377 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000378
Chris Lattner3db6cae2008-07-21 18:19:38 +0000379 if (!Protocols.empty())
380 Out << ">";
381 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000382
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000383 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000384 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000385 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000386 E = OID->ivar_end(); I != E; ++I) {
387 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000388 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000389 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000390 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000391 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000392
Steve Naroff09c47192009-01-09 15:36:25 +0000393 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(),
394 E = OID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000395 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000396 bool eol_needed = false;
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000397 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
398 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000399 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000400
401 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
402 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000403 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000404
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000405 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000406 // FIXME: implement the rest...
407}
408
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000409void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000410 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000411
Steve Naroff09c47192009-01-09 15:36:25 +0000412 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(),
413 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000414 PrintObjCPropertyDecl(*I);
415 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000416 // FIXME: implement the rest...
417}
418
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000419void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000420 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000421 << PID->getClassInterface()->getNameAsString()
422 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000423 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
424 E = PID->propimpl_end(); I != E; ++I)
425 PrintObjCPropertyImplDecl(*I);
426 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000427 // FIXME: implement the rest...
428}
429
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000430void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000431 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000432 << PID->getClassInterface()->getNameAsString()
433 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000434 // Output property declarations.
Steve Naroff09c47192009-01-09 15:36:25 +0000435 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(),
436 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000437 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000438 Out << "@end\n";
439
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000440 // FIXME: implement the rest...
441}
442
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000443void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000444 Out << "@compatibility_alias " << AID->getNameAsString()
445 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000446}
447
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000448/// PrintObjCPropertyDecl - print a property declaration.
449///
450void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000451 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
452 Out << "@required\n";
453 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
454 Out << "@optional\n";
455
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000456 Out << "@property";
457 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
458 bool first = true;
459 Out << " (";
460 if (PDecl->getPropertyAttributes() &
461 ObjCPropertyDecl::OBJC_PR_readonly) {
462 Out << (first ? ' ' : ',') << "readonly";
463 first = false;
464 }
465
466 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
467 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000468 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000469 first = false;
470 }
471 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
472 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000473 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000474 first = false;
475 }
476
477 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
478 Out << (first ? ' ' : ',') << "assign";
479 first = false;
480 }
481
482 if (PDecl->getPropertyAttributes() &
483 ObjCPropertyDecl::OBJC_PR_readwrite) {
484 Out << (first ? ' ' : ',') << "readwrite";
485 first = false;
486 }
487
488 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
489 Out << (first ? ' ' : ',') << "retain";
490 first = false;
491 }
492
493 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
494 Out << (first ? ' ' : ',') << "copy";
495 first = false;
496 }
497
498 if (PDecl->getPropertyAttributes() &
499 ObjCPropertyDecl::OBJC_PR_nonatomic) {
500 Out << (first ? ' ' : ',') << "nonatomic";
501 first = false;
502 }
503 Out << " )";
504 }
505 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000506 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000507
508 Out << ";\n";
509}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000510
511/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
512/// declaration syntax.
513///
514void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000515 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000516 Out << "\n@synthesize ";
517 else
518 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000519 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000520 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000521 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000522 Out << ";\n";
523}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000524
525/// PrintTemplateParams - Print a template parameter list and recursively print
526/// it's underlying top-level definition.
527void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
528 // TODO: Write template parameters.
529 Out << "template <...> ";
530 PrintDecl(TD->getTemplatedDecl());
531}
532
533
534
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000535//===----------------------------------------------------------------------===//
536/// ASTPrinter - Pretty-printer of ASTs
537
Chris Lattner3d4997d2007-09-15 23:02:28 +0000538namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000539 class ASTPrinter : public ASTConsumer, public DeclPrinter {
540 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000541 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000542
Chris Lattner3d4997d2007-09-15 23:02:28 +0000543 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000544 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000546 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000547}
Chris Lattner6000dac2007-08-08 22:51:59 +0000548
Ted Kremeneka95d3752008-09-13 05:16:45 +0000549ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000550 return new ASTPrinter(out);
551}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000552
553//===----------------------------------------------------------------------===//
554/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000555
556namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000557 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000558 SourceManager *SM;
559 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000560 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000561
Ted Kremenek95041a22007-12-19 22:51:13 +0000562 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000563 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000564 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000565
566 virtual void HandleTopLevelDecl(Decl *D) {
567 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
568 PrintFunctionDeclStart(FD);
569
570 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000571 Out << '\n';
572 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000573 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000574 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000575 }
576 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
577 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000578 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000579 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000580 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000581 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000583 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000584 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000585 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000586 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000587 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000588 } else if (isa<FileScopeAsmDecl>(D)) {
589 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000590 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000591 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
Ted Kremenek63bbe532008-03-14 17:31:00 +0000592 << "'\n";
Steve Naroff1a2b90d2008-05-23 18:50:58 +0000593 if (MD->getBody()) {
594 // FIXME: convert dumper to use std::ostream?
595 MD->getBody()->dumpAll(*SM);
596 Out << '\n';
597 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000598 } else if (isa<ObjCImplementationDecl>(D)) {
599 Out << "Read objc implementation decl\n";
Daniel Dunbar539ced12008-10-05 00:31:15 +0000600 } else if (isa<ObjCCategoryImplDecl>(D)) {
601 Out << "Read objc category implementation decl\n";
Eli Friedmanf595eb02008-12-16 22:14:15 +0000602 } else if (isa<LinkageSpecDecl>(D)) {
603 Out << "Read linkage spec decl\n";
Douglas Gregor305c22e2009-01-23 01:10:18 +0000604 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
605 Out << "Read top-level variable decl: '" << ND->getNameAsString()
606 << "'\n";
Eli Friedmanf595eb02008-12-16 22:14:15 +0000607 } else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000608 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000609 }
610 }
611 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000612}
613
Chris Lattner3d4997d2007-09-15 23:02:28 +0000614ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
615
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000616//===----------------------------------------------------------------------===//
617/// ASTViewer - AST Visualization
618
Ted Kremenek80de08f2007-09-19 21:29:43 +0000619namespace {
620 class ASTViewer : public ASTConsumer {
621 SourceManager *SM;
622 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000623 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000624 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000625 }
626
627 virtual void HandleTopLevelDecl(Decl *D) {
628 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000629 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000630
631 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000632 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000633 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000634 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000635 }
636 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000637 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
638 DeclPrinter().PrintObjCMethodDecl(MD);
639
640 if (MD->getBody()) {
641 llvm::cerr << '\n';
642 MD->getBody()->viewAST();
643 llvm::cerr << '\n';
644 }
645 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000646 }
647 };
648}
649
650ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
651
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000652//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000653/// DeclContextPrinter - Decl and DeclContext Visualization
654
655namespace {
656
657class DeclContextPrinter : public ASTConsumer {
658 llvm::raw_ostream& Out;
659public:
660 DeclContextPrinter() : Out(llvm::errs()) {}
661
662 void HandleTranslationUnit(TranslationUnit& TU) {
663 TranslationUnitDecl* TUD = TU.getContext().getTranslationUnitDecl();
664 PrintDeclContext(TUD, 4);
665 }
666
667 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
668};
669
670void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
671 unsigned Indentation) {
672 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000673 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000674 case Decl::TranslationUnit:
675 Out << "[translation unit] " << DC;
676 break;
677 case Decl::Namespace: {
678 Out << "[namespace] ";
679 NamespaceDecl* ND = NamespaceDecl::castFromDeclContext(DC);
680 Out << ND->getNameAsString();
681 break;
682 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000683 case Decl::Enum: {
684 EnumDecl* ED = EnumDecl::castFromDeclContext(DC);
685 if (ED->isDefinition())
686 Out << "[enum] ";
687 else
688 Out << "<enum> ";
689 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000690 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000691 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000692 case Decl::Record: {
693 RecordDecl* RD = RecordDecl::castFromDeclContext(DC);
694 if (RD->isDefinition())
695 Out << "[struct] ";
696 else
697 Out << "<struct> ";
698 Out << RD->getNameAsString();
699 break;
700 }
701 case Decl::CXXRecord: {
702 CXXRecordDecl* RD = CXXRecordDecl::castFromDeclContext(DC);
703 if (RD->isDefinition())
704 Out << "[class] ";
705 else
706 Out << "<class> ";
707 Out << RD->getNameAsString() << " " << DC;
708 break;
709 }
710 case Decl::ObjCMethod:
711 Out << "[objc method]";
712 break;
713 case Decl::ObjCInterface:
714 Out << "[objc interface]";
715 break;
716 case Decl::ObjCCategory:
717 Out << "[objc category]";
718 break;
719 case Decl::ObjCProtocol:
720 Out << "[objc protocol]";
721 break;
722 case Decl::ObjCImplementation:
723 Out << "[objc implementation]";
724 break;
725 case Decl::ObjCCategoryImpl:
726 Out << "[objc categoryimpl]";
727 break;
728 case Decl::LinkageSpec:
729 Out << "[linkage spec]";
730 break;
731 case Decl::Block:
732 Out << "[block]";
733 break;
734 case Decl::Function: {
735 FunctionDecl* FD = FunctionDecl::castFromDeclContext(DC);
736 if (FD->isThisDeclarationADefinition())
737 Out << "[function] ";
738 else
739 Out << "<function> ";
740 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000741 // Print the parameters.
742 Out << "(";
743 bool PrintComma = false;
744 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
745 E = FD->param_end(); I != E; ++I) {
746 if (PrintComma)
747 Out << ", ";
748 else
749 PrintComma = true;
750 Out << (*I)->getNameAsString();
751 }
752 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000753 break;
754 }
755 case Decl::CXXMethod: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000756 CXXMethodDecl* D = CXXMethodDecl::castFromDeclContext(DC);
757 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000758 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000759 else if (D->isImplicit())
760 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000761 else
762 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000763 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000764 // Print the parameters.
765 Out << "(";
766 bool PrintComma = false;
767 for (FunctionDecl::param_const_iterator I = D->param_begin(),
768 E = D->param_end(); I != E; ++I) {
769 if (PrintComma)
770 Out << ", ";
771 else
772 PrintComma = true;
773 Out << (*I)->getNameAsString();
774 }
775 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000776
777 // Check the semantic DeclContext.
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000778 DeclContext* SemaDC = D->getDeclContext();
779 DeclContext* LexicalDC = D->getLexicalDeclContext();
780 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000781 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000782
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000783 break;
784 }
785 case Decl::CXXConstructor: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000786 CXXConstructorDecl* D = CXXConstructorDecl::castFromDeclContext(DC);
787 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000788 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000789 else if (D->isImplicit())
790 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000791 else
792 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000793 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000794 // Print the parameters.
795 Out << "(";
796 bool PrintComma = false;
797 for (FunctionDecl::param_const_iterator I = D->param_begin(),
798 E = D->param_end(); I != E; ++I) {
799 if (PrintComma)
800 Out << ", ";
801 else
802 PrintComma = true;
803 Out << (*I)->getNameAsString();
804 }
805 Out << ")";
806
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000807 // Check the semantic DC.
808 DeclContext* SemaDC = D->getDeclContext();
809 DeclContext* LexicalDC = D->getLexicalDeclContext();
810 if (SemaDC != LexicalDC)
811 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000812 break;
813 }
814 case Decl::CXXDestructor: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000815 CXXDestructorDecl* D = CXXDestructorDecl::castFromDeclContext(DC);
816 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000817 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000818 else if (D->isImplicit())
819 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000820 else
821 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000822 Out << D->getNameAsString();
823 // Check the semantic DC.
824 DeclContext* SemaDC = D->getDeclContext();
825 DeclContext* LexicalDC = D->getLexicalDeclContext();
826 if (SemaDC != LexicalDC)
827 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000828 break;
829 }
830 case Decl::CXXConversion: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000831 CXXConversionDecl* D = CXXConversionDecl::castFromDeclContext(DC);
832 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000833 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000834 else if (D->isImplicit())
835 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000836 else
837 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000838 Out << D->getNameAsString();
839 // Check the semantic DC.
840 DeclContext* SemaDC = D->getDeclContext();
841 DeclContext* LexicalDC = D->getLexicalDeclContext();
842 if (SemaDC != LexicalDC)
843 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000844 break;
845 }
846
847 default:
848 assert(0 && "a decl that inherits DeclContext isn't handled");
849 }
850
851 Out << "\n";
852
853 // Print decls in the DeclContext.
854 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
855 I != E; ++I) {
856 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000857 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000858
859 Decl::Kind DK = I->getKind();
860 switch (DK) {
861 case Decl::Namespace:
862 case Decl::Enum:
863 case Decl::Record:
864 case Decl::CXXRecord:
865 case Decl::ObjCMethod:
866 case Decl::ObjCInterface:
867 case Decl::ObjCCategory:
868 case Decl::ObjCProtocol:
869 case Decl::ObjCImplementation:
870 case Decl::ObjCCategoryImpl:
871 case Decl::LinkageSpec:
872 case Decl::Block:
873 case Decl::Function:
874 case Decl::CXXMethod:
875 case Decl::CXXConstructor:
876 case Decl::CXXDestructor:
877 case Decl::CXXConversion:
878 {
879 DeclContext* DC = Decl::castToDeclContext(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000880 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000881 break;
882 }
883 case Decl::Field: {
884 FieldDecl* FD = cast<FieldDecl>(*I);
885 Out << "<field> " << FD->getNameAsString() << "\n";
886 break;
887 }
888 case Decl::Typedef: {
889 TypedefDecl* TD = cast<TypedefDecl>(*I);
890 Out << "<typedef> " << TD->getNameAsString() << "\n";
891 break;
892 }
893 case Decl::EnumConstant: {
894 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
895 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
896 break;
897 }
898 case Decl::Var: {
899 VarDecl* VD = cast<VarDecl>(*I);
900 Out << "<var> " << VD->getNameAsString() << "\n";
901 break;
902 }
903 case Decl::ImplicitParam: {
904 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
905 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
906 break;
907 }
908 case Decl::CXXClassVar: {
909 CXXClassVarDecl* CVD = cast<CXXClassVarDecl>(*I);
910 Out << "<static member var> " << CVD->getNameAsString() << "\n";
911 break;
912 }
913 case Decl::ParmVar: {
914 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
915 Out << "<parameter> " << PVD->getNameAsString() << "\n";
916 break;
917 }
918 case Decl::ObjCProperty: {
919 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
920 Out << "<objc property> " << OPD->getNameAsString() << "\n";
921 break;
922 }
923 default:
924 fprintf(stderr, "DeclKind: %d\n", DK);
925 assert(0 && "decl unhandled");
926 }
927 }
928}
929
930}
931
932ASTConsumer *clang::CreateDeclContextPrinter() {
933 return new DeclContextPrinter();
934}
935
936//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000937/// InheritanceViewer - C++ Inheritance Visualization
938
939namespace {
940class InheritanceViewer : public ASTConsumer {
941 const std::string clsname;
942public:
943 InheritanceViewer(const std::string& cname) : clsname(cname) {}
944
945 void HandleTranslationUnit(TranslationUnit& TU) {
946 ASTContext& C = TU.getContext();
947 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
948 if (CXXRecordType *T = dyn_cast<CXXRecordType>(*I)) {
949 CXXRecordDecl* D = T->getDecl();
950 // FIXME: This lookup needs to be generalized to handle namespaces and
951 // (when we support them) templates.
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000952 if (D->getNameAsString() == clsname) {
Douglas Gregor1f812302008-10-24 19:53:54 +0000953 D->viewInheritance(C);
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000954 }
955 }
956 }
957};
958}
959
960ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
961 return new InheritanceViewer(clsname);
962}
963
964//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000965// AST Serializer
966
967namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000968
969class ASTSerializer : public ASTConsumer {
970protected:
Nico Weberdae86962008-08-09 18:32:11 +0000971 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000972
Ted Kremenekf06c9282007-12-19 23:49:37 +0000973public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000974 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000975};
Eli Friedmand8a65c12008-06-09 20:02:51 +0000976
Ted Kremenekf06c9282007-12-19 23:49:37 +0000977class SingleFileSerializer : public ASTSerializer {
978 const llvm::sys::Path FName;
979public:
Nico Weberdae86962008-08-09 18:32:11 +0000980 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
981 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000982
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000983 virtual void HandleTranslationUnit(TranslationUnit& TU) {
Nico Weberdae86962008-08-09 18:32:11 +0000984 if (Diags.hasErrorOccurred())
985 return;
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000986 EmitASTBitcodeFile(&TU, FName);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000987 }
988};
989
990class BuildSerializer : public ASTSerializer {
991 llvm::sys::Path EmitDir;
992public:
Nico Weberdae86962008-08-09 18:32:11 +0000993 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
994 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000995
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000996 virtual void HandleTranslationUnit(TranslationUnit& TU) {
997 if (Diags.hasErrorOccurred())
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000998 return;
999
Nico Weberbe1eb5c2008-08-10 19:20:05 +00001000 SourceManager& SourceMgr = TU.getContext().getSourceManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +00001001 FileID ID = SourceMgr.getMainFileID();
1002 assert(!ID.isInvalid() && "MainFileID not set!");
Ted Kremenek54117722007-12-20 00:34:58 +00001003 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
Chris Lattner2b2453a2009-01-17 06:22:33 +00001004 assert(FE && "No FileEntry for main file.");
Ted Kremenek54117722007-12-20 00:34:58 +00001005
1006 // FIXME: This is not portable to Windows.
1007 // FIXME: This logic should probably be moved elsewhere later.
1008
Ted Kremenekee533642007-12-20 19:47:16 +00001009 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +00001010
1011 std::vector<char> buf;
1012 buf.reserve(strlen(FE->getName())+100);
1013
1014 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +00001015 FName.appendComponent(&buf[0]);
1016 FName.createDirectoryOnDisk(true);
1017 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +00001018 assert (false && "Could not create 'device' serialization directory.");
1019 return;
1020 }
Ted Kremenekee533642007-12-20 19:47:16 +00001021
Ted Kremenek54117722007-12-20 00:34:58 +00001022 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +00001023 FName.appendComponent(&buf[0]);
Nico Weberbe1eb5c2008-08-10 19:20:05 +00001024 EmitASTBitcodeFile(&TU, FName);
Ted Kremenek54117722007-12-20 00:34:58 +00001025
Ted Kremenekee533642007-12-20 19:47:16 +00001026 // Now emit the sources.
1027
Ted Kremenek54117722007-12-20 00:34:58 +00001028 }
Ted Kremenekf06c9282007-12-19 23:49:37 +00001029};
1030
1031
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001032} // end anonymous namespace
1033
1034
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001035ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +00001036 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001037 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +00001038
Ted Kremenekf06c9282007-12-19 23:49:37 +00001039 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +00001040 if (InFile == "-") {
1041 llvm::cerr <<
1042 "error: Cannot use --serialize with -o for source read from STDIN.\n";
1043 return NULL;
1044 }
1045
Ted Kremenekf06c9282007-12-19 23:49:37 +00001046 // The user specified an AST-emission directory. Determine if the path
1047 // is absolute.
1048 llvm::sys::Path EmitDir(OutputFile);
1049
1050 if (!EmitDir.isAbsolute()) {
1051 llvm::cerr <<
1052 "error: Output directory for --serialize must be an absolute path.\n";
1053
1054 return NULL;
1055 }
1056
1057 // Create the directory if it does not exist.
1058 EmitDir.createDirectoryOnDisk(true);
1059 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
1060 llvm::cerr <<
1061 "error: Could not create output directory for --serialize.\n";
1062
1063 return NULL;
1064 }
1065
Ted Kremenek54117722007-12-20 00:34:58 +00001066 // FIXME: We should probably only allow using BuildSerializer when
1067 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +00001068 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +00001069 }
1070
1071 // The user did not specify an output directory for serialized ASTs.
1072 // Serialize the translation to a single file whose name is the same
1073 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +00001074
Ted Kremenekf06c9282007-12-19 23:49:37 +00001075 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +00001076 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +00001077 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001078}