blob: 1dbfb040d3449be0ff0e3344cf94b83c6b93c18a [file] [log] [blame]
Chris Lattnereb8c9632007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnereb8c9632007-10-07 06:04:32 +000010// AST Consumer Implementations.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnereb8c9632007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Ted Kremenekac881932007-12-18 21:34:28 +000015#include "clang/AST/TranslationUnit.h"
Ted Kremenekfc17b8a2007-12-20 00:34:58 +000016#include "clang/Basic/SourceManager.h"
17#include "clang/Basic/FileManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/AST/AST.h"
Chris Lattnerb73abd52007-09-15 23:02:28 +000019#include "clang/AST/ASTConsumer.h"
Ted Kremenek97f75312007-08-21 21:42:03 +000020#include "clang/AST/CFG.h"
Ted Kremenekcdf8e842007-12-21 21:42:19 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremeneke805c4a2007-09-06 23:00:42 +000022#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000023#include "llvm/Support/Streams.h"
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +000024#include <fstream>
Ted Kremeneke09391a2007-11-27 21:46:50 +000025
Chris Lattner95578782007-08-08 22:51:59 +000026using namespace clang;
Chris Lattner4b009652007-07-25 00:24:17 +000027
Ted Kremeneke09391a2007-11-27 21:46:50 +000028//===----------------------------------------------------------------------===//
29/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner95578782007-08-08 22:51:59 +000030
Ted Kremeneke09391a2007-11-27 21:46:50 +000031namespace {
32 class DeclPrinter {
33 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000034 std::ostream& Out;
Ted Kremeneke09391a2007-11-27 21:46:50 +000035
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000036 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
37 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +000038
Chris Lattner1c1aabb2008-01-02 21:04:16 +000039 void PrintDecl(Decl *D);
Ted Kremeneke09391a2007-11-27 21:46:50 +000040 void PrintFunctionDeclStart(FunctionDecl *FD);
41 void PrintTypeDefDecl(TypedefDecl *TD);
Ted Kremenek42730c52008-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 Kremeneke09391a2007-11-27 21:46:50 +000049 };
50} // end anonymous namespace
51
Chris Lattner1c1aabb2008-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 Kremenek42730c52008-01-07 19:49:32 +000061 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000062 // Do nothing, methods definitions are printed in
Ted Kremenek42730c52008-01-07 19:49:32 +000063 // PrintObjCImplementationDecl.
Chris Lattner1c1aabb2008-01-02 21:04:16 +000064 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
65 PrintTypeDefDecl(TD);
Ted Kremenek42730c52008-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 Lattner1c1aabb2008-01-02 21:04:16 +000072 Out << "@protocol ";
73 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +000074 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattner1c1aabb2008-01-02 21:04:16 +000075 if (i) Out << ", ";
76 Out << D->getName();
77 }
78 Out << ";\n";
Ted Kremenek42730c52008-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 Lattner1c1aabb2008-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 Kremeneke09391a2007-11-27 21:46:50 +0000102void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000103 bool HasBody = FD->getBody();
104
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000105 Out << '\n';
Chris Lattner987058a2007-08-26 04:02:13 +0000106
107 switch (FD->getStorageClass()) {
108 default: assert(0 && "Unknown storage class");
109 case FunctionDecl::None: break;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000110 case FunctionDecl::Extern: Out << "extern "; break;
111 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner987058a2007-08-26 04:02:13 +0000112 }
113
114 if (FD->isInline())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000115 Out << "inline ";
Chris Lattner987058a2007-08-26 04:02:13 +0000116
Chris Lattner4b009652007-07-25 00:24:17 +0000117 std::string Proto = FD->getName();
Chris Lattner934fff62007-12-03 21:43:25 +0000118 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +0000119
Chris Lattner934fff62007-12-03 21:43:25 +0000120 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Kremenekbe2ea3b2007-11-28 21:32:21 +0000142 Out << Proto;
Chris Lattner4b009652007-07-25 00:24:17 +0000143
Chris Lattner95578782007-08-08 22:51:59 +0000144 if (!FD->getBody())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000145 Out << ";\n";
Chris Lattner95578782007-08-08 22:51:59 +0000146 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +0000147}
148
Ted Kremeneke09391a2007-11-27 21:46:50 +0000149void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000150 std::string S = TD->getName();
151 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000152 Out << "typedef " << S << ";\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000153}
154
Ted Kremenek42730c52008-01-07 19:49:32 +0000155void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000156 if (OMD->isInstance())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000157 Out << "\n- ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000158 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000159 Out << "\n+ ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000160 if (!OMD->getResultType().isNull())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000161 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000162 // FIXME: just print original selector name!
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000163 Out << OMD->getSelector().getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000164
165 for (int i = 0; i < OMD->getNumParams(); i++) {
166 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000167 // FIXME: selector is missing here!
168 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000169 }
170}
171
Ted Kremenek42730c52008-01-07 19:49:32 +0000172void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000173 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000174 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000175
176 if (SID)
177 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000178 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000179 Out << "@implementation " << I;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000180
Ted Kremenek42730c52008-01-07 19:49:32 +0000181 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000182 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000183 ObjCMethodDecl *OMD = *I;
184 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000185 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000186 Out << ' ';
187 OMD->getBody()->printPretty(Out);
188 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000189 }
190 }
191
Ted Kremenek42730c52008-01-07 19:49:32 +0000192 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000193 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000194 ObjCMethodDecl *OMD = *I;
195 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000196 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000197 Out << ' ';
198 OMD->getBody()->printPretty(Out);
199 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000200 }
201 }
202
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000203 Out << "@end\n";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000204}
205
206
Ted Kremenek42730c52008-01-07 19:49:32 +0000207void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000208 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000209 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000210
211 if (SID)
212 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000213 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000214 Out << "@interface " << I;
215
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000216 // Protocols?
217 int count = OID->getNumIntfRefProtocols();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000218
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000219 if (count > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000220 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000221 for (int i = 0; i < count; i++)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000222 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000223 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000224
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000225 if (count > 0)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000226 Out << ">\n";
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000227 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000228 Out << '\n';
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000229
Chris Lattnerc7b06752007-12-12 07:56:42 +0000230 if (OID->getNumInstanceVariables() > 0) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000231 Out << '{';
Ted Kremenek42730c52008-01-07 19:49:32 +0000232 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerc7b06752007-12-12 07:56:42 +0000233 E = OID->ivar_end(); I != E; ++I) {
234 Out << '\t' << (*I)->getType().getAsString()
235 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000236 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000237 Out << "}\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000238 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000239
240 int NumProperties = OID->getNumPropertyDecl();
241 if (NumProperties > 0) {
242 for (int i = 0; i < NumProperties; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000243 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000244 Out << "@property";
Ted Kremenek42730c52008-01-07 19:49:32 +0000245 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000246 bool first = true;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000247 Out << " (";
Ted Kremenek42730c52008-01-07 19:49:32 +0000248 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000249 {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000250 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000251 first = false;
252 }
253
Ted Kremenek42730c52008-01-07 19:49:32 +0000254 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter)
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000255 {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000256 Out << (first ? ' ' : ',') << "getter = "
257 << PDecl->getGetterName()->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000258 first = false;
259 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000260 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter)
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000261 {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000262 Out << (first ? ' ' : ',') << "setter = "
263 << PDecl->getSetterName()->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000264 first = false;
265 }
266
Ted Kremenek42730c52008-01-07 19:49:32 +0000267 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign)
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000268 {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000269 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000270 first = false;
271 }
272
Ted Kremenek42730c52008-01-07 19:49:32 +0000273 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000274 {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000275 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000276 first = false;
277 }
278
Ted Kremenek42730c52008-01-07 19:49:32 +0000279 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain)
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000280 {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000281 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000282 first = false;
283 }
284
Ted Kremenek42730c52008-01-07 19:49:32 +0000285 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy)
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000286 {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000287 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000288 first = false;
289 }
290
Ted Kremenek42730c52008-01-07 19:49:32 +0000291 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000292 {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000293 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000294 first = false;
295 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000296 Out << " )";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000297 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000298
Ted Kremenek42730c52008-01-07 19:49:32 +0000299 ObjCIvarDecl **IDecl = PDecl->getPropertyDecls();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000300
301 Out << ' ' << IDecl[0]->getType().getAsString()
302 << ' ' << IDecl[0]->getName();
303
304 for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
305 Out << ", " << IDecl[j]->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000306
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000307 Out << ";\n";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000308 }
309 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000310
311 Out << "@end\n";
Steve Narofffaed3bf2007-09-10 20:51:04 +0000312 // FIXME: implement the rest...
313}
314
Ted Kremenek42730c52008-01-07 19:49:32 +0000315void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000316 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000317 // FIXME: implement the rest...
318}
319
Ted Kremenek42730c52008-01-07 19:49:32 +0000320void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000321 Out << "@implementation "
322 << PID->getClassInterface()->getName()
323 << '(' << PID->getName() << ");\n";
324
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000325 // FIXME: implement the rest...
326}
327
Ted Kremenek42730c52008-01-07 19:49:32 +0000328void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000329 Out << "@interface "
330 << PID->getClassInterface()->getName()
331 << '(' << PID->getName() << ");\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000332 // FIXME: implement the rest...
333}
334
Ted Kremenek42730c52008-01-07 19:49:32 +0000335void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000336 Out << "@compatibility_alias " << AID->getName()
337 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000338}
339
Ted Kremeneke09391a2007-11-27 21:46:50 +0000340//===----------------------------------------------------------------------===//
341/// ASTPrinter - Pretty-printer of ASTs
342
Chris Lattnerb73abd52007-09-15 23:02:28 +0000343namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000344 class ASTPrinter : public ASTConsumer, public DeclPrinter {
345 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000346 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000347
Chris Lattnerb73abd52007-09-15 23:02:28 +0000348 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000349 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000350 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000351 };
Chris Lattner4b009652007-07-25 00:24:17 +0000352}
Chris Lattner95578782007-08-08 22:51:59 +0000353
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000354ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
355 return new ASTPrinter(out);
356}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000357
358//===----------------------------------------------------------------------===//
359/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000360
361namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000362 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000363 SourceManager *SM;
364 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000365 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000366
Ted Kremenek17861c52007-12-19 22:51:13 +0000367 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000368 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000369 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000370
371 virtual void HandleTopLevelDecl(Decl *D) {
372 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
373 PrintFunctionDeclStart(FD);
374
375 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000376 Out << '\n';
377 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000378 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000379 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000380 }
381 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
382 PrintTypeDefDecl(TD);
383 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000384 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000385 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000386 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000387 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000388 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000389 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000390 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000391 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000392 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000393 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000394 Out << "Read objc fwd class decl\n";
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000395 } else {
396 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000397 }
398 }
399 };
Chris Lattner95578782007-08-08 22:51:59 +0000400}
401
Chris Lattnerb73abd52007-09-15 23:02:28 +0000402ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
403
Ted Kremeneke09391a2007-11-27 21:46:50 +0000404//===----------------------------------------------------------------------===//
405/// ASTViewer - AST Visualization
406
Ted Kremenekb6976a22007-09-19 21:29:43 +0000407namespace {
408 class ASTViewer : public ASTConsumer {
409 SourceManager *SM;
410 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000411 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000412 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000413 }
414
415 virtual void HandleTopLevelDecl(Decl *D) {
416 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000417 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000418
419 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000420 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000421 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000422 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000423 }
424 }
425 }
426 };
427}
428
429ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
430
431
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000432//===----------------------------------------------------------------------===//
433// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
434// the CFGs for all function definitions.
435
436namespace {
437
Chris Lattner52332d02007-09-15 23:21:08 +0000438class CFGVisitor : public ASTConsumer {
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000439public:
Chris Lattner52332d02007-09-15 23:21:08 +0000440 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000441 virtual void VisitCFG(CFG& C) = 0;
442 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000443
444 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000445};
446
447} // end anonymous namespace
448
Chris Lattner52332d02007-09-15 23:21:08 +0000449void CFGVisitor::HandleTopLevelDecl(Decl *D) {
450 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
451 if (!FD || !FD->getBody())
452 return;
453
454 if (printFuncDeclStart()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000455 DeclPrinter().PrintFunctionDeclStart(FD);
456 llvm::cerr << '\n';
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000457 }
Chris Lattner52332d02007-09-15 23:21:08 +0000458
Ted Kremenek3e88d752007-09-17 17:10:02 +0000459 CFG *C = CFG::buildCFG(FD->getBody());
460 VisitCFG(*C);
461 delete C;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000462}
463
464//===----------------------------------------------------------------------===//
465// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
466
467namespace {
468 class CFGDumper : public CFGVisitor {
469 const bool UseGraphviz;
470 public:
471 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
472
Chris Lattner52332d02007-09-15 23:21:08 +0000473 virtual void VisitCFG(CFG &C) {
474 if (UseGraphviz)
475 C.viewCFG();
476 else
477 C.dump();
478 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000479 };
480} // end anonymous namespace
481
Chris Lattner52332d02007-09-15 23:21:08 +0000482ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
483 return new CFGDumper(ViewGraphs);
Ted Kremenek97f75312007-08-21 21:42:03 +0000484}
485
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000486//===----------------------------------------------------------------------===//
487// AnalyzeLiveVariables - perform live variable analysis and dump results
488
489namespace {
490 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000491 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000492 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000493 virtual void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000494 SM = &Context.getSourceManager();
Chris Lattner52332d02007-09-15 23:21:08 +0000495 }
496
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000497 virtual void VisitCFG(CFG& C) {
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000498 LiveVariables L(C);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000499 L.runOnCFG(C);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000500 L.dumpBlockLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000501 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000502 };
503} // end anonymous namespace
504
Chris Lattner52332d02007-09-15 23:21:08 +0000505ASTConsumer *clang::CreateLiveVarAnalyzer() {
506 return new LivenessVisitor();
Ted Kremenekaa04c512007-09-06 00:17:54 +0000507}
508
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000509//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000510// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000511
512namespace {
513 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000514 Diagnostic &Diags;
515 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000516 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000517 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000518 virtual void Initialize(ASTContext &Context) {
Chris Lattner52332d02007-09-15 23:21:08 +0000519 Ctx = &Context;
520 }
521
522 virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000523 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000524 };
525} // end anonymous namespace
526
Chris Lattner52332d02007-09-15 23:21:08 +0000527ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
528 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000529}
Chris Lattner129758d2007-09-16 19:46:59 +0000530
531//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000532// Unitialized Values - run checker to flag potential uses of uninitalized
533// variables.
534
535namespace {
536 class UninitValsVisitor : public CFGVisitor {
537 Diagnostic &Diags;
538 ASTContext *Ctx;
539 public:
540 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000541 virtual void Initialize(ASTContext &Context) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000542 Ctx = &Context;
543 }
544
545 virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); }
546 virtual bool printFuncDeclStart() { return false; }
547 };
548} // end anonymous namespace
549
550ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
551 return new UninitValsVisitor(Diags);
552}
553
554//===----------------------------------------------------------------------===//
Chris Lattner129758d2007-09-16 19:46:59 +0000555// LLVM Emitter
556
557#include "clang/Basic/Diagnostic.h"
Devang Patela8fccb82007-10-31 20:01:01 +0000558#include "clang/Basic/TargetInfo.h"
Chris Lattner129758d2007-09-16 19:46:59 +0000559#include "clang/CodeGen/ModuleBuilder.h"
560#include "llvm/Module.h"
Devang Patela8fccb82007-10-31 20:01:01 +0000561#include "llvm/Target/TargetData.h"
562#include "llvm/Target/TargetMachine.h"
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000563#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner129758d2007-09-16 19:46:59 +0000564
565namespace {
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000566 class CodeGenerator : public ASTConsumer {
Chris Lattner129758d2007-09-16 19:46:59 +0000567 Diagnostic &Diags;
Devang Patela8fccb82007-10-31 20:01:01 +0000568 const llvm::TargetData *TD;
Chris Lattner129758d2007-09-16 19:46:59 +0000569 ASTContext *Ctx;
Chris Lattnerdb6be562007-11-28 05:34:05 +0000570 const LangOptions &Features;
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000571 protected:
572 llvm::Module *M;
Chris Lattner07f44dc2007-11-13 18:16:41 +0000573 CodeGen::CodeGenModule *Builder;
Chris Lattner129758d2007-09-16 19:46:59 +0000574 public:
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000575 CodeGenerator(Diagnostic &diags, const LangOptions &LO)
Chris Lattnerdb6be562007-11-28 05:34:05 +0000576 : Diags(diags)
577 , Features(LO) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000578 virtual void Initialize(ASTContext &Context) {
Chris Lattner129758d2007-09-16 19:46:59 +0000579 Ctx = &Context;
580 M = new llvm::Module("foo");
Devang Patela8fccb82007-10-31 20:01:01 +0000581 M->setTargetTriple(Ctx->Target.getTargetTriple());
Chris Lattner9efc5012007-12-13 17:34:31 +0000582 M->setDataLayout(Ctx->Target.getTargetDescription());
Devang Patela8fccb82007-10-31 20:01:01 +0000583 TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
Chris Lattner22595b82007-12-02 01:40:18 +0000584 Builder = CodeGen::Init(Context, Features, *M, *TD, Diags);
Chris Lattner129758d2007-09-16 19:46:59 +0000585 }
586
587 virtual void HandleTopLevelDecl(Decl *D) {
588 // If an error occurred, stop code generation, but continue parsing and
589 // semantic analysis (to ensure all warnings and errors are emitted).
590 if (Diags.hasErrorOccurred())
591 return;
592
593 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
594 CodeGen::CodeGenFunction(Builder, FD);
595 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
596 CodeGen::CodeGenGlobalVar(Builder, FVD);
597 } else {
Steve Naroff3340c232007-11-17 21:21:01 +0000598 assert(isa<TypeDecl>(D) && "Only expected type decls here");
Chris Lattner129758d2007-09-16 19:46:59 +0000599 // don't codegen for now, eventually pass down for debug info.
Ted Kremenek21189012007-12-19 23:49:37 +0000600 //std::cerr << "Read top-level typedef decl: '"
601 // << D->getName() << "'\n";
Chris Lattner129758d2007-09-16 19:46:59 +0000602 }
603 }
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000604 };
605}
606
607namespace {
608 class LLVMEmitter : public CodeGenerator {
609 public:
610 LLVMEmitter(Diagnostic &diags, const LangOptions &LO)
611 : CodeGenerator(diags,LO) {}
612
Chris Lattner129758d2007-09-16 19:46:59 +0000613 ~LLVMEmitter() {
614 CodeGen::Terminate(Builder);
615
616 // Print the generated code.
Ted Kremenek8df2a4d2007-12-13 17:50:11 +0000617 M->print(llvm::cout.stream());
Chris Lattner129758d2007-09-16 19:46:59 +0000618 delete M;
619 }
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000620 };
621}
Chris Lattner129758d2007-09-16 19:46:59 +0000622
Ted Kremenek21189012007-12-19 23:49:37 +0000623ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags,
624 const LangOptions &Features) {
Chris Lattnerdb6be562007-11-28 05:34:05 +0000625 return new LLVMEmitter(Diags, Features);
Chris Lattner129758d2007-09-16 19:46:59 +0000626}
627
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000628namespace {
629 class BCWriter : public CodeGenerator {
630 public:
631 std::ostream& Out;
632
633 BCWriter(std::ostream* out, Diagnostic &diags, const LangOptions &LO)
634 : CodeGenerator(diags,LO)
635 , Out(*out) {}
636
637 ~BCWriter() {
638 CodeGen::Terminate(Builder);
639 llvm::WriteBitcodeToFile(M, Out);
640 delete M;
641 }
642 };
643}
644
645ASTConsumer *clang::CreateBCWriter(const std::string& InFile,
646 const std::string& OutputFile,
647 Diagnostic &Diags,
648 const LangOptions &Features) {
649 std::string FileName = OutputFile;
Christopher Lamb092768b2007-12-24 03:23:55 +0000650
651 std::ostream *Out;
Christopher Lambb8c8a3f2007-12-24 20:59:36 +0000652 if (OutputFile == "-")
Christopher Lamb361888b2007-12-24 20:56:07 +0000653 Out = llvm::cout.stream();
654 else if (!OutputFile.size()) {
Christopher Lambb8c8a3f2007-12-24 20:59:36 +0000655 if (InFile == "-")
656 Out = llvm::cout.stream();
657 else {
658 llvm::sys::Path Path(InFile);
659 Path.eraseSuffix();
660 Path.appendSuffix("bc");
661 FileName = Path.toString();
Christopher Lamb8af8b062007-12-24 23:49:33 +0000662 Out = new std::ofstream(FileName.c_str(),
663 std::ios_base::binary|std::ios_base::out);
Christopher Lambb8c8a3f2007-12-24 20:59:36 +0000664 }
Christopher Lamb092768b2007-12-24 03:23:55 +0000665 } else {
Christopher Lamb8af8b062007-12-24 23:49:33 +0000666 Out = new std::ofstream(FileName.c_str(),
667 std::ios_base::binary|std::ios_base::out);
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000668 }
669
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000670 return new BCWriter(Out, Diags, Features);
671}
672
Ted Kremenek397de012007-12-13 00:37:31 +0000673//===----------------------------------------------------------------------===//
674// AST Serializer
675
676namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000677
678class ASTSerializer : public ASTConsumer {
679protected:
680 Diagnostic &Diags;
681 TranslationUnit TU;
682public:
683 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
684 : Diags(diags), TU(LO) {}
685
686 virtual void Initialize(ASTContext &Context) {
687 TU.setContext(&Context);
688 }
689
690 virtual void HandleTopLevelDecl(Decl *D) {
691 if (Diags.hasErrorOccurred())
692 return;
693
694 TU.AddTopLevelDecl(D);
695 }
696};
697
698class SingleFileSerializer : public ASTSerializer {
699 const llvm::sys::Path FName;
700public:
701 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
702 const LangOptions &LO)
703 : ASTSerializer(diags,LO), FName(F) {}
704
705 ~SingleFileSerializer() {
706 EmitASTBitcodeFile(TU,FName);
707 }
708};
709
710class BuildSerializer : public ASTSerializer {
711 llvm::sys::Path EmitDir;
712public:
713 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremenek397de012007-12-13 00:37:31 +0000714 const LangOptions &LO)
Ted Kremenek21189012007-12-19 23:49:37 +0000715 : ASTSerializer(diags,LO), EmitDir(dir) {}
716
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000717 ~BuildSerializer() {
718 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
719 unsigned ID = SourceMgr.getMainFileID();
720 assert (ID && "MainFileID not set!");
721 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
722 assert (FE && "No FileEntry for main file.");
723
724 // FIXME: This is not portable to Windows.
725 // FIXME: This logic should probably be moved elsewhere later.
726
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000727 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000728
729 std::vector<char> buf;
730 buf.reserve(strlen(FE->getName())+100);
731
732 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000733 FName.appendComponent(&buf[0]);
734 FName.createDirectoryOnDisk(true);
735 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000736 assert (false && "Could not create 'device' serialization directory.");
737 return;
738 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000739
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000740 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000741 FName.appendComponent(&buf[0]);
742 EmitASTBitcodeFile(TU,FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000743
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000744 // Now emit the sources.
745
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000746 }
Ted Kremenek21189012007-12-19 23:49:37 +0000747};
748
749
Ted Kremenek397de012007-12-13 00:37:31 +0000750} // end anonymous namespace
751
752
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000753ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000754 const std::string& OutputFile,
Ted Kremenek397de012007-12-13 00:37:31 +0000755 Diagnostic &Diags,
756 const LangOptions &Features) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000757
Ted Kremenek21189012007-12-19 23:49:37 +0000758 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000759 if (InFile == "-") {
760 llvm::cerr <<
761 "error: Cannot use --serialize with -o for source read from STDIN.\n";
762 return NULL;
763 }
764
Ted Kremenek21189012007-12-19 23:49:37 +0000765 // The user specified an AST-emission directory. Determine if the path
766 // is absolute.
767 llvm::sys::Path EmitDir(OutputFile);
768
769 if (!EmitDir.isAbsolute()) {
770 llvm::cerr <<
771 "error: Output directory for --serialize must be an absolute path.\n";
772
773 return NULL;
774 }
775
776 // Create the directory if it does not exist.
777 EmitDir.createDirectoryOnDisk(true);
778 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
779 llvm::cerr <<
780 "error: Could not create output directory for --serialize.\n";
781
782 return NULL;
783 }
784
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000785 // FIXME: We should probably only allow using BuildSerializer when
786 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenek21189012007-12-19 23:49:37 +0000787 return new BuildSerializer(EmitDir, Diags, Features);
788 }
789
790 // The user did not specify an output directory for serialized ASTs.
791 // Serialize the translation to a single file whose name is the same
792 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000793
Ted Kremenek21189012007-12-19 23:49:37 +0000794 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000795 FName.appendSuffix("ast");
Ted Kremenek21189012007-12-19 23:49:37 +0000796 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremenek397de012007-12-13 00:37:31 +0000797}