blob: aa49e2adc548ec902f8db57b871c99a94ccbc109 [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 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
Chris Lattner4b1daf02008-01-10 01:43:14 +000036 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekea75c552007-11-28 21:32:21 +000037 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000038
Chris Lattneref5a85d2008-01-02 21:04:16 +000039 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000040 void PrintFunctionDeclStart(FunctionDecl *FD);
41 void PrintTypeDefDecl(TypedefDecl *TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000042 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
43 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
44 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
45 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
46 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
47 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
48 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000049 };
50} // end anonymous namespace
51
Chris Lattneref5a85d2008-01-02 21:04:16 +000052void DeclPrinter:: PrintDecl(Decl *D) {
53 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
54 PrintFunctionDeclStart(FD);
55
56 if (FD->getBody()) {
57 Out << ' ';
58 FD->getBody()->printPretty(Out);
59 Out << '\n';
60 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000061 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000062 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000063 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000064 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
65 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000066 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
67 PrintObjCInterfaceDecl(OID);
68 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
69 PrintObjCProtocolDecl(PID);
70 } else if (ObjCForwardProtocolDecl *OFPD =
71 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000072 Out << "@protocol ";
73 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000074 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000075 if (i) Out << ", ";
76 Out << D->getName();
77 }
78 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000079 } else if (ObjCImplementationDecl *OID =
80 dyn_cast<ObjCImplementationDecl>(D)) {
81 PrintObjCImplementationDecl(OID);
82 } else if (ObjCCategoryImplDecl *OID =
83 dyn_cast<ObjCCategoryImplDecl>(D)) {
84 PrintObjCCategoryImplDecl(OID);
85 } else if (ObjCCategoryDecl *OID =
86 dyn_cast<ObjCCategoryDecl>(D)) {
87 PrintObjCCategoryDecl(OID);
88 } else if (ObjCCompatibleAliasDecl *OID =
89 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
90 PrintObjCCompatibleAliasDecl(OID);
91 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000092 Out << "@class [printing todo]\n";
93 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
94 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
95 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
96 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
97 } else {
98 assert(0 && "Unknown decl type!");
99 }
100}
101
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000102void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 bool HasBody = FD->getBody();
104
Ted Kremenekea75c552007-11-28 21:32:21 +0000105 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000106
107 switch (FD->getStorageClass()) {
108 default: assert(0 && "Unknown storage class");
109 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000110 case FunctionDecl::Extern: Out << "extern "; break;
111 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000112 }
113
114 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000115 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000118 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000119
Chris Lattner0d6ca112007-12-03 21:43:25 +0000120 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 Proto += "(";
122 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
123 if (i) Proto += ", ";
124 std::string ParamStr;
125 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
126
127 FT->getArgType(i).getAsStringInternal(ParamStr);
128 Proto += ParamStr;
129 }
130
131 if (FT->isVariadic()) {
132 if (FD->getNumParams()) Proto += ", ";
133 Proto += "...";
134 }
135 Proto += ")";
136 } else {
137 assert(isa<FunctionTypeNoProto>(AFT));
138 Proto += "()";
139 }
140
141 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000142 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000143
Chris Lattner6000dac2007-08-08 22:51:59 +0000144 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000145 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000146 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000147}
148
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000149void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 std::string S = TD->getName();
151 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000152 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000153}
154
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000155void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000156 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000157 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000158 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000159 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000160 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000161 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000162 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000163 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000164
165 for (int i = 0; i < OMD->getNumParams(); i++) {
166 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000167 // FIXME: selector is missing here!
168 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000169 }
170}
171
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000172void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000173 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000174 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000175
176 if (SID)
177 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000178 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000179 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000180
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000181 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000182 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183 ObjCMethodDecl *OMD = *I;
184 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000185 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000186 Out << ' ';
187 OMD->getBody()->printPretty(Out);
188 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000189 }
190 }
191
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000192 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000193 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCMethodDecl *OMD = *I;
195 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000196 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000197 Out << ' ';
198 OMD->getBody()->printPretty(Out);
199 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000200 }
201 }
202
Ted Kremenekea75c552007-11-28 21:32:21 +0000203 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000204}
205
206
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000207void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000208 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000209 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000210
211 if (SID)
212 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000213 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000214 Out << "@interface " << I;
215
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000216 // Protocols?
217 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000218
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000219 if (count > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000220 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000221 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000222 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000223 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000224
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000225 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000226 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000227 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000228 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000229
Chris Lattnerbe6df082007-12-12 07:56:42 +0000230 if (OID->getNumInstanceVariables() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000231 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000232 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000233 E = OID->ivar_end(); I != E; ++I) {
234 Out << '\t' << (*I)->getType().getAsString()
235 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000236 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000237 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000238 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000239
240 int NumProperties = OID->getNumPropertyDecl();
241 if (NumProperties > 0) {
242 for (int i = 0; i < NumProperties; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000243 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekea75c552007-11-28 21:32:21 +0000244 Out << "@property";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000245 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000246 bool first = true;
Ted Kremenekea75c552007-11-28 21:32:21 +0000247 Out << " (";
Chris Lattner4b1daf02008-01-10 01:43:14 +0000248 if (PDecl->getPropertyAttributes() &
249 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000250 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000251 first = false;
252 }
253
Chris Lattner4b1daf02008-01-10 01:43:14 +0000254 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000255 Out << (first ? ' ' : ',') << "getter = "
256 << PDecl->getGetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000257 first = false;
258 }
Chris Lattner4b1daf02008-01-10 01:43:14 +0000259 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000260 Out << (first ? ' ' : ',') << "setter = "
261 << PDecl->getSetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000262 first = false;
263 }
264
Chris Lattner4b1daf02008-01-10 01:43:14 +0000265 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000266 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000267 first = false;
268 }
269
Chris Lattner4b1daf02008-01-10 01:43:14 +0000270 if (PDecl->getPropertyAttributes() &
271 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000272 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000273 first = false;
274 }
275
Chris Lattner4b1daf02008-01-10 01:43:14 +0000276 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000277 Out << (first ? ' ' : ',') << "retain";
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_copy) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000282 Out << (first ? ' ' : ',') << "copy";
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_nonatomic) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000288 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000289 first = false;
290 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000291 Out << " )";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000292 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000293
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000294 ObjCIvarDecl **IDecl = PDecl->getPropertyDecls();
Ted Kremenekea75c552007-11-28 21:32:21 +0000295
296 Out << ' ' << IDecl[0]->getType().getAsString()
297 << ' ' << IDecl[0]->getName();
298
299 for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
300 Out << ", " << IDecl[j]->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000301
Ted Kremenekea75c552007-11-28 21:32:21 +0000302 Out << ";\n";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000303 }
304 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000305
306 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000307 // FIXME: implement the rest...
308}
309
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000310void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000311 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000312 // FIXME: implement the rest...
313}
314
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000315void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000316 Out << "@implementation "
317 << PID->getClassInterface()->getName()
318 << '(' << PID->getName() << ");\n";
319
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000320 // FIXME: implement the rest...
321}
322
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000323void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000324 Out << "@interface "
325 << PID->getClassInterface()->getName()
326 << '(' << PID->getName() << ");\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000327 // FIXME: implement the rest...
328}
329
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000330void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000331 Out << "@compatibility_alias " << AID->getName()
332 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000333}
334
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000335//===----------------------------------------------------------------------===//
336/// ASTPrinter - Pretty-printer of ASTs
337
Chris Lattner3d4997d2007-09-15 23:02:28 +0000338namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000339 class ASTPrinter : public ASTConsumer, public DeclPrinter {
340 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000341 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000342
Chris Lattner3d4997d2007-09-15 23:02:28 +0000343 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000344 PrintDecl(D);
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";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000380 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000381 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000382 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000383 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000384 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000385 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000386 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000387 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +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//===----------------------------------------------------------------------===//
Ted Kremeneke603df42008-01-08 18:04:06 +0000550// GRConstProp - Perform intra-procedural, path-sensitive constant propagation.
551
552namespace {
553 class GRConstPropVisitor : public CFGVisitor {
554 public:
555 virtual void Initialize(ASTContext &Context) {}
556
557 virtual void VisitCFG(CFG& C) {
558 // FIXME: Implement.
559 assert (false && "Not yet implemented.");
560 }
561 };
562} // end anonymous namespace
563
564ASTConsumer *clang::CreateGRConstProp() {
565 return new GRConstPropVisitor();
566}
567
568//===----------------------------------------------------------------------===//
Chris Lattner580980b2007-09-16 19:46:59 +0000569// LLVM Emitter
570
571#include "clang/Basic/Diagnostic.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000572#include "clang/Basic/TargetInfo.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000573#include "clang/CodeGen/ModuleBuilder.h"
574#include "llvm/Module.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000575#include "llvm/Target/TargetData.h"
576#include "llvm/Target/TargetMachine.h"
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000577#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000578
579namespace {
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000580 class CodeGenerator : public ASTConsumer {
Chris Lattner580980b2007-09-16 19:46:59 +0000581 Diagnostic &Diags;
Devang Patel7a4718e2007-10-31 20:01:01 +0000582 const llvm::TargetData *TD;
Chris Lattner580980b2007-09-16 19:46:59 +0000583 ASTContext *Ctx;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000584 const LangOptions &Features;
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000585 protected:
586 llvm::Module *M;
Chris Lattnera36c4862007-11-13 18:16:41 +0000587 CodeGen::CodeGenModule *Builder;
Chris Lattner580980b2007-09-16 19:46:59 +0000588 public:
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000589 CodeGenerator(Diagnostic &diags, const LangOptions &LO)
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000590 : Diags(diags)
591 , Features(LO) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000592 virtual void Initialize(ASTContext &Context) {
Chris Lattner580980b2007-09-16 19:46:59 +0000593 Ctx = &Context;
594 M = new llvm::Module("foo");
Devang Patel7a4718e2007-10-31 20:01:01 +0000595 M->setTargetTriple(Ctx->Target.getTargetTriple());
Chris Lattner0404cd82007-12-13 17:34:31 +0000596 M->setDataLayout(Ctx->Target.getTargetDescription());
Devang Patel7a4718e2007-10-31 20:01:01 +0000597 TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
Chris Lattnerfb97b032007-12-02 01:40:18 +0000598 Builder = CodeGen::Init(Context, Features, *M, *TD, Diags);
Chris Lattner580980b2007-09-16 19:46:59 +0000599 }
600
601 virtual void HandleTopLevelDecl(Decl *D) {
602 // If an error occurred, stop code generation, but continue parsing and
603 // semantic analysis (to ensure all warnings and errors are emitted).
604 if (Diags.hasErrorOccurred())
605 return;
606
607 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
608 CodeGen::CodeGenFunction(Builder, FD);
609 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
610 CodeGen::CodeGenGlobalVar(Builder, FVD);
611 } else {
Steve Naroff91578f32007-11-17 21:21:01 +0000612 assert(isa<TypeDecl>(D) && "Only expected type decls here");
Chris Lattner580980b2007-09-16 19:46:59 +0000613 // don't codegen for now, eventually pass down for debug info.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000614 //std::cerr << "Read top-level typedef decl: '"
615 // << D->getName() << "'\n";
Chris Lattner580980b2007-09-16 19:46:59 +0000616 }
617 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000618 };
619}
620
621namespace {
622 class LLVMEmitter : public CodeGenerator {
623 public:
624 LLVMEmitter(Diagnostic &diags, const LangOptions &LO)
625 : CodeGenerator(diags,LO) {}
626
Chris Lattner580980b2007-09-16 19:46:59 +0000627 ~LLVMEmitter() {
628 CodeGen::Terminate(Builder);
629
630 // Print the generated code.
Ted Kremenek3821d402007-12-13 17:50:11 +0000631 M->print(llvm::cout.stream());
Chris Lattner580980b2007-09-16 19:46:59 +0000632 delete M;
633 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000634 };
635}
Chris Lattner580980b2007-09-16 19:46:59 +0000636
Ted Kremenekf06c9282007-12-19 23:49:37 +0000637ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags,
638 const LangOptions &Features) {
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000639 return new LLVMEmitter(Diags, Features);
Chris Lattner580980b2007-09-16 19:46:59 +0000640}
641
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000642namespace {
643 class BCWriter : public CodeGenerator {
644 public:
645 std::ostream& Out;
646
647 BCWriter(std::ostream* out, Diagnostic &diags, const LangOptions &LO)
648 : CodeGenerator(diags,LO)
649 , Out(*out) {}
650
651 ~BCWriter() {
652 CodeGen::Terminate(Builder);
653 llvm::WriteBitcodeToFile(M, Out);
654 delete M;
655 }
656 };
657}
658
659ASTConsumer *clang::CreateBCWriter(const std::string& InFile,
660 const std::string& OutputFile,
661 Diagnostic &Diags,
662 const LangOptions &Features) {
663 std::string FileName = OutputFile;
Christopher Lamb2d6c0652007-12-24 03:23:55 +0000664
665 std::ostream *Out;
Christopher Lamb396f9fe2007-12-24 20:59:36 +0000666 if (OutputFile == "-")
Christopher Lamb8bd848f2007-12-24 20:56:07 +0000667 Out = llvm::cout.stream();
668 else if (!OutputFile.size()) {
Christopher Lamb396f9fe2007-12-24 20:59:36 +0000669 if (InFile == "-")
670 Out = llvm::cout.stream();
671 else {
672 llvm::sys::Path Path(InFile);
673 Path.eraseSuffix();
674 Path.appendSuffix("bc");
675 FileName = Path.toString();
Christopher Lamb4c92b432007-12-24 23:49:33 +0000676 Out = new std::ofstream(FileName.c_str(),
677 std::ios_base::binary|std::ios_base::out);
Christopher Lamb396f9fe2007-12-24 20:59:36 +0000678 }
Christopher Lamb2d6c0652007-12-24 03:23:55 +0000679 } else {
Christopher Lamb4c92b432007-12-24 23:49:33 +0000680 Out = new std::ofstream(FileName.c_str(),
681 std::ios_base::binary|std::ios_base::out);
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000682 }
683
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000684 return new BCWriter(Out, Diags, Features);
685}
686
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000687//===----------------------------------------------------------------------===//
688// AST Serializer
689
690namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000691
692class ASTSerializer : public ASTConsumer {
693protected:
694 Diagnostic &Diags;
695 TranslationUnit TU;
696public:
697 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
698 : Diags(diags), TU(LO) {}
699
700 virtual void Initialize(ASTContext &Context) {
701 TU.setContext(&Context);
702 }
703
704 virtual void HandleTopLevelDecl(Decl *D) {
705 if (Diags.hasErrorOccurred())
706 return;
707
708 TU.AddTopLevelDecl(D);
709 }
710};
711
712class SingleFileSerializer : public ASTSerializer {
713 const llvm::sys::Path FName;
714public:
715 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
716 const LangOptions &LO)
717 : ASTSerializer(diags,LO), FName(F) {}
718
719 ~SingleFileSerializer() {
720 EmitASTBitcodeFile(TU,FName);
721 }
722};
723
724class BuildSerializer : public ASTSerializer {
725 llvm::sys::Path EmitDir;
726public:
727 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000728 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000729 : ASTSerializer(diags,LO), EmitDir(dir) {}
730
Ted Kremenek54117722007-12-20 00:34:58 +0000731 ~BuildSerializer() {
732 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
733 unsigned ID = SourceMgr.getMainFileID();
734 assert (ID && "MainFileID not set!");
735 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
736 assert (FE && "No FileEntry for main file.");
737
738 // FIXME: This is not portable to Windows.
739 // FIXME: This logic should probably be moved elsewhere later.
740
Ted Kremenekee533642007-12-20 19:47:16 +0000741 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000742
743 std::vector<char> buf;
744 buf.reserve(strlen(FE->getName())+100);
745
746 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000747 FName.appendComponent(&buf[0]);
748 FName.createDirectoryOnDisk(true);
749 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000750 assert (false && "Could not create 'device' serialization directory.");
751 return;
752 }
Ted Kremenekee533642007-12-20 19:47:16 +0000753
Ted Kremenek54117722007-12-20 00:34:58 +0000754 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000755 FName.appendComponent(&buf[0]);
756 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000757
Ted Kremenekee533642007-12-20 19:47:16 +0000758 // Now emit the sources.
759
Ted Kremenek54117722007-12-20 00:34:58 +0000760 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000761};
762
763
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000764} // end anonymous namespace
765
766
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000767ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000768 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000769 Diagnostic &Diags,
770 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000771
Ted Kremenekf06c9282007-12-19 23:49:37 +0000772 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000773 if (InFile == "-") {
774 llvm::cerr <<
775 "error: Cannot use --serialize with -o for source read from STDIN.\n";
776 return NULL;
777 }
778
Ted Kremenekf06c9282007-12-19 23:49:37 +0000779 // The user specified an AST-emission directory. Determine if the path
780 // is absolute.
781 llvm::sys::Path EmitDir(OutputFile);
782
783 if (!EmitDir.isAbsolute()) {
784 llvm::cerr <<
785 "error: Output directory for --serialize must be an absolute path.\n";
786
787 return NULL;
788 }
789
790 // Create the directory if it does not exist.
791 EmitDir.createDirectoryOnDisk(true);
792 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
793 llvm::cerr <<
794 "error: Could not create output directory for --serialize.\n";
795
796 return NULL;
797 }
798
Ted Kremenek54117722007-12-20 00:34:58 +0000799 // FIXME: We should probably only allow using BuildSerializer when
800 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000801 return new BuildSerializer(EmitDir, Diags, Features);
802 }
803
804 // The user did not specify an output directory for serialized ASTs.
805 // Serialize the translation to a single file whose name is the same
806 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000807
Ted Kremenekf06c9282007-12-19 23:49:37 +0000808 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000809 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000810 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000811}