blob: 1383a5ed5d2811fe52982fd2c0cf558cff8e11ee [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 Kremenek2f83f792008-06-20 21:55:29 +000023#include "clang/AST/ParentMap.h"
Ted Kremenekcdf8e842007-12-21 21:42:19 +000024#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremeneke805c4a2007-09-06 23:00:42 +000025#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekb1983ba2008-04-10 22:16:52 +000026#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
27#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000028#include "llvm/Support/Streams.h"
Ted Kremenek0118bb52008-02-18 21:21:23 +000029#include "llvm/Support/Timer.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000030#include "llvm/ADT/OwningPtr.h"
31
Chris Lattner95578782007-08-08 22:51:59 +000032using namespace clang;
Chris Lattner4b009652007-07-25 00:24:17 +000033
Ted Kremeneke09391a2007-11-27 21:46:50 +000034//===----------------------------------------------------------------------===//
35/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner95578782007-08-08 22:51:59 +000036
Ted Kremeneke09391a2007-11-27 21:46:50 +000037namespace {
38 class DeclPrinter {
39 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000040 std::ostream& Out;
Ted Kremeneke09391a2007-11-27 21:46:50 +000041
Chris Lattner216012f2008-01-10 01:43:14 +000042 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000043 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +000044
Chris Lattner1c1aabb2008-01-02 21:04:16 +000045 void PrintDecl(Decl *D);
Ted Kremeneke09391a2007-11-27 21:46:50 +000046 void PrintFunctionDeclStart(FunctionDecl *FD);
47 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattner806a5f52008-01-12 07:05:38 +000048 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremenek42730c52008-01-07 19:49:32 +000049 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
50 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
51 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
52 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
53 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
54 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
55 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +000056 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +000057 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Ted Kremeneke09391a2007-11-27 21:46:50 +000058 };
59} // end anonymous namespace
60
Chris Lattner1c1aabb2008-01-02 21:04:16 +000061void DeclPrinter:: PrintDecl(Decl *D) {
62 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
63 PrintFunctionDeclStart(FD);
64
65 if (FD->getBody()) {
66 Out << ' ';
67 FD->getBody()->printPretty(Out);
68 Out << '\n';
69 }
Ted Kremenek42730c52008-01-07 19:49:32 +000070 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000071 // Do nothing, methods definitions are printed in
Ted Kremenek42730c52008-01-07 19:49:32 +000072 // PrintObjCImplementationDecl.
Chris Lattner1c1aabb2008-01-02 21:04:16 +000073 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
74 PrintTypeDefDecl(TD);
Ted Kremenek42730c52008-01-07 19:49:32 +000075 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
76 PrintObjCInterfaceDecl(OID);
77 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
78 PrintObjCProtocolDecl(PID);
79 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattner43b885f2008-02-25 21:04:36 +000080 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000081 Out << "@protocol ";
82 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +000083 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattner1c1aabb2008-01-02 21:04:16 +000084 if (i) Out << ", ";
85 Out << D->getName();
86 }
87 Out << ";\n";
Ted Kremenek42730c52008-01-07 19:49:32 +000088 } else if (ObjCImplementationDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000089 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000090 PrintObjCImplementationDecl(OID);
91 } else if (ObjCCategoryImplDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000092 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000093 PrintObjCCategoryImplDecl(OID);
94 } else if (ObjCCategoryDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000095 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000096 PrintObjCCategoryDecl(OID);
97 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000098 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000099 PrintObjCCompatibleAliasDecl(OID);
Chris Lattnere1349142008-06-21 21:40:20 +0000100 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
101 Out << "@class ";
102 ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
103 for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
104 const ObjCInterfaceDecl *D = ForwardDecls[i];
105 if (i) Out << ", ";
106 Out << D->getName();
107 }
108 Out << ";\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000109 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
110 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
111 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
112 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner806a5f52008-01-12 07:05:38 +0000113 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
114 PrintLinkageSpec(LSD);
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000115 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
116 Out << "asm(";
117 AD->getAsmString()->printPretty(Out);
118 Out << ")\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000119 } else {
120 assert(0 && "Unknown decl type!");
121 }
122}
123
Ted Kremeneke09391a2007-11-27 21:46:50 +0000124void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 bool HasBody = FD->getBody();
126
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000127 Out << '\n';
Chris Lattner987058a2007-08-26 04:02:13 +0000128
129 switch (FD->getStorageClass()) {
130 default: assert(0 && "Unknown storage class");
131 case FunctionDecl::None: break;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000132 case FunctionDecl::Extern: Out << "extern "; break;
133 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenekc6d26e02008-04-15 03:57:09 +0000134 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner987058a2007-08-26 04:02:13 +0000135 }
136
137 if (FD->isInline())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000138 Out << "inline ";
Chris Lattner987058a2007-08-26 04:02:13 +0000139
Chris Lattner4b009652007-07-25 00:24:17 +0000140 std::string Proto = FD->getName();
Chris Lattner934fff62007-12-03 21:43:25 +0000141 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +0000142
Chris Lattner934fff62007-12-03 21:43:25 +0000143 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000144 Proto += "(";
145 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
146 if (i) Proto += ", ";
147 std::string ParamStr;
148 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
149
150 FT->getArgType(i).getAsStringInternal(ParamStr);
151 Proto += ParamStr;
152 }
153
154 if (FT->isVariadic()) {
155 if (FD->getNumParams()) Proto += ", ";
156 Proto += "...";
157 }
158 Proto += ")";
159 } else {
160 assert(isa<FunctionTypeNoProto>(AFT));
161 Proto += "()";
162 }
163
164 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000165 Out << Proto;
Chris Lattner4b009652007-07-25 00:24:17 +0000166
Chris Lattner95578782007-08-08 22:51:59 +0000167 if (!FD->getBody())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000168 Out << ";\n";
Chris Lattner95578782007-08-08 22:51:59 +0000169 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +0000170}
171
Ted Kremeneke09391a2007-11-27 21:46:50 +0000172void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000173 std::string S = TD->getName();
174 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000175 Out << "typedef " << S << ";\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000176}
177
Chris Lattner806a5f52008-01-12 07:05:38 +0000178void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
179 const char *l;
180 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
181 l = "C";
Chris Lattner690c2872008-04-08 05:52:18 +0000182 else {
183 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
184 "unknown language in linkage specification");
Chris Lattner806a5f52008-01-12 07:05:38 +0000185 l = "C++";
Chris Lattner690c2872008-04-08 05:52:18 +0000186 }
Chris Lattner806a5f52008-01-12 07:05:38 +0000187 Out << "extern \"" << l << "\" { ";
188 PrintDecl(LS->getDecl());
189 Out << "}\n";
190}
191
Ted Kremenek42730c52008-01-07 19:49:32 +0000192void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000193 if (OMD->isInstance())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000194 Out << "\n- ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000195 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000196 Out << "\n+ ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000197 if (!OMD->getResultType().isNull())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000198 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000199 // FIXME: just print original selector name!
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000200 Out << OMD->getSelector().getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000201
Chris Lattner685d7922008-03-16 01:07:14 +0000202 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000203 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000204 // FIXME: selector is missing here!
205 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000206 }
207}
208
Ted Kremenek42730c52008-01-07 19:49:32 +0000209void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000210 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000211 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000212
213 if (SID)
214 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000215 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000216 Out << "@implementation " << I;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000217
Ted Kremenek42730c52008-01-07 19:49:32 +0000218 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000219 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000220 ObjCMethodDecl *OMD = *I;
221 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000222 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000223 Out << ' ';
224 OMD->getBody()->printPretty(Out);
225 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000226 }
227 }
228
Ted Kremenek42730c52008-01-07 19:49:32 +0000229 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000230 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000231 ObjCMethodDecl *OMD = *I;
232 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000233 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000234 Out << ' ';
235 OMD->getBody()->printPretty(Out);
236 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000237 }
238 }
239
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000240 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
241 E = OID->propimpl_end(); I != E; ++I)
242 PrintObjCPropertyImplDecl(*I);
243
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000244 Out << "@end\n";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000245}
246
247
Ted Kremenek42730c52008-01-07 19:49:32 +0000248void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000249 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000250 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000251
252 if (SID)
253 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000254 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000255 Out << "@interface " << I;
256
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000257 // Protocols?
258 int count = OID->getNumIntfRefProtocols();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000259
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000260 if (count > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000261 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000262 for (int i = 0; i < count; i++)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000263 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000264 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000265
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000266 if (count > 0)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000267 Out << ">\n";
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000268 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000269 Out << '\n';
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000270
Chris Lattnerec4979b2008-03-16 21:08:55 +0000271 if (OID->ivar_size() > 0) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000272 Out << '{';
Ted Kremenek42730c52008-01-07 19:49:32 +0000273 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerc7b06752007-12-12 07:56:42 +0000274 E = OID->ivar_end(); I != E; ++I) {
275 Out << '\t' << (*I)->getType().getAsString()
276 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000277 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000278 Out << "}\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000279 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000280
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000281 for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
282 E = OID->classprop_end(); I != E; ++I)
283 PrintObjCPropertyDecl(*I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000284 bool eol_needed = false;
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000285 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
286 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000287 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000288
289 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
290 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000291 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000292
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000293 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Narofffaed3bf2007-09-10 20:51:04 +0000294 // FIXME: implement the rest...
295}
296
Ted Kremenek42730c52008-01-07 19:49:32 +0000297void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000298 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000299
300 for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
301 E = PID->classprop_end(); I != E; ++I)
302 PrintObjCPropertyDecl(*I);
303 Out << "@end\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000304 // FIXME: implement the rest...
305}
306
Ted Kremenek42730c52008-01-07 19:49:32 +0000307void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000308 Out << "@implementation "
309 << PID->getClassInterface()->getName()
310 << '(' << PID->getName() << ");\n";
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000311 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
312 E = PID->propimpl_end(); I != E; ++I)
313 PrintObjCPropertyImplDecl(*I);
314 Out << "@end\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000315 // FIXME: implement the rest...
316}
317
Ted Kremenek42730c52008-01-07 19:49:32 +0000318void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000319 Out << "@interface "
320 << PID->getClassInterface()->getName()
321 << '(' << PID->getName() << ");\n";
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000322 // Output property declarations.
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000323 for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
324 E = PID->classprop_end(); I != E; ++I)
325 PrintObjCPropertyDecl(*I);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000326 Out << "@end\n";
327
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000328 // FIXME: implement the rest...
329}
330
Ted Kremenek42730c52008-01-07 19:49:32 +0000331void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000332 Out << "@compatibility_alias " << AID->getName()
333 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000334}
335
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000336/// PrintObjCPropertyDecl - print a property declaration.
337///
338void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000339 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
340 Out << "@required\n";
341 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
342 Out << "@optional\n";
343
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000344 Out << "@property";
345 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
346 bool first = true;
347 Out << " (";
348 if (PDecl->getPropertyAttributes() &
349 ObjCPropertyDecl::OBJC_PR_readonly) {
350 Out << (first ? ' ' : ',') << "readonly";
351 first = false;
352 }
353
354 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
355 Out << (first ? ' ' : ',') << "getter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000356 << PDecl->getGetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000357 first = false;
358 }
359 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
360 Out << (first ? ' ' : ',') << "setter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000361 << PDecl->getSetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000362 first = false;
363 }
364
365 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
366 Out << (first ? ' ' : ',') << "assign";
367 first = false;
368 }
369
370 if (PDecl->getPropertyAttributes() &
371 ObjCPropertyDecl::OBJC_PR_readwrite) {
372 Out << (first ? ' ' : ',') << "readwrite";
373 first = false;
374 }
375
376 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
377 Out << (first ? ' ' : ',') << "retain";
378 first = false;
379 }
380
381 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
382 Out << (first ? ' ' : ',') << "copy";
383 first = false;
384 }
385
386 if (PDecl->getPropertyAttributes() &
387 ObjCPropertyDecl::OBJC_PR_nonatomic) {
388 Out << (first ? ' ' : ',') << "nonatomic";
389 first = false;
390 }
391 Out << " )";
392 }
393 Out << ' ' << PDecl->getType().getAsString()
394 << ' ' << PDecl->getName();
395
396 Out << ";\n";
397}
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000398
399/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
400/// declaration syntax.
401///
402void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
403 if (PID->getPropertyImplementation() ==
404 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE)
405 Out << "\n@synthesize ";
406 else
407 Out << "\n@dynamic ";
408 Out << PID->getPropertyDecl()->getName();
409 if (PID->getPropertyIvarDecl())
410 Out << "=" << PID->getPropertyIvarDecl()->getName();
411 Out << ";\n";
412}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000413//===----------------------------------------------------------------------===//
414/// ASTPrinter - Pretty-printer of ASTs
415
Chris Lattnerb73abd52007-09-15 23:02:28 +0000416namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000417 class ASTPrinter : public ASTConsumer, public DeclPrinter {
418 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000419 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000420
Chris Lattnerb73abd52007-09-15 23:02:28 +0000421 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000422 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000423 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000424 };
Chris Lattner4b009652007-07-25 00:24:17 +0000425}
Chris Lattner95578782007-08-08 22:51:59 +0000426
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000427ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
428 return new ASTPrinter(out);
429}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000430
431//===----------------------------------------------------------------------===//
432/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000433
434namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000435 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000436 SourceManager *SM;
437 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000438 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000439
Ted Kremenek17861c52007-12-19 22:51:13 +0000440 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000441 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000442 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000443
444 virtual void HandleTopLevelDecl(Decl *D) {
445 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
446 PrintFunctionDeclStart(FD);
447
448 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000449 Out << '\n';
450 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000451 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000452 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000453 }
454 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
455 PrintTypeDefDecl(TD);
456 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000457 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000458 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000459 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000460 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000461 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000462 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000463 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000464 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000465 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000466 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000467 Out << "Read objc fwd class decl\n";
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000468 } else if (isa<FileScopeAsmDecl>(D)) {
469 Out << "Read file scope asm decl\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000470 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
471 Out << "Read objc method decl: '" << MD->getSelector().getName()
472 << "'\n";
Steve Naroff045e0e02008-05-23 18:50:58 +0000473 if (MD->getBody()) {
474 // FIXME: convert dumper to use std::ostream?
475 MD->getBody()->dumpAll(*SM);
476 Out << '\n';
477 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000478 } else if (isa<ObjCImplementationDecl>(D)) {
479 Out << "Read objc implementation decl\n";
480 }
481 else {
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000482 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000483 }
484 }
485 };
Chris Lattner95578782007-08-08 22:51:59 +0000486}
487
Chris Lattnerb73abd52007-09-15 23:02:28 +0000488ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
489
Ted Kremeneke09391a2007-11-27 21:46:50 +0000490//===----------------------------------------------------------------------===//
491/// ASTViewer - AST Visualization
492
Ted Kremenekb6976a22007-09-19 21:29:43 +0000493namespace {
494 class ASTViewer : public ASTConsumer {
495 SourceManager *SM;
496 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000497 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000498 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000499 }
500
501 virtual void HandleTopLevelDecl(Decl *D) {
502 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000503 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000504
505 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000506 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000507 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000508 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000509 }
510 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000511 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
512 DeclPrinter().PrintObjCMethodDecl(MD);
513
514 if (MD->getBody()) {
515 llvm::cerr << '\n';
516 MD->getBody()->viewAST();
517 llvm::cerr << '\n';
518 }
519 }
Ted Kremenekb6976a22007-09-19 21:29:43 +0000520 }
521 };
522}
523
524ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
525
526
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000527//===----------------------------------------------------------------------===//
528// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
529// the CFGs for all function definitions.
530
531namespace {
532
Chris Lattner52332d02007-09-15 23:21:08 +0000533class CFGVisitor : public ASTConsumer {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000534 std::string FName;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000535public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000536 CFGVisitor(const std::string& fname) : FName(fname) {}
537 CFGVisitor() : FName("") {}
538
Chris Lattner52332d02007-09-15 23:21:08 +0000539 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek5d257d42008-03-14 17:31:00 +0000540 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000541 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000542
543 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000544};
545
546} // end anonymous namespace
547
Chris Lattner52332d02007-09-15 23:21:08 +0000548void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000549
Ted Kremenek5d257d42008-03-14 17:31:00 +0000550 CFG *C = NULL;
551
552 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
553
554 if (!FD->getBody())
555 return;
556
557 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
558 return;
Chris Lattner52332d02007-09-15 23:21:08 +0000559
Ted Kremenek5d257d42008-03-14 17:31:00 +0000560 if (printFuncDeclStart()) {
561 DeclPrinter().PrintFunctionDeclStart(FD);
562 llvm::cerr << '\n';
563 }
564
565 C = CFG::buildCFG(FD->getBody());
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000566 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000567 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner52332d02007-09-15 23:21:08 +0000568
Ted Kremenek5d257d42008-03-14 17:31:00 +0000569 if (!MD->getBody())
570 return;
Ted Kremenek2f0c0e12008-03-14 18:14:50 +0000571
572 if (FName.size() > 0 && FName != MD->getSelector().getName())
573 return;
Ted Kremenek5d257d42008-03-14 17:31:00 +0000574
575 if (printFuncDeclStart()) {
576 DeclPrinter().PrintObjCMethodDecl(MD);
577 llvm::cerr << '\n';
578 }
579
580 C = CFG::buildCFG(MD->getBody());
581 }
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000582
583 if (C) {
Ted Kremenek5d257d42008-03-14 17:31:00 +0000584 VisitCFG(*C, *D);
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000585 delete C;
586 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000587}
588
589//===----------------------------------------------------------------------===//
590// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
591
592namespace {
593 class CFGDumper : public CFGVisitor {
594 const bool UseGraphviz;
595 public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000596 CFGDumper(bool use_graphviz, const std::string& fname)
597 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000598
Ted Kremenek5d257d42008-03-14 17:31:00 +0000599 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattner52332d02007-09-15 23:21:08 +0000600 if (UseGraphviz)
601 C.viewCFG();
602 else
603 C.dump();
604 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000605 };
606} // end anonymous namespace
607
Ted Kremenek83390ec2008-02-22 20:00:31 +0000608ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
609 return new CFGDumper(ViewGraphs, FName);
Ted Kremenek97f75312007-08-21 21:42:03 +0000610}
611
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000612//===----------------------------------------------------------------------===//
613// AnalyzeLiveVariables - perform live variable analysis and dump results
614
615namespace {
616 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000617 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000618 public:
Ted Kremenekb278abb2008-02-22 20:13:09 +0000619 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
620
Ted Kremenek17861c52007-12-19 22:51:13 +0000621 virtual void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000622 SM = &Context.getSourceManager();
Chris Lattner52332d02007-09-15 23:21:08 +0000623 }
624
Ted Kremenek5d257d42008-03-14 17:31:00 +0000625 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000626 LiveVariables L(C);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000627 L.runOnCFG(C);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000628 L.dumpBlockLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000629 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000630 };
631} // end anonymous namespace
632
Ted Kremenekb278abb2008-02-22 20:13:09 +0000633ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
634 return new LivenessVisitor(fname);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000635}
636
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000637//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000638// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000639
640namespace {
641 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000642 Diagnostic &Diags;
643 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000644 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000645 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000646 virtual void Initialize(ASTContext &Context) {
Chris Lattner52332d02007-09-15 23:21:08 +0000647 Ctx = &Context;
648 }
649
Ted Kremenek5d257d42008-03-14 17:31:00 +0000650 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek2f83f792008-06-20 21:55:29 +0000651 llvm::OwningPtr<ParentMap> PM(new ParentMap(CD.getCodeBody()));
652 CheckDeadStores(C, *Ctx, *PM, Diags);
Ted Kremenekd03aece2008-01-29 05:13:23 +0000653 }
654
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000655 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000656 };
657} // end anonymous namespace
658
Chris Lattner52332d02007-09-15 23:21:08 +0000659ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
660 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000661}
Chris Lattner129758d2007-09-16 19:46:59 +0000662
663//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000664// Unitialized Values - run checker to flag potential uses of uninitalized
665// variables.
666
667namespace {
668 class UninitValsVisitor : public CFGVisitor {
669 Diagnostic &Diags;
670 ASTContext *Ctx;
671 public:
672 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000673 virtual void Initialize(ASTContext &Context) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000674 Ctx = &Context;
675 }
676
Ted Kremenek5d257d42008-03-14 17:31:00 +0000677 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekd03aece2008-01-29 05:13:23 +0000678 CheckUninitializedValues(C, *Ctx, Diags);
679 }
680
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000681 virtual bool printFuncDeclStart() { return false; }
682 };
683} // end anonymous namespace
684
685ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
686 return new UninitValsVisitor(Diags);
687}
688
689//===----------------------------------------------------------------------===//
Ted Kremenekfe82a542008-04-16 16:39:56 +0000690// CheckerConsumer - Generic Driver for running intra-procedural path-sensitive
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000691// analyses.
692
693namespace {
694
695class CheckerConsumer : public CFGVisitor {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000696protected:
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000697 Diagnostic &Diags;
698 ASTContext* Ctx;
Ted Kremenekfe82a542008-04-16 16:39:56 +0000699 Preprocessor* PP;
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000700 PreprocessorFactory* PPF;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000701 const std::string& HTMLDir;
702 bool Visualize;
703 bool TrimGraph;
704 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenek517cb512008-04-14 18:40:58 +0000705 bool AnalyzeAll;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000706public:
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000707 CheckerConsumer(Diagnostic &diags, Preprocessor* pp, PreprocessorFactory* ppf,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000708 const std::string& fname,
709 const std::string& htmldir,
710 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000711 : CFGVisitor(fname), Diags(diags), PP(pp), PPF(ppf), HTMLDir(htmldir),
Ted Kremenek517cb512008-04-14 18:40:58 +0000712 Visualize(visualize), TrimGraph(trim), AnalyzeAll(analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000713
714 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
715 virtual void VisitCFG(CFG& C, Decl&);
716 virtual bool printFuncDeclStart() { return false; }
717
718 virtual const char* getCheckerName() = 0;
Ted Kremenek102d42e2008-04-29 05:13:59 +0000719 virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) = 0;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000720};
721} // end anonymous namespace
722
723void CheckerConsumer::VisitCFG(CFG& C, Decl& CD) {
724
725 if (Diags.hasErrorOccurred())
726 return;
727
728 SourceLocation Loc = CD.getLocation();
729
Ted Kremenek517cb512008-04-14 18:40:58 +0000730 if (!Loc.isFileID())
731 return;
732
Ted Kremenek8a11ed22008-04-14 21:14:41 +0000733 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000734 return;
735
736 // Lazily create the diagnostic client.
737
738 if (!HTMLDir.empty() && PD.get() == NULL)
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000739 PD.reset(CreateHTMLDiagnosticClient(HTMLDir, PP, PPF));
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000740
741
742 if (!Visualize) {
743
744 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
745 llvm::cerr << "ANALYZE: "
746 << Ctx->getSourceManager().getSourceName(FD->getLocation())
747 << ' '
748 << FD->getIdentifier()->getName()
749 << '\n';
750 }
751 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
752 llvm::cerr << "ANALYZE (ObjC Method): "
753 << Ctx->getSourceManager().getSourceName(MD->getLocation())
754 << " '"
755 << MD->getSelector().getName() << "'\n";
756 }
757 }
758 else
759 llvm::cerr << '\n';
760
Ted Kremenek102d42e2008-04-29 05:13:59 +0000761 std::vector<GRTransferFuncs*> TFs;
762 getTransferFunctions(TFs);
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000763
Ted Kremenek102d42e2008-04-29 05:13:59 +0000764 while (!TFs.empty()) {
765
766 // Construct the analysis engine.
767 GRExprEngine Eng(C, CD, *Ctx);
768
769 // Set base transfer functions.
770 llvm::OwningPtr<GRTransferFuncs> TF(TFs.back());
771 TFs.pop_back();
772
773 Eng.setTransferFunctions(TF.get());
774
775 // Execute the worklist algorithm.
776 Eng.ExecuteWorkList();
777
778 // Display warnings.
779 Eng.EmitWarnings(Diags, PD.get());
780
781 #ifndef NDEBUG
782 if (Visualize) Eng.ViewGraph(TrimGraph);
783 #endif
784 }
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000785}
786
787//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +0000788// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremenek3b451132008-01-08 18:04:06 +0000789
790namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000791class GRSimpleValsVisitor : public CheckerConsumer {
792public:
Ted Kremenekfe82a542008-04-16 16:39:56 +0000793 GRSimpleValsVisitor(Diagnostic &diags, Preprocessor* pp,
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000794 PreprocessorFactory* ppf,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000795 const std::string& fname, const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000796 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000797 : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize,
798 trim, analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000799
800 virtual const char* getCheckerName() { return "GRSimpleVals"; }
801
Ted Kremenek102d42e2008-04-29 05:13:59 +0000802 virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) {
803 return TFs.push_back(MakeGRSimpleValsTF());
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000804 }
805};
Ted Kremenek3b451132008-01-08 18:04:06 +0000806} // end anonymous namespace
807
Ted Kremenek0118bb52008-02-18 21:21:23 +0000808ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000809 Preprocessor* PP,
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000810 PreprocessorFactory* PPF,
Ted Kremenek0118bb52008-02-18 21:21:23 +0000811 const std::string& FunctionName,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000812 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000813 bool Visualize, bool TrimGraph,
814 bool AnalyzeAll) {
Ted Kremenek0118bb52008-02-18 21:21:23 +0000815
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000816 return new GRSimpleValsVisitor(Diags, PP, PPF, FunctionName, HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000817 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek3b451132008-01-08 18:04:06 +0000818}
819
Ted Kremenek827f93b2008-03-06 00:08:09 +0000820
821//===----------------------------------------------------------------------===//
822// Core Foundation Reference Counting Checker
823
824namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000825class CFRefCountCheckerVisitor : public CheckerConsumer {
Ted Kremenek102d42e2008-04-29 05:13:59 +0000826 const LangOptions& LangOpts;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000827public:
Ted Kremenekfe82a542008-04-16 16:39:56 +0000828 CFRefCountCheckerVisitor(Diagnostic &diags, Preprocessor* pp,
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000829 PreprocessorFactory* ppf,
Ted Kremenek102d42e2008-04-29 05:13:59 +0000830 const LangOptions& lopts,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000831 const std::string& fname,
832 const std::string& htmldir,
833 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000834 : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize,
Ted Kremenek102d42e2008-04-29 05:13:59 +0000835 trim, analyzeAll), LangOpts(lopts) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000836
837 virtual const char* getCheckerName() { return "CFRefCountChecker"; }
838
Ted Kremenek102d42e2008-04-29 05:13:59 +0000839 virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) {
840 switch (LangOpts.getGCMode()) {
841 case LangOptions::NonGC:
Ted Kremenek2f62f352008-05-02 18:01:49 +0000842 TFs.push_back(MakeCFRefCountTF(*Ctx, false, true, LangOpts));
Ted Kremenek102d42e2008-04-29 05:13:59 +0000843 break;
844
845 case LangOptions::GCOnly:
Ted Kremenek2f62f352008-05-02 18:01:49 +0000846 TFs.push_back(MakeCFRefCountTF(*Ctx, true, true, LangOpts));
Ted Kremenek102d42e2008-04-29 05:13:59 +0000847 break;
848
849 case LangOptions::HybridGC:
Ted Kremenek2f62f352008-05-02 18:01:49 +0000850 TFs.push_back(MakeCFRefCountTF(*Ctx, false, true, LangOpts));
851 TFs.push_back(MakeCFRefCountTF(*Ctx, true, false, LangOpts));
Ted Kremenek102d42e2008-04-29 05:13:59 +0000852 break;
853 }
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000854 }
855};
Ted Kremenek827f93b2008-03-06 00:08:09 +0000856} // end anonymous namespace
857
Ted Kremenek827f93b2008-03-06 00:08:09 +0000858ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000859 Preprocessor* PP,
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000860 PreprocessorFactory* PPF,
Ted Kremenek102d42e2008-04-29 05:13:59 +0000861 const LangOptions& LangOpts,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000862 const std::string& FunctionName,
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000863 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000864 bool Visualize, bool TrimGraph,
865 bool AnalyzeAll) {
Ted Kremenek827f93b2008-03-06 00:08:09 +0000866
Ted Kremenek102d42e2008-04-29 05:13:59 +0000867 return new CFRefCountCheckerVisitor(Diags, PP, PPF, LangOpts, FunctionName,
868 HTMLDir, Visualize, TrimGraph,
869 AnalyzeAll);
Ted Kremenek827f93b2008-03-06 00:08:09 +0000870}
871
Ted Kremenek3b451132008-01-08 18:04:06 +0000872//===----------------------------------------------------------------------===//
Ted Kremenek397de012007-12-13 00:37:31 +0000873// AST Serializer
874
875namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000876
877class ASTSerializer : public ASTConsumer {
878protected:
Ted Kremenek863b01f2008-04-23 16:25:39 +0000879 TranslationUnit* TU;
880
Ted Kremenek21189012007-12-19 23:49:37 +0000881public:
Eli Friedmanb4563692008-06-09 20:02:51 +0000882 ASTSerializer() : TU(0) {}
883
884 virtual void InitializeTU(TranslationUnit &tu) {
885 TU = &tu;
Ted Kremenek21189012007-12-19 23:49:37 +0000886 }
Eli Friedmanb4563692008-06-09 20:02:51 +0000887
Ted Kremenek21189012007-12-19 23:49:37 +0000888};
Eli Friedmanb4563692008-06-09 20:02:51 +0000889
Ted Kremenek21189012007-12-19 23:49:37 +0000890class SingleFileSerializer : public ASTSerializer {
891 const llvm::sys::Path FName;
892public:
Eli Friedmanb4563692008-06-09 20:02:51 +0000893 SingleFileSerializer(const llvm::sys::Path& F) : FName(F) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000894
895 ~SingleFileSerializer() {
Ted Kremenek863b01f2008-04-23 16:25:39 +0000896 EmitASTBitcodeFile(TU, FName);
Ted Kremenek21189012007-12-19 23:49:37 +0000897 }
898};
899
900class BuildSerializer : public ASTSerializer {
901 llvm::sys::Path EmitDir;
902public:
Eli Friedmanb4563692008-06-09 20:02:51 +0000903 BuildSerializer(const llvm::sys::Path& dir) : EmitDir(dir) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000904
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000905 ~BuildSerializer() {
Ted Kremenek863b01f2008-04-23 16:25:39 +0000906
907 if (!TU)
908 return;
909
910 SourceManager& SourceMgr = TU->getContext().getSourceManager();
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000911 unsigned ID = SourceMgr.getMainFileID();
912 assert (ID && "MainFileID not set!");
913 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
914 assert (FE && "No FileEntry for main file.");
915
916 // FIXME: This is not portable to Windows.
917 // FIXME: This logic should probably be moved elsewhere later.
918
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000919 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000920
921 std::vector<char> buf;
922 buf.reserve(strlen(FE->getName())+100);
923
924 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000925 FName.appendComponent(&buf[0]);
926 FName.createDirectoryOnDisk(true);
927 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000928 assert (false && "Could not create 'device' serialization directory.");
929 return;
930 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000931
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000932 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000933 FName.appendComponent(&buf[0]);
Ted Kremenek863b01f2008-04-23 16:25:39 +0000934 EmitASTBitcodeFile(TU, FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000935
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000936 // Now emit the sources.
937
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000938 }
Ted Kremenek21189012007-12-19 23:49:37 +0000939};
940
941
Ted Kremenek397de012007-12-13 00:37:31 +0000942} // end anonymous namespace
943
944
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000945ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000946 const std::string& OutputFile,
Ted Kremenek842126e2008-06-04 15:55:15 +0000947 Diagnostic &Diags) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000948
Ted Kremenek21189012007-12-19 23:49:37 +0000949 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000950 if (InFile == "-") {
951 llvm::cerr <<
952 "error: Cannot use --serialize with -o for source read from STDIN.\n";
953 return NULL;
954 }
955
Ted Kremenek21189012007-12-19 23:49:37 +0000956 // The user specified an AST-emission directory. Determine if the path
957 // is absolute.
958 llvm::sys::Path EmitDir(OutputFile);
959
960 if (!EmitDir.isAbsolute()) {
961 llvm::cerr <<
962 "error: Output directory for --serialize must be an absolute path.\n";
963
964 return NULL;
965 }
966
967 // Create the directory if it does not exist.
968 EmitDir.createDirectoryOnDisk(true);
969 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
970 llvm::cerr <<
971 "error: Could not create output directory for --serialize.\n";
972
973 return NULL;
974 }
975
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000976 // FIXME: We should probably only allow using BuildSerializer when
977 // the ASTs come from parsed source files, and not from .ast files.
Eli Friedmanb4563692008-06-09 20:02:51 +0000978 return new BuildSerializer(EmitDir);
Ted Kremenek21189012007-12-19 23:49:37 +0000979 }
980
981 // The user did not specify an output directory for serialized ASTs.
982 // Serialize the translation to a single file whose name is the same
983 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000984
Ted Kremenek21189012007-12-19 23:49:37 +0000985 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000986 FName.appendSuffix("ast");
Eli Friedmanb4563692008-06-09 20:02:51 +0000987 return new SingleFileSerializer(FName);
Ted Kremenek397de012007-12-13 00:37:31 +0000988}