blob: a24cd0478e014bb9c32d02e1dc08726efb632e72 [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"
Chris Lattner8ee3c032008-02-06 02:01:47 +000016#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000020#include "clang/AST/ASTConsumer.h"
Ted Kremenekfddd5182007-08-21 21:42:03 +000021#include "clang/AST/CFG.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000022#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000023#include "clang/Analysis/Analyses/GRSimpleVals.h"
Ted Kremenek055c2752007-09-06 23:00:42 +000024#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000025#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000026#include "llvm/Support/Timer.h"
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 =
Chris Lattnerc81c8142008-02-25 21:04:36 +000073 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 =
Chris Lattnerc81c8142008-02-25 21:04:36 +000082 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000083 PrintObjCImplementationDecl(OID);
84 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000085 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000086 PrintObjCCategoryImplDecl(OID);
87 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000088 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000089 PrintObjCCategoryDecl(OID);
90 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000091 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000092 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);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000101 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
102 Out << "asm(";
103 AD->getAsmString()->printPretty(Out);
104 Out << ")\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000105 } else {
106 assert(0 && "Unknown decl type!");
107 }
108}
109
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000110void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 bool HasBody = FD->getBody();
112
Ted Kremenekea75c552007-11-28 21:32:21 +0000113 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000114
115 switch (FD->getStorageClass()) {
116 default: assert(0 && "Unknown storage class");
117 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000118 case FunctionDecl::Extern: Out << "extern "; break;
119 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000120 }
121
122 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000123 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000126 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000127
Chris Lattner0d6ca112007-12-03 21:43:25 +0000128 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 Proto += "(";
130 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
131 if (i) Proto += ", ";
132 std::string ParamStr;
133 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
134
135 FT->getArgType(i).getAsStringInternal(ParamStr);
136 Proto += ParamStr;
137 }
138
139 if (FT->isVariadic()) {
140 if (FD->getNumParams()) Proto += ", ";
141 Proto += "...";
142 }
143 Proto += ")";
144 } else {
145 assert(isa<FunctionTypeNoProto>(AFT));
146 Proto += "()";
147 }
148
149 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000150 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000151
Chris Lattner6000dac2007-08-08 22:51:59 +0000152 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000153 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000154 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000155}
156
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000157void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 std::string S = TD->getName();
159 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000160 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000161}
162
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000163void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
164 const char *l;
165 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
166 l = "C";
167 else if (LS->getLanguage() == LinkageSpecDecl::lang_cxx)
168 l = "C++";
169 else assert(0 && "unknown language in linkage specification");
170 Out << "extern \"" << l << "\" { ";
171 PrintDecl(LS->getDecl());
172 Out << "}\n";
173}
174
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000176 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000177 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000178 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000179 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000180 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000181 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000182 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000183 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000184
185 for (int i = 0; i < OMD->getNumParams(); i++) {
186 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000187 // FIXME: selector is missing here!
188 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000189 }
190}
191
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000192void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000193 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000195
196 if (SID)
197 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000198 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000199 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000200
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000201 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000202 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000203 ObjCMethodDecl *OMD = *I;
204 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000205 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000206 Out << ' ';
207 OMD->getBody()->printPretty(Out);
208 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000209 }
210 }
211
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000212 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000213 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000214 ObjCMethodDecl *OMD = *I;
215 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000216 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000217 Out << ' ';
218 OMD->getBody()->printPretty(Out);
219 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000220 }
221 }
222
Ted Kremenekea75c552007-11-28 21:32:21 +0000223 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000224}
225
226
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000227void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000228 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000229 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000230
231 if (SID)
232 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000233 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000234 Out << "@interface " << I;
235
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000236 // Protocols?
237 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000238
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000239 if (count > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000240 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000241 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000242 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000243 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000244
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000245 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000246 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000247 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000248 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000249
Chris Lattnerbe6df082007-12-12 07:56:42 +0000250 if (OID->getNumInstanceVariables() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000251 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000252 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000253 E = OID->ivar_end(); I != E; ++I) {
254 Out << '\t' << (*I)->getType().getAsString()
255 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000256 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000257 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000258 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000259
260 int NumProperties = OID->getNumPropertyDecl();
261 if (NumProperties > 0) {
262 for (int i = 0; i < NumProperties; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000263 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekea75c552007-11-28 21:32:21 +0000264 Out << "@property";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000265 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000266 bool first = true;
Ted Kremenekea75c552007-11-28 21:32:21 +0000267 Out << " (";
Chris Lattner4b1daf02008-01-10 01:43:14 +0000268 if (PDecl->getPropertyAttributes() &
269 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000270 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000271 first = false;
272 }
273
Chris Lattner4b1daf02008-01-10 01:43:14 +0000274 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000275 Out << (first ? ' ' : ',') << "getter = "
276 << PDecl->getGetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000277 first = false;
278 }
Chris Lattner4b1daf02008-01-10 01:43:14 +0000279 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000280 Out << (first ? ' ' : ',') << "setter = "
281 << PDecl->getSetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000282 first = false;
283 }
284
Chris Lattner4b1daf02008-01-10 01:43:14 +0000285 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000286 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000287 first = false;
288 }
289
Chris Lattner4b1daf02008-01-10 01:43:14 +0000290 if (PDecl->getPropertyAttributes() &
291 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000292 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000293 first = false;
294 }
295
Chris Lattner4b1daf02008-01-10 01:43:14 +0000296 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000297 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000298 first = false;
299 }
300
Chris Lattner4b1daf02008-01-10 01:43:14 +0000301 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000302 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000303 first = false;
304 }
305
Chris Lattner4b1daf02008-01-10 01:43:14 +0000306 if (PDecl->getPropertyAttributes() &
307 ObjCPropertyDecl::OBJC_PR_nonatomic) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000308 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000309 first = false;
310 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000311 Out << " )";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000312 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000313
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000314 ObjCIvarDecl **IDecl = PDecl->getPropertyDecls();
Ted Kremenekea75c552007-11-28 21:32:21 +0000315
316 Out << ' ' << IDecl[0]->getType().getAsString()
317 << ' ' << IDecl[0]->getName();
318
319 for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
320 Out << ", " << IDecl[j]->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000321
Ted Kremenekea75c552007-11-28 21:32:21 +0000322 Out << ";\n";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000323 }
324 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000325
326 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000327 // FIXME: implement the rest...
328}
329
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000330void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000331 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000332 // FIXME: implement the rest...
333}
334
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000335void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000336 Out << "@implementation "
337 << PID->getClassInterface()->getName()
338 << '(' << PID->getName() << ");\n";
339
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000340 // FIXME: implement the rest...
341}
342
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000343void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000344 Out << "@interface "
345 << PID->getClassInterface()->getName()
346 << '(' << PID->getName() << ");\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000347 // FIXME: implement the rest...
348}
349
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000350void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000351 Out << "@compatibility_alias " << AID->getName()
352 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000353}
354
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000355//===----------------------------------------------------------------------===//
356/// ASTPrinter - Pretty-printer of ASTs
357
Chris Lattner3d4997d2007-09-15 23:02:28 +0000358namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000359 class ASTPrinter : public ASTConsumer, public DeclPrinter {
360 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000361 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000362
Chris Lattner3d4997d2007-09-15 23:02:28 +0000363 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000364 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000366 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000367}
Chris Lattner6000dac2007-08-08 22:51:59 +0000368
Ted Kremenekea75c552007-11-28 21:32:21 +0000369ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
370 return new ASTPrinter(out);
371}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000372
373//===----------------------------------------------------------------------===//
374/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000375
376namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000377 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000378 SourceManager *SM;
379 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000380 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000381
Ted Kremenek95041a22007-12-19 22:51:13 +0000382 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000383 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000384 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000385
386 virtual void HandleTopLevelDecl(Decl *D) {
387 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
388 PrintFunctionDeclStart(FD);
389
390 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000391 Out << '\n';
392 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000393 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000394 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000395 }
396 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
397 PrintTypeDefDecl(TD);
398 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000399 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000400 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000401 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000402 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000403 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000404 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000405 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000406 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000407 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000408 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000409 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000410 } else if (isa<FileScopeAsmDecl>(D)) {
411 Out << "Read file scope asm decl\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000412 } else {
413 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000414 }
415 }
416 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000417}
418
Chris Lattner3d4997d2007-09-15 23:02:28 +0000419ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
420
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000421//===----------------------------------------------------------------------===//
422/// ASTViewer - AST Visualization
423
Ted Kremenek80de08f2007-09-19 21:29:43 +0000424namespace {
425 class ASTViewer : public ASTConsumer {
426 SourceManager *SM;
427 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000428 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000429 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000430 }
431
432 virtual void HandleTopLevelDecl(Decl *D) {
433 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000434 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000435
436 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000437 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000438 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000439 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000440 }
441 }
442 }
443 };
444}
445
446ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
447
448
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000449//===----------------------------------------------------------------------===//
450// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
451// the CFGs for all function definitions.
452
453namespace {
454
Chris Lattnerc0508f92007-09-15 23:21:08 +0000455class CFGVisitor : public ASTConsumer {
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000456 std::string FName;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000457public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000458 CFGVisitor(const std::string& fname) : FName(fname) {}
459 CFGVisitor() : FName("") {}
460
Chris Lattnerc0508f92007-09-15 23:21:08 +0000461 // CFG Visitor interface to be implemented by subclass.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000462 virtual void VisitCFG(CFG& C, FunctionDecl& FD) = 0;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000463 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000464
465 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000466};
467
468} // end anonymous namespace
469
Chris Lattnerc0508f92007-09-15 23:21:08 +0000470void CFGVisitor::HandleTopLevelDecl(Decl *D) {
471 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000472
Chris Lattnerc0508f92007-09-15 23:21:08 +0000473 if (!FD || !FD->getBody())
474 return;
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000475
476 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
477 return;
Chris Lattnerc0508f92007-09-15 23:21:08 +0000478
479 if (printFuncDeclStart()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000480 DeclPrinter().PrintFunctionDeclStart(FD);
481 llvm::cerr << '\n';
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000482 }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000483
Ted Kremenek12259662007-09-17 17:10:02 +0000484 CFG *C = CFG::buildCFG(FD->getBody());
Ted Kremenekbffaa832008-01-29 05:13:23 +0000485 VisitCFG(*C, *FD);
Ted Kremenek12259662007-09-17 17:10:02 +0000486 delete C;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000487}
488
489//===----------------------------------------------------------------------===//
490// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
491
492namespace {
493 class CFGDumper : public CFGVisitor {
494 const bool UseGraphviz;
495 public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000496 CFGDumper(bool use_graphviz, const std::string& fname)
497 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000498
Ted Kremenekbffaa832008-01-29 05:13:23 +0000499 virtual void VisitCFG(CFG& C, FunctionDecl&) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000500 if (UseGraphviz)
501 C.viewCFG();
502 else
503 C.dump();
504 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000505 };
506} // end anonymous namespace
507
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000508ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
509 return new CFGDumper(ViewGraphs, FName);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000510}
511
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000512//===----------------------------------------------------------------------===//
513// AnalyzeLiveVariables - perform live variable analysis and dump results
514
515namespace {
516 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000517 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000518 public:
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000519 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
520
Ted Kremenek95041a22007-12-19 22:51:13 +0000521 virtual void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000522 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000523 }
524
Ted Kremenekbffaa832008-01-29 05:13:23 +0000525 virtual void VisitCFG(CFG& C, FunctionDecl& FD) {
526 LiveVariables L(C, FD);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000527 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000528 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000529 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000530 };
531} // end anonymous namespace
532
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000533ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
534 return new LivenessVisitor(fname);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000535}
536
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000537//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000538// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000539
540namespace {
541 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000542 Diagnostic &Diags;
543 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000544 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000545 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000546 virtual void Initialize(ASTContext &Context) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000547 Ctx = &Context;
548 }
549
Ted Kremenekbffaa832008-01-29 05:13:23 +0000550 virtual void VisitCFG(CFG& C, FunctionDecl& FD) {
551 CheckDeadStores(C, FD, *Ctx, Diags);
552 }
553
Ted Kremenek567a7e62007-09-07 23:54:15 +0000554 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000555 };
556} // end anonymous namespace
557
Chris Lattnerc0508f92007-09-15 23:21:08 +0000558ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
559 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000560}
Chris Lattner580980b2007-09-16 19:46:59 +0000561
562//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000563// Unitialized Values - run checker to flag potential uses of uninitalized
564// variables.
565
566namespace {
567 class UninitValsVisitor : public CFGVisitor {
568 Diagnostic &Diags;
569 ASTContext *Ctx;
570 public:
571 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000572 virtual void Initialize(ASTContext &Context) {
Ted Kremenek2bf55142007-09-17 20:49:30 +0000573 Ctx = &Context;
574 }
575
Ted Kremenekbffaa832008-01-29 05:13:23 +0000576 virtual void VisitCFG(CFG& C, FunctionDecl&) {
577 CheckUninitializedValues(C, *Ctx, Diags);
578 }
579
Ted Kremenek2bf55142007-09-17 20:49:30 +0000580 virtual bool printFuncDeclStart() { return false; }
581 };
582} // end anonymous namespace
583
584ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
585 return new UninitValsVisitor(Diags);
586}
587
588//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +0000589// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremeneke603df42008-01-08 18:04:06 +0000590
591namespace {
Ted Kremeneke01c9872008-02-14 22:36:46 +0000592 class GRSimpleValsVisitor : public CFGVisitor {
Ted Kremenek19227e32008-02-07 06:33:19 +0000593 Diagnostic &Diags;
Ted Kremenek874d63f2008-01-24 02:02:54 +0000594 ASTContext* Ctx;
Ted Kremenekd55fe522008-02-15 00:35:38 +0000595 bool Visualize;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000596 bool TrimGraph;
Ted Kremeneke603df42008-01-08 18:04:06 +0000597 public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000598 GRSimpleValsVisitor(Diagnostic &diags, const std::string& fname,
Ted Kremenekffe0f432008-03-07 22:58:01 +0000599 bool visualize, bool trim)
600 : CFGVisitor(fname), Diags(diags), Visualize(visualize), TrimGraph(trim){}
Ted Kremeneke603df42008-01-08 18:04:06 +0000601
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000602 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
Ted Kremenekbffaa832008-01-29 05:13:23 +0000603 virtual void VisitCFG(CFG& C, FunctionDecl&);
Ted Kremenekcb330932008-02-18 21:21:23 +0000604 virtual bool printFuncDeclStart() { return false; }
Ted Kremeneke603df42008-01-08 18:04:06 +0000605 };
606} // end anonymous namespace
607
Ted Kremenekcb330932008-02-18 21:21:23 +0000608ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
609 const std::string& FunctionName,
Ted Kremenekffe0f432008-03-07 22:58:01 +0000610 bool Visualize, bool TrimGraph) {
Ted Kremenekcb330932008-02-18 21:21:23 +0000611
Ted Kremenekffe0f432008-03-07 22:58:01 +0000612 return new GRSimpleValsVisitor(Diags, FunctionName, Visualize, TrimGraph);
Ted Kremeneke603df42008-01-08 18:04:06 +0000613}
614
Ted Kremeneke01c9872008-02-14 22:36:46 +0000615void GRSimpleValsVisitor::VisitCFG(CFG& C, FunctionDecl& FD) {
Ted Kremenekcb330932008-02-18 21:21:23 +0000616
Ted Kremenek71ac9c42008-02-22 19:10:58 +0000617 SourceLocation Loc = FD.getLocation();
Ted Kremenekb9c31f02008-02-19 02:33:31 +0000618
Ted Kremenek71ac9c42008-02-22 19:10:58 +0000619 if (!Loc.isFileID() ||
620 Loc.getFileID() != Ctx->getSourceManager().getMainFileID())
621 return;
Ted Kremenekb9c31f02008-02-19 02:33:31 +0000622
Ted Kremenekcb330932008-02-18 21:21:23 +0000623 if (!Visualize) {
624 llvm::cerr << "ANALYZE: " << FD.getIdentifier()->getName() << ' '
625 << Ctx->getSourceManager().getSourceName(FD.getLocation())
626 << ' ';
627
628 llvm::Timer T("GRSimpleVals");
629 T.startTimer();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000630 unsigned size = RunGRSimpleVals(C, FD, *Ctx, Diags, false, false);
Ted Kremenekcb330932008-02-18 21:21:23 +0000631 T.stopTimer();
Ted Kremenek9dca0622008-02-19 00:22:37 +0000632 llvm::cerr << size << ' ' << T.getWallTime() << '\n';
Ted Kremenekcb330932008-02-18 21:21:23 +0000633 }
634 else {
635 llvm::cerr << '\n';
Ted Kremenekffe0f432008-03-07 22:58:01 +0000636 RunGRSimpleVals(C, FD, *Ctx, Diags, Visualize, TrimGraph);
Ted Kremenekcb330932008-02-18 21:21:23 +0000637 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000638}
639
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000640
641//===----------------------------------------------------------------------===//
642// Core Foundation Reference Counting Checker
643
644namespace {
645 class CFRefCountCheckerVisitor : public CFGVisitor {
646 Diagnostic &Diags;
647 ASTContext* Ctx;
648
649 public:
650 CFRefCountCheckerVisitor(Diagnostic &diags, const std::string& fname)
651 : CFGVisitor(fname), Diags(diags) {}
652
653 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
654 virtual void VisitCFG(CFG& C, FunctionDecl&);
655 virtual bool printFuncDeclStart() { return false; }
656 };
657} // end anonymous namespace
658
659
660ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
661 const std::string& FunctionName) {
662
663 return new CFRefCountCheckerVisitor(Diags, FunctionName);
664}
665
666void CFRefCountCheckerVisitor::VisitCFG(CFG& C, FunctionDecl& FD) {
667
668 SourceLocation Loc = FD.getLocation();
669
670 if (!Loc.isFileID() ||
671 Loc.getFileID() != Ctx->getSourceManager().getMainFileID())
672 return;
673
674 CheckCFRefCount(C, FD, *Ctx, Diags);
675}
676
Ted Kremeneke603df42008-01-08 18:04:06 +0000677//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000678// AST Serializer
679
680namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000681
682class ASTSerializer : public ASTConsumer {
683protected:
684 Diagnostic &Diags;
685 TranslationUnit TU;
686public:
687 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
688 : Diags(diags), TU(LO) {}
689
690 virtual void Initialize(ASTContext &Context) {
691 TU.setContext(&Context);
692 }
693
694 virtual void HandleTopLevelDecl(Decl *D) {
695 if (Diags.hasErrorOccurred())
696 return;
697
698 TU.AddTopLevelDecl(D);
699 }
700};
701
702class SingleFileSerializer : public ASTSerializer {
703 const llvm::sys::Path FName;
704public:
705 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
706 const LangOptions &LO)
707 : ASTSerializer(diags,LO), FName(F) {}
708
709 ~SingleFileSerializer() {
710 EmitASTBitcodeFile(TU,FName);
711 }
712};
713
714class BuildSerializer : public ASTSerializer {
715 llvm::sys::Path EmitDir;
716public:
717 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000718 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000719 : ASTSerializer(diags,LO), EmitDir(dir) {}
720
Ted Kremenek54117722007-12-20 00:34:58 +0000721 ~BuildSerializer() {
722 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
723 unsigned ID = SourceMgr.getMainFileID();
724 assert (ID && "MainFileID not set!");
725 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
726 assert (FE && "No FileEntry for main file.");
727
728 // FIXME: This is not portable to Windows.
729 // FIXME: This logic should probably be moved elsewhere later.
730
Ted Kremenekee533642007-12-20 19:47:16 +0000731 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000732
733 std::vector<char> buf;
734 buf.reserve(strlen(FE->getName())+100);
735
736 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000737 FName.appendComponent(&buf[0]);
738 FName.createDirectoryOnDisk(true);
739 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000740 assert (false && "Could not create 'device' serialization directory.");
741 return;
742 }
Ted Kremenekee533642007-12-20 19:47:16 +0000743
Ted Kremenek54117722007-12-20 00:34:58 +0000744 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000745 FName.appendComponent(&buf[0]);
746 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000747
Ted Kremenekee533642007-12-20 19:47:16 +0000748 // Now emit the sources.
749
Ted Kremenek54117722007-12-20 00:34:58 +0000750 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000751};
752
753
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000754} // end anonymous namespace
755
756
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000757ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000758 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000759 Diagnostic &Diags,
760 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000761
Ted Kremenekf06c9282007-12-19 23:49:37 +0000762 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000763 if (InFile == "-") {
764 llvm::cerr <<
765 "error: Cannot use --serialize with -o for source read from STDIN.\n";
766 return NULL;
767 }
768
Ted Kremenekf06c9282007-12-19 23:49:37 +0000769 // The user specified an AST-emission directory. Determine if the path
770 // is absolute.
771 llvm::sys::Path EmitDir(OutputFile);
772
773 if (!EmitDir.isAbsolute()) {
774 llvm::cerr <<
775 "error: Output directory for --serialize must be an absolute path.\n";
776
777 return NULL;
778 }
779
780 // Create the directory if it does not exist.
781 EmitDir.createDirectoryOnDisk(true);
782 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
783 llvm::cerr <<
784 "error: Could not create output directory for --serialize.\n";
785
786 return NULL;
787 }
788
Ted Kremenek54117722007-12-20 00:34:58 +0000789 // FIXME: We should probably only allow using BuildSerializer when
790 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000791 return new BuildSerializer(EmitDir, Diags, Features);
792 }
793
794 // The user did not specify an output directory for serialized ASTs.
795 // Serialize the translation to a single file whose name is the same
796 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000797
Ted Kremenekf06c9282007-12-19 23:49:37 +0000798 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000799 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000800 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000801}