blob: 9653526afb1417eb1541fa9396bb7fc6c0915737 [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 Kremenek4dc41cc2008-03-31 18:26:32 +000015#include "HTMLDiagnostics.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000017#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.h"
Ted Kremenekfddd5182007-08-21 21:42:03 +000022#include "clang/AST/CFG.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000023#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek055c2752007-09-06 23:00:42 +000024#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekd71ed262008-04-10 22:16:52 +000025#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
26#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000027#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000028#include "llvm/Support/Timer.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000029#include "llvm/ADT/OwningPtr.h"
30
Chris Lattner6000dac2007-08-08 22:51:59 +000031using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033//===----------------------------------------------------------------------===//
34/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000035
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000036namespace {
37 class DeclPrinter {
38 public:
Ted Kremenekea75c552007-11-28 21:32:21 +000039 std::ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000040
Chris Lattner4b1daf02008-01-10 01:43:14 +000041 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekea75c552007-11-28 21:32:21 +000042 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000043
Chris Lattneref5a85d2008-01-02 21:04:16 +000044 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000045 void PrintFunctionDeclStart(FunctionDecl *FD);
46 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000047 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000048 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
49 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
50 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
51 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
52 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
53 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
54 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +000055 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000056 };
57} // end anonymous namespace
58
Chris Lattneref5a85d2008-01-02 21:04:16 +000059void DeclPrinter:: PrintDecl(Decl *D) {
60 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
61 PrintFunctionDeclStart(FD);
62
63 if (FD->getBody()) {
64 Out << ' ';
65 FD->getBody()->printPretty(Out);
66 Out << '\n';
67 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000068 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000069 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000070 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000071 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
72 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000073 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
74 PrintObjCInterfaceDecl(OID);
75 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
76 PrintObjCProtocolDecl(PID);
77 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000078 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000079 Out << "@protocol ";
80 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000081 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000082 if (i) Out << ", ";
83 Out << D->getName();
84 }
85 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000086 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000087 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000088 PrintObjCImplementationDecl(OID);
89 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000090 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 PrintObjCCategoryImplDecl(OID);
92 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000093 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000094 PrintObjCCategoryDecl(OID);
95 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000096 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 PrintObjCCompatibleAliasDecl(OID);
98 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000099 Out << "@class [printing todo]\n";
100 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
101 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
102 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
103 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000104 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
105 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000106 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
107 Out << "asm(";
108 AD->getAsmString()->printPretty(Out);
109 Out << ")\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000110 } else {
111 assert(0 && "Unknown decl type!");
112 }
113}
114
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000115void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 bool HasBody = FD->getBody();
117
Ted Kremenekea75c552007-11-28 21:32:21 +0000118 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000119
120 switch (FD->getStorageClass()) {
121 default: assert(0 && "Unknown storage class");
122 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000123 case FunctionDecl::Extern: Out << "extern "; break;
124 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000125 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000126 }
127
128 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000129 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000132 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000133
Chris Lattner0d6ca112007-12-03 21:43:25 +0000134 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 Proto += "(";
136 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
137 if (i) Proto += ", ";
138 std::string ParamStr;
139 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
140
141 FT->getArgType(i).getAsStringInternal(ParamStr);
142 Proto += ParamStr;
143 }
144
145 if (FT->isVariadic()) {
146 if (FD->getNumParams()) Proto += ", ";
147 Proto += "...";
148 }
149 Proto += ")";
150 } else {
151 assert(isa<FunctionTypeNoProto>(AFT));
152 Proto += "()";
153 }
154
155 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000156 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000157
Chris Lattner6000dac2007-08-08 22:51:59 +0000158 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000159 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000160 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000161}
162
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000163void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 std::string S = TD->getName();
165 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000166 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000169void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
170 const char *l;
171 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
172 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000173 else {
174 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
175 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000176 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000177 }
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000178 Out << "extern \"" << l << "\" { ";
179 PrintDecl(LS->getDecl());
180 Out << "}\n";
181}
182
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000184 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000185 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000186 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000187 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000188 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000189 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000190 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000191 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000192
Chris Lattner58cce3b2008-03-16 01:07:14 +0000193 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000194 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000195 // FIXME: selector is missing here!
196 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000197 }
198}
199
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000200void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000201 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000202 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000203
204 if (SID)
205 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000206 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000207 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000208
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000209 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000210 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000211 ObjCMethodDecl *OMD = *I;
212 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000213 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000214 Out << ' ';
215 OMD->getBody()->printPretty(Out);
216 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000217 }
218 }
219
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000220 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000221 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000222 ObjCMethodDecl *OMD = *I;
223 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000224 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000225 Out << ' ';
226 OMD->getBody()->printPretty(Out);
227 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000228 }
229 }
230
Ted Kremenekea75c552007-11-28 21:32:21 +0000231 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000232}
233
234
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000235void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000236 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000237 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000238
239 if (SID)
240 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000241 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000242 Out << "@interface " << I;
243
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000244 // Protocols?
245 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000246
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000247 if (count > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000248 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000249 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000250 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000251 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000252
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000253 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000254 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000255 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000256 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000257
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000258 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000259 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000260 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000261 E = OID->ivar_end(); I != E; ++I) {
262 Out << '\t' << (*I)->getType().getAsString()
263 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000264 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000265 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000266 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000267
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000268 for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
269 E = OID->classprop_end(); I != E; ++I)
270 PrintObjCPropertyDecl(*I);
Ted Kremenekea75c552007-11-28 21:32:21 +0000271
272 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000273 // FIXME: implement the rest...
274}
275
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000276void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000277 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000278
279 for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
280 E = PID->classprop_end(); I != E; ++I)
281 PrintObjCPropertyDecl(*I);
282 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000283 // FIXME: implement the rest...
284}
285
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000286void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000287 Out << "@implementation "
288 << PID->getClassInterface()->getName()
289 << '(' << PID->getName() << ");\n";
290
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000291 // FIXME: implement the rest...
292}
293
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000294void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000295 Out << "@interface "
296 << PID->getClassInterface()->getName()
297 << '(' << PID->getName() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000298 // Output property declarations.
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000299 for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
300 E = PID->classprop_end(); I != E; ++I)
301 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000302 Out << "@end\n";
303
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000304 // FIXME: implement the rest...
305}
306
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000307void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000308 Out << "@compatibility_alias " << AID->getName()
309 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000310}
311
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000312/// PrintObjCPropertyDecl - print a property declaration.
313///
314void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
315
316 Out << "@property";
317 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
318 bool first = true;
319 Out << " (";
320 if (PDecl->getPropertyAttributes() &
321 ObjCPropertyDecl::OBJC_PR_readonly) {
322 Out << (first ? ' ' : ',') << "readonly";
323 first = false;
324 }
325
326 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
327 Out << (first ? ' ' : ',') << "getter = "
328 << PDecl->getGetterName()->getName();
329 first = false;
330 }
331 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
332 Out << (first ? ' ' : ',') << "setter = "
333 << PDecl->getSetterName()->getName();
334 first = false;
335 }
336
337 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
338 Out << (first ? ' ' : ',') << "assign";
339 first = false;
340 }
341
342 if (PDecl->getPropertyAttributes() &
343 ObjCPropertyDecl::OBJC_PR_readwrite) {
344 Out << (first ? ' ' : ',') << "readwrite";
345 first = false;
346 }
347
348 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
349 Out << (first ? ' ' : ',') << "retain";
350 first = false;
351 }
352
353 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
354 Out << (first ? ' ' : ',') << "copy";
355 first = false;
356 }
357
358 if (PDecl->getPropertyAttributes() &
359 ObjCPropertyDecl::OBJC_PR_nonatomic) {
360 Out << (first ? ' ' : ',') << "nonatomic";
361 first = false;
362 }
363 Out << " )";
364 }
365 Out << ' ' << PDecl->getType().getAsString()
366 << ' ' << PDecl->getName();
367
368 Out << ";\n";
369}
370
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000371//===----------------------------------------------------------------------===//
372/// ASTPrinter - Pretty-printer of ASTs
373
Chris Lattner3d4997d2007-09-15 23:02:28 +0000374namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000375 class ASTPrinter : public ASTConsumer, public DeclPrinter {
376 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000377 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000378
Chris Lattner3d4997d2007-09-15 23:02:28 +0000379 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000380 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000382 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000383}
Chris Lattner6000dac2007-08-08 22:51:59 +0000384
Ted Kremenekea75c552007-11-28 21:32:21 +0000385ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
386 return new ASTPrinter(out);
387}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000388
389//===----------------------------------------------------------------------===//
390/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000391
392namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000393 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000394 SourceManager *SM;
395 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000396 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000397
Ted Kremenek95041a22007-12-19 22:51:13 +0000398 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000399 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000400 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000401
402 virtual void HandleTopLevelDecl(Decl *D) {
403 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
404 PrintFunctionDeclStart(FD);
405
406 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000407 Out << '\n';
408 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000409 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000410 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000411 }
412 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
413 PrintTypeDefDecl(TD);
414 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000415 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000416 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000417 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000418 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000419 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000420 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000421 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000422 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000423 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000424 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000425 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000426 } else if (isa<FileScopeAsmDecl>(D)) {
427 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000428 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
429 Out << "Read objc method decl: '" << MD->getSelector().getName()
430 << "'\n";
431 } else if (isa<ObjCImplementationDecl>(D)) {
432 Out << "Read objc implementation decl\n";
433 }
434 else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000435 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000436 }
437 }
438 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000439}
440
Chris Lattner3d4997d2007-09-15 23:02:28 +0000441ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
442
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000443//===----------------------------------------------------------------------===//
444/// ASTViewer - AST Visualization
445
Ted Kremenek80de08f2007-09-19 21:29:43 +0000446namespace {
447 class ASTViewer : public ASTConsumer {
448 SourceManager *SM;
449 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000450 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000451 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000452 }
453
454 virtual void HandleTopLevelDecl(Decl *D) {
455 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000456 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000457
458 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000459 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000460 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000461 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000462 }
463 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000464 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
465 DeclPrinter().PrintObjCMethodDecl(MD);
466
467 if (MD->getBody()) {
468 llvm::cerr << '\n';
469 MD->getBody()->viewAST();
470 llvm::cerr << '\n';
471 }
472 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000473 }
474 };
475}
476
477ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
478
479
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000480//===----------------------------------------------------------------------===//
481// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
482// the CFGs for all function definitions.
483
484namespace {
485
Chris Lattnerc0508f92007-09-15 23:21:08 +0000486class CFGVisitor : public ASTConsumer {
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000487 std::string FName;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000488public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000489 CFGVisitor(const std::string& fname) : FName(fname) {}
490 CFGVisitor() : FName("") {}
491
Chris Lattnerc0508f92007-09-15 23:21:08 +0000492 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek63bbe532008-03-14 17:31:00 +0000493 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000494 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000495
496 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000497};
498
499} // end anonymous namespace
500
Chris Lattnerc0508f92007-09-15 23:21:08 +0000501void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000502
Ted Kremenek63bbe532008-03-14 17:31:00 +0000503 CFG *C = NULL;
504
505 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
506
507 if (!FD->getBody())
508 return;
509
510 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
511 return;
Chris Lattnerc0508f92007-09-15 23:21:08 +0000512
Ted Kremenek63bbe532008-03-14 17:31:00 +0000513 if (printFuncDeclStart()) {
514 DeclPrinter().PrintFunctionDeclStart(FD);
515 llvm::cerr << '\n';
516 }
517
518 C = CFG::buildCFG(FD->getBody());
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000519 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000520 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000521
Ted Kremenek63bbe532008-03-14 17:31:00 +0000522 if (!MD->getBody())
523 return;
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000524
525 if (FName.size() > 0 && FName != MD->getSelector().getName())
526 return;
Ted Kremenek63bbe532008-03-14 17:31:00 +0000527
528 if (printFuncDeclStart()) {
529 DeclPrinter().PrintObjCMethodDecl(MD);
530 llvm::cerr << '\n';
531 }
532
533 C = CFG::buildCFG(MD->getBody());
534 }
Ted Kremenek4102af92008-03-13 03:04:22 +0000535
536 if (C) {
Ted Kremenek63bbe532008-03-14 17:31:00 +0000537 VisitCFG(*C, *D);
Ted Kremenek4102af92008-03-13 03:04:22 +0000538 delete C;
539 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000540}
541
542//===----------------------------------------------------------------------===//
543// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
544
545namespace {
546 class CFGDumper : public CFGVisitor {
547 const bool UseGraphviz;
548 public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000549 CFGDumper(bool use_graphviz, const std::string& fname)
550 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000551
Ted Kremenek63bbe532008-03-14 17:31:00 +0000552 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000553 if (UseGraphviz)
554 C.viewCFG();
555 else
556 C.dump();
557 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000558 };
559} // end anonymous namespace
560
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000561ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
562 return new CFGDumper(ViewGraphs, FName);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000563}
564
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000565//===----------------------------------------------------------------------===//
566// AnalyzeLiveVariables - perform live variable analysis and dump results
567
568namespace {
569 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000570 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000571 public:
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000572 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
573
Ted Kremenek95041a22007-12-19 22:51:13 +0000574 virtual void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000575 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000576 }
577
Ted Kremenek63bbe532008-03-14 17:31:00 +0000578 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000579 LiveVariables L(C);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000580 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000581 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000582 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000583 };
584} // end anonymous namespace
585
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000586ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
587 return new LivenessVisitor(fname);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000588}
589
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000590//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000591// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000592
593namespace {
594 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000595 Diagnostic &Diags;
596 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000597 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000598 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000599 virtual void Initialize(ASTContext &Context) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000600 Ctx = &Context;
601 }
602
Ted Kremenek63bbe532008-03-14 17:31:00 +0000603 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000604 CheckDeadStores(C, *Ctx, Diags);
Ted Kremenekbffaa832008-01-29 05:13:23 +0000605 }
606
Ted Kremenek567a7e62007-09-07 23:54:15 +0000607 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000608 };
609} // end anonymous namespace
610
Chris Lattnerc0508f92007-09-15 23:21:08 +0000611ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
612 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000613}
Chris Lattner580980b2007-09-16 19:46:59 +0000614
615//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000616// Unitialized Values - run checker to flag potential uses of uninitalized
617// variables.
618
619namespace {
620 class UninitValsVisitor : public CFGVisitor {
621 Diagnostic &Diags;
622 ASTContext *Ctx;
623 public:
624 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000625 virtual void Initialize(ASTContext &Context) {
Ted Kremenek2bf55142007-09-17 20:49:30 +0000626 Ctx = &Context;
627 }
628
Ted Kremenek63bbe532008-03-14 17:31:00 +0000629 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekbffaa832008-01-29 05:13:23 +0000630 CheckUninitializedValues(C, *Ctx, Diags);
631 }
632
Ted Kremenek2bf55142007-09-17 20:49:30 +0000633 virtual bool printFuncDeclStart() { return false; }
634 };
635} // end anonymous namespace
636
637ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
638 return new UninitValsVisitor(Diags);
639}
640
641//===----------------------------------------------------------------------===//
Ted Kremenek47abe762008-04-16 16:39:56 +0000642// CheckerConsumer - Generic Driver for running intra-procedural path-sensitive
Ted Kremenekd71ed262008-04-10 22:16:52 +0000643// analyses.
644
645namespace {
646
647class CheckerConsumer : public CFGVisitor {
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000648protected:
Ted Kremenekd71ed262008-04-10 22:16:52 +0000649 Diagnostic &Diags;
650 ASTContext* Ctx;
Ted Kremenek47abe762008-04-16 16:39:56 +0000651 Preprocessor* PP;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000652 PreprocessorFactory* PPF;
Ted Kremenekd71ed262008-04-10 22:16:52 +0000653 const std::string& HTMLDir;
654 bool Visualize;
655 bool TrimGraph;
656 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenek55af98c2008-04-14 18:40:58 +0000657 bool AnalyzeAll;
Ted Kremenekd71ed262008-04-10 22:16:52 +0000658public:
Ted Kremenek339b9c22008-04-17 22:31:54 +0000659 CheckerConsumer(Diagnostic &diags, Preprocessor* pp, PreprocessorFactory* ppf,
Ted Kremenek47abe762008-04-16 16:39:56 +0000660 const std::string& fname,
661 const std::string& htmldir,
662 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek339b9c22008-04-17 22:31:54 +0000663 : CFGVisitor(fname), Diags(diags), PP(pp), PPF(ppf), HTMLDir(htmldir),
Ted Kremenek55af98c2008-04-14 18:40:58 +0000664 Visualize(visualize), TrimGraph(trim), AnalyzeAll(analyzeAll) {}
Ted Kremenekd71ed262008-04-10 22:16:52 +0000665
666 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
667 virtual void VisitCFG(CFG& C, Decl&);
668 virtual bool printFuncDeclStart() { return false; }
669
670 virtual const char* getCheckerName() = 0;
671 virtual GRTransferFuncs* getTransferFunctions() = 0;
672};
673} // end anonymous namespace
674
675void CheckerConsumer::VisitCFG(CFG& C, Decl& CD) {
676
677 if (Diags.hasErrorOccurred())
678 return;
679
680 SourceLocation Loc = CD.getLocation();
681
Ted Kremenek55af98c2008-04-14 18:40:58 +0000682 if (!Loc.isFileID())
683 return;
684
Ted Kremenek080c40b2008-04-14 21:14:41 +0000685 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
Ted Kremenekd71ed262008-04-10 22:16:52 +0000686 return;
687
688 // Lazily create the diagnostic client.
689
690 if (!HTMLDir.empty() && PD.get() == NULL)
Ted Kremenek339b9c22008-04-17 22:31:54 +0000691 PD.reset(CreateHTMLDiagnosticClient(HTMLDir, PP, PPF));
Ted Kremenekd71ed262008-04-10 22:16:52 +0000692
693
694 if (!Visualize) {
695
696 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
697 llvm::cerr << "ANALYZE: "
698 << Ctx->getSourceManager().getSourceName(FD->getLocation())
699 << ' '
700 << FD->getIdentifier()->getName()
701 << '\n';
702 }
703 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
704 llvm::cerr << "ANALYZE (ObjC Method): "
705 << Ctx->getSourceManager().getSourceName(MD->getLocation())
706 << " '"
707 << MD->getSelector().getName() << "'\n";
708 }
709 }
710 else
711 llvm::cerr << '\n';
712
713 // Construct the analysis engine.
714 GRExprEngine Eng(C, CD, *Ctx);
715
716 // Set base transfer functions.
717 llvm::OwningPtr<GRTransferFuncs> TF(getTransferFunctions());
718 Eng.setTransferFunctions(TF.get());
719
720 // Execute the worklist algorithm.
721 Eng.ExecuteWorkList();
722
723 // Display warnings.
724 Eng.EmitWarnings(Diags, PD.get());
725
726#ifndef NDEBUG
727 if (Visualize) Eng.ViewGraph(TrimGraph);
728#endif
729}
730
731//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +0000732// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremeneke603df42008-01-08 18:04:06 +0000733
734namespace {
Ted Kremenekd71ed262008-04-10 22:16:52 +0000735class GRSimpleValsVisitor : public CheckerConsumer {
736public:
Ted Kremenek47abe762008-04-16 16:39:56 +0000737 GRSimpleValsVisitor(Diagnostic &diags, Preprocessor* pp,
Ted Kremenek339b9c22008-04-17 22:31:54 +0000738 PreprocessorFactory* ppf,
Ted Kremenek47abe762008-04-16 16:39:56 +0000739 const std::string& fname, const std::string& htmldir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000740 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek339b9c22008-04-17 22:31:54 +0000741 : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize,
742 trim, analyzeAll) {}
Ted Kremenekd71ed262008-04-10 22:16:52 +0000743
744 virtual const char* getCheckerName() { return "GRSimpleVals"; }
745
746 virtual GRTransferFuncs* getTransferFunctions() {
747 return MakeGRSimpleValsTF();
748 }
749};
Ted Kremeneke603df42008-01-08 18:04:06 +0000750} // end anonymous namespace
751
Ted Kremenekcb330932008-02-18 21:21:23 +0000752ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
Ted Kremenek47abe762008-04-16 16:39:56 +0000753 Preprocessor* PP,
Ted Kremenek339b9c22008-04-17 22:31:54 +0000754 PreprocessorFactory* PPF,
Ted Kremenekcb330932008-02-18 21:21:23 +0000755 const std::string& FunctionName,
Ted Kremenek4dc41cc2008-03-31 18:26:32 +0000756 const std::string& HTMLDir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000757 bool Visualize, bool TrimGraph,
758 bool AnalyzeAll) {
Ted Kremenekcb330932008-02-18 21:21:23 +0000759
Ted Kremenek339b9c22008-04-17 22:31:54 +0000760 return new GRSimpleValsVisitor(Diags, PP, PPF, FunctionName, HTMLDir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000761 Visualize, TrimGraph, AnalyzeAll);
Ted Kremeneke603df42008-01-08 18:04:06 +0000762}
763
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000764
765//===----------------------------------------------------------------------===//
766// Core Foundation Reference Counting Checker
767
768namespace {
Ted Kremenekd71ed262008-04-10 22:16:52 +0000769class CFRefCountCheckerVisitor : public CheckerConsumer {
770public:
Ted Kremenek47abe762008-04-16 16:39:56 +0000771 CFRefCountCheckerVisitor(Diagnostic &diags, Preprocessor* pp,
Ted Kremenek339b9c22008-04-17 22:31:54 +0000772 PreprocessorFactory* ppf,
Ted Kremenek47abe762008-04-16 16:39:56 +0000773 const std::string& fname,
774 const std::string& htmldir,
775 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek339b9c22008-04-17 22:31:54 +0000776 : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize,
777 trim, analyzeAll) {}
Ted Kremenekd71ed262008-04-10 22:16:52 +0000778
779 virtual const char* getCheckerName() { return "CFRefCountChecker"; }
780
781 virtual GRTransferFuncs* getTransferFunctions() {
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000782 return MakeCFRefCountTF(*Ctx);
Ted Kremenekd71ed262008-04-10 22:16:52 +0000783 }
784};
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000785} // end anonymous namespace
786
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000787ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
Ted Kremenek47abe762008-04-16 16:39:56 +0000788 Preprocessor* PP,
Ted Kremenek339b9c22008-04-17 22:31:54 +0000789 PreprocessorFactory* PPF,
Ted Kremenek4dc41cc2008-03-31 18:26:32 +0000790 const std::string& FunctionName,
Ted Kremenekd71ed262008-04-10 22:16:52 +0000791 const std::string& HTMLDir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000792 bool Visualize, bool TrimGraph,
793 bool AnalyzeAll) {
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000794
Ted Kremenek339b9c22008-04-17 22:31:54 +0000795 return new CFRefCountCheckerVisitor(Diags, PP, PPF, FunctionName, HTMLDir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000796 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000797}
798
Ted Kremeneke603df42008-01-08 18:04:06 +0000799//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000800// AST Serializer
801
802namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000803
804class ASTSerializer : public ASTConsumer {
805protected:
806 Diagnostic &Diags;
807 TranslationUnit TU;
808public:
809 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
810 : Diags(diags), TU(LO) {}
811
812 virtual void Initialize(ASTContext &Context) {
813 TU.setContext(&Context);
814 }
815
816 virtual void HandleTopLevelDecl(Decl *D) {
817 if (Diags.hasErrorOccurred())
818 return;
819
820 TU.AddTopLevelDecl(D);
821 }
822};
823
824class SingleFileSerializer : public ASTSerializer {
825 const llvm::sys::Path FName;
826public:
827 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
828 const LangOptions &LO)
829 : ASTSerializer(diags,LO), FName(F) {}
830
831 ~SingleFileSerializer() {
832 EmitASTBitcodeFile(TU,FName);
833 }
834};
835
836class BuildSerializer : public ASTSerializer {
837 llvm::sys::Path EmitDir;
838public:
839 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000840 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000841 : ASTSerializer(diags,LO), EmitDir(dir) {}
842
Ted Kremenek54117722007-12-20 00:34:58 +0000843 ~BuildSerializer() {
844 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
845 unsigned ID = SourceMgr.getMainFileID();
846 assert (ID && "MainFileID not set!");
847 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
848 assert (FE && "No FileEntry for main file.");
849
850 // FIXME: This is not portable to Windows.
851 // FIXME: This logic should probably be moved elsewhere later.
852
Ted Kremenekee533642007-12-20 19:47:16 +0000853 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000854
855 std::vector<char> buf;
856 buf.reserve(strlen(FE->getName())+100);
857
858 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000859 FName.appendComponent(&buf[0]);
860 FName.createDirectoryOnDisk(true);
861 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000862 assert (false && "Could not create 'device' serialization directory.");
863 return;
864 }
Ted Kremenekee533642007-12-20 19:47:16 +0000865
Ted Kremenek54117722007-12-20 00:34:58 +0000866 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000867 FName.appendComponent(&buf[0]);
868 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000869
Ted Kremenekee533642007-12-20 19:47:16 +0000870 // Now emit the sources.
871
Ted Kremenek54117722007-12-20 00:34:58 +0000872 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000873};
874
875
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000876} // end anonymous namespace
877
878
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000879ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000880 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000881 Diagnostic &Diags,
882 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000883
Ted Kremenekf06c9282007-12-19 23:49:37 +0000884 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000885 if (InFile == "-") {
886 llvm::cerr <<
887 "error: Cannot use --serialize with -o for source read from STDIN.\n";
888 return NULL;
889 }
890
Ted Kremenekf06c9282007-12-19 23:49:37 +0000891 // The user specified an AST-emission directory. Determine if the path
892 // is absolute.
893 llvm::sys::Path EmitDir(OutputFile);
894
895 if (!EmitDir.isAbsolute()) {
896 llvm::cerr <<
897 "error: Output directory for --serialize must be an absolute path.\n";
898
899 return NULL;
900 }
901
902 // Create the directory if it does not exist.
903 EmitDir.createDirectoryOnDisk(true);
904 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
905 llvm::cerr <<
906 "error: Could not create output directory for --serialize.\n";
907
908 return NULL;
909 }
910
Ted Kremenek54117722007-12-20 00:34:58 +0000911 // FIXME: We should probably only allow using BuildSerializer when
912 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000913 return new BuildSerializer(EmitDir, Diags, Features);
914 }
915
916 // The user did not specify an output directory for serialized ASTs.
917 // Serialize the translation to a single file whose name is the same
918 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000919
Ted Kremenekf06c9282007-12-19 23:49:37 +0000920 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000921 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000922 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000923}