blob: 05debe38d492b2c91a34574d3bfd9d585156ca23 [file] [log] [blame]
Chris Lattnereb8c9632007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnereb8c9632007-10-07 06:04:32 +000010// AST Consumer Implementations.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnereb8c9632007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000015#include "HTMLDiagnostics.h"
Ted Kremenekac881932007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000017#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenekfc17b8a2007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/AST/AST.h"
Chris Lattnerb73abd52007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.h"
Ted Kremenek97f75312007-08-21 21:42:03 +000022#include "clang/AST/CFG.h"
Ted Kremenekcdf8e842007-12-21 21:42:19 +000023#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremeneke805c4a2007-09-06 23:00:42 +000024#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekb1983ba2008-04-10 22:16:52 +000025#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
26#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000027#include "llvm/Support/Streams.h"
Ted Kremenek0118bb52008-02-18 21:21:23 +000028#include "llvm/Support/Timer.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000029#include "llvm/ADT/OwningPtr.h"
30
Chris Lattner95578782007-08-08 22:51:59 +000031using namespace clang;
Chris Lattner4b009652007-07-25 00:24:17 +000032
Ted Kremeneke09391a2007-11-27 21:46:50 +000033//===----------------------------------------------------------------------===//
34/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner95578782007-08-08 22:51:59 +000035
Ted Kremeneke09391a2007-11-27 21:46:50 +000036namespace {
37 class DeclPrinter {
38 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000039 std::ostream& Out;
Ted Kremeneke09391a2007-11-27 21:46:50 +000040
Chris Lattner216012f2008-01-10 01:43:14 +000041 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000042 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +000043
Chris Lattner1c1aabb2008-01-02 21:04:16 +000044 void PrintDecl(Decl *D);
Ted Kremeneke09391a2007-11-27 21:46:50 +000045 void PrintFunctionDeclStart(FunctionDecl *FD);
46 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattner806a5f52008-01-12 07:05:38 +000047 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremenek42730c52008-01-07 19:49:32 +000048 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
49 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
50 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
51 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
52 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
53 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
54 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Ted Kremeneke09391a2007-11-27 21:46:50 +000055 };
56} // end anonymous namespace
57
Chris Lattner1c1aabb2008-01-02 21:04:16 +000058void DeclPrinter:: PrintDecl(Decl *D) {
59 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
60 PrintFunctionDeclStart(FD);
61
62 if (FD->getBody()) {
63 Out << ' ';
64 FD->getBody()->printPretty(Out);
65 Out << '\n';
66 }
Ted Kremenek42730c52008-01-07 19:49:32 +000067 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000068 // Do nothing, methods definitions are printed in
Ted Kremenek42730c52008-01-07 19:49:32 +000069 // PrintObjCImplementationDecl.
Chris Lattner1c1aabb2008-01-02 21:04:16 +000070 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
71 PrintTypeDefDecl(TD);
Ted Kremenek42730c52008-01-07 19:49:32 +000072 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
73 PrintObjCInterfaceDecl(OID);
74 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
75 PrintObjCProtocolDecl(PID);
76 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattner43b885f2008-02-25 21:04:36 +000077 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000078 Out << "@protocol ";
79 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +000080 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattner1c1aabb2008-01-02 21:04:16 +000081 if (i) Out << ", ";
82 Out << D->getName();
83 }
84 Out << ";\n";
Ted Kremenek42730c52008-01-07 19:49:32 +000085 } else if (ObjCImplementationDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000086 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000087 PrintObjCImplementationDecl(OID);
88 } else if (ObjCCategoryImplDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000089 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000090 PrintObjCCategoryImplDecl(OID);
91 } else if (ObjCCategoryDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000092 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000093 PrintObjCCategoryDecl(OID);
94 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000095 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000096 PrintObjCCompatibleAliasDecl(OID);
97 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000098 Out << "@class [printing todo]\n";
99 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
100 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
101 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
102 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner806a5f52008-01-12 07:05:38 +0000103 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
104 PrintLinkageSpec(LSD);
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000105 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
106 Out << "asm(";
107 AD->getAsmString()->printPretty(Out);
108 Out << ")\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000109 } else {
110 assert(0 && "Unknown decl type!");
111 }
112}
113
Ted Kremeneke09391a2007-11-27 21:46:50 +0000114void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000115 bool HasBody = FD->getBody();
116
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000117 Out << '\n';
Chris Lattner987058a2007-08-26 04:02:13 +0000118
119 switch (FD->getStorageClass()) {
120 default: assert(0 && "Unknown storage class");
121 case FunctionDecl::None: break;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000122 case FunctionDecl::Extern: Out << "extern "; break;
123 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner987058a2007-08-26 04:02:13 +0000124 }
125
126 if (FD->isInline())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000127 Out << "inline ";
Chris Lattner987058a2007-08-26 04:02:13 +0000128
Chris Lattner4b009652007-07-25 00:24:17 +0000129 std::string Proto = FD->getName();
Chris Lattner934fff62007-12-03 21:43:25 +0000130 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +0000131
Chris Lattner934fff62007-12-03 21:43:25 +0000132 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000133 Proto += "(";
134 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
135 if (i) Proto += ", ";
136 std::string ParamStr;
137 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
138
139 FT->getArgType(i).getAsStringInternal(ParamStr);
140 Proto += ParamStr;
141 }
142
143 if (FT->isVariadic()) {
144 if (FD->getNumParams()) Proto += ", ";
145 Proto += "...";
146 }
147 Proto += ")";
148 } else {
149 assert(isa<FunctionTypeNoProto>(AFT));
150 Proto += "()";
151 }
152
153 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000154 Out << Proto;
Chris Lattner4b009652007-07-25 00:24:17 +0000155
Chris Lattner95578782007-08-08 22:51:59 +0000156 if (!FD->getBody())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000157 Out << ";\n";
Chris Lattner95578782007-08-08 22:51:59 +0000158 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +0000159}
160
Ted Kremeneke09391a2007-11-27 21:46:50 +0000161void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000162 std::string S = TD->getName();
163 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000164 Out << "typedef " << S << ";\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000165}
166
Chris Lattner806a5f52008-01-12 07:05:38 +0000167void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
168 const char *l;
169 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
170 l = "C";
Chris Lattner690c2872008-04-08 05:52:18 +0000171 else {
172 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
173 "unknown language in linkage specification");
Chris Lattner806a5f52008-01-12 07:05:38 +0000174 l = "C++";
Chris Lattner690c2872008-04-08 05:52:18 +0000175 }
Chris Lattner806a5f52008-01-12 07:05:38 +0000176 Out << "extern \"" << l << "\" { ";
177 PrintDecl(LS->getDecl());
178 Out << "}\n";
179}
180
Ted Kremenek42730c52008-01-07 19:49:32 +0000181void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000182 if (OMD->isInstance())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000183 Out << "\n- ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000184 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000185 Out << "\n+ ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000186 if (!OMD->getResultType().isNull())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000187 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000188 // FIXME: just print original selector name!
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000189 Out << OMD->getSelector().getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000190
Chris Lattner685d7922008-03-16 01:07:14 +0000191 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000192 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000193 // FIXME: selector is missing here!
194 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000195 }
196}
197
Ted Kremenek42730c52008-01-07 19:49:32 +0000198void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000199 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000200 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000201
202 if (SID)
203 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000204 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000205 Out << "@implementation " << I;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000206
Ted Kremenek42730c52008-01-07 19:49:32 +0000207 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000208 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000209 ObjCMethodDecl *OMD = *I;
210 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000211 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000212 Out << ' ';
213 OMD->getBody()->printPretty(Out);
214 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000215 }
216 }
217
Ted Kremenek42730c52008-01-07 19:49:32 +0000218 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000219 E = OID->classmeth_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 Kremenekbe2ea3b2007-11-28 21:32:21 +0000229 Out << "@end\n";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000230}
231
232
Ted Kremenek42730c52008-01-07 19:49:32 +0000233void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000234 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000235 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000236
237 if (SID)
238 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000239 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000240 Out << "@interface " << I;
241
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000242 // Protocols?
243 int count = OID->getNumIntfRefProtocols();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000244
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000245 if (count > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000246 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000247 for (int i = 0; i < count; i++)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000248 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000249 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000250
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000251 if (count > 0)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000252 Out << ">\n";
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000253 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000254 Out << '\n';
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000255
Chris Lattnerec4979b2008-03-16 21:08:55 +0000256 if (OID->ivar_size() > 0) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000257 Out << '{';
Ted Kremenek42730c52008-01-07 19:49:32 +0000258 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerc7b06752007-12-12 07:56:42 +0000259 E = OID->ivar_end(); I != E; ++I) {
260 Out << '\t' << (*I)->getType().getAsString()
261 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000262 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000263 Out << "}\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000264 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000265
266 int NumProperties = OID->getNumPropertyDecl();
267 if (NumProperties > 0) {
268 for (int i = 0; i < NumProperties; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000269 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000270 Out << "@property";
Ted Kremenek42730c52008-01-07 19:49:32 +0000271 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000272 bool first = true;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000273 Out << " (";
Chris Lattner216012f2008-01-10 01:43:14 +0000274 if (PDecl->getPropertyAttributes() &
275 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000276 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000277 first = false;
278 }
279
Chris Lattner216012f2008-01-10 01:43:14 +0000280 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000281 Out << (first ? ' ' : ',') << "getter = "
282 << PDecl->getGetterName()->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000283 first = false;
284 }
Chris Lattner216012f2008-01-10 01:43:14 +0000285 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000286 Out << (first ? ' ' : ',') << "setter = "
287 << PDecl->getSetterName()->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000288 first = false;
289 }
290
Chris Lattner216012f2008-01-10 01:43:14 +0000291 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000292 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000293 first = false;
294 }
295
Chris Lattner216012f2008-01-10 01:43:14 +0000296 if (PDecl->getPropertyAttributes() &
297 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000298 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000299 first = false;
300 }
301
Chris Lattner216012f2008-01-10 01:43:14 +0000302 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000303 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000304 first = false;
305 }
306
Chris Lattner216012f2008-01-10 01:43:14 +0000307 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000308 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000309 first = false;
310 }
311
Chris Lattner216012f2008-01-10 01:43:14 +0000312 if (PDecl->getPropertyAttributes() &
313 ObjCPropertyDecl::OBJC_PR_nonatomic) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000314 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000315 first = false;
316 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000317 Out << " )";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000318 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000319
Chris Lattner526bf912008-03-17 01:24:41 +0000320 ObjCPropertyDecl::propdecl_iterator
321 I = PDecl->propdecl_begin(), E = PDecl->propdecl_end();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000322
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000323 Out << ' ' << PDecl->getType().getAsString()
Chris Lattner526bf912008-03-17 01:24:41 +0000324 << ' ' << (*I)->getName();
325
326 for (++I; I != E; ++I)
327 Out << ", " << (*I)->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000328
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000329 Out << ";\n";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000330 }
331 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000332
333 Out << "@end\n";
Steve Narofffaed3bf2007-09-10 20:51:04 +0000334 // FIXME: implement the rest...
335}
336
Ted Kremenek42730c52008-01-07 19:49:32 +0000337void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000338 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000339 // FIXME: implement the rest...
340}
341
Ted Kremenek42730c52008-01-07 19:49:32 +0000342void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000343 Out << "@implementation "
344 << PID->getClassInterface()->getName()
345 << '(' << PID->getName() << ");\n";
346
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000347 // FIXME: implement the rest...
348}
349
Ted Kremenek42730c52008-01-07 19:49:32 +0000350void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000351 Out << "@interface "
352 << PID->getClassInterface()->getName()
353 << '(' << PID->getName() << ");\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000354 // FIXME: implement the rest...
355}
356
Ted Kremenek42730c52008-01-07 19:49:32 +0000357void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000358 Out << "@compatibility_alias " << AID->getName()
359 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000360}
361
Ted Kremeneke09391a2007-11-27 21:46:50 +0000362//===----------------------------------------------------------------------===//
363/// ASTPrinter - Pretty-printer of ASTs
364
Chris Lattnerb73abd52007-09-15 23:02:28 +0000365namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000366 class ASTPrinter : public ASTConsumer, public DeclPrinter {
367 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000368 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000369
Chris Lattnerb73abd52007-09-15 23:02:28 +0000370 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000371 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000372 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000373 };
Chris Lattner4b009652007-07-25 00:24:17 +0000374}
Chris Lattner95578782007-08-08 22:51:59 +0000375
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000376ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
377 return new ASTPrinter(out);
378}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000379
380//===----------------------------------------------------------------------===//
381/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000382
383namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000384 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000385 SourceManager *SM;
386 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000387 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000388
Ted Kremenek17861c52007-12-19 22:51:13 +0000389 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000390 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000391 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000392
393 virtual void HandleTopLevelDecl(Decl *D) {
394 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
395 PrintFunctionDeclStart(FD);
396
397 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000398 Out << '\n';
399 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000400 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000401 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000402 }
403 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
404 PrintTypeDefDecl(TD);
405 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000406 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000407 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000408 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000409 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000410 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000411 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000412 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000413 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000414 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000415 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000416 Out << "Read objc fwd class decl\n";
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000417 } else if (isa<FileScopeAsmDecl>(D)) {
418 Out << "Read file scope asm decl\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000419 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
420 Out << "Read objc method decl: '" << MD->getSelector().getName()
421 << "'\n";
422 } else if (isa<ObjCImplementationDecl>(D)) {
423 Out << "Read objc implementation decl\n";
424 }
425 else {
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000426 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000427 }
428 }
429 };
Chris Lattner95578782007-08-08 22:51:59 +0000430}
431
Chris Lattnerb73abd52007-09-15 23:02:28 +0000432ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
433
Ted Kremeneke09391a2007-11-27 21:46:50 +0000434//===----------------------------------------------------------------------===//
435/// ASTViewer - AST Visualization
436
Ted Kremenekb6976a22007-09-19 21:29:43 +0000437namespace {
438 class ASTViewer : public ASTConsumer {
439 SourceManager *SM;
440 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000441 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000442 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000443 }
444
445 virtual void HandleTopLevelDecl(Decl *D) {
446 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000447 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000448
449 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000450 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000451 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000452 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000453 }
454 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000455 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
456 DeclPrinter().PrintObjCMethodDecl(MD);
457
458 if (MD->getBody()) {
459 llvm::cerr << '\n';
460 MD->getBody()->viewAST();
461 llvm::cerr << '\n';
462 }
463 }
Ted Kremenekb6976a22007-09-19 21:29:43 +0000464 }
465 };
466}
467
468ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
469
470
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000471//===----------------------------------------------------------------------===//
472// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
473// the CFGs for all function definitions.
474
475namespace {
476
Chris Lattner52332d02007-09-15 23:21:08 +0000477class CFGVisitor : public ASTConsumer {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000478 std::string FName;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000479public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000480 CFGVisitor(const std::string& fname) : FName(fname) {}
481 CFGVisitor() : FName("") {}
482
Chris Lattner52332d02007-09-15 23:21:08 +0000483 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek5d257d42008-03-14 17:31:00 +0000484 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000485 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000486
487 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000488};
489
490} // end anonymous namespace
491
Chris Lattner52332d02007-09-15 23:21:08 +0000492void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000493
Ted Kremenek5d257d42008-03-14 17:31:00 +0000494 CFG *C = NULL;
495
496 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
497
498 if (!FD->getBody())
499 return;
500
501 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
502 return;
Chris Lattner52332d02007-09-15 23:21:08 +0000503
Ted Kremenek5d257d42008-03-14 17:31:00 +0000504 if (printFuncDeclStart()) {
505 DeclPrinter().PrintFunctionDeclStart(FD);
506 llvm::cerr << '\n';
507 }
508
509 C = CFG::buildCFG(FD->getBody());
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000510 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000511 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner52332d02007-09-15 23:21:08 +0000512
Ted Kremenek5d257d42008-03-14 17:31:00 +0000513 if (!MD->getBody())
514 return;
Ted Kremenek2f0c0e12008-03-14 18:14:50 +0000515
516 if (FName.size() > 0 && FName != MD->getSelector().getName())
517 return;
Ted Kremenek5d257d42008-03-14 17:31:00 +0000518
519 if (printFuncDeclStart()) {
520 DeclPrinter().PrintObjCMethodDecl(MD);
521 llvm::cerr << '\n';
522 }
523
524 C = CFG::buildCFG(MD->getBody());
525 }
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000526
527 if (C) {
Ted Kremenek5d257d42008-03-14 17:31:00 +0000528 VisitCFG(*C, *D);
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000529 delete C;
530 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000531}
532
533//===----------------------------------------------------------------------===//
534// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
535
536namespace {
537 class CFGDumper : public CFGVisitor {
538 const bool UseGraphviz;
539 public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000540 CFGDumper(bool use_graphviz, const std::string& fname)
541 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000542
Ted Kremenek5d257d42008-03-14 17:31:00 +0000543 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattner52332d02007-09-15 23:21:08 +0000544 if (UseGraphviz)
545 C.viewCFG();
546 else
547 C.dump();
548 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000549 };
550} // end anonymous namespace
551
Ted Kremenek83390ec2008-02-22 20:00:31 +0000552ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
553 return new CFGDumper(ViewGraphs, FName);
Ted Kremenek97f75312007-08-21 21:42:03 +0000554}
555
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000556//===----------------------------------------------------------------------===//
557// AnalyzeLiveVariables - perform live variable analysis and dump results
558
559namespace {
560 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000561 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000562 public:
Ted Kremenekb278abb2008-02-22 20:13:09 +0000563 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
564
Ted Kremenek17861c52007-12-19 22:51:13 +0000565 virtual void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000566 SM = &Context.getSourceManager();
Chris Lattner52332d02007-09-15 23:21:08 +0000567 }
568
Ted Kremenek5d257d42008-03-14 17:31:00 +0000569 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000570 LiveVariables L(C);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000571 L.runOnCFG(C);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000572 L.dumpBlockLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000573 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000574 };
575} // end anonymous namespace
576
Ted Kremenekb278abb2008-02-22 20:13:09 +0000577ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
578 return new LivenessVisitor(fname);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000579}
580
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000581//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000582// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000583
584namespace {
585 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000586 Diagnostic &Diags;
587 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000588 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000589 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000590 virtual void Initialize(ASTContext &Context) {
Chris Lattner52332d02007-09-15 23:21:08 +0000591 Ctx = &Context;
592 }
593
Ted Kremenek5d257d42008-03-14 17:31:00 +0000594 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000595 CheckDeadStores(C, *Ctx, Diags);
Ted Kremenekd03aece2008-01-29 05:13:23 +0000596 }
597
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000598 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000599 };
600} // end anonymous namespace
601
Chris Lattner52332d02007-09-15 23:21:08 +0000602ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
603 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000604}
Chris Lattner129758d2007-09-16 19:46:59 +0000605
606//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000607// Unitialized Values - run checker to flag potential uses of uninitalized
608// variables.
609
610namespace {
611 class UninitValsVisitor : public CFGVisitor {
612 Diagnostic &Diags;
613 ASTContext *Ctx;
614 public:
615 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000616 virtual void Initialize(ASTContext &Context) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000617 Ctx = &Context;
618 }
619
Ted Kremenek5d257d42008-03-14 17:31:00 +0000620 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekd03aece2008-01-29 05:13:23 +0000621 CheckUninitializedValues(C, *Ctx, Diags);
622 }
623
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000624 virtual bool printFuncDeclStart() { return false; }
625 };
626} // end anonymous namespace
627
628ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
629 return new UninitValsVisitor(Diags);
630}
631
632//===----------------------------------------------------------------------===//
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000633// CheckeRConsumer - Generic Driver for running intra-procedural path-sensitive
634// analyses.
635
636namespace {
637
638class CheckerConsumer : public CFGVisitor {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000639protected:
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000640 Diagnostic &Diags;
641 ASTContext* Ctx;
642 const std::string& HTMLDir;
643 bool Visualize;
644 bool TrimGraph;
645 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenek517cb512008-04-14 18:40:58 +0000646 bool AnalyzeAll;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000647public:
648 CheckerConsumer(Diagnostic &diags, const std::string& fname,
649 const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000650 bool visualize, bool trim, bool analyzeAll)
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000651 : CFGVisitor(fname), Diags(diags), HTMLDir(htmldir),
Ted Kremenek517cb512008-04-14 18:40:58 +0000652 Visualize(visualize), TrimGraph(trim), AnalyzeAll(analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000653
654 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
655 virtual void VisitCFG(CFG& C, Decl&);
656 virtual bool printFuncDeclStart() { return false; }
657
658 virtual const char* getCheckerName() = 0;
659 virtual GRTransferFuncs* getTransferFunctions() = 0;
660};
661} // end anonymous namespace
662
663void CheckerConsumer::VisitCFG(CFG& C, Decl& CD) {
664
665 if (Diags.hasErrorOccurred())
666 return;
667
668 SourceLocation Loc = CD.getLocation();
669
Ted Kremenek517cb512008-04-14 18:40:58 +0000670 if (!Loc.isFileID())
671 return;
672
Ted Kremenek8a11ed22008-04-14 21:14:41 +0000673 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000674 return;
675
676 // Lazily create the diagnostic client.
677
678 if (!HTMLDir.empty() && PD.get() == NULL)
679 PD.reset(CreateHTMLDiagnosticClient(HTMLDir));
680
681
682 if (!Visualize) {
683
684 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
685 llvm::cerr << "ANALYZE: "
686 << Ctx->getSourceManager().getSourceName(FD->getLocation())
687 << ' '
688 << FD->getIdentifier()->getName()
689 << '\n';
690 }
691 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
692 llvm::cerr << "ANALYZE (ObjC Method): "
693 << Ctx->getSourceManager().getSourceName(MD->getLocation())
694 << " '"
695 << MD->getSelector().getName() << "'\n";
696 }
697 }
698 else
699 llvm::cerr << '\n';
700
701 // Construct the analysis engine.
702 GRExprEngine Eng(C, CD, *Ctx);
703
704 // Set base transfer functions.
705 llvm::OwningPtr<GRTransferFuncs> TF(getTransferFunctions());
706 Eng.setTransferFunctions(TF.get());
707
708 // Execute the worklist algorithm.
709 Eng.ExecuteWorkList();
710
711 // Display warnings.
712 Eng.EmitWarnings(Diags, PD.get());
713
714#ifndef NDEBUG
715 if (Visualize) Eng.ViewGraph(TrimGraph);
716#endif
717}
718
719//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +0000720// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremenek3b451132008-01-08 18:04:06 +0000721
722namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000723class GRSimpleValsVisitor : public CheckerConsumer {
724public:
725 GRSimpleValsVisitor(Diagnostic &diags, const std::string& fname,
726 const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000727 bool visualize, bool trim, bool analyzeAll)
728 : CheckerConsumer(diags, fname, htmldir, visualize, trim, analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000729
730 virtual const char* getCheckerName() { return "GRSimpleVals"; }
731
732 virtual GRTransferFuncs* getTransferFunctions() {
733 return MakeGRSimpleValsTF();
734 }
735};
Ted Kremenek3b451132008-01-08 18:04:06 +0000736} // end anonymous namespace
737
Ted Kremenek0118bb52008-02-18 21:21:23 +0000738ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
739 const std::string& FunctionName,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000740 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000741 bool Visualize, bool TrimGraph,
742 bool AnalyzeAll) {
Ted Kremenek0118bb52008-02-18 21:21:23 +0000743
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000744 return new GRSimpleValsVisitor(Diags, FunctionName, HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000745 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek3b451132008-01-08 18:04:06 +0000746}
747
Ted Kremenek827f93b2008-03-06 00:08:09 +0000748
749//===----------------------------------------------------------------------===//
750// Core Foundation Reference Counting Checker
751
752namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000753class CFRefCountCheckerVisitor : public CheckerConsumer {
754public:
755 CFRefCountCheckerVisitor(Diagnostic &diags, const std::string& fname,
756 const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000757 bool visualize, bool trim, bool analyzeAll)
758 : CheckerConsumer(diags, fname, htmldir, visualize, trim, analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000759
760 virtual const char* getCheckerName() { return "CFRefCountChecker"; }
761
762 virtual GRTransferFuncs* getTransferFunctions() {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000763 return MakeCFRefCountTF(*Ctx);
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000764 }
765};
Ted Kremenek827f93b2008-03-06 00:08:09 +0000766} // end anonymous namespace
767
Ted Kremenek827f93b2008-03-06 00:08:09 +0000768ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000769 const std::string& FunctionName,
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000770 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000771 bool Visualize, bool TrimGraph,
772 bool AnalyzeAll) {
Ted Kremenek827f93b2008-03-06 00:08:09 +0000773
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000774 return new CFRefCountCheckerVisitor(Diags, FunctionName, HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000775 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek827f93b2008-03-06 00:08:09 +0000776}
777
Ted Kremenek3b451132008-01-08 18:04:06 +0000778//===----------------------------------------------------------------------===//
Ted Kremenek397de012007-12-13 00:37:31 +0000779// AST Serializer
780
781namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000782
783class ASTSerializer : public ASTConsumer {
784protected:
785 Diagnostic &Diags;
786 TranslationUnit TU;
787public:
788 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
789 : Diags(diags), TU(LO) {}
790
791 virtual void Initialize(ASTContext &Context) {
792 TU.setContext(&Context);
793 }
794
795 virtual void HandleTopLevelDecl(Decl *D) {
796 if (Diags.hasErrorOccurred())
797 return;
798
799 TU.AddTopLevelDecl(D);
800 }
801};
802
803class SingleFileSerializer : public ASTSerializer {
804 const llvm::sys::Path FName;
805public:
806 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
807 const LangOptions &LO)
808 : ASTSerializer(diags,LO), FName(F) {}
809
810 ~SingleFileSerializer() {
811 EmitASTBitcodeFile(TU,FName);
812 }
813};
814
815class BuildSerializer : public ASTSerializer {
816 llvm::sys::Path EmitDir;
817public:
818 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremenek397de012007-12-13 00:37:31 +0000819 const LangOptions &LO)
Ted Kremenek21189012007-12-19 23:49:37 +0000820 : ASTSerializer(diags,LO), EmitDir(dir) {}
821
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000822 ~BuildSerializer() {
823 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
824 unsigned ID = SourceMgr.getMainFileID();
825 assert (ID && "MainFileID not set!");
826 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
827 assert (FE && "No FileEntry for main file.");
828
829 // FIXME: This is not portable to Windows.
830 // FIXME: This logic should probably be moved elsewhere later.
831
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000832 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000833
834 std::vector<char> buf;
835 buf.reserve(strlen(FE->getName())+100);
836
837 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000838 FName.appendComponent(&buf[0]);
839 FName.createDirectoryOnDisk(true);
840 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000841 assert (false && "Could not create 'device' serialization directory.");
842 return;
843 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000844
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000845 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000846 FName.appendComponent(&buf[0]);
847 EmitASTBitcodeFile(TU,FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000848
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000849 // Now emit the sources.
850
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000851 }
Ted Kremenek21189012007-12-19 23:49:37 +0000852};
853
854
Ted Kremenek397de012007-12-13 00:37:31 +0000855} // end anonymous namespace
856
857
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000858ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000859 const std::string& OutputFile,
Ted Kremenek397de012007-12-13 00:37:31 +0000860 Diagnostic &Diags,
861 const LangOptions &Features) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000862
Ted Kremenek21189012007-12-19 23:49:37 +0000863 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000864 if (InFile == "-") {
865 llvm::cerr <<
866 "error: Cannot use --serialize with -o for source read from STDIN.\n";
867 return NULL;
868 }
869
Ted Kremenek21189012007-12-19 23:49:37 +0000870 // The user specified an AST-emission directory. Determine if the path
871 // is absolute.
872 llvm::sys::Path EmitDir(OutputFile);
873
874 if (!EmitDir.isAbsolute()) {
875 llvm::cerr <<
876 "error: Output directory for --serialize must be an absolute path.\n";
877
878 return NULL;
879 }
880
881 // Create the directory if it does not exist.
882 EmitDir.createDirectoryOnDisk(true);
883 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
884 llvm::cerr <<
885 "error: Could not create output directory for --serialize.\n";
886
887 return NULL;
888 }
889
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000890 // FIXME: We should probably only allow using BuildSerializer when
891 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenek21189012007-12-19 23:49:37 +0000892 return new BuildSerializer(EmitDir, Diags, Features);
893 }
894
895 // The user did not specify an output directory for serialized ASTs.
896 // Serialize the translation to a single file whose name is the same
897 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000898
Ted Kremenek21189012007-12-19 23:49:37 +0000899 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000900 FName.appendSuffix("ast");
Ted Kremenek21189012007-12-19 23:49:37 +0000901 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremenek397de012007-12-13 00:37:31 +0000902}