blob: df9d2e4fe719be224c480ac10634de2f963b28e6 [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 Lattner97e8b6f2007-10-07 06:04:32 +00005// This file was developed by Chris Lattner and is distributed under the
Reid Spencer5f016e22007-07-11 17:01:13 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 Kremenek055c2752007-09-06 23:00:42 +000022#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000023#include "llvm/Support/Streams.h"
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +000024#include <fstream>
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000025
Chris Lattner6000dac2007-08-08 22:51:59 +000026using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000027
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000028//===----------------------------------------------------------------------===//
29/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000030
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000031namespace {
32 class DeclPrinter {
33 public:
Ted Kremenekea75c552007-11-28 21:32:21 +000034 std::ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000035
Ted Kremenekea75c552007-11-28 21:32:21 +000036 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
37 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000038
39 void PrintFunctionDeclStart(FunctionDecl *FD);
40 void PrintTypeDefDecl(TypedefDecl *TD);
41 void PrintObjcMethodDecl(ObjcMethodDecl *OMD);
42 void PrintObjcImplementationDecl(ObjcImplementationDecl *OID);
43 void PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID);
44 void PrintObjcProtocolDecl(ObjcProtocolDecl *PID);
45 void PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID);
46 void PrintObjcCategoryDecl(ObjcCategoryDecl *PID);
47 void PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID);
48 };
49} // end anonymous namespace
50
51void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +000052 bool HasBody = FD->getBody();
53
Ted Kremenekea75c552007-11-28 21:32:21 +000054 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +000055
56 switch (FD->getStorageClass()) {
57 default: assert(0 && "Unknown storage class");
58 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +000059 case FunctionDecl::Extern: Out << "extern "; break;
60 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +000061 }
62
63 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +000064 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +000065
Reid Spencer5f016e22007-07-11 17:01:13 +000066 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +000067 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +000068
Chris Lattner0d6ca112007-12-03 21:43:25 +000069 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000070 Proto += "(";
71 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
72 if (i) Proto += ", ";
73 std::string ParamStr;
74 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
75
76 FT->getArgType(i).getAsStringInternal(ParamStr);
77 Proto += ParamStr;
78 }
79
80 if (FT->isVariadic()) {
81 if (FD->getNumParams()) Proto += ", ";
82 Proto += "...";
83 }
84 Proto += ")";
85 } else {
86 assert(isa<FunctionTypeNoProto>(AFT));
87 Proto += "()";
88 }
89
90 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +000091 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +000092
Chris Lattner6000dac2007-08-08 22:51:59 +000093 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +000094 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +000095 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +000096}
97
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000098void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +000099 std::string S = TD->getName();
100 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000101 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000102}
103
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000104void DeclPrinter::PrintObjcMethodDecl(ObjcMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000105 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000106 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000107 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000108 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000109 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000110 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000111 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000112 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000113
114 for (int i = 0; i < OMD->getNumParams(); i++) {
115 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000116 // FIXME: selector is missing here!
117 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000118 }
119}
120
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000121void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000122 std::string I = OID->getName();
123 ObjcInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000124
125 if (SID)
126 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000127 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000128 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000129
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000130 for (ObjcImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
131 E = OID->instmeth_end(); I != E; ++I) {
132 ObjcMethodDecl *OMD = *I;
133 PrintObjcMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000134 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000135 Out << ' ';
136 OMD->getBody()->printPretty(Out);
137 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000138 }
139 }
140
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000141 for (ObjcImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
142 E = OID->classmeth_end(); I != E; ++I) {
143 ObjcMethodDecl *OMD = *I;
144 PrintObjcMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000145 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000146 Out << ' ';
147 OMD->getBody()->printPretty(Out);
148 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000149 }
150 }
151
Ted Kremenekea75c552007-11-28 21:32:21 +0000152 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000153}
154
155
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000156void DeclPrinter::PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000157 std::string I = OID->getName();
158 ObjcInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000159
160 if (SID)
161 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000162 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000163 Out << "@interface " << I;
164
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000165 // Protocols?
166 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000167
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000168 if (count > 0) {
169 ObjcProtocolDecl **refProtocols = OID->getReferencedProtocols();
170 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000171 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000172 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000173
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000174 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000175 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000176 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000177 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000178
Chris Lattnerbe6df082007-12-12 07:56:42 +0000179 if (OID->getNumInstanceVariables() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000180 Out << '{';
Chris Lattnerbe6df082007-12-12 07:56:42 +0000181 for (ObjcInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
182 E = OID->ivar_end(); I != E; ++I) {
183 Out << '\t' << (*I)->getType().getAsString()
184 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000185 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000186 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000187 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000188
189 int NumProperties = OID->getNumPropertyDecl();
190 if (NumProperties > 0) {
191 for (int i = 0; i < NumProperties; i++) {
192 ObjcPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekea75c552007-11-28 21:32:21 +0000193 Out << "@property";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000194 if (PDecl->getPropertyAttributes() != ObjcPropertyDecl::OBJC_PR_noattr) {
195 bool first = true;
Ted Kremenekea75c552007-11-28 21:32:21 +0000196 Out << " (";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000197 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readonly)
198 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000199 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000200 first = false;
201 }
202
203 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_getter)
204 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000205 Out << (first ? ' ' : ',') << "getter = "
206 << PDecl->getGetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000207 first = false;
208 }
209 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_setter)
210 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000211 Out << (first ? ' ' : ',') << "setter = "
212 << PDecl->getSetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000213 first = false;
214 }
215
216 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_assign)
217 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000218 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000219 first = false;
220 }
221
222 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readwrite)
223 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000224 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000225 first = false;
226 }
227
228 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_retain)
229 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000230 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000231 first = false;
232 }
233
234 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_copy)
235 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000236 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000237 first = false;
238 }
239
240 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_nonatomic)
241 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000242 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000243 first = false;
244 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000245 Out << " )";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000246 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000247
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000248 ObjcIvarDecl **IDecl = PDecl->getPropertyDecls();
Ted Kremenekea75c552007-11-28 21:32:21 +0000249
250 Out << ' ' << IDecl[0]->getType().getAsString()
251 << ' ' << IDecl[0]->getName();
252
253 for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
254 Out << ", " << IDecl[j]->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000255
Ted Kremenekea75c552007-11-28 21:32:21 +0000256 Out << ";\n";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000257 }
258 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000259
260 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000261 // FIXME: implement the rest...
262}
263
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000264void DeclPrinter::PrintObjcProtocolDecl(ObjcProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000265 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000266 // FIXME: implement the rest...
267}
268
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000269void DeclPrinter::PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000270 Out << "@implementation "
271 << PID->getClassInterface()->getName()
272 << '(' << PID->getName() << ");\n";
273
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000274 // FIXME: implement the rest...
275}
276
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000277void DeclPrinter::PrintObjcCategoryDecl(ObjcCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000278 Out << "@interface "
279 << PID->getClassInterface()->getName()
280 << '(' << PID->getName() << ");\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000281 // FIXME: implement the rest...
282}
283
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000284void DeclPrinter::PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000285 Out << "@compatibility_alias " << AID->getName()
286 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000287}
288
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000289//===----------------------------------------------------------------------===//
290/// ASTPrinter - Pretty-printer of ASTs
291
Chris Lattner3d4997d2007-09-15 23:02:28 +0000292namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000293 class ASTPrinter : public ASTConsumer, public DeclPrinter {
294 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000295 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000296
Chris Lattner3d4997d2007-09-15 23:02:28 +0000297 virtual void HandleTopLevelDecl(Decl *D) {
298 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
299 PrintFunctionDeclStart(FD);
300
301 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000302 Out << ' ';
303 FD->getBody()->printPretty(Out);
304 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000305 }
Steve Naroffaaa3cf82007-11-13 23:48:03 +0000306 } else if (isa<ObjcMethodDecl>(D)) {
307 // Do nothing, methods definitions are printed in
308 // PrintObjcImplementationDecl.
Chris Lattner3d4997d2007-09-15 23:02:28 +0000309 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
310 PrintTypeDefDecl(TD);
Chris Lattner9fa5e652007-10-06 18:52:10 +0000311 } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
312 PrintObjcInterfaceDecl(OID);
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000313 } else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) {
314 PrintObjcProtocolDecl(PID);
Chris Lattner9fa5e652007-10-06 18:52:10 +0000315 } else if (ObjcForwardProtocolDecl *OFPD =
316 dyn_cast<ObjcForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000317 Out << "@protocol ";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000318 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
319 const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000320 if (i) Out << ", ";
321 Out << D->getName();
Chris Lattner9fa5e652007-10-06 18:52:10 +0000322 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000323 Out << ";\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000324 } else if (ObjcImplementationDecl *OID =
325 dyn_cast<ObjcImplementationDecl>(D)) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000326 PrintObjcImplementationDecl(OID);
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000327 } else if (ObjcCategoryImplDecl *OID =
328 dyn_cast<ObjcCategoryImplDecl>(D)) {
329 PrintObjcCategoryImplDecl(OID);
330 } else if (ObjcCategoryDecl *OID =
331 dyn_cast<ObjcCategoryDecl>(D)) {
332 PrintObjcCategoryDecl(OID);
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000333 } else if (ObjcCompatibleAliasDecl *OID =
334 dyn_cast<ObjcCompatibleAliasDecl>(D)) {
335 PrintObjcCompatibleAliasDecl(OID);
Chris Lattner9fa5e652007-10-06 18:52:10 +0000336 } else if (isa<ObjcClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000337 Out << "@class [printing todo]\n";
Steve Naroff1f644322007-11-28 22:54:11 +0000338 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
339 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000340 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000341 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000342 } else {
343 assert(0 && "Unknown decl type!");
Chris Lattner6000dac2007-08-08 22:51:59 +0000344 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000346 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000347}
Chris Lattner6000dac2007-08-08 22:51:59 +0000348
Ted Kremenekea75c552007-11-28 21:32:21 +0000349ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
350 return new ASTPrinter(out);
351}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000352
353//===----------------------------------------------------------------------===//
354/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000355
356namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000357 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000358 SourceManager *SM;
359 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000360 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000361
Ted Kremenek95041a22007-12-19 22:51:13 +0000362 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000363 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000364 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000365
366 virtual void HandleTopLevelDecl(Decl *D) {
367 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
368 PrintFunctionDeclStart(FD);
369
370 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000371 Out << '\n';
372 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000373 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000374 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000375 }
376 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
377 PrintTypeDefDecl(TD);
378 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000379 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000380 } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000381 Out << "Read objc interface '" << OID->getName() << "'\n";
Steve Naroff8de28262007-10-14 17:03:01 +0000382 } else if (ObjcProtocolDecl *OPD = dyn_cast<ObjcProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000383 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Steve Naroff8de28262007-10-14 17:03:01 +0000384 } else if (ObjcCategoryDecl *OCD = dyn_cast<ObjcCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000385 Out << "Read objc category '" << OCD->getName() << "'\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000386 } else if (isa<ObjcForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000387 Out << "Read objc fwd protocol decl\n";
Steve Naroff8de28262007-10-14 17:03:01 +0000388 } else if (isa<ObjcClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000389 Out << "Read objc fwd class decl\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000390 } else {
391 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000392 }
393 }
394 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000395}
396
Chris Lattner3d4997d2007-09-15 23:02:28 +0000397ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
398
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000399//===----------------------------------------------------------------------===//
400/// ASTViewer - AST Visualization
401
Ted Kremenek80de08f2007-09-19 21:29:43 +0000402namespace {
403 class ASTViewer : public ASTConsumer {
404 SourceManager *SM;
405 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000406 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000407 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000408 }
409
410 virtual void HandleTopLevelDecl(Decl *D) {
411 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000412 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000413
414 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000415 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000416 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000417 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000418 }
419 }
420 }
421 };
422}
423
424ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
425
426
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000427//===----------------------------------------------------------------------===//
428// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
429// the CFGs for all function definitions.
430
431namespace {
432
Chris Lattnerc0508f92007-09-15 23:21:08 +0000433class CFGVisitor : public ASTConsumer {
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000434public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000435 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000436 virtual void VisitCFG(CFG& C) = 0;
437 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000438
439 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000440};
441
442} // end anonymous namespace
443
Chris Lattnerc0508f92007-09-15 23:21:08 +0000444void CFGVisitor::HandleTopLevelDecl(Decl *D) {
445 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
446 if (!FD || !FD->getBody())
447 return;
448
449 if (printFuncDeclStart()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000450 DeclPrinter().PrintFunctionDeclStart(FD);
451 llvm::cerr << '\n';
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000452 }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000453
Ted Kremenek12259662007-09-17 17:10:02 +0000454 CFG *C = CFG::buildCFG(FD->getBody());
455 VisitCFG(*C);
456 delete C;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000457}
458
459//===----------------------------------------------------------------------===//
460// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
461
462namespace {
463 class CFGDumper : public CFGVisitor {
464 const bool UseGraphviz;
465 public:
466 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
467
Chris Lattnerc0508f92007-09-15 23:21:08 +0000468 virtual void VisitCFG(CFG &C) {
469 if (UseGraphviz)
470 C.viewCFG();
471 else
472 C.dump();
473 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000474 };
475} // end anonymous namespace
476
Chris Lattnerc0508f92007-09-15 23:21:08 +0000477ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
478 return new CFGDumper(ViewGraphs);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000479}
480
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000481//===----------------------------------------------------------------------===//
482// AnalyzeLiveVariables - perform live variable analysis and dump results
483
484namespace {
485 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000486 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000487 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000488 virtual void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000489 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000490 }
491
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000492 virtual void VisitCFG(CFG& C) {
Ted Kremenek11e72182007-10-01 20:33:52 +0000493 LiveVariables L(C);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000494 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000495 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000496 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000497 };
498} // end anonymous namespace
499
Chris Lattnerc0508f92007-09-15 23:21:08 +0000500ASTConsumer *clang::CreateLiveVarAnalyzer() {
501 return new LivenessVisitor();
Ted Kremeneke4e63342007-09-06 00:17:54 +0000502}
503
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000504//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000505// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000506
507namespace {
508 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000509 Diagnostic &Diags;
510 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000511 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000512 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000513 virtual void Initialize(ASTContext &Context) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000514 Ctx = &Context;
515 }
516
517 virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
Ted Kremenek567a7e62007-09-07 23:54:15 +0000518 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000519 };
520} // end anonymous namespace
521
Chris Lattnerc0508f92007-09-15 23:21:08 +0000522ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
523 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000524}
Chris Lattner580980b2007-09-16 19:46:59 +0000525
526//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000527// Unitialized Values - run checker to flag potential uses of uninitalized
528// variables.
529
530namespace {
531 class UninitValsVisitor : public CFGVisitor {
532 Diagnostic &Diags;
533 ASTContext *Ctx;
534 public:
535 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000536 virtual void Initialize(ASTContext &Context) {
Ted Kremenek2bf55142007-09-17 20:49:30 +0000537 Ctx = &Context;
538 }
539
540 virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); }
541 virtual bool printFuncDeclStart() { return false; }
542 };
543} // end anonymous namespace
544
545ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
546 return new UninitValsVisitor(Diags);
547}
548
549//===----------------------------------------------------------------------===//
Chris Lattner580980b2007-09-16 19:46:59 +0000550// LLVM Emitter
551
552#include "clang/Basic/Diagnostic.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000553#include "clang/Basic/TargetInfo.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000554#include "clang/CodeGen/ModuleBuilder.h"
555#include "llvm/Module.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000556#include "llvm/Target/TargetData.h"
557#include "llvm/Target/TargetMachine.h"
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000558#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000559
560namespace {
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000561 class CodeGenerator : public ASTConsumer {
Chris Lattner580980b2007-09-16 19:46:59 +0000562 Diagnostic &Diags;
Devang Patel7a4718e2007-10-31 20:01:01 +0000563 const llvm::TargetData *TD;
Chris Lattner580980b2007-09-16 19:46:59 +0000564 ASTContext *Ctx;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000565 const LangOptions &Features;
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000566 protected:
567 llvm::Module *M;
Chris Lattnera36c4862007-11-13 18:16:41 +0000568 CodeGen::CodeGenModule *Builder;
Chris Lattner580980b2007-09-16 19:46:59 +0000569 public:
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000570 CodeGenerator(Diagnostic &diags, const LangOptions &LO)
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000571 : Diags(diags)
572 , Features(LO) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000573 virtual void Initialize(ASTContext &Context) {
Chris Lattner580980b2007-09-16 19:46:59 +0000574 Ctx = &Context;
575 M = new llvm::Module("foo");
Devang Patel7a4718e2007-10-31 20:01:01 +0000576 M->setTargetTriple(Ctx->Target.getTargetTriple());
Chris Lattner0404cd82007-12-13 17:34:31 +0000577 M->setDataLayout(Ctx->Target.getTargetDescription());
Devang Patel7a4718e2007-10-31 20:01:01 +0000578 TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
Chris Lattnerfb97b032007-12-02 01:40:18 +0000579 Builder = CodeGen::Init(Context, Features, *M, *TD, Diags);
Chris Lattner580980b2007-09-16 19:46:59 +0000580 }
581
582 virtual void HandleTopLevelDecl(Decl *D) {
583 // If an error occurred, stop code generation, but continue parsing and
584 // semantic analysis (to ensure all warnings and errors are emitted).
585 if (Diags.hasErrorOccurred())
586 return;
587
588 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
589 CodeGen::CodeGenFunction(Builder, FD);
590 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
591 CodeGen::CodeGenGlobalVar(Builder, FVD);
592 } else {
Steve Naroff91578f32007-11-17 21:21:01 +0000593 assert(isa<TypeDecl>(D) && "Only expected type decls here");
Chris Lattner580980b2007-09-16 19:46:59 +0000594 // don't codegen for now, eventually pass down for debug info.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000595 //std::cerr << "Read top-level typedef decl: '"
596 // << D->getName() << "'\n";
Chris Lattner580980b2007-09-16 19:46:59 +0000597 }
598 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000599 };
600}
601
602namespace {
603 class LLVMEmitter : public CodeGenerator {
604 public:
605 LLVMEmitter(Diagnostic &diags, const LangOptions &LO)
606 : CodeGenerator(diags,LO) {}
607
Chris Lattner580980b2007-09-16 19:46:59 +0000608 ~LLVMEmitter() {
609 CodeGen::Terminate(Builder);
610
611 // Print the generated code.
Ted Kremenek3821d402007-12-13 17:50:11 +0000612 M->print(llvm::cout.stream());
Chris Lattner580980b2007-09-16 19:46:59 +0000613 delete M;
614 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000615 };
616}
Chris Lattner580980b2007-09-16 19:46:59 +0000617
Ted Kremenekf06c9282007-12-19 23:49:37 +0000618ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags,
619 const LangOptions &Features) {
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000620 return new LLVMEmitter(Diags, Features);
Chris Lattner580980b2007-09-16 19:46:59 +0000621}
622
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000623namespace {
624 class BCWriter : public CodeGenerator {
625 public:
626 std::ostream& Out;
627
628 BCWriter(std::ostream* out, Diagnostic &diags, const LangOptions &LO)
629 : CodeGenerator(diags,LO)
630 , Out(*out) {}
631
632 ~BCWriter() {
633 CodeGen::Terminate(Builder);
634 llvm::WriteBitcodeToFile(M, Out);
635 delete M;
636 }
637 };
638}
639
640ASTConsumer *clang::CreateBCWriter(const std::string& InFile,
641 const std::string& OutputFile,
642 Diagnostic &Diags,
643 const LangOptions &Features) {
644 std::string FileName = OutputFile;
Christopher Lamb2d6c0652007-12-24 03:23:55 +0000645
646 std::ostream *Out;
Christopher Lamb8bd848f2007-12-24 20:56:07 +0000647 if (OutputFile == "-" || InFile == "-")
648 Out = llvm::cout.stream();
649 else if (!OutputFile.size()) {
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000650 llvm::sys::Path Path(InFile);
651 Path.eraseSuffix();
652 Path.appendSuffix("bc");
653 FileName = Path.toString();
Christopher Lamb2d6c0652007-12-24 03:23:55 +0000654 Out = new std::ofstream(FileName.c_str());
Christopher Lamb2d6c0652007-12-24 03:23:55 +0000655 } else {
656 Out = new std::ofstream(FileName.c_str());
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000657 }
658
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000659 return new BCWriter(Out, Diags, Features);
660}
661
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000662//===----------------------------------------------------------------------===//
663// AST Serializer
664
665namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000666
667class ASTSerializer : public ASTConsumer {
668protected:
669 Diagnostic &Diags;
670 TranslationUnit TU;
671public:
672 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
673 : Diags(diags), TU(LO) {}
674
675 virtual void Initialize(ASTContext &Context) {
676 TU.setContext(&Context);
677 }
678
679 virtual void HandleTopLevelDecl(Decl *D) {
680 if (Diags.hasErrorOccurred())
681 return;
682
683 TU.AddTopLevelDecl(D);
684 }
685};
686
687class SingleFileSerializer : public ASTSerializer {
688 const llvm::sys::Path FName;
689public:
690 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
691 const LangOptions &LO)
692 : ASTSerializer(diags,LO), FName(F) {}
693
694 ~SingleFileSerializer() {
695 EmitASTBitcodeFile(TU,FName);
696 }
697};
698
699class BuildSerializer : public ASTSerializer {
700 llvm::sys::Path EmitDir;
701public:
702 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000703 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000704 : ASTSerializer(diags,LO), EmitDir(dir) {}
705
Ted Kremenek54117722007-12-20 00:34:58 +0000706 ~BuildSerializer() {
707 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
708 unsigned ID = SourceMgr.getMainFileID();
709 assert (ID && "MainFileID not set!");
710 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
711 assert (FE && "No FileEntry for main file.");
712
713 // FIXME: This is not portable to Windows.
714 // FIXME: This logic should probably be moved elsewhere later.
715
Ted Kremenekee533642007-12-20 19:47:16 +0000716 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000717
718 std::vector<char> buf;
719 buf.reserve(strlen(FE->getName())+100);
720
721 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000722 FName.appendComponent(&buf[0]);
723 FName.createDirectoryOnDisk(true);
724 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000725 assert (false && "Could not create 'device' serialization directory.");
726 return;
727 }
Ted Kremenekee533642007-12-20 19:47:16 +0000728
Ted Kremenek54117722007-12-20 00:34:58 +0000729 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000730 FName.appendComponent(&buf[0]);
731 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000732
Ted Kremenekee533642007-12-20 19:47:16 +0000733 // Now emit the sources.
734
Ted Kremenek54117722007-12-20 00:34:58 +0000735 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000736};
737
738
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000739} // end anonymous namespace
740
741
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000742ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000743 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000744 Diagnostic &Diags,
745 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000746
Ted Kremenekf06c9282007-12-19 23:49:37 +0000747 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000748 if (InFile == "-") {
749 llvm::cerr <<
750 "error: Cannot use --serialize with -o for source read from STDIN.\n";
751 return NULL;
752 }
753
Ted Kremenekf06c9282007-12-19 23:49:37 +0000754 // The user specified an AST-emission directory. Determine if the path
755 // is absolute.
756 llvm::sys::Path EmitDir(OutputFile);
757
758 if (!EmitDir.isAbsolute()) {
759 llvm::cerr <<
760 "error: Output directory for --serialize must be an absolute path.\n";
761
762 return NULL;
763 }
764
765 // Create the directory if it does not exist.
766 EmitDir.createDirectoryOnDisk(true);
767 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
768 llvm::cerr <<
769 "error: Could not create output directory for --serialize.\n";
770
771 return NULL;
772 }
773
Ted Kremenek54117722007-12-20 00:34:58 +0000774 // FIXME: We should probably only allow using BuildSerializer when
775 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000776 return new BuildSerializer(EmitDir, Diags, Features);
777 }
778
779 // The user did not specify an output directory for serialized ASTs.
780 // Serialize the translation to a single file whose name is the same
781 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000782
Ted Kremenekf06c9282007-12-19 23:49:37 +0000783 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000784 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000785 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000786}