blob: 69f344cac46326edc6f78fd88b7a7860f8e5c1e9 [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 Kremenek77cda502007-12-18 21:34:28 +000015#include "clang/AST/TranslationUnit.h"
Ted Kremenek54117722007-12-20 00:34:58 +000016#include "clang/Basic/SourceManager.h"
17#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000019#include "clang/AST/ASTConsumer.h"
Ted Kremenekfddd5182007-08-21 21:42:03 +000020#include "clang/AST/CFG.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekee985462008-01-16 18:18:48 +000022#include "clang/Analysis/Analyses/GRConstants.h"
Ted Kremenek055c2752007-09-06 23:00:42 +000023#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000024#include "llvm/Support/Streams.h"
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +000025#include <fstream>
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000026
Chris Lattner6000dac2007-08-08 22:51:59 +000027using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000028
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000029//===----------------------------------------------------------------------===//
30/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000031
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000032namespace {
33 class DeclPrinter {
34 public:
Ted Kremenekea75c552007-11-28 21:32:21 +000035 std::ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000036
Chris Lattner4b1daf02008-01-10 01:43:14 +000037 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekea75c552007-11-28 21:32:21 +000038 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000039
Chris Lattneref5a85d2008-01-02 21:04:16 +000040 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000041 void PrintFunctionDeclStart(FunctionDecl *FD);
42 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000043 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000044 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
45 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
46 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
47 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
48 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
49 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
50 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000051 };
52} // end anonymous namespace
53
Chris Lattneref5a85d2008-01-02 21:04:16 +000054void DeclPrinter:: PrintDecl(Decl *D) {
55 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
56 PrintFunctionDeclStart(FD);
57
58 if (FD->getBody()) {
59 Out << ' ';
60 FD->getBody()->printPretty(Out);
61 Out << '\n';
62 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000063 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000064 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000065 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000066 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
67 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000068 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
69 PrintObjCInterfaceDecl(OID);
70 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
71 PrintObjCProtocolDecl(PID);
72 } else if (ObjCForwardProtocolDecl *OFPD =
73 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000074 Out << "@protocol ";
75 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000077 if (i) Out << ", ";
78 Out << D->getName();
79 }
80 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000081 } else if (ObjCImplementationDecl *OID =
82 dyn_cast<ObjCImplementationDecl>(D)) {
83 PrintObjCImplementationDecl(OID);
84 } else if (ObjCCategoryImplDecl *OID =
85 dyn_cast<ObjCCategoryImplDecl>(D)) {
86 PrintObjCCategoryImplDecl(OID);
87 } else if (ObjCCategoryDecl *OID =
88 dyn_cast<ObjCCategoryDecl>(D)) {
89 PrintObjCCategoryDecl(OID);
90 } else if (ObjCCompatibleAliasDecl *OID =
91 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
92 PrintObjCCompatibleAliasDecl(OID);
93 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000094 Out << "@class [printing todo]\n";
95 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
96 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
97 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
98 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +000099 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
100 PrintLinkageSpec(LSD);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000101 } else {
102 assert(0 && "Unknown decl type!");
103 }
104}
105
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000106void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 bool HasBody = FD->getBody();
108
Ted Kremenekea75c552007-11-28 21:32:21 +0000109 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000110
111 switch (FD->getStorageClass()) {
112 default: assert(0 && "Unknown storage class");
113 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000114 case FunctionDecl::Extern: Out << "extern "; break;
115 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000116 }
117
118 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000119 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000120
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000122 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000123
Chris Lattner0d6ca112007-12-03 21:43:25 +0000124 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 Proto += "(";
126 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
127 if (i) Proto += ", ";
128 std::string ParamStr;
129 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
130
131 FT->getArgType(i).getAsStringInternal(ParamStr);
132 Proto += ParamStr;
133 }
134
135 if (FT->isVariadic()) {
136 if (FD->getNumParams()) Proto += ", ";
137 Proto += "...";
138 }
139 Proto += ")";
140 } else {
141 assert(isa<FunctionTypeNoProto>(AFT));
142 Proto += "()";
143 }
144
145 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000146 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000147
Chris Lattner6000dac2007-08-08 22:51:59 +0000148 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000149 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000150 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000151}
152
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000153void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 std::string S = TD->getName();
155 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000156 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000157}
158
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000159void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
160 const char *l;
161 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
162 l = "C";
163 else if (LS->getLanguage() == LinkageSpecDecl::lang_cxx)
164 l = "C++";
165 else assert(0 && "unknown language in linkage specification");
166 Out << "extern \"" << l << "\" { ";
167 PrintDecl(LS->getDecl());
168 Out << "}\n";
169}
170
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000171void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000172 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000173 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000174 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000175 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000176 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000177 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000178 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000179 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000180
181 for (int i = 0; i < OMD->getNumParams(); i++) {
182 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000183 // FIXME: selector is missing here!
184 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000185 }
186}
187
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000188void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000189 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000190 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000191
192 if (SID)
193 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000194 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000195 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000196
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000197 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000198 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000199 ObjCMethodDecl *OMD = *I;
200 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000201 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000202 Out << ' ';
203 OMD->getBody()->printPretty(Out);
204 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000205 }
206 }
207
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000208 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000209 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000210 ObjCMethodDecl *OMD = *I;
211 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000212 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000213 Out << ' ';
214 OMD->getBody()->printPretty(Out);
215 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000216 }
217 }
218
Ted Kremenekea75c552007-11-28 21:32:21 +0000219 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000220}
221
222
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000223void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000224 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000225 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000226
227 if (SID)
228 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000229 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000230 Out << "@interface " << I;
231
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000232 // Protocols?
233 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000234
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000235 if (count > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000236 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000237 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000238 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000239 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000240
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000241 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000242 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000243 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000244 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000245
Chris Lattnerbe6df082007-12-12 07:56:42 +0000246 if (OID->getNumInstanceVariables() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000247 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000248 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000249 E = OID->ivar_end(); I != E; ++I) {
250 Out << '\t' << (*I)->getType().getAsString()
251 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000252 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000253 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000254 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000255
256 int NumProperties = OID->getNumPropertyDecl();
257 if (NumProperties > 0) {
258 for (int i = 0; i < NumProperties; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000259 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekea75c552007-11-28 21:32:21 +0000260 Out << "@property";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000261 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000262 bool first = true;
Ted Kremenekea75c552007-11-28 21:32:21 +0000263 Out << " (";
Chris Lattner4b1daf02008-01-10 01:43:14 +0000264 if (PDecl->getPropertyAttributes() &
265 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000266 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000267 first = false;
268 }
269
Chris Lattner4b1daf02008-01-10 01:43:14 +0000270 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000271 Out << (first ? ' ' : ',') << "getter = "
272 << PDecl->getGetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000273 first = false;
274 }
Chris Lattner4b1daf02008-01-10 01:43:14 +0000275 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000276 Out << (first ? ' ' : ',') << "setter = "
277 << PDecl->getSetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000278 first = false;
279 }
280
Chris Lattner4b1daf02008-01-10 01:43:14 +0000281 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000282 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000283 first = false;
284 }
285
Chris Lattner4b1daf02008-01-10 01:43:14 +0000286 if (PDecl->getPropertyAttributes() &
287 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000288 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000289 first = false;
290 }
291
Chris Lattner4b1daf02008-01-10 01:43:14 +0000292 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000293 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000294 first = false;
295 }
296
Chris Lattner4b1daf02008-01-10 01:43:14 +0000297 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000298 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000299 first = false;
300 }
301
Chris Lattner4b1daf02008-01-10 01:43:14 +0000302 if (PDecl->getPropertyAttributes() &
303 ObjCPropertyDecl::OBJC_PR_nonatomic) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000304 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000305 first = false;
306 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000307 Out << " )";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000308 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000309
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000310 ObjCIvarDecl **IDecl = PDecl->getPropertyDecls();
Ted Kremenekea75c552007-11-28 21:32:21 +0000311
312 Out << ' ' << IDecl[0]->getType().getAsString()
313 << ' ' << IDecl[0]->getName();
314
315 for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
316 Out << ", " << IDecl[j]->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000317
Ted Kremenekea75c552007-11-28 21:32:21 +0000318 Out << ";\n";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000319 }
320 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000321
322 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000323 // FIXME: implement the rest...
324}
325
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000326void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000327 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000328 // FIXME: implement the rest...
329}
330
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000331void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000332 Out << "@implementation "
333 << PID->getClassInterface()->getName()
334 << '(' << PID->getName() << ");\n";
335
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000336 // FIXME: implement the rest...
337}
338
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000339void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000340 Out << "@interface "
341 << PID->getClassInterface()->getName()
342 << '(' << PID->getName() << ");\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000343 // FIXME: implement the rest...
344}
345
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000346void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000347 Out << "@compatibility_alias " << AID->getName()
348 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000349}
350
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000351//===----------------------------------------------------------------------===//
352/// ASTPrinter - Pretty-printer of ASTs
353
Chris Lattner3d4997d2007-09-15 23:02:28 +0000354namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000355 class ASTPrinter : public ASTConsumer, public DeclPrinter {
356 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000357 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000358
Chris Lattner3d4997d2007-09-15 23:02:28 +0000359 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000360 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000362 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000363}
Chris Lattner6000dac2007-08-08 22:51:59 +0000364
Ted Kremenekea75c552007-11-28 21:32:21 +0000365ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
366 return new ASTPrinter(out);
367}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000368
369//===----------------------------------------------------------------------===//
370/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000371
372namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000373 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000374 SourceManager *SM;
375 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000376 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000377
Ted Kremenek95041a22007-12-19 22:51:13 +0000378 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000379 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000380 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000381
382 virtual void HandleTopLevelDecl(Decl *D) {
383 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
384 PrintFunctionDeclStart(FD);
385
386 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000387 Out << '\n';
388 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000389 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000390 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000391 }
392 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
393 PrintTypeDefDecl(TD);
394 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000395 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000397 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000398 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000399 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000400 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000401 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000402 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000403 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000404 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000405 Out << "Read objc fwd class decl\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000406 } else {
407 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000408 }
409 }
410 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000411}
412
Chris Lattner3d4997d2007-09-15 23:02:28 +0000413ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
414
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000415//===----------------------------------------------------------------------===//
416/// ASTViewer - AST Visualization
417
Ted Kremenek80de08f2007-09-19 21:29:43 +0000418namespace {
419 class ASTViewer : public ASTConsumer {
420 SourceManager *SM;
421 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000422 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000423 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000424 }
425
426 virtual void HandleTopLevelDecl(Decl *D) {
427 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000428 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000429
430 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000431 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000432 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000433 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000434 }
435 }
436 }
437 };
438}
439
440ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
441
442
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000443//===----------------------------------------------------------------------===//
444// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
445// the CFGs for all function definitions.
446
447namespace {
448
Chris Lattnerc0508f92007-09-15 23:21:08 +0000449class CFGVisitor : public ASTConsumer {
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000450public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000451 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000452 virtual void VisitCFG(CFG& C) = 0;
453 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000454
455 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000456};
457
458} // end anonymous namespace
459
Chris Lattnerc0508f92007-09-15 23:21:08 +0000460void CFGVisitor::HandleTopLevelDecl(Decl *D) {
461 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
462 if (!FD || !FD->getBody())
463 return;
464
465 if (printFuncDeclStart()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000466 DeclPrinter().PrintFunctionDeclStart(FD);
467 llvm::cerr << '\n';
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000468 }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000469
Ted Kremenek12259662007-09-17 17:10:02 +0000470 CFG *C = CFG::buildCFG(FD->getBody());
471 VisitCFG(*C);
472 delete C;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000473}
474
475//===----------------------------------------------------------------------===//
476// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
477
478namespace {
479 class CFGDumper : public CFGVisitor {
480 const bool UseGraphviz;
481 public:
482 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
483
Chris Lattnerc0508f92007-09-15 23:21:08 +0000484 virtual void VisitCFG(CFG &C) {
485 if (UseGraphviz)
486 C.viewCFG();
487 else
488 C.dump();
489 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000490 };
491} // end anonymous namespace
492
Chris Lattnerc0508f92007-09-15 23:21:08 +0000493ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
494 return new CFGDumper(ViewGraphs);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000495}
496
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000497//===----------------------------------------------------------------------===//
498// AnalyzeLiveVariables - perform live variable analysis and dump results
499
500namespace {
501 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000502 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000503 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000504 virtual void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000505 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000506 }
507
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000508 virtual void VisitCFG(CFG& C) {
Ted Kremenek11e72182007-10-01 20:33:52 +0000509 LiveVariables L(C);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000510 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000511 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000512 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000513 };
514} // end anonymous namespace
515
Chris Lattnerc0508f92007-09-15 23:21:08 +0000516ASTConsumer *clang::CreateLiveVarAnalyzer() {
517 return new LivenessVisitor();
Ted Kremeneke4e63342007-09-06 00:17:54 +0000518}
519
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000520//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000521// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000522
523namespace {
524 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000525 Diagnostic &Diags;
526 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000527 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000528 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000529 virtual void Initialize(ASTContext &Context) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000530 Ctx = &Context;
531 }
532
533 virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
Ted Kremenek567a7e62007-09-07 23:54:15 +0000534 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000535 };
536} // end anonymous namespace
537
Chris Lattnerc0508f92007-09-15 23:21:08 +0000538ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
539 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000540}
Chris Lattner580980b2007-09-16 19:46:59 +0000541
542//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000543// Unitialized Values - run checker to flag potential uses of uninitalized
544// variables.
545
546namespace {
547 class UninitValsVisitor : public CFGVisitor {
548 Diagnostic &Diags;
549 ASTContext *Ctx;
550 public:
551 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000552 virtual void Initialize(ASTContext &Context) {
Ted Kremenek2bf55142007-09-17 20:49:30 +0000553 Ctx = &Context;
554 }
555
556 virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); }
557 virtual bool printFuncDeclStart() { return false; }
558 };
559} // end anonymous namespace
560
561ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
562 return new UninitValsVisitor(Diags);
563}
564
565//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +0000566// GRConstants - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremeneke603df42008-01-08 18:04:06 +0000567
568namespace {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000569 class GRConstantsVisitor : public ASTConsumer {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000570 ASTContext* Ctx;
Ted Kremeneke603df42008-01-08 18:04:06 +0000571 public:
Ted Kremeneke603df42008-01-08 18:04:06 +0000572
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000573 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
574 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremeneke603df42008-01-08 18:04:06 +0000575 };
576} // end anonymous namespace
577
Ted Kremenek874d63f2008-01-24 02:02:54 +0000578ASTConsumer* clang::CreateGRConstants() {
Ted Kremenekee985462008-01-16 18:18:48 +0000579 return new GRConstantsVisitor();
Ted Kremeneke603df42008-01-08 18:04:06 +0000580}
581
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000582void GRConstantsVisitor::HandleTopLevelDecl(Decl *D) {
583 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
584
585 if (!FD || !FD->getBody())
586 return;
587
588 DeclPrinter().PrintFunctionDeclStart(FD);
589 llvm::cerr << '\n';
590
591 CFG *C = CFG::buildCFG(FD->getBody());
592 RunGRConstants(*C, *FD, *Ctx);
593 delete C;
594}
595
Ted Kremeneke603df42008-01-08 18:04:06 +0000596//===----------------------------------------------------------------------===//
Chris Lattner580980b2007-09-16 19:46:59 +0000597// LLVM Emitter
598
599#include "clang/Basic/Diagnostic.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000600#include "clang/Basic/TargetInfo.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000601#include "clang/CodeGen/ModuleBuilder.h"
602#include "llvm/Module.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000603#include "llvm/Target/TargetData.h"
604#include "llvm/Target/TargetMachine.h"
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000605#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000606
607namespace {
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000608 class CodeGenerator : public ASTConsumer {
Chris Lattner580980b2007-09-16 19:46:59 +0000609 Diagnostic &Diags;
Devang Patel7a4718e2007-10-31 20:01:01 +0000610 const llvm::TargetData *TD;
Chris Lattner580980b2007-09-16 19:46:59 +0000611 ASTContext *Ctx;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000612 const LangOptions &Features;
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000613 protected:
614 llvm::Module *M;
Chris Lattnera36c4862007-11-13 18:16:41 +0000615 CodeGen::CodeGenModule *Builder;
Chris Lattner580980b2007-09-16 19:46:59 +0000616 public:
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000617 CodeGenerator(Diagnostic &diags, const LangOptions &LO)
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000618 : Diags(diags)
619 , Features(LO) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000620 virtual void Initialize(ASTContext &Context) {
Chris Lattner580980b2007-09-16 19:46:59 +0000621 Ctx = &Context;
622 M = new llvm::Module("foo");
Devang Patel7a4718e2007-10-31 20:01:01 +0000623 M->setTargetTriple(Ctx->Target.getTargetTriple());
Chris Lattner0404cd82007-12-13 17:34:31 +0000624 M->setDataLayout(Ctx->Target.getTargetDescription());
Devang Patel7a4718e2007-10-31 20:01:01 +0000625 TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
Chris Lattnerfb97b032007-12-02 01:40:18 +0000626 Builder = CodeGen::Init(Context, Features, *M, *TD, Diags);
Chris Lattner580980b2007-09-16 19:46:59 +0000627 }
628
629 virtual void HandleTopLevelDecl(Decl *D) {
630 // If an error occurred, stop code generation, but continue parsing and
631 // semantic analysis (to ensure all warnings and errors are emitted).
632 if (Diags.hasErrorOccurred())
633 return;
634
635 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
636 CodeGen::CodeGenFunction(Builder, FD);
637 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
638 CodeGen::CodeGenGlobalVar(Builder, FVD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000639 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
640 CodeGen::CodeGenLinkageSpec(Builder, LSD);
Chris Lattner580980b2007-09-16 19:46:59 +0000641 } else {
Steve Naroff91578f32007-11-17 21:21:01 +0000642 assert(isa<TypeDecl>(D) && "Only expected type decls here");
Chris Lattner580980b2007-09-16 19:46:59 +0000643 // don't codegen for now, eventually pass down for debug info.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000644 //std::cerr << "Read top-level typedef decl: '"
645 // << D->getName() << "'\n";
Chris Lattner580980b2007-09-16 19:46:59 +0000646 }
647 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000648 };
649}
650
651namespace {
652 class LLVMEmitter : public CodeGenerator {
653 public:
654 LLVMEmitter(Diagnostic &diags, const LangOptions &LO)
655 : CodeGenerator(diags,LO) {}
656
Chris Lattner580980b2007-09-16 19:46:59 +0000657 ~LLVMEmitter() {
658 CodeGen::Terminate(Builder);
659
660 // Print the generated code.
Ted Kremenek3821d402007-12-13 17:50:11 +0000661 M->print(llvm::cout.stream());
Chris Lattner580980b2007-09-16 19:46:59 +0000662 delete M;
663 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000664 };
665}
Chris Lattner580980b2007-09-16 19:46:59 +0000666
Ted Kremenekf06c9282007-12-19 23:49:37 +0000667ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags,
668 const LangOptions &Features) {
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000669 return new LLVMEmitter(Diags, Features);
Chris Lattner580980b2007-09-16 19:46:59 +0000670}
671
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000672namespace {
673 class BCWriter : public CodeGenerator {
674 public:
675 std::ostream& Out;
676
677 BCWriter(std::ostream* out, Diagnostic &diags, const LangOptions &LO)
678 : CodeGenerator(diags,LO)
679 , Out(*out) {}
680
681 ~BCWriter() {
682 CodeGen::Terminate(Builder);
683 llvm::WriteBitcodeToFile(M, Out);
684 delete M;
685 }
686 };
687}
688
689ASTConsumer *clang::CreateBCWriter(const std::string& InFile,
690 const std::string& OutputFile,
691 Diagnostic &Diags,
692 const LangOptions &Features) {
693 std::string FileName = OutputFile;
Christopher Lamb2d6c0652007-12-24 03:23:55 +0000694
695 std::ostream *Out;
Christopher Lamb396f9fe2007-12-24 20:59:36 +0000696 if (OutputFile == "-")
Christopher Lamb8bd848f2007-12-24 20:56:07 +0000697 Out = llvm::cout.stream();
698 else if (!OutputFile.size()) {
Christopher Lamb396f9fe2007-12-24 20:59:36 +0000699 if (InFile == "-")
700 Out = llvm::cout.stream();
701 else {
702 llvm::sys::Path Path(InFile);
703 Path.eraseSuffix();
704 Path.appendSuffix("bc");
705 FileName = Path.toString();
Christopher Lamb4c92b432007-12-24 23:49:33 +0000706 Out = new std::ofstream(FileName.c_str(),
707 std::ios_base::binary|std::ios_base::out);
Christopher Lamb396f9fe2007-12-24 20:59:36 +0000708 }
Christopher Lamb2d6c0652007-12-24 03:23:55 +0000709 } else {
Christopher Lamb4c92b432007-12-24 23:49:33 +0000710 Out = new std::ofstream(FileName.c_str(),
711 std::ios_base::binary|std::ios_base::out);
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000712 }
713
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000714 return new BCWriter(Out, Diags, Features);
715}
716
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000717//===----------------------------------------------------------------------===//
718// AST Serializer
719
720namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000721
722class ASTSerializer : public ASTConsumer {
723protected:
724 Diagnostic &Diags;
725 TranslationUnit TU;
726public:
727 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
728 : Diags(diags), TU(LO) {}
729
730 virtual void Initialize(ASTContext &Context) {
731 TU.setContext(&Context);
732 }
733
734 virtual void HandleTopLevelDecl(Decl *D) {
735 if (Diags.hasErrorOccurred())
736 return;
737
738 TU.AddTopLevelDecl(D);
739 }
740};
741
742class SingleFileSerializer : public ASTSerializer {
743 const llvm::sys::Path FName;
744public:
745 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
746 const LangOptions &LO)
747 : ASTSerializer(diags,LO), FName(F) {}
748
749 ~SingleFileSerializer() {
750 EmitASTBitcodeFile(TU,FName);
751 }
752};
753
754class BuildSerializer : public ASTSerializer {
755 llvm::sys::Path EmitDir;
756public:
757 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000758 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000759 : ASTSerializer(diags,LO), EmitDir(dir) {}
760
Ted Kremenek54117722007-12-20 00:34:58 +0000761 ~BuildSerializer() {
762 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
763 unsigned ID = SourceMgr.getMainFileID();
764 assert (ID && "MainFileID not set!");
765 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
766 assert (FE && "No FileEntry for main file.");
767
768 // FIXME: This is not portable to Windows.
769 // FIXME: This logic should probably be moved elsewhere later.
770
Ted Kremenekee533642007-12-20 19:47:16 +0000771 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000772
773 std::vector<char> buf;
774 buf.reserve(strlen(FE->getName())+100);
775
776 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000777 FName.appendComponent(&buf[0]);
778 FName.createDirectoryOnDisk(true);
779 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000780 assert (false && "Could not create 'device' serialization directory.");
781 return;
782 }
Ted Kremenekee533642007-12-20 19:47:16 +0000783
Ted Kremenek54117722007-12-20 00:34:58 +0000784 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000785 FName.appendComponent(&buf[0]);
786 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000787
Ted Kremenekee533642007-12-20 19:47:16 +0000788 // Now emit the sources.
789
Ted Kremenek54117722007-12-20 00:34:58 +0000790 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000791};
792
793
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000794} // end anonymous namespace
795
796
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000797ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000798 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000799 Diagnostic &Diags,
800 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000801
Ted Kremenekf06c9282007-12-19 23:49:37 +0000802 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000803 if (InFile == "-") {
804 llvm::cerr <<
805 "error: Cannot use --serialize with -o for source read from STDIN.\n";
806 return NULL;
807 }
808
Ted Kremenekf06c9282007-12-19 23:49:37 +0000809 // The user specified an AST-emission directory. Determine if the path
810 // is absolute.
811 llvm::sys::Path EmitDir(OutputFile);
812
813 if (!EmitDir.isAbsolute()) {
814 llvm::cerr <<
815 "error: Output directory for --serialize must be an absolute path.\n";
816
817 return NULL;
818 }
819
820 // Create the directory if it does not exist.
821 EmitDir.createDirectoryOnDisk(true);
822 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
823 llvm::cerr <<
824 "error: Could not create output directory for --serialize.\n";
825
826 return NULL;
827 }
828
Ted Kremenek54117722007-12-20 00:34:58 +0000829 // FIXME: We should probably only allow using BuildSerializer when
830 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000831 return new BuildSerializer(EmitDir, Diags, Features);
832 }
833
834 // The user did not specify an output directory for serialized ASTs.
835 // Serialize the translation to a single file whose name is the same
836 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000837
Ted Kremenekf06c9282007-12-19 23:49:37 +0000838 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000839 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000840 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000841}