blob: d05a307087b1866bafa20f6b602b86dda8fa02a4 [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 }
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000319 Out << ' ' << PDecl->getType().getAsString()
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000320 << ' ' << PDecl->getName();
321
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000322 Out << ";\n";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000323 }
324 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000325
326 Out << "@end\n";
Steve Narofffaed3bf2007-09-10 20:51:04 +0000327 // FIXME: implement the rest...
328}
329
Ted Kremenek42730c52008-01-07 19:49:32 +0000330void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000331 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000332 // FIXME: implement the rest...
333}
334
Ted Kremenek42730c52008-01-07 19:49:32 +0000335void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000336 Out << "@implementation "
337 << PID->getClassInterface()->getName()
338 << '(' << PID->getName() << ");\n";
339
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000340 // FIXME: implement the rest...
341}
342
Ted Kremenek42730c52008-01-07 19:49:32 +0000343void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000344 Out << "@interface "
345 << PID->getClassInterface()->getName()
346 << '(' << PID->getName() << ");\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000347 // FIXME: implement the rest...
348}
349
Ted Kremenek42730c52008-01-07 19:49:32 +0000350void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000351 Out << "@compatibility_alias " << AID->getName()
352 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000353}
354
Ted Kremeneke09391a2007-11-27 21:46:50 +0000355//===----------------------------------------------------------------------===//
356/// ASTPrinter - Pretty-printer of ASTs
357
Chris Lattnerb73abd52007-09-15 23:02:28 +0000358namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000359 class ASTPrinter : public ASTConsumer, public DeclPrinter {
360 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000361 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000362
Chris Lattnerb73abd52007-09-15 23:02:28 +0000363 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000364 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000365 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000366 };
Chris Lattner4b009652007-07-25 00:24:17 +0000367}
Chris Lattner95578782007-08-08 22:51:59 +0000368
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000369ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
370 return new ASTPrinter(out);
371}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000372
373//===----------------------------------------------------------------------===//
374/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000375
376namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000377 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000378 SourceManager *SM;
379 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000380 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000381
Ted Kremenek17861c52007-12-19 22:51:13 +0000382 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000383 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000384 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000385
386 virtual void HandleTopLevelDecl(Decl *D) {
387 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
388 PrintFunctionDeclStart(FD);
389
390 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000391 Out << '\n';
392 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000393 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000394 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000395 }
396 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
397 PrintTypeDefDecl(TD);
398 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000399 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000400 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000401 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000402 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000403 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000404 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000405 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000406 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000407 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000408 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000409 Out << "Read objc fwd class decl\n";
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000410 } else if (isa<FileScopeAsmDecl>(D)) {
411 Out << "Read file scope asm decl\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000412 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
413 Out << "Read objc method decl: '" << MD->getSelector().getName()
414 << "'\n";
415 } else if (isa<ObjCImplementationDecl>(D)) {
416 Out << "Read objc implementation decl\n";
417 }
418 else {
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000419 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000420 }
421 }
422 };
Chris Lattner95578782007-08-08 22:51:59 +0000423}
424
Chris Lattnerb73abd52007-09-15 23:02:28 +0000425ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
426
Ted Kremeneke09391a2007-11-27 21:46:50 +0000427//===----------------------------------------------------------------------===//
428/// ASTViewer - AST Visualization
429
Ted Kremenekb6976a22007-09-19 21:29:43 +0000430namespace {
431 class ASTViewer : public ASTConsumer {
432 SourceManager *SM;
433 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000434 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000435 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000436 }
437
438 virtual void HandleTopLevelDecl(Decl *D) {
439 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000440 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000441
442 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000443 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000444 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000445 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000446 }
447 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000448 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
449 DeclPrinter().PrintObjCMethodDecl(MD);
450
451 if (MD->getBody()) {
452 llvm::cerr << '\n';
453 MD->getBody()->viewAST();
454 llvm::cerr << '\n';
455 }
456 }
Ted Kremenekb6976a22007-09-19 21:29:43 +0000457 }
458 };
459}
460
461ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
462
463
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000464//===----------------------------------------------------------------------===//
465// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
466// the CFGs for all function definitions.
467
468namespace {
469
Chris Lattner52332d02007-09-15 23:21:08 +0000470class CFGVisitor : public ASTConsumer {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000471 std::string FName;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000472public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000473 CFGVisitor(const std::string& fname) : FName(fname) {}
474 CFGVisitor() : FName("") {}
475
Chris Lattner52332d02007-09-15 23:21:08 +0000476 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek5d257d42008-03-14 17:31:00 +0000477 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000478 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000479
480 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000481};
482
483} // end anonymous namespace
484
Chris Lattner52332d02007-09-15 23:21:08 +0000485void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000486
Ted Kremenek5d257d42008-03-14 17:31:00 +0000487 CFG *C = NULL;
488
489 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
490
491 if (!FD->getBody())
492 return;
493
494 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
495 return;
Chris Lattner52332d02007-09-15 23:21:08 +0000496
Ted Kremenek5d257d42008-03-14 17:31:00 +0000497 if (printFuncDeclStart()) {
498 DeclPrinter().PrintFunctionDeclStart(FD);
499 llvm::cerr << '\n';
500 }
501
502 C = CFG::buildCFG(FD->getBody());
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000503 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000504 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner52332d02007-09-15 23:21:08 +0000505
Ted Kremenek5d257d42008-03-14 17:31:00 +0000506 if (!MD->getBody())
507 return;
Ted Kremenek2f0c0e12008-03-14 18:14:50 +0000508
509 if (FName.size() > 0 && FName != MD->getSelector().getName())
510 return;
Ted Kremenek5d257d42008-03-14 17:31:00 +0000511
512 if (printFuncDeclStart()) {
513 DeclPrinter().PrintObjCMethodDecl(MD);
514 llvm::cerr << '\n';
515 }
516
517 C = CFG::buildCFG(MD->getBody());
518 }
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000519
520 if (C) {
Ted Kremenek5d257d42008-03-14 17:31:00 +0000521 VisitCFG(*C, *D);
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000522 delete C;
523 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000524}
525
526//===----------------------------------------------------------------------===//
527// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
528
529namespace {
530 class CFGDumper : public CFGVisitor {
531 const bool UseGraphviz;
532 public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000533 CFGDumper(bool use_graphviz, const std::string& fname)
534 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000535
Ted Kremenek5d257d42008-03-14 17:31:00 +0000536 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattner52332d02007-09-15 23:21:08 +0000537 if (UseGraphviz)
538 C.viewCFG();
539 else
540 C.dump();
541 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000542 };
543} // end anonymous namespace
544
Ted Kremenek83390ec2008-02-22 20:00:31 +0000545ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
546 return new CFGDumper(ViewGraphs, FName);
Ted Kremenek97f75312007-08-21 21:42:03 +0000547}
548
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000549//===----------------------------------------------------------------------===//
550// AnalyzeLiveVariables - perform live variable analysis and dump results
551
552namespace {
553 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000554 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000555 public:
Ted Kremenekb278abb2008-02-22 20:13:09 +0000556 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
557
Ted Kremenek17861c52007-12-19 22:51:13 +0000558 virtual void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000559 SM = &Context.getSourceManager();
Chris Lattner52332d02007-09-15 23:21:08 +0000560 }
561
Ted Kremenek5d257d42008-03-14 17:31:00 +0000562 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000563 LiveVariables L(C);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000564 L.runOnCFG(C);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000565 L.dumpBlockLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000566 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000567 };
568} // end anonymous namespace
569
Ted Kremenekb278abb2008-02-22 20:13:09 +0000570ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
571 return new LivenessVisitor(fname);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000572}
573
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000574//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000575// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000576
577namespace {
578 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000579 Diagnostic &Diags;
580 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000581 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000582 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000583 virtual void Initialize(ASTContext &Context) {
Chris Lattner52332d02007-09-15 23:21:08 +0000584 Ctx = &Context;
585 }
586
Ted Kremenek5d257d42008-03-14 17:31:00 +0000587 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000588 CheckDeadStores(C, *Ctx, Diags);
Ted Kremenekd03aece2008-01-29 05:13:23 +0000589 }
590
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000591 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000592 };
593} // end anonymous namespace
594
Chris Lattner52332d02007-09-15 23:21:08 +0000595ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
596 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000597}
Chris Lattner129758d2007-09-16 19:46:59 +0000598
599//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000600// Unitialized Values - run checker to flag potential uses of uninitalized
601// variables.
602
603namespace {
604 class UninitValsVisitor : public CFGVisitor {
605 Diagnostic &Diags;
606 ASTContext *Ctx;
607 public:
608 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000609 virtual void Initialize(ASTContext &Context) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000610 Ctx = &Context;
611 }
612
Ted Kremenek5d257d42008-03-14 17:31:00 +0000613 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekd03aece2008-01-29 05:13:23 +0000614 CheckUninitializedValues(C, *Ctx, Diags);
615 }
616
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000617 virtual bool printFuncDeclStart() { return false; }
618 };
619} // end anonymous namespace
620
621ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
622 return new UninitValsVisitor(Diags);
623}
624
625//===----------------------------------------------------------------------===//
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000626// CheckeRConsumer - Generic Driver for running intra-procedural path-sensitive
627// analyses.
628
629namespace {
630
631class CheckerConsumer : public CFGVisitor {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000632protected:
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000633 Diagnostic &Diags;
634 ASTContext* Ctx;
635 const std::string& HTMLDir;
636 bool Visualize;
637 bool TrimGraph;
638 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenek517cb512008-04-14 18:40:58 +0000639 bool AnalyzeAll;
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000640public:
641 CheckerConsumer(Diagnostic &diags, const std::string& fname,
642 const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000643 bool visualize, bool trim, bool analyzeAll)
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000644 : CFGVisitor(fname), Diags(diags), HTMLDir(htmldir),
Ted Kremenek517cb512008-04-14 18:40:58 +0000645 Visualize(visualize), TrimGraph(trim), AnalyzeAll(analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000646
647 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
648 virtual void VisitCFG(CFG& C, Decl&);
649 virtual bool printFuncDeclStart() { return false; }
650
651 virtual const char* getCheckerName() = 0;
652 virtual GRTransferFuncs* getTransferFunctions() = 0;
653};
654} // end anonymous namespace
655
656void CheckerConsumer::VisitCFG(CFG& C, Decl& CD) {
657
658 if (Diags.hasErrorOccurred())
659 return;
660
661 SourceLocation Loc = CD.getLocation();
662
Ted Kremenek517cb512008-04-14 18:40:58 +0000663 if (!Loc.isFileID())
664 return;
665
Ted Kremenek8a11ed22008-04-14 21:14:41 +0000666 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000667 return;
668
669 // Lazily create the diagnostic client.
670
671 if (!HTMLDir.empty() && PD.get() == NULL)
672 PD.reset(CreateHTMLDiagnosticClient(HTMLDir));
673
674
675 if (!Visualize) {
676
677 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
678 llvm::cerr << "ANALYZE: "
679 << Ctx->getSourceManager().getSourceName(FD->getLocation())
680 << ' '
681 << FD->getIdentifier()->getName()
682 << '\n';
683 }
684 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
685 llvm::cerr << "ANALYZE (ObjC Method): "
686 << Ctx->getSourceManager().getSourceName(MD->getLocation())
687 << " '"
688 << MD->getSelector().getName() << "'\n";
689 }
690 }
691 else
692 llvm::cerr << '\n';
693
694 // Construct the analysis engine.
695 GRExprEngine Eng(C, CD, *Ctx);
696
697 // Set base transfer functions.
698 llvm::OwningPtr<GRTransferFuncs> TF(getTransferFunctions());
699 Eng.setTransferFunctions(TF.get());
700
701 // Execute the worklist algorithm.
702 Eng.ExecuteWorkList();
703
704 // Display warnings.
705 Eng.EmitWarnings(Diags, PD.get());
706
707#ifndef NDEBUG
708 if (Visualize) Eng.ViewGraph(TrimGraph);
709#endif
710}
711
712//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +0000713// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremenek3b451132008-01-08 18:04:06 +0000714
715namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000716class GRSimpleValsVisitor : public CheckerConsumer {
717public:
718 GRSimpleValsVisitor(Diagnostic &diags, const std::string& fname,
719 const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000720 bool visualize, bool trim, bool analyzeAll)
721 : CheckerConsumer(diags, fname, htmldir, visualize, trim, analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000722
723 virtual const char* getCheckerName() { return "GRSimpleVals"; }
724
725 virtual GRTransferFuncs* getTransferFunctions() {
726 return MakeGRSimpleValsTF();
727 }
728};
Ted Kremenek3b451132008-01-08 18:04:06 +0000729} // end anonymous namespace
730
Ted Kremenek0118bb52008-02-18 21:21:23 +0000731ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
732 const std::string& FunctionName,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000733 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000734 bool Visualize, bool TrimGraph,
735 bool AnalyzeAll) {
Ted Kremenek0118bb52008-02-18 21:21:23 +0000736
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000737 return new GRSimpleValsVisitor(Diags, FunctionName, HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000738 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek3b451132008-01-08 18:04:06 +0000739}
740
Ted Kremenek827f93b2008-03-06 00:08:09 +0000741
742//===----------------------------------------------------------------------===//
743// Core Foundation Reference Counting Checker
744
745namespace {
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000746class CFRefCountCheckerVisitor : public CheckerConsumer {
747public:
748 CFRefCountCheckerVisitor(Diagnostic &diags, const std::string& fname,
749 const std::string& htmldir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000750 bool visualize, bool trim, bool analyzeAll)
751 : CheckerConsumer(diags, fname, htmldir, visualize, trim, analyzeAll) {}
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000752
753 virtual const char* getCheckerName() { return "CFRefCountChecker"; }
754
755 virtual GRTransferFuncs* getTransferFunctions() {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000756 return MakeCFRefCountTF(*Ctx);
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000757 }
758};
Ted Kremenek827f93b2008-03-06 00:08:09 +0000759} // end anonymous namespace
760
Ted Kremenek827f93b2008-03-06 00:08:09 +0000761ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000762 const std::string& FunctionName,
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000763 const std::string& HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000764 bool Visualize, bool TrimGraph,
765 bool AnalyzeAll) {
Ted Kremenek827f93b2008-03-06 00:08:09 +0000766
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000767 return new CFRefCountCheckerVisitor(Diags, FunctionName, HTMLDir,
Ted Kremenek517cb512008-04-14 18:40:58 +0000768 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek827f93b2008-03-06 00:08:09 +0000769}
770
Ted Kremenek3b451132008-01-08 18:04:06 +0000771//===----------------------------------------------------------------------===//
Ted Kremenek397de012007-12-13 00:37:31 +0000772// AST Serializer
773
774namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000775
776class ASTSerializer : public ASTConsumer {
777protected:
778 Diagnostic &Diags;
779 TranslationUnit TU;
780public:
781 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
782 : Diags(diags), TU(LO) {}
783
784 virtual void Initialize(ASTContext &Context) {
785 TU.setContext(&Context);
786 }
787
788 virtual void HandleTopLevelDecl(Decl *D) {
789 if (Diags.hasErrorOccurred())
790 return;
791
792 TU.AddTopLevelDecl(D);
793 }
794};
795
796class SingleFileSerializer : public ASTSerializer {
797 const llvm::sys::Path FName;
798public:
799 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
800 const LangOptions &LO)
801 : ASTSerializer(diags,LO), FName(F) {}
802
803 ~SingleFileSerializer() {
804 EmitASTBitcodeFile(TU,FName);
805 }
806};
807
808class BuildSerializer : public ASTSerializer {
809 llvm::sys::Path EmitDir;
810public:
811 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremenek397de012007-12-13 00:37:31 +0000812 const LangOptions &LO)
Ted Kremenek21189012007-12-19 23:49:37 +0000813 : ASTSerializer(diags,LO), EmitDir(dir) {}
814
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000815 ~BuildSerializer() {
816 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
817 unsigned ID = SourceMgr.getMainFileID();
818 assert (ID && "MainFileID not set!");
819 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
820 assert (FE && "No FileEntry for main file.");
821
822 // FIXME: This is not portable to Windows.
823 // FIXME: This logic should probably be moved elsewhere later.
824
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000825 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000826
827 std::vector<char> buf;
828 buf.reserve(strlen(FE->getName())+100);
829
830 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000831 FName.appendComponent(&buf[0]);
832 FName.createDirectoryOnDisk(true);
833 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000834 assert (false && "Could not create 'device' serialization directory.");
835 return;
836 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000837
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000838 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000839 FName.appendComponent(&buf[0]);
840 EmitASTBitcodeFile(TU,FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000841
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000842 // Now emit the sources.
843
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000844 }
Ted Kremenek21189012007-12-19 23:49:37 +0000845};
846
847
Ted Kremenek397de012007-12-13 00:37:31 +0000848} // end anonymous namespace
849
850
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000851ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000852 const std::string& OutputFile,
Ted Kremenek397de012007-12-13 00:37:31 +0000853 Diagnostic &Diags,
854 const LangOptions &Features) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000855
Ted Kremenek21189012007-12-19 23:49:37 +0000856 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000857 if (InFile == "-") {
858 llvm::cerr <<
859 "error: Cannot use --serialize with -o for source read from STDIN.\n";
860 return NULL;
861 }
862
Ted Kremenek21189012007-12-19 23:49:37 +0000863 // The user specified an AST-emission directory. Determine if the path
864 // is absolute.
865 llvm::sys::Path EmitDir(OutputFile);
866
867 if (!EmitDir.isAbsolute()) {
868 llvm::cerr <<
869 "error: Output directory for --serialize must be an absolute path.\n";
870
871 return NULL;
872 }
873
874 // Create the directory if it does not exist.
875 EmitDir.createDirectoryOnDisk(true);
876 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
877 llvm::cerr <<
878 "error: Could not create output directory for --serialize.\n";
879
880 return NULL;
881 }
882
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000883 // FIXME: We should probably only allow using BuildSerializer when
884 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenek21189012007-12-19 23:49:37 +0000885 return new BuildSerializer(EmitDir, Diags, Features);
886 }
887
888 // The user did not specify an output directory for serialized ASTs.
889 // Serialize the translation to a single file whose name is the same
890 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000891
Ted Kremenek21189012007-12-19 23:49:37 +0000892 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000893 FName.appendSuffix("ast");
Ted Kremenek21189012007-12-19 23:49:37 +0000894 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremenek397de012007-12-13 00:37:31 +0000895}