blob: 4dba24bdcea7fd4147c4748bbc66e081ce086888 [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"
Chris Lattner6000dac2007-08-08 22:51:59 +000025using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000026
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000027//===----------------------------------------------------------------------===//
28/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000029
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000030namespace {
31 class DeclPrinter {
32 public:
Ted Kremenekea75c552007-11-28 21:32:21 +000033 std::ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000034
Chris Lattner4b1daf02008-01-10 01:43:14 +000035 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekea75c552007-11-28 21:32:21 +000036 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000037
Chris Lattneref5a85d2008-01-02 21:04:16 +000038 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000039 void PrintFunctionDeclStart(FunctionDecl *FD);
40 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000041 void PrintLinkageSpec(LinkageSpecDecl *LS);
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";
Chris Lattnerc6fdc342008-01-12 07:05:38 +000097 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
98 PrintLinkageSpec(LSD);
Chris Lattneref5a85d2008-01-02 21:04:16 +000099 } else {
100 assert(0 && "Unknown decl type!");
101 }
102}
103
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000104void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 bool HasBody = FD->getBody();
106
Ted Kremenekea75c552007-11-28 21:32:21 +0000107 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000108
109 switch (FD->getStorageClass()) {
110 default: assert(0 && "Unknown storage class");
111 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000112 case FunctionDecl::Extern: Out << "extern "; break;
113 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000114 }
115
116 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000117 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000120 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000121
Chris Lattner0d6ca112007-12-03 21:43:25 +0000122 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 Proto += "(";
124 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
125 if (i) Proto += ", ";
126 std::string ParamStr;
127 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
128
129 FT->getArgType(i).getAsStringInternal(ParamStr);
130 Proto += ParamStr;
131 }
132
133 if (FT->isVariadic()) {
134 if (FD->getNumParams()) Proto += ", ";
135 Proto += "...";
136 }
137 Proto += ")";
138 } else {
139 assert(isa<FunctionTypeNoProto>(AFT));
140 Proto += "()";
141 }
142
143 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000144 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000145
Chris Lattner6000dac2007-08-08 22:51:59 +0000146 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000147 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000148 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000149}
150
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000151void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 std::string S = TD->getName();
153 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000154 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000155}
156
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000157void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
158 const char *l;
159 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
160 l = "C";
161 else if (LS->getLanguage() == LinkageSpecDecl::lang_cxx)
162 l = "C++";
163 else assert(0 && "unknown language in linkage specification");
164 Out << "extern \"" << l << "\" { ";
165 PrintDecl(LS->getDecl());
166 Out << "}\n";
167}
168
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000169void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000170 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000171 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000172 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000173 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000174 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000175 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000176 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000177 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000178
179 for (int i = 0; i < OMD->getNumParams(); i++) {
180 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000181 // FIXME: selector is missing here!
182 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000183 }
184}
185
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000186void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000187 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000188 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000189
190 if (SID)
191 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000192 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000193 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000194
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000195 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000196 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000197 ObjCMethodDecl *OMD = *I;
198 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000199 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000200 Out << ' ';
201 OMD->getBody()->printPretty(Out);
202 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000203 }
204 }
205
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000206 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000207 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000208 ObjCMethodDecl *OMD = *I;
209 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000210 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000211 Out << ' ';
212 OMD->getBody()->printPretty(Out);
213 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000214 }
215 }
216
Ted Kremenekea75c552007-11-28 21:32:21 +0000217 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000218}
219
220
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000221void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000222 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000223 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000224
225 if (SID)
226 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000227 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000228 Out << "@interface " << I;
229
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000230 // Protocols?
231 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000232
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000233 if (count > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000234 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000235 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000236 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000237 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000238
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000239 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000240 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000241 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000242 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000243
Chris Lattnerbe6df082007-12-12 07:56:42 +0000244 if (OID->getNumInstanceVariables() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000245 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000246 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000247 E = OID->ivar_end(); I != E; ++I) {
248 Out << '\t' << (*I)->getType().getAsString()
249 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000250 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000251 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000252 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000253
254 int NumProperties = OID->getNumPropertyDecl();
255 if (NumProperties > 0) {
256 for (int i = 0; i < NumProperties; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000257 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekea75c552007-11-28 21:32:21 +0000258 Out << "@property";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000259 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000260 bool first = true;
Ted Kremenekea75c552007-11-28 21:32:21 +0000261 Out << " (";
Chris Lattner4b1daf02008-01-10 01:43:14 +0000262 if (PDecl->getPropertyAttributes() &
263 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000264 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000265 first = false;
266 }
267
Chris Lattner4b1daf02008-01-10 01:43:14 +0000268 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000269 Out << (first ? ' ' : ',') << "getter = "
270 << PDecl->getGetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000271 first = false;
272 }
Chris Lattner4b1daf02008-01-10 01:43:14 +0000273 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000274 Out << (first ? ' ' : ',') << "setter = "
275 << PDecl->getSetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000276 first = false;
277 }
278
Chris Lattner4b1daf02008-01-10 01:43:14 +0000279 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000280 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000281 first = false;
282 }
283
Chris Lattner4b1daf02008-01-10 01:43:14 +0000284 if (PDecl->getPropertyAttributes() &
285 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000286 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000287 first = false;
288 }
289
Chris Lattner4b1daf02008-01-10 01:43:14 +0000290 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000291 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000292 first = false;
293 }
294
Chris Lattner4b1daf02008-01-10 01:43:14 +0000295 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000296 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000297 first = false;
298 }
299
Chris Lattner4b1daf02008-01-10 01:43:14 +0000300 if (PDecl->getPropertyAttributes() &
301 ObjCPropertyDecl::OBJC_PR_nonatomic) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000302 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000303 first = false;
304 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000305 Out << " )";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000306 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000307
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000308 ObjCIvarDecl **IDecl = PDecl->getPropertyDecls();
Ted Kremenekea75c552007-11-28 21:32:21 +0000309
310 Out << ' ' << IDecl[0]->getType().getAsString()
311 << ' ' << IDecl[0]->getName();
312
313 for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
314 Out << ", " << IDecl[j]->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000315
Ted Kremenekea75c552007-11-28 21:32:21 +0000316 Out << ";\n";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000317 }
318 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000319
320 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000321 // FIXME: implement the rest...
322}
323
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000324void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000325 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000326 // FIXME: implement the rest...
327}
328
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000329void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000330 Out << "@implementation "
331 << PID->getClassInterface()->getName()
332 << '(' << PID->getName() << ");\n";
333
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000334 // FIXME: implement the rest...
335}
336
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000337void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000338 Out << "@interface "
339 << PID->getClassInterface()->getName()
340 << '(' << PID->getName() << ");\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000341 // FIXME: implement the rest...
342}
343
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000344void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000345 Out << "@compatibility_alias " << AID->getName()
346 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000347}
348
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000349//===----------------------------------------------------------------------===//
350/// ASTPrinter - Pretty-printer of ASTs
351
Chris Lattner3d4997d2007-09-15 23:02:28 +0000352namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000353 class ASTPrinter : public ASTConsumer, public DeclPrinter {
354 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000355 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000356
Chris Lattner3d4997d2007-09-15 23:02:28 +0000357 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000358 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000360 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000361}
Chris Lattner6000dac2007-08-08 22:51:59 +0000362
Ted Kremenekea75c552007-11-28 21:32:21 +0000363ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
364 return new ASTPrinter(out);
365}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000366
367//===----------------------------------------------------------------------===//
368/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000369
370namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000371 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000372 SourceManager *SM;
373 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000374 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000375
Ted Kremenek95041a22007-12-19 22:51:13 +0000376 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000377 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000378 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000379
380 virtual void HandleTopLevelDecl(Decl *D) {
381 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
382 PrintFunctionDeclStart(FD);
383
384 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000385 Out << '\n';
386 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000387 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000388 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000389 }
390 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
391 PrintTypeDefDecl(TD);
392 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000393 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000394 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000395 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000397 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000398 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000399 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000400 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000401 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000402 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000403 Out << "Read objc fwd class decl\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000404 } else {
405 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000406 }
407 }
408 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000409}
410
Chris Lattner3d4997d2007-09-15 23:02:28 +0000411ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
412
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000413//===----------------------------------------------------------------------===//
414/// ASTViewer - AST Visualization
415
Ted Kremenek80de08f2007-09-19 21:29:43 +0000416namespace {
417 class ASTViewer : public ASTConsumer {
418 SourceManager *SM;
419 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000420 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000421 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000422 }
423
424 virtual void HandleTopLevelDecl(Decl *D) {
425 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000426 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000427
428 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000429 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000430 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000431 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000432 }
433 }
434 }
435 };
436}
437
438ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
439
440
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000441//===----------------------------------------------------------------------===//
442// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
443// the CFGs for all function definitions.
444
445namespace {
446
Chris Lattnerc0508f92007-09-15 23:21:08 +0000447class CFGVisitor : public ASTConsumer {
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000448public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000449 // CFG Visitor interface to be implemented by subclass.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000450 virtual void VisitCFG(CFG& C, FunctionDecl& FD) = 0;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000451 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000452
453 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000454};
455
456} // end anonymous namespace
457
Chris Lattnerc0508f92007-09-15 23:21:08 +0000458void CFGVisitor::HandleTopLevelDecl(Decl *D) {
459 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
460 if (!FD || !FD->getBody())
461 return;
462
463 if (printFuncDeclStart()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000464 DeclPrinter().PrintFunctionDeclStart(FD);
465 llvm::cerr << '\n';
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000466 }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000467
Ted Kremenek12259662007-09-17 17:10:02 +0000468 CFG *C = CFG::buildCFG(FD->getBody());
Ted Kremenekbffaa832008-01-29 05:13:23 +0000469 VisitCFG(*C, *FD);
Ted Kremenek12259662007-09-17 17:10:02 +0000470 delete C;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000471}
472
473//===----------------------------------------------------------------------===//
474// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
475
476namespace {
477 class CFGDumper : public CFGVisitor {
478 const bool UseGraphviz;
479 public:
480 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
481
Ted Kremenekbffaa832008-01-29 05:13:23 +0000482 virtual void VisitCFG(CFG& C, FunctionDecl&) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000483 if (UseGraphviz)
484 C.viewCFG();
485 else
486 C.dump();
487 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000488 };
489} // end anonymous namespace
490
Chris Lattnerc0508f92007-09-15 23:21:08 +0000491ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
492 return new CFGDumper(ViewGraphs);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000493}
494
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000495//===----------------------------------------------------------------------===//
496// AnalyzeLiveVariables - perform live variable analysis and dump results
497
498namespace {
499 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000500 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000501 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000502 virtual void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000503 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000504 }
505
Ted Kremenekbffaa832008-01-29 05:13:23 +0000506 virtual void VisitCFG(CFG& C, FunctionDecl& FD) {
507 LiveVariables L(C, FD);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000508 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000509 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000510 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000511 };
512} // end anonymous namespace
513
Chris Lattnerc0508f92007-09-15 23:21:08 +0000514ASTConsumer *clang::CreateLiveVarAnalyzer() {
515 return new LivenessVisitor();
Ted Kremeneke4e63342007-09-06 00:17:54 +0000516}
517
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000518//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000519// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000520
521namespace {
522 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000523 Diagnostic &Diags;
524 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000525 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000526 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000527 virtual void Initialize(ASTContext &Context) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000528 Ctx = &Context;
529 }
530
Ted Kremenekbffaa832008-01-29 05:13:23 +0000531 virtual void VisitCFG(CFG& C, FunctionDecl& FD) {
532 CheckDeadStores(C, FD, *Ctx, Diags);
533 }
534
Ted Kremenek567a7e62007-09-07 23:54:15 +0000535 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000536 };
537} // end anonymous namespace
538
Chris Lattnerc0508f92007-09-15 23:21:08 +0000539ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
540 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000541}
Chris Lattner580980b2007-09-16 19:46:59 +0000542
543//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000544// Unitialized Values - run checker to flag potential uses of uninitalized
545// variables.
546
547namespace {
548 class UninitValsVisitor : public CFGVisitor {
549 Diagnostic &Diags;
550 ASTContext *Ctx;
551 public:
552 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000553 virtual void Initialize(ASTContext &Context) {
Ted Kremenek2bf55142007-09-17 20:49:30 +0000554 Ctx = &Context;
555 }
556
Ted Kremenekbffaa832008-01-29 05:13:23 +0000557 virtual void VisitCFG(CFG& C, FunctionDecl&) {
558 CheckUninitializedValues(C, *Ctx, Diags);
559 }
560
Ted Kremenek2bf55142007-09-17 20:49:30 +0000561 virtual bool printFuncDeclStart() { return false; }
562 };
563} // end anonymous namespace
564
565ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
566 return new UninitValsVisitor(Diags);
567}
568
569//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +0000570// GRConstants - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremeneke603df42008-01-08 18:04:06 +0000571
572namespace {
Ted Kremenekbffaa832008-01-29 05:13:23 +0000573 class GRConstantsVisitor : public CFGVisitor {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000574 ASTContext* Ctx;
Ted Kremeneke603df42008-01-08 18:04:06 +0000575 public:
Ted Kremeneke603df42008-01-08 18:04:06 +0000576
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000577 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
Ted Kremenekbffaa832008-01-29 05:13:23 +0000578 virtual void VisitCFG(CFG& C, FunctionDecl&);
Ted Kremeneke603df42008-01-08 18:04:06 +0000579 };
580} // end anonymous namespace
581
Ted Kremenek874d63f2008-01-24 02:02:54 +0000582ASTConsumer* clang::CreateGRConstants() {
Ted Kremenekee985462008-01-16 18:18:48 +0000583 return new GRConstantsVisitor();
Ted Kremeneke603df42008-01-08 18:04:06 +0000584}
585
Ted Kremenekbffaa832008-01-29 05:13:23 +0000586void GRConstantsVisitor::VisitCFG(CFG& C, FunctionDecl& FD) {
587 RunGRConstants(C, FD, *Ctx);
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000588}
589
Ted Kremeneke603df42008-01-08 18:04:06 +0000590//===----------------------------------------------------------------------===//
Chris Lattner580980b2007-09-16 19:46:59 +0000591// LLVM Emitter
592
593#include "clang/Basic/Diagnostic.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000594#include "clang/Basic/TargetInfo.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000595#include "clang/CodeGen/ModuleBuilder.h"
596#include "llvm/Module.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000597#include "llvm/Target/TargetData.h"
598#include "llvm/Target/TargetMachine.h"
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000599#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000600
601namespace {
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000602 class CodeGenerator : public ASTConsumer {
Chris Lattner580980b2007-09-16 19:46:59 +0000603 Diagnostic &Diags;
Devang Patel7a4718e2007-10-31 20:01:01 +0000604 const llvm::TargetData *TD;
Chris Lattner580980b2007-09-16 19:46:59 +0000605 ASTContext *Ctx;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000606 const LangOptions &Features;
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000607 protected:
Chris Lattnere66b65c2008-02-06 01:42:25 +0000608 llvm::Module *&M;
Chris Lattnera36c4862007-11-13 18:16:41 +0000609 CodeGen::CodeGenModule *Builder;
Chris Lattner580980b2007-09-16 19:46:59 +0000610 public:
Chris Lattnere66b65c2008-02-06 01:42:25 +0000611 CodeGenerator(Diagnostic &diags, const LangOptions &LO,
612 llvm::Module *&DestModule)
613 : Diags(diags), Features(LO), M(DestModule) {}
614
615 ~CodeGenerator() {
616 CodeGen::Terminate(Builder);
617 }
618
Ted Kremenek95041a22007-12-19 22:51:13 +0000619 virtual void Initialize(ASTContext &Context) {
Chris Lattner580980b2007-09-16 19:46:59 +0000620 Ctx = &Context;
Chris Lattnere66b65c2008-02-06 01:42:25 +0000621
Devang Patel7a4718e2007-10-31 20:01:01 +0000622 M->setTargetTriple(Ctx->Target.getTargetTriple());
Chris Lattner0404cd82007-12-13 17:34:31 +0000623 M->setDataLayout(Ctx->Target.getTargetDescription());
Devang Patel7a4718e2007-10-31 20:01:01 +0000624 TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
Chris Lattnerfb97b032007-12-02 01:40:18 +0000625 Builder = CodeGen::Init(Context, Features, *M, *TD, Diags);
Chris Lattner580980b2007-09-16 19:46:59 +0000626 }
627
628 virtual void HandleTopLevelDecl(Decl *D) {
629 // If an error occurred, stop code generation, but continue parsing and
630 // semantic analysis (to ensure all warnings and errors are emitted).
631 if (Diags.hasErrorOccurred())
632 return;
633
634 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
635 CodeGen::CodeGenFunction(Builder, FD);
636 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
637 CodeGen::CodeGenGlobalVar(Builder, FVD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000638 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
639 CodeGen::CodeGenLinkageSpec(Builder, LSD);
Chris Lattner580980b2007-09-16 19:46:59 +0000640 } else {
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000641 CodeGen::CodeGenTypeDecl(Builder, cast<TypeDecl>(D));
Chris Lattner580980b2007-09-16 19:46:59 +0000642 }
643 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000644 };
645}
646
Chris Lattnere66b65c2008-02-06 01:42:25 +0000647ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags,
648 const LangOptions &Features,
649 llvm::Module *&DestModule) {
650 return new CodeGenerator(Diags, Features, DestModule);
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000651}
652
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000653//===----------------------------------------------------------------------===//
654// AST Serializer
655
656namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000657
658class ASTSerializer : public ASTConsumer {
659protected:
660 Diagnostic &Diags;
661 TranslationUnit TU;
662public:
663 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
664 : Diags(diags), TU(LO) {}
665
666 virtual void Initialize(ASTContext &Context) {
667 TU.setContext(&Context);
668 }
669
670 virtual void HandleTopLevelDecl(Decl *D) {
671 if (Diags.hasErrorOccurred())
672 return;
673
674 TU.AddTopLevelDecl(D);
675 }
676};
677
678class SingleFileSerializer : public ASTSerializer {
679 const llvm::sys::Path FName;
680public:
681 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
682 const LangOptions &LO)
683 : ASTSerializer(diags,LO), FName(F) {}
684
685 ~SingleFileSerializer() {
686 EmitASTBitcodeFile(TU,FName);
687 }
688};
689
690class BuildSerializer : public ASTSerializer {
691 llvm::sys::Path EmitDir;
692public:
693 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000694 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000695 : ASTSerializer(diags,LO), EmitDir(dir) {}
696
Ted Kremenek54117722007-12-20 00:34:58 +0000697 ~BuildSerializer() {
698 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
699 unsigned ID = SourceMgr.getMainFileID();
700 assert (ID && "MainFileID not set!");
701 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
702 assert (FE && "No FileEntry for main file.");
703
704 // FIXME: This is not portable to Windows.
705 // FIXME: This logic should probably be moved elsewhere later.
706
Ted Kremenekee533642007-12-20 19:47:16 +0000707 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000708
709 std::vector<char> buf;
710 buf.reserve(strlen(FE->getName())+100);
711
712 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000713 FName.appendComponent(&buf[0]);
714 FName.createDirectoryOnDisk(true);
715 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000716 assert (false && "Could not create 'device' serialization directory.");
717 return;
718 }
Ted Kremenekee533642007-12-20 19:47:16 +0000719
Ted Kremenek54117722007-12-20 00:34:58 +0000720 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000721 FName.appendComponent(&buf[0]);
722 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000723
Ted Kremenekee533642007-12-20 19:47:16 +0000724 // Now emit the sources.
725
Ted Kremenek54117722007-12-20 00:34:58 +0000726 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000727};
728
729
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000730} // end anonymous namespace
731
732
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000733ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000734 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000735 Diagnostic &Diags,
736 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000737
Ted Kremenekf06c9282007-12-19 23:49:37 +0000738 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000739 if (InFile == "-") {
740 llvm::cerr <<
741 "error: Cannot use --serialize with -o for source read from STDIN.\n";
742 return NULL;
743 }
744
Ted Kremenekf06c9282007-12-19 23:49:37 +0000745 // The user specified an AST-emission directory. Determine if the path
746 // is absolute.
747 llvm::sys::Path EmitDir(OutputFile);
748
749 if (!EmitDir.isAbsolute()) {
750 llvm::cerr <<
751 "error: Output directory for --serialize must be an absolute path.\n";
752
753 return NULL;
754 }
755
756 // Create the directory if it does not exist.
757 EmitDir.createDirectoryOnDisk(true);
758 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
759 llvm::cerr <<
760 "error: Could not create output directory for --serialize.\n";
761
762 return NULL;
763 }
764
Ted Kremenek54117722007-12-20 00:34:58 +0000765 // FIXME: We should probably only allow using BuildSerializer when
766 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000767 return new BuildSerializer(EmitDir, Diags, Features);
768 }
769
770 // The user did not specify an output directory for serialized ASTs.
771 // Serialize the translation to a single file whose name is the same
772 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000773
Ted Kremenekf06c9282007-12-19 23:49:37 +0000774 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000775 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000776 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000777}