blob: 3f4b4e23d495c0d8ee5c9f835ccf692459083011 [file] [log] [blame]
Chris Lattnereb8c9632007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnereb8c9632007-10-07 06:04:32 +000010// AST Consumer Implementations.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnereb8c9632007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000015#include "HTMLDiagnostics.h"
Ted Kremenekac881932007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000017#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenekfc17b8a2007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/AST/AST.h"
Chris Lattnerb73abd52007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.h"
Ted Kremenek97f75312007-08-21 21:42:03 +000022#include "clang/AST/CFG.h"
Ted Kremenekcdf8e842007-12-21 21:42:19 +000023#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremeneke805c4a2007-09-06 23:00:42 +000024#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekb1983ba2008-04-10 22:16:52 +000025#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
26#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000027#include "llvm/Support/Streams.h"
Ted Kremenek0118bb52008-02-18 21:21:23 +000028#include "llvm/Support/Timer.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000029#include "llvm/ADT/OwningPtr.h"
30
Chris Lattner95578782007-08-08 22:51:59 +000031using namespace clang;
Chris Lattner4b009652007-07-25 00:24:17 +000032
Ted Kremeneke09391a2007-11-27 21:46:50 +000033//===----------------------------------------------------------------------===//
34/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner95578782007-08-08 22:51:59 +000035
Ted Kremeneke09391a2007-11-27 21:46:50 +000036namespace {
37 class DeclPrinter {
38 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000039 std::ostream& Out;
Ted Kremeneke09391a2007-11-27 21:46:50 +000040
Chris Lattner216012f2008-01-10 01:43:14 +000041 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000042 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +000043
Chris Lattner1c1aabb2008-01-02 21:04:16 +000044 void PrintDecl(Decl *D);
Ted Kremeneke09391a2007-11-27 21:46:50 +000045 void PrintFunctionDeclStart(FunctionDecl *FD);
46 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattner806a5f52008-01-12 07:05:38 +000047 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremenek42730c52008-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);
Ted Kremeneke09391a2007-11-27 21:46:50 +000055 };
56} // end anonymous namespace
57
Chris Lattner1c1aabb2008-01-02 21:04:16 +000058void DeclPrinter:: PrintDecl(Decl *D) {
59 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
60 PrintFunctionDeclStart(FD);
61
62 if (FD->getBody()) {
63 Out << ' ';
64 FD->getBody()->printPretty(Out);
65 Out << '\n';
66 }
Ted Kremenek42730c52008-01-07 19:49:32 +000067 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000068 // Do nothing, methods definitions are printed in
Ted Kremenek42730c52008-01-07 19:49:32 +000069 // PrintObjCImplementationDecl.
Chris Lattner1c1aabb2008-01-02 21:04:16 +000070 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
71 PrintTypeDefDecl(TD);
Ted Kremenek42730c52008-01-07 19:49:32 +000072 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
73 PrintObjCInterfaceDecl(OID);
74 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
75 PrintObjCProtocolDecl(PID);
76 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattner43b885f2008-02-25 21:04:36 +000077 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000078 Out << "@protocol ";
79 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +000080 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattner1c1aabb2008-01-02 21:04:16 +000081 if (i) Out << ", ";
82 Out << D->getName();
83 }
84 Out << ";\n";
Ted Kremenek42730c52008-01-07 19:49:32 +000085 } else if (ObjCImplementationDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000086 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000087 PrintObjCImplementationDecl(OID);
88 } else if (ObjCCategoryImplDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000089 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000090 PrintObjCCategoryImplDecl(OID);
91 } else if (ObjCCategoryDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000092 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000093 PrintObjCCategoryDecl(OID);
94 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000095 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000096 PrintObjCCompatibleAliasDecl(OID);
97 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000098 Out << "@class [printing todo]\n";
99 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
100 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
101 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
102 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner806a5f52008-01-12 07:05:38 +0000103 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
104 PrintLinkageSpec(LSD);
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000105 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
106 Out << "asm(";
107 AD->getAsmString()->printPretty(Out);
108 Out << ")\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000109 } else {
110 assert(0 && "Unknown decl type!");
111 }
112}
113
Ted Kremeneke09391a2007-11-27 21:46:50 +0000114void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000115 bool HasBody = FD->getBody();
116
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000117 Out << '\n';
Chris Lattner987058a2007-08-26 04:02:13 +0000118
119 switch (FD->getStorageClass()) {
120 default: assert(0 && "Unknown storage class");
121 case FunctionDecl::None: break;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000122 case FunctionDecl::Extern: Out << "extern "; break;
123 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenekc6d26e02008-04-15 03:57:09 +0000124 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner987058a2007-08-26 04:02:13 +0000125 }
126
127 if (FD->isInline())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000128 Out << "inline ";
Chris Lattner987058a2007-08-26 04:02:13 +0000129
Chris Lattner4b009652007-07-25 00:24:17 +0000130 std::string Proto = FD->getName();
Chris Lattner934fff62007-12-03 21:43:25 +0000131 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +0000132
Chris Lattner934fff62007-12-03 21:43:25 +0000133 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000134 Proto += "(";
135 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
136 if (i) Proto += ", ";
137 std::string ParamStr;
138 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
139
140 FT->getArgType(i).getAsStringInternal(ParamStr);
141 Proto += ParamStr;
142 }
143
144 if (FT->isVariadic()) {
145 if (FD->getNumParams()) Proto += ", ";
146 Proto += "...";
147 }
148 Proto += ")";
149 } else {
150 assert(isa<FunctionTypeNoProto>(AFT));
151 Proto += "()";
152 }
153
154 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000155 Out << Proto;
Chris Lattner4b009652007-07-25 00:24:17 +0000156
Chris Lattner95578782007-08-08 22:51:59 +0000157 if (!FD->getBody())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000158 Out << ";\n";
Chris Lattner95578782007-08-08 22:51:59 +0000159 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +0000160}
161
Ted Kremeneke09391a2007-11-27 21:46:50 +0000162void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000163 std::string S = TD->getName();
164 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000165 Out << "typedef " << S << ";\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000166}
167
Chris Lattner806a5f52008-01-12 07:05:38 +0000168void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
169 const char *l;
170 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
171 l = "C";
Chris Lattner690c2872008-04-08 05:52:18 +0000172 else {
173 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
174 "unknown language in linkage specification");
Chris Lattner806a5f52008-01-12 07:05:38 +0000175 l = "C++";
Chris Lattner690c2872008-04-08 05:52:18 +0000176 }
Chris Lattner806a5f52008-01-12 07:05:38 +0000177 Out << "extern \"" << l << "\" { ";
178 PrintDecl(LS->getDecl());
179 Out << "}\n";
180}
181
Ted Kremenek42730c52008-01-07 19:49:32 +0000182void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000183 if (OMD->isInstance())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000184 Out << "\n- ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000185 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000186 Out << "\n+ ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000187 if (!OMD->getResultType().isNull())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000188 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000189 // FIXME: just print original selector name!
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000190 Out << OMD->getSelector().getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000191
Chris Lattner685d7922008-03-16 01:07:14 +0000192 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000193 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000194 // FIXME: selector is missing here!
195 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000196 }
197}
198
Ted Kremenek42730c52008-01-07 19:49:32 +0000199void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000200 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000201 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000202
203 if (SID)
204 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000205 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000206 Out << "@implementation " << I;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000207
Ted Kremenek42730c52008-01-07 19:49:32 +0000208 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000209 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000210 ObjCMethodDecl *OMD = *I;
211 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000212 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000213 Out << ' ';
214 OMD->getBody()->printPretty(Out);
215 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000216 }
217 }
218
Ted Kremenek42730c52008-01-07 19:49:32 +0000219 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000220 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000221 ObjCMethodDecl *OMD = *I;
222 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000223 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000224 Out << ' ';
225 OMD->getBody()->printPretty(Out);
226 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000227 }
228 }
229
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000230 Out << "@end\n";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000231}
232
233
Ted Kremenek42730c52008-01-07 19:49:32 +0000234void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000235 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000236 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000237
238 if (SID)
239 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000240 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000241 Out << "@interface " << I;
242
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000243 // Protocols?
244 int count = OID->getNumIntfRefProtocols();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000245
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000246 if (count > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000247 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000248 for (int i = 0; i < count; i++)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000249 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000250 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000251
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000252 if (count > 0)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000253 Out << ">\n";
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000254 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000255 Out << '\n';
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000256
Chris Lattnerec4979b2008-03-16 21:08:55 +0000257 if (OID->ivar_size() > 0) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000258 Out << '{';
Ted Kremenek42730c52008-01-07 19:49:32 +0000259 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerc7b06752007-12-12 07:56:42 +0000260 E = OID->ivar_end(); I != E; ++I) {
261 Out << '\t' << (*I)->getType().getAsString()
262 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000263 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000264 Out << "}\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000265 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000266
267 int NumProperties = OID->getNumPropertyDecl();
268 if (NumProperties > 0) {
269 for (int i = 0; i < NumProperties; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000270 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000271 Out << "@property";
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000273 bool first = true;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000274 Out << " (";
Chris Lattner216012f2008-01-10 01:43:14 +0000275 if (PDecl->getPropertyAttributes() &
276 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000277 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000278 first = false;
279 }
280
Chris Lattner216012f2008-01-10 01:43:14 +0000281 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000282 Out << (first ? ' ' : ',') << "getter = "
283 << PDecl->getGetterName()->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000284 first = false;
285 }
Chris Lattner216012f2008-01-10 01:43:14 +0000286 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000287 Out << (first ? ' ' : ',') << "setter = "
288 << PDecl->getSetterName()->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000289 first = false;
290 }
291
Chris Lattner216012f2008-01-10 01:43:14 +0000292 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000293 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000294 first = false;
295 }
296
Chris Lattner216012f2008-01-10 01:43:14 +0000297 if (PDecl->getPropertyAttributes() &
298 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000299 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000300 first = false;
301 }
302
Chris Lattner216012f2008-01-10 01:43:14 +0000303 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000304 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000305 first = false;
306 }
307
Chris Lattner216012f2008-01-10 01:43:14 +0000308 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000309 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000310 first = false;
311 }
312
Chris Lattner216012f2008-01-10 01:43:14 +0000313 if (PDecl->getPropertyAttributes() &
314 ObjCPropertyDecl::OBJC_PR_nonatomic) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000315 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000316 first = false;
317 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000318 Out << " )";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000319 }
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000320 Out << ' ' << PDecl->getType().getAsString()
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000321 << ' ' << PDecl->getName();
322
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000323 Out << ";\n";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000324 }
325 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000326
327 Out << "@end\n";
Steve Narofffaed3bf2007-09-10 20:51:04 +0000328 // FIXME: implement the rest...
329}
330
Ted Kremenek42730c52008-01-07 19:49:32 +0000331void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000332 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000333 // FIXME: implement the rest...
334}
335
Ted Kremenek42730c52008-01-07 19:49:32 +0000336void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000337 Out << "@implementation "
338 << PID->getClassInterface()->getName()
339 << '(' << PID->getName() << ");\n";
340
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000341 // FIXME: implement the rest...
342}
343
Ted Kremenek42730c52008-01-07 19:49:32 +0000344void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000345 Out << "@interface "
346 << PID->getClassInterface()->getName()
347 << '(' << PID->getName() << ");\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000348 // FIXME: implement the rest...
349}
350
Ted Kremenek42730c52008-01-07 19:49:32 +0000351void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000352 Out << "@compatibility_alias " << AID->getName()
353 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000354}
355
Ted Kremeneke09391a2007-11-27 21:46:50 +0000356//===----------------------------------------------------------------------===//
357/// ASTPrinter - Pretty-printer of ASTs
358
Chris Lattnerb73abd52007-09-15 23:02:28 +0000359namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000360 class ASTPrinter : public ASTConsumer, public DeclPrinter {
361 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000362 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000363
Chris Lattnerb73abd52007-09-15 23:02:28 +0000364 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000365 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000366 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000367 };
Chris Lattner4b009652007-07-25 00:24:17 +0000368}
Chris Lattner95578782007-08-08 22:51:59 +0000369
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000370ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
371 return new ASTPrinter(out);
372}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000373
374//===----------------------------------------------------------------------===//
375/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000376
377namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000378 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000379 SourceManager *SM;
380 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000381 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000382
Ted Kremenek17861c52007-12-19 22:51:13 +0000383 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000384 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000385 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000386
387 virtual void HandleTopLevelDecl(Decl *D) {
388 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
389 PrintFunctionDeclStart(FD);
390
391 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000392 Out << '\n';
393 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000394 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000395 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000396 }
397 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
398 PrintTypeDefDecl(TD);
399 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000400 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000401 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000402 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000403 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000404 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000405 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000406 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000407 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000408 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000409 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000410 Out << "Read objc fwd class decl\n";
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000411 } else if (isa<FileScopeAsmDecl>(D)) {
412 Out << "Read file scope asm decl\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000413 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
414 Out << "Read objc method decl: '" << MD->getSelector().getName()
415 << "'\n";
416 } else if (isa<ObjCImplementationDecl>(D)) {
417 Out << "Read objc implementation decl\n";
418 }
419 else {
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000420 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000421 }
422 }
423 };
Chris Lattner95578782007-08-08 22:51:59 +0000424}
425
Chris Lattnerb73abd52007-09-15 23:02:28 +0000426ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
427
Ted Kremeneke09391a2007-11-27 21:46:50 +0000428//===----------------------------------------------------------------------===//
429/// ASTViewer - AST Visualization
430
Ted Kremenekb6976a22007-09-19 21:29:43 +0000431namespace {
432 class ASTViewer : public ASTConsumer {
433 SourceManager *SM;
434 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000435 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000436 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000437 }
438
439 virtual void HandleTopLevelDecl(Decl *D) {
440 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000441 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000442
443 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000444 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000445 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000446 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000447 }
448 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000449 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
450 DeclPrinter().PrintObjCMethodDecl(MD);
451
452 if (MD->getBody()) {
453 llvm::cerr << '\n';
454 MD->getBody()->viewAST();
455 llvm::cerr << '\n';
456 }
457 }
Ted Kremenekb6976a22007-09-19 21:29:43 +0000458 }
459 };
460}
461
462ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
463
464
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000465//===----------------------------------------------------------------------===//
466// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
467// the CFGs for all function definitions.
468
469namespace {
470
Chris Lattner52332d02007-09-15 23:21:08 +0000471class CFGVisitor : public ASTConsumer {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000472 std::string FName;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000473public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000474 CFGVisitor(const std::string& fname) : FName(fname) {}
475 CFGVisitor() : FName("") {}
476
Chris Lattner52332d02007-09-15 23:21:08 +0000477 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek5d257d42008-03-14 17:31:00 +0000478 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000479 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000480
481 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000482};
483
484} // end anonymous namespace
485
Chris Lattner52332d02007-09-15 23:21:08 +0000486void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000487
Ted Kremenek5d257d42008-03-14 17:31:00 +0000488 CFG *C = NULL;
489
490 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
491
492 if (!FD->getBody())
493 return;
494
495 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
496 return;
Chris Lattner52332d02007-09-15 23:21:08 +0000497
Ted Kremenek5d257d42008-03-14 17:31:00 +0000498 if (printFuncDeclStart()) {
499 DeclPrinter().PrintFunctionDeclStart(FD);
500 llvm::cerr << '\n';
501 }
502
503 C = CFG::buildCFG(FD->getBody());
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000504 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000505 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner52332d02007-09-15 23:21:08 +0000506
Ted Kremenek5d257d42008-03-14 17:31:00 +0000507 if (!MD->getBody())
508 return;
Ted Kremenek2f0c0e12008-03-14 18:14:50 +0000509
510 if (FName.size() > 0 && FName != MD->getSelector().getName())
511 return;
Ted Kremenek5d257d42008-03-14 17:31:00 +0000512
513 if (printFuncDeclStart()) {
514 DeclPrinter().PrintObjCMethodDecl(MD);
515 llvm::cerr << '\n';
516 }
517
518 C = CFG::buildCFG(MD->getBody());
519 }
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000520
521 if (C) {
Ted Kremenek5d257d42008-03-14 17:31:00 +0000522 VisitCFG(*C, *D);
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000523 delete C;
524 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000525}
526
527//===----------------------------------------------------------------------===//
528// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
529
530namespace {
531 class CFGDumper : public CFGVisitor {
532 const bool UseGraphviz;
533 public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000534 CFGDumper(bool use_graphviz, const std::string& fname)
535 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000536
Ted Kremenek5d257d42008-03-14 17:31:00 +0000537 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattner52332d02007-09-15 23:21:08 +0000538 if (UseGraphviz)
539 C.viewCFG();
540 else
541 C.dump();
542 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000543 };
544} // end anonymous namespace
545
Ted Kremenek83390ec2008-02-22 20:00:31 +0000546ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
547 return new CFGDumper(ViewGraphs, FName);
Ted Kremenek97f75312007-08-21 21:42:03 +0000548}
549
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000550//===----------------------------------------------------------------------===//
551// AnalyzeLiveVariables - perform live variable analysis and dump results
552
553namespace {
554 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000555 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000556 public:
Ted Kremenekb278abb2008-02-22 20:13:09 +0000557 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
558
Ted Kremenek17861c52007-12-19 22:51:13 +0000559 virtual void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000560 SM = &Context.getSourceManager();
Chris Lattner52332d02007-09-15 23:21:08 +0000561 }
562
Ted Kremenek5d257d42008-03-14 17:31:00 +0000563 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000564 LiveVariables L(C);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000565 L.runOnCFG(C);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000566 L.dumpBlockLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000567 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000568 };
569} // end anonymous namespace
570
Ted Kremenekb278abb2008-02-22 20:13:09 +0000571ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
572 return new LivenessVisitor(fname);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000573}
574
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000575//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000576// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000577
578namespace {
579 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000580 Diagnostic &Diags;
581 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000582 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000583 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000584 virtual void Initialize(ASTContext &Context) {
Chris Lattner52332d02007-09-15 23:21:08 +0000585 Ctx = &Context;
586 }
587
Ted Kremenek5d257d42008-03-14 17:31:00 +0000588 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000589 CheckDeadStores(C, *Ctx, Diags);
Ted Kremenekd03aece2008-01-29 05:13:23 +0000590 }
591
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000592 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000593 };
594} // end anonymous namespace
595
Chris Lattner52332d02007-09-15 23:21:08 +0000596ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
597 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000598}
Chris Lattner129758d2007-09-16 19:46:59 +0000599
600//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000601// Unitialized Values - run checker to flag potential uses of uninitalized
602// variables.
603
604namespace {
605 class UninitValsVisitor : public CFGVisitor {
606 Diagnostic &Diags;
607 ASTContext *Ctx;
608 public:
609 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000610 virtual void Initialize(ASTContext &Context) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000611 Ctx = &Context;
612 }
613
Ted Kremenek5d257d42008-03-14 17:31:00 +0000614 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekd03aece2008-01-29 05:13:23 +0000615 CheckUninitializedValues(C, *Ctx, Diags);
616 }
617
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000618 virtual bool printFuncDeclStart() { return false; }
619 };
620} // end anonymous namespace
621
622ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
623 return new UninitValsVisitor(Diags);
624}
625
626//===----------------------------------------------------------------------===//
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000627// CheckeRConsumer - Generic Driver for running intra-procedural path-sensitive
628// analyses.
629
630namespace {
631
632class CheckerConsumer : public CFGVisitor {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000633protected:
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000634 Diagnostic &Diags;
635 ASTContext* Ctx;
636 const std::string& HTMLDir;
637 bool Visualize;
638 bool TrimGraph;
639 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenek517cb512008-04-14 18:40:58 +0000640 bool AnalyzeAll;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000641public:
642 CheckerConsumer(Diagnostic &diags, const std::string& fname,
643 const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000644 bool visualize, bool trim, bool analyzeAll)
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000645 : CFGVisitor(fname), Diags(diags), HTMLDir(htmldir),
Ted Kremenek517cb512008-04-14 18:40:58 +0000646 Visualize(visualize), TrimGraph(trim), AnalyzeAll(analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000647
648 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
649 virtual void VisitCFG(CFG& C, Decl&);
650 virtual bool printFuncDeclStart() { return false; }
651
652 virtual const char* getCheckerName() = 0;
653 virtual GRTransferFuncs* getTransferFunctions() = 0;
654};
655} // end anonymous namespace
656
657void CheckerConsumer::VisitCFG(CFG& C, Decl& CD) {
658
659 if (Diags.hasErrorOccurred())
660 return;
661
662 SourceLocation Loc = CD.getLocation();
663
Ted Kremenek517cb512008-04-14 18:40:58 +0000664 if (!Loc.isFileID())
665 return;
666
Ted Kremenek8a11ed22008-04-14 21:14:41 +0000667 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000668 return;
669
670 // Lazily create the diagnostic client.
671
672 if (!HTMLDir.empty() && PD.get() == NULL)
673 PD.reset(CreateHTMLDiagnosticClient(HTMLDir));
674
675
676 if (!Visualize) {
677
678 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
679 llvm::cerr << "ANALYZE: "
680 << Ctx->getSourceManager().getSourceName(FD->getLocation())
681 << ' '
682 << FD->getIdentifier()->getName()
683 << '\n';
684 }
685 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
686 llvm::cerr << "ANALYZE (ObjC Method): "
687 << Ctx->getSourceManager().getSourceName(MD->getLocation())
688 << " '"
689 << MD->getSelector().getName() << "'\n";
690 }
691 }
692 else
693 llvm::cerr << '\n';
694
695 // Construct the analysis engine.
696 GRExprEngine Eng(C, CD, *Ctx);
697
698 // Set base transfer functions.
699 llvm::OwningPtr<GRTransferFuncs> TF(getTransferFunctions());
700 Eng.setTransferFunctions(TF.get());
701
702 // Execute the worklist algorithm.
703 Eng.ExecuteWorkList();
704
705 // Display warnings.
706 Eng.EmitWarnings(Diags, PD.get());
707
708#ifndef NDEBUG
709 if (Visualize) Eng.ViewGraph(TrimGraph);
710#endif
711}
712
713//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +0000714// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremenek3b451132008-01-08 18:04:06 +0000715
716namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000717class GRSimpleValsVisitor : public CheckerConsumer {
718public:
719 GRSimpleValsVisitor(Diagnostic &diags, const std::string& fname,
720 const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000721 bool visualize, bool trim, bool analyzeAll)
722 : CheckerConsumer(diags, fname, htmldir, visualize, trim, analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000723
724 virtual const char* getCheckerName() { return "GRSimpleVals"; }
725
726 virtual GRTransferFuncs* getTransferFunctions() {
727 return MakeGRSimpleValsTF();
728 }
729};
Ted Kremenek3b451132008-01-08 18:04:06 +0000730} // end anonymous namespace
731
Ted Kremenek0118bb52008-02-18 21:21:23 +0000732ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
733 const std::string& FunctionName,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000734 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000735 bool Visualize, bool TrimGraph,
736 bool AnalyzeAll) {
Ted Kremenek0118bb52008-02-18 21:21:23 +0000737
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000738 return new GRSimpleValsVisitor(Diags, FunctionName, HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000739 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek3b451132008-01-08 18:04:06 +0000740}
741
Ted Kremenek827f93b2008-03-06 00:08:09 +0000742
743//===----------------------------------------------------------------------===//
744// Core Foundation Reference Counting Checker
745
746namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000747class CFRefCountCheckerVisitor : public CheckerConsumer {
748public:
749 CFRefCountCheckerVisitor(Diagnostic &diags, const std::string& fname,
750 const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000751 bool visualize, bool trim, bool analyzeAll)
752 : CheckerConsumer(diags, fname, htmldir, visualize, trim, analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000753
754 virtual const char* getCheckerName() { return "CFRefCountChecker"; }
755
756 virtual GRTransferFuncs* getTransferFunctions() {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000757 return MakeCFRefCountTF(*Ctx);
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000758 }
759};
Ted Kremenek827f93b2008-03-06 00:08:09 +0000760} // end anonymous namespace
761
Ted Kremenek827f93b2008-03-06 00:08:09 +0000762ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000763 const std::string& FunctionName,
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000764 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000765 bool Visualize, bool TrimGraph,
766 bool AnalyzeAll) {
Ted Kremenek827f93b2008-03-06 00:08:09 +0000767
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000768 return new CFRefCountCheckerVisitor(Diags, FunctionName, HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000769 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek827f93b2008-03-06 00:08:09 +0000770}
771
Ted Kremenek3b451132008-01-08 18:04:06 +0000772//===----------------------------------------------------------------------===//
Ted Kremenek397de012007-12-13 00:37:31 +0000773// AST Serializer
774
775namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000776
777class ASTSerializer : public ASTConsumer {
778protected:
779 Diagnostic &Diags;
780 TranslationUnit TU;
781public:
782 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
783 : Diags(diags), TU(LO) {}
784
785 virtual void Initialize(ASTContext &Context) {
786 TU.setContext(&Context);
787 }
788
789 virtual void HandleTopLevelDecl(Decl *D) {
790 if (Diags.hasErrorOccurred())
791 return;
792
793 TU.AddTopLevelDecl(D);
794 }
795};
796
797class SingleFileSerializer : public ASTSerializer {
798 const llvm::sys::Path FName;
799public:
800 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
801 const LangOptions &LO)
802 : ASTSerializer(diags,LO), FName(F) {}
803
804 ~SingleFileSerializer() {
805 EmitASTBitcodeFile(TU,FName);
806 }
807};
808
809class BuildSerializer : public ASTSerializer {
810 llvm::sys::Path EmitDir;
811public:
812 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremenek397de012007-12-13 00:37:31 +0000813 const LangOptions &LO)
Ted Kremenek21189012007-12-19 23:49:37 +0000814 : ASTSerializer(diags,LO), EmitDir(dir) {}
815
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000816 ~BuildSerializer() {
817 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
818 unsigned ID = SourceMgr.getMainFileID();
819 assert (ID && "MainFileID not set!");
820 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
821 assert (FE && "No FileEntry for main file.");
822
823 // FIXME: This is not portable to Windows.
824 // FIXME: This logic should probably be moved elsewhere later.
825
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000826 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000827
828 std::vector<char> buf;
829 buf.reserve(strlen(FE->getName())+100);
830
831 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000832 FName.appendComponent(&buf[0]);
833 FName.createDirectoryOnDisk(true);
834 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000835 assert (false && "Could not create 'device' serialization directory.");
836 return;
837 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000838
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000839 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000840 FName.appendComponent(&buf[0]);
841 EmitASTBitcodeFile(TU,FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000842
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000843 // Now emit the sources.
844
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000845 }
Ted Kremenek21189012007-12-19 23:49:37 +0000846};
847
848
Ted Kremenek397de012007-12-13 00:37:31 +0000849} // end anonymous namespace
850
851
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000852ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000853 const std::string& OutputFile,
Ted Kremenek397de012007-12-13 00:37:31 +0000854 Diagnostic &Diags,
855 const LangOptions &Features) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000856
Ted Kremenek21189012007-12-19 23:49:37 +0000857 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000858 if (InFile == "-") {
859 llvm::cerr <<
860 "error: Cannot use --serialize with -o for source read from STDIN.\n";
861 return NULL;
862 }
863
Ted Kremenek21189012007-12-19 23:49:37 +0000864 // The user specified an AST-emission directory. Determine if the path
865 // is absolute.
866 llvm::sys::Path EmitDir(OutputFile);
867
868 if (!EmitDir.isAbsolute()) {
869 llvm::cerr <<
870 "error: Output directory for --serialize must be an absolute path.\n";
871
872 return NULL;
873 }
874
875 // Create the directory if it does not exist.
876 EmitDir.createDirectoryOnDisk(true);
877 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
878 llvm::cerr <<
879 "error: Could not create output directory for --serialize.\n";
880
881 return NULL;
882 }
883
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000884 // FIXME: We should probably only allow using BuildSerializer when
885 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenek21189012007-12-19 23:49:37 +0000886 return new BuildSerializer(EmitDir, Diags, Features);
887 }
888
889 // The user did not specify an output directory for serialized ASTs.
890 // Serialize the translation to a single file whose name is the same
891 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000892
Ted Kremenek21189012007-12-19 23:49:37 +0000893 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000894 FName.appendSuffix("ast");
Ted Kremenek21189012007-12-19 23:49:37 +0000895 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremenek397de012007-12-13 00:37:31 +0000896}