blob: 92add4195e22e136dcc886b19395980534e9a33b [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);
100 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000101 Out << "@class [printing todo]\n";
102 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
103 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
104 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
105 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner806a5f52008-01-12 07:05:38 +0000106 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
107 PrintLinkageSpec(LSD);
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000108 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
109 Out << "asm(";
110 AD->getAsmString()->printPretty(Out);
111 Out << ")\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000112 } else {
113 assert(0 && "Unknown decl type!");
114 }
115}
116
Ted Kremeneke09391a2007-11-27 21:46:50 +0000117void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000118 bool HasBody = FD->getBody();
119
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000120 Out << '\n';
Chris Lattner987058a2007-08-26 04:02:13 +0000121
122 switch (FD->getStorageClass()) {
123 default: assert(0 && "Unknown storage class");
124 case FunctionDecl::None: break;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000125 case FunctionDecl::Extern: Out << "extern "; break;
126 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenekc6d26e02008-04-15 03:57:09 +0000127 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner987058a2007-08-26 04:02:13 +0000128 }
129
130 if (FD->isInline())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000131 Out << "inline ";
Chris Lattner987058a2007-08-26 04:02:13 +0000132
Chris Lattner4b009652007-07-25 00:24:17 +0000133 std::string Proto = FD->getName();
Chris Lattner934fff62007-12-03 21:43:25 +0000134 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +0000135
Chris Lattner934fff62007-12-03 21:43:25 +0000136 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000137 Proto += "(";
138 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
139 if (i) Proto += ", ";
140 std::string ParamStr;
141 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
142
143 FT->getArgType(i).getAsStringInternal(ParamStr);
144 Proto += ParamStr;
145 }
146
147 if (FT->isVariadic()) {
148 if (FD->getNumParams()) Proto += ", ";
149 Proto += "...";
150 }
151 Proto += ")";
152 } else {
153 assert(isa<FunctionTypeNoProto>(AFT));
154 Proto += "()";
155 }
156
157 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000158 Out << Proto;
Chris Lattner4b009652007-07-25 00:24:17 +0000159
Chris Lattner95578782007-08-08 22:51:59 +0000160 if (!FD->getBody())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000161 Out << ";\n";
Chris Lattner95578782007-08-08 22:51:59 +0000162 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +0000163}
164
Ted Kremeneke09391a2007-11-27 21:46:50 +0000165void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000166 std::string S = TD->getName();
167 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000168 Out << "typedef " << S << ";\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000169}
170
Chris Lattner806a5f52008-01-12 07:05:38 +0000171void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
172 const char *l;
173 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
174 l = "C";
Chris Lattner690c2872008-04-08 05:52:18 +0000175 else {
176 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
177 "unknown language in linkage specification");
Chris Lattner806a5f52008-01-12 07:05:38 +0000178 l = "C++";
Chris Lattner690c2872008-04-08 05:52:18 +0000179 }
Chris Lattner806a5f52008-01-12 07:05:38 +0000180 Out << "extern \"" << l << "\" { ";
181 PrintDecl(LS->getDecl());
182 Out << "}\n";
183}
184
Ted Kremenek42730c52008-01-07 19:49:32 +0000185void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000186 if (OMD->isInstance())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000187 Out << "\n- ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000188 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000189 Out << "\n+ ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000190 if (!OMD->getResultType().isNull())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000191 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000192 // FIXME: just print original selector name!
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000193 Out << OMD->getSelector().getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000194
Chris Lattner685d7922008-03-16 01:07:14 +0000195 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000196 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000197 // FIXME: selector is missing here!
198 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000199 }
200}
201
Ted Kremenek42730c52008-01-07 19:49:32 +0000202void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000203 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000204 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000205
206 if (SID)
207 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000208 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000209 Out << "@implementation " << I;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000210
Ted Kremenek42730c52008-01-07 19:49:32 +0000211 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000212 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000213 ObjCMethodDecl *OMD = *I;
214 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000215 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000216 Out << ' ';
217 OMD->getBody()->printPretty(Out);
218 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000219 }
220 }
221
Ted Kremenek42730c52008-01-07 19:49:32 +0000222 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000223 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000224 ObjCMethodDecl *OMD = *I;
225 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000226 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000227 Out << ' ';
228 OMD->getBody()->printPretty(Out);
229 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000230 }
231 }
232
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000233 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
234 E = OID->propimpl_end(); I != E; ++I)
235 PrintObjCPropertyImplDecl(*I);
236
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000237 Out << "@end\n";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000238}
239
240
Ted Kremenek42730c52008-01-07 19:49:32 +0000241void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000242 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000243 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000244
245 if (SID)
246 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000247 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000248 Out << "@interface " << I;
249
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000250 // Protocols?
251 int count = OID->getNumIntfRefProtocols();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000252
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000253 if (count > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000254 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000255 for (int i = 0; i < count; i++)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000256 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000257 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000258
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000259 if (count > 0)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000260 Out << ">\n";
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000261 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000262 Out << '\n';
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000263
Chris Lattnerec4979b2008-03-16 21:08:55 +0000264 if (OID->ivar_size() > 0) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000265 Out << '{';
Ted Kremenek42730c52008-01-07 19:49:32 +0000266 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerc7b06752007-12-12 07:56:42 +0000267 E = OID->ivar_end(); I != E; ++I) {
268 Out << '\t' << (*I)->getType().getAsString()
269 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000270 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000271 Out << "}\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000272 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000273
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000274 for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
275 E = OID->classprop_end(); I != E; ++I)
276 PrintObjCPropertyDecl(*I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000277 bool eol_needed = false;
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000278 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
279 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000280 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000281
282 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
283 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000284 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000285
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000286 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Narofffaed3bf2007-09-10 20:51:04 +0000287 // FIXME: implement the rest...
288}
289
Ted Kremenek42730c52008-01-07 19:49:32 +0000290void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000291 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000292
293 for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
294 E = PID->classprop_end(); I != E; ++I)
295 PrintObjCPropertyDecl(*I);
296 Out << "@end\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000297 // FIXME: implement the rest...
298}
299
Ted Kremenek42730c52008-01-07 19:49:32 +0000300void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000301 Out << "@implementation "
302 << PID->getClassInterface()->getName()
303 << '(' << PID->getName() << ");\n";
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000304 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
305 E = PID->propimpl_end(); I != E; ++I)
306 PrintObjCPropertyImplDecl(*I);
307 Out << "@end\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000308 // FIXME: implement the rest...
309}
310
Ted Kremenek42730c52008-01-07 19:49:32 +0000311void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000312 Out << "@interface "
313 << PID->getClassInterface()->getName()
314 << '(' << PID->getName() << ");\n";
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000315 // Output property declarations.
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000316 for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
317 E = PID->classprop_end(); I != E; ++I)
318 PrintObjCPropertyDecl(*I);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000319 Out << "@end\n";
320
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000321 // FIXME: implement the rest...
322}
323
Ted Kremenek42730c52008-01-07 19:49:32 +0000324void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000325 Out << "@compatibility_alias " << AID->getName()
326 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000327}
328
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000329/// PrintObjCPropertyDecl - print a property declaration.
330///
331void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000332 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
333 Out << "@required\n";
334 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
335 Out << "@optional\n";
336
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000337 Out << "@property";
338 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
339 bool first = true;
340 Out << " (";
341 if (PDecl->getPropertyAttributes() &
342 ObjCPropertyDecl::OBJC_PR_readonly) {
343 Out << (first ? ' ' : ',') << "readonly";
344 first = false;
345 }
346
347 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
348 Out << (first ? ' ' : ',') << "getter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000349 << PDecl->getGetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000350 first = false;
351 }
352 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
353 Out << (first ? ' ' : ',') << "setter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000354 << PDecl->getSetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000355 first = false;
356 }
357
358 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
359 Out << (first ? ' ' : ',') << "assign";
360 first = false;
361 }
362
363 if (PDecl->getPropertyAttributes() &
364 ObjCPropertyDecl::OBJC_PR_readwrite) {
365 Out << (first ? ' ' : ',') << "readwrite";
366 first = false;
367 }
368
369 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
370 Out << (first ? ' ' : ',') << "retain";
371 first = false;
372 }
373
374 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
375 Out << (first ? ' ' : ',') << "copy";
376 first = false;
377 }
378
379 if (PDecl->getPropertyAttributes() &
380 ObjCPropertyDecl::OBJC_PR_nonatomic) {
381 Out << (first ? ' ' : ',') << "nonatomic";
382 first = false;
383 }
384 Out << " )";
385 }
386 Out << ' ' << PDecl->getType().getAsString()
387 << ' ' << PDecl->getName();
388
389 Out << ";\n";
390}
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000391
392/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
393/// declaration syntax.
394///
395void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
396 if (PID->getPropertyImplementation() ==
397 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE)
398 Out << "\n@synthesize ";
399 else
400 Out << "\n@dynamic ";
401 Out << PID->getPropertyDecl()->getName();
402 if (PID->getPropertyIvarDecl())
403 Out << "=" << PID->getPropertyIvarDecl()->getName();
404 Out << ";\n";
405}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000406//===----------------------------------------------------------------------===//
407/// ASTPrinter - Pretty-printer of ASTs
408
Chris Lattnerb73abd52007-09-15 23:02:28 +0000409namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000410 class ASTPrinter : public ASTConsumer, public DeclPrinter {
411 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000412 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000413
Chris Lattnerb73abd52007-09-15 23:02:28 +0000414 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000415 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000416 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000417 };
Chris Lattner4b009652007-07-25 00:24:17 +0000418}
Chris Lattner95578782007-08-08 22:51:59 +0000419
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000420ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
421 return new ASTPrinter(out);
422}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000423
424//===----------------------------------------------------------------------===//
425/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000426
427namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000428 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000429 SourceManager *SM;
430 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000431 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000432
Ted Kremenek17861c52007-12-19 22:51:13 +0000433 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000434 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000435 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000436
437 virtual void HandleTopLevelDecl(Decl *D) {
438 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
439 PrintFunctionDeclStart(FD);
440
441 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000442 Out << '\n';
443 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000444 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000445 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000446 }
447 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
448 PrintTypeDefDecl(TD);
449 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000450 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000451 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000452 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000453 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000454 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000455 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000456 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000457 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000458 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000459 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000460 Out << "Read objc fwd class decl\n";
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000461 } else if (isa<FileScopeAsmDecl>(D)) {
462 Out << "Read file scope asm decl\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000463 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
464 Out << "Read objc method decl: '" << MD->getSelector().getName()
465 << "'\n";
Steve Naroff045e0e02008-05-23 18:50:58 +0000466 if (MD->getBody()) {
467 // FIXME: convert dumper to use std::ostream?
468 MD->getBody()->dumpAll(*SM);
469 Out << '\n';
470 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000471 } else if (isa<ObjCImplementationDecl>(D)) {
472 Out << "Read objc implementation decl\n";
473 }
474 else {
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000475 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000476 }
477 }
478 };
Chris Lattner95578782007-08-08 22:51:59 +0000479}
480
Chris Lattnerb73abd52007-09-15 23:02:28 +0000481ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
482
Ted Kremeneke09391a2007-11-27 21:46:50 +0000483//===----------------------------------------------------------------------===//
484/// ASTViewer - AST Visualization
485
Ted Kremenekb6976a22007-09-19 21:29:43 +0000486namespace {
487 class ASTViewer : public ASTConsumer {
488 SourceManager *SM;
489 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000490 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000491 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000492 }
493
494 virtual void HandleTopLevelDecl(Decl *D) {
495 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000496 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000497
498 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000499 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000500 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000501 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000502 }
503 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000504 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
505 DeclPrinter().PrintObjCMethodDecl(MD);
506
507 if (MD->getBody()) {
508 llvm::cerr << '\n';
509 MD->getBody()->viewAST();
510 llvm::cerr << '\n';
511 }
512 }
Ted Kremenekb6976a22007-09-19 21:29:43 +0000513 }
514 };
515}
516
517ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
518
519
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000520//===----------------------------------------------------------------------===//
521// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
522// the CFGs for all function definitions.
523
524namespace {
525
Chris Lattner52332d02007-09-15 23:21:08 +0000526class CFGVisitor : public ASTConsumer {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000527 std::string FName;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000528public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000529 CFGVisitor(const std::string& fname) : FName(fname) {}
530 CFGVisitor() : FName("") {}
531
Chris Lattner52332d02007-09-15 23:21:08 +0000532 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek5d257d42008-03-14 17:31:00 +0000533 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000534 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000535
536 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000537};
538
539} // end anonymous namespace
540
Chris Lattner52332d02007-09-15 23:21:08 +0000541void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000542
Ted Kremenek5d257d42008-03-14 17:31:00 +0000543 CFG *C = NULL;
544
545 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
546
547 if (!FD->getBody())
548 return;
549
550 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
551 return;
Chris Lattner52332d02007-09-15 23:21:08 +0000552
Ted Kremenek5d257d42008-03-14 17:31:00 +0000553 if (printFuncDeclStart()) {
554 DeclPrinter().PrintFunctionDeclStart(FD);
555 llvm::cerr << '\n';
556 }
557
558 C = CFG::buildCFG(FD->getBody());
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000559 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000560 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner52332d02007-09-15 23:21:08 +0000561
Ted Kremenek5d257d42008-03-14 17:31:00 +0000562 if (!MD->getBody())
563 return;
Ted Kremenek2f0c0e12008-03-14 18:14:50 +0000564
565 if (FName.size() > 0 && FName != MD->getSelector().getName())
566 return;
Ted Kremenek5d257d42008-03-14 17:31:00 +0000567
568 if (printFuncDeclStart()) {
569 DeclPrinter().PrintObjCMethodDecl(MD);
570 llvm::cerr << '\n';
571 }
572
573 C = CFG::buildCFG(MD->getBody());
574 }
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000575
576 if (C) {
Ted Kremenek5d257d42008-03-14 17:31:00 +0000577 VisitCFG(*C, *D);
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000578 delete C;
579 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000580}
581
582//===----------------------------------------------------------------------===//
583// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
584
585namespace {
586 class CFGDumper : public CFGVisitor {
587 const bool UseGraphviz;
588 public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000589 CFGDumper(bool use_graphviz, const std::string& fname)
590 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000591
Ted Kremenek5d257d42008-03-14 17:31:00 +0000592 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattner52332d02007-09-15 23:21:08 +0000593 if (UseGraphviz)
594 C.viewCFG();
595 else
596 C.dump();
597 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000598 };
599} // end anonymous namespace
600
Ted Kremenek83390ec2008-02-22 20:00:31 +0000601ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
602 return new CFGDumper(ViewGraphs, FName);
Ted Kremenek97f75312007-08-21 21:42:03 +0000603}
604
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000605//===----------------------------------------------------------------------===//
606// AnalyzeLiveVariables - perform live variable analysis and dump results
607
608namespace {
609 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000610 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000611 public:
Ted Kremenekb278abb2008-02-22 20:13:09 +0000612 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
613
Ted Kremenek17861c52007-12-19 22:51:13 +0000614 virtual void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000615 SM = &Context.getSourceManager();
Chris Lattner52332d02007-09-15 23:21:08 +0000616 }
617
Ted Kremenek5d257d42008-03-14 17:31:00 +0000618 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000619 LiveVariables L(C);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000620 L.runOnCFG(C);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000621 L.dumpBlockLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000622 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000623 };
624} // end anonymous namespace
625
Ted Kremenekb278abb2008-02-22 20:13:09 +0000626ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
627 return new LivenessVisitor(fname);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000628}
629
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000630//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000631// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000632
633namespace {
634 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000635 Diagnostic &Diags;
636 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000637 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000638 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000639 virtual void Initialize(ASTContext &Context) {
Chris Lattner52332d02007-09-15 23:21:08 +0000640 Ctx = &Context;
641 }
642
Ted Kremenek5d257d42008-03-14 17:31:00 +0000643 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek2f83f792008-06-20 21:55:29 +0000644 llvm::OwningPtr<ParentMap> PM(new ParentMap(CD.getCodeBody()));
645 CheckDeadStores(C, *Ctx, *PM, Diags);
Ted Kremenekd03aece2008-01-29 05:13:23 +0000646 }
647
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000648 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000649 };
650} // end anonymous namespace
651
Chris Lattner52332d02007-09-15 23:21:08 +0000652ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
653 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000654}
Chris Lattner129758d2007-09-16 19:46:59 +0000655
656//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000657// Unitialized Values - run checker to flag potential uses of uninitalized
658// variables.
659
660namespace {
661 class UninitValsVisitor : public CFGVisitor {
662 Diagnostic &Diags;
663 ASTContext *Ctx;
664 public:
665 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000666 virtual void Initialize(ASTContext &Context) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000667 Ctx = &Context;
668 }
669
Ted Kremenek5d257d42008-03-14 17:31:00 +0000670 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekd03aece2008-01-29 05:13:23 +0000671 CheckUninitializedValues(C, *Ctx, Diags);
672 }
673
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000674 virtual bool printFuncDeclStart() { return false; }
675 };
676} // end anonymous namespace
677
678ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
679 return new UninitValsVisitor(Diags);
680}
681
682//===----------------------------------------------------------------------===//
Ted Kremenekfe82a542008-04-16 16:39:56 +0000683// CheckerConsumer - Generic Driver for running intra-procedural path-sensitive
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000684// analyses.
685
686namespace {
687
688class CheckerConsumer : public CFGVisitor {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000689protected:
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000690 Diagnostic &Diags;
691 ASTContext* Ctx;
Ted Kremenekfe82a542008-04-16 16:39:56 +0000692 Preprocessor* PP;
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000693 PreprocessorFactory* PPF;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000694 const std::string& HTMLDir;
695 bool Visualize;
696 bool TrimGraph;
697 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenek517cb512008-04-14 18:40:58 +0000698 bool AnalyzeAll;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000699public:
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000700 CheckerConsumer(Diagnostic &diags, Preprocessor* pp, PreprocessorFactory* ppf,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000701 const std::string& fname,
702 const std::string& htmldir,
703 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000704 : CFGVisitor(fname), Diags(diags), PP(pp), PPF(ppf), HTMLDir(htmldir),
Ted Kremenek517cb512008-04-14 18:40:58 +0000705 Visualize(visualize), TrimGraph(trim), AnalyzeAll(analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000706
707 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
708 virtual void VisitCFG(CFG& C, Decl&);
709 virtual bool printFuncDeclStart() { return false; }
710
711 virtual const char* getCheckerName() = 0;
Ted Kremenek102d42e2008-04-29 05:13:59 +0000712 virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) = 0;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000713};
714} // end anonymous namespace
715
716void CheckerConsumer::VisitCFG(CFG& C, Decl& CD) {
717
718 if (Diags.hasErrorOccurred())
719 return;
720
721 SourceLocation Loc = CD.getLocation();
722
Ted Kremenek517cb512008-04-14 18:40:58 +0000723 if (!Loc.isFileID())
724 return;
725
Ted Kremenek8a11ed22008-04-14 21:14:41 +0000726 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000727 return;
728
729 // Lazily create the diagnostic client.
730
731 if (!HTMLDir.empty() && PD.get() == NULL)
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000732 PD.reset(CreateHTMLDiagnosticClient(HTMLDir, PP, PPF));
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000733
734
735 if (!Visualize) {
736
737 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
738 llvm::cerr << "ANALYZE: "
739 << Ctx->getSourceManager().getSourceName(FD->getLocation())
740 << ' '
741 << FD->getIdentifier()->getName()
742 << '\n';
743 }
744 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
745 llvm::cerr << "ANALYZE (ObjC Method): "
746 << Ctx->getSourceManager().getSourceName(MD->getLocation())
747 << " '"
748 << MD->getSelector().getName() << "'\n";
749 }
750 }
751 else
752 llvm::cerr << '\n';
753
Ted Kremenek102d42e2008-04-29 05:13:59 +0000754 std::vector<GRTransferFuncs*> TFs;
755 getTransferFunctions(TFs);
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000756
Ted Kremenek102d42e2008-04-29 05:13:59 +0000757 while (!TFs.empty()) {
758
759 // Construct the analysis engine.
760 GRExprEngine Eng(C, CD, *Ctx);
761
762 // Set base transfer functions.
763 llvm::OwningPtr<GRTransferFuncs> TF(TFs.back());
764 TFs.pop_back();
765
766 Eng.setTransferFunctions(TF.get());
767
768 // Execute the worklist algorithm.
769 Eng.ExecuteWorkList();
770
771 // Display warnings.
772 Eng.EmitWarnings(Diags, PD.get());
773
774 #ifndef NDEBUG
775 if (Visualize) Eng.ViewGraph(TrimGraph);
776 #endif
777 }
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000778}
779
780//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +0000781// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremenek3b451132008-01-08 18:04:06 +0000782
783namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000784class GRSimpleValsVisitor : public CheckerConsumer {
785public:
Ted Kremenekfe82a542008-04-16 16:39:56 +0000786 GRSimpleValsVisitor(Diagnostic &diags, Preprocessor* pp,
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000787 PreprocessorFactory* ppf,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000788 const std::string& fname, const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000789 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000790 : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize,
791 trim, analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000792
793 virtual const char* getCheckerName() { return "GRSimpleVals"; }
794
Ted Kremenek102d42e2008-04-29 05:13:59 +0000795 virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) {
796 return TFs.push_back(MakeGRSimpleValsTF());
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000797 }
798};
Ted Kremenek3b451132008-01-08 18:04:06 +0000799} // end anonymous namespace
800
Ted Kremenek0118bb52008-02-18 21:21:23 +0000801ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000802 Preprocessor* PP,
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000803 PreprocessorFactory* PPF,
Ted Kremenek0118bb52008-02-18 21:21:23 +0000804 const std::string& FunctionName,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000805 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000806 bool Visualize, bool TrimGraph,
807 bool AnalyzeAll) {
Ted Kremenek0118bb52008-02-18 21:21:23 +0000808
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000809 return new GRSimpleValsVisitor(Diags, PP, PPF, FunctionName, HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000810 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek3b451132008-01-08 18:04:06 +0000811}
812
Ted Kremenek827f93b2008-03-06 00:08:09 +0000813
814//===----------------------------------------------------------------------===//
815// Core Foundation Reference Counting Checker
816
817namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000818class CFRefCountCheckerVisitor : public CheckerConsumer {
Ted Kremenek102d42e2008-04-29 05:13:59 +0000819 const LangOptions& LangOpts;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000820public:
Ted Kremenekfe82a542008-04-16 16:39:56 +0000821 CFRefCountCheckerVisitor(Diagnostic &diags, Preprocessor* pp,
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000822 PreprocessorFactory* ppf,
Ted Kremenek102d42e2008-04-29 05:13:59 +0000823 const LangOptions& lopts,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000824 const std::string& fname,
825 const std::string& htmldir,
826 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000827 : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize,
Ted Kremenek102d42e2008-04-29 05:13:59 +0000828 trim, analyzeAll), LangOpts(lopts) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000829
830 virtual const char* getCheckerName() { return "CFRefCountChecker"; }
831
Ted Kremenek102d42e2008-04-29 05:13:59 +0000832 virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) {
833 switch (LangOpts.getGCMode()) {
834 case LangOptions::NonGC:
Ted Kremenek2f62f352008-05-02 18:01:49 +0000835 TFs.push_back(MakeCFRefCountTF(*Ctx, false, true, LangOpts));
Ted Kremenek102d42e2008-04-29 05:13:59 +0000836 break;
837
838 case LangOptions::GCOnly:
Ted Kremenek2f62f352008-05-02 18:01:49 +0000839 TFs.push_back(MakeCFRefCountTF(*Ctx, true, true, LangOpts));
Ted Kremenek102d42e2008-04-29 05:13:59 +0000840 break;
841
842 case LangOptions::HybridGC:
Ted Kremenek2f62f352008-05-02 18:01:49 +0000843 TFs.push_back(MakeCFRefCountTF(*Ctx, false, true, LangOpts));
844 TFs.push_back(MakeCFRefCountTF(*Ctx, true, false, LangOpts));
Ted Kremenek102d42e2008-04-29 05:13:59 +0000845 break;
846 }
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000847 }
848};
Ted Kremenek827f93b2008-03-06 00:08:09 +0000849} // end anonymous namespace
850
Ted Kremenek827f93b2008-03-06 00:08:09 +0000851ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
Ted Kremenekfe82a542008-04-16 16:39:56 +0000852 Preprocessor* PP,
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000853 PreprocessorFactory* PPF,
Ted Kremenek102d42e2008-04-29 05:13:59 +0000854 const LangOptions& LangOpts,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000855 const std::string& FunctionName,
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000856 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000857 bool Visualize, bool TrimGraph,
858 bool AnalyzeAll) {
Ted Kremenek827f93b2008-03-06 00:08:09 +0000859
Ted Kremenek102d42e2008-04-29 05:13:59 +0000860 return new CFRefCountCheckerVisitor(Diags, PP, PPF, LangOpts, FunctionName,
861 HTMLDir, Visualize, TrimGraph,
862 AnalyzeAll);
Ted Kremenek827f93b2008-03-06 00:08:09 +0000863}
864
Ted Kremenek3b451132008-01-08 18:04:06 +0000865//===----------------------------------------------------------------------===//
Ted Kremenek397de012007-12-13 00:37:31 +0000866// AST Serializer
867
868namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000869
870class ASTSerializer : public ASTConsumer {
871protected:
Ted Kremenek863b01f2008-04-23 16:25:39 +0000872 TranslationUnit* TU;
873
Ted Kremenek21189012007-12-19 23:49:37 +0000874public:
Eli Friedmanb4563692008-06-09 20:02:51 +0000875 ASTSerializer() : TU(0) {}
876
877 virtual void InitializeTU(TranslationUnit &tu) {
878 TU = &tu;
Ted Kremenek21189012007-12-19 23:49:37 +0000879 }
Eli Friedmanb4563692008-06-09 20:02:51 +0000880
Ted Kremenek21189012007-12-19 23:49:37 +0000881};
Eli Friedmanb4563692008-06-09 20:02:51 +0000882
Ted Kremenek21189012007-12-19 23:49:37 +0000883class SingleFileSerializer : public ASTSerializer {
884 const llvm::sys::Path FName;
885public:
Eli Friedmanb4563692008-06-09 20:02:51 +0000886 SingleFileSerializer(const llvm::sys::Path& F) : FName(F) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000887
888 ~SingleFileSerializer() {
Ted Kremenek863b01f2008-04-23 16:25:39 +0000889 EmitASTBitcodeFile(TU, FName);
Ted Kremenek21189012007-12-19 23:49:37 +0000890 }
891};
892
893class BuildSerializer : public ASTSerializer {
894 llvm::sys::Path EmitDir;
895public:
Eli Friedmanb4563692008-06-09 20:02:51 +0000896 BuildSerializer(const llvm::sys::Path& dir) : EmitDir(dir) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000897
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000898 ~BuildSerializer() {
Ted Kremenek863b01f2008-04-23 16:25:39 +0000899
900 if (!TU)
901 return;
902
903 SourceManager& SourceMgr = TU->getContext().getSourceManager();
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000904 unsigned ID = SourceMgr.getMainFileID();
905 assert (ID && "MainFileID not set!");
906 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
907 assert (FE && "No FileEntry for main file.");
908
909 // FIXME: This is not portable to Windows.
910 // FIXME: This logic should probably be moved elsewhere later.
911
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000912 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000913
914 std::vector<char> buf;
915 buf.reserve(strlen(FE->getName())+100);
916
917 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000918 FName.appendComponent(&buf[0]);
919 FName.createDirectoryOnDisk(true);
920 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000921 assert (false && "Could not create 'device' serialization directory.");
922 return;
923 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000924
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000925 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000926 FName.appendComponent(&buf[0]);
Ted Kremenek863b01f2008-04-23 16:25:39 +0000927 EmitASTBitcodeFile(TU, FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000928
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000929 // Now emit the sources.
930
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000931 }
Ted Kremenek21189012007-12-19 23:49:37 +0000932};
933
934
Ted Kremenek397de012007-12-13 00:37:31 +0000935} // end anonymous namespace
936
937
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000938ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000939 const std::string& OutputFile,
Ted Kremenek842126e2008-06-04 15:55:15 +0000940 Diagnostic &Diags) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000941
Ted Kremenek21189012007-12-19 23:49:37 +0000942 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000943 if (InFile == "-") {
944 llvm::cerr <<
945 "error: Cannot use --serialize with -o for source read from STDIN.\n";
946 return NULL;
947 }
948
Ted Kremenek21189012007-12-19 23:49:37 +0000949 // The user specified an AST-emission directory. Determine if the path
950 // is absolute.
951 llvm::sys::Path EmitDir(OutputFile);
952
953 if (!EmitDir.isAbsolute()) {
954 llvm::cerr <<
955 "error: Output directory for --serialize must be an absolute path.\n";
956
957 return NULL;
958 }
959
960 // Create the directory if it does not exist.
961 EmitDir.createDirectoryOnDisk(true);
962 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
963 llvm::cerr <<
964 "error: Could not create output directory for --serialize.\n";
965
966 return NULL;
967 }
968
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000969 // FIXME: We should probably only allow using BuildSerializer when
970 // the ASTs come from parsed source files, and not from .ast files.
Eli Friedmanb4563692008-06-09 20:02:51 +0000971 return new BuildSerializer(EmitDir);
Ted Kremenek21189012007-12-19 23:49:37 +0000972 }
973
974 // The user did not specify an output directory for serialized ASTs.
975 // Serialize the translation to a single file whose name is the same
976 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000977
Ted Kremenek21189012007-12-19 23:49:37 +0000978 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000979 FName.appendSuffix("ast");
Eli Friedmanb4563692008-06-09 20:02:51 +0000980 return new SingleFileSerializer(FName);
Ted Kremenek397de012007-12-13 00:37:31 +0000981}