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