blob: 3c104150f1ac178400c3f38a9836d21e65cd9280 [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 Kremenekbe2ea3b2007-11-28 21:32:21 +000025#include "llvm/Support/Streams.h"
Ted Kremenek0118bb52008-02-18 21:21:23 +000026#include "llvm/Support/Timer.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000027#include "llvm/ADT/OwningPtr.h"
28
Chris Lattner95578782007-08-08 22:51:59 +000029using namespace clang;
Chris Lattner4b009652007-07-25 00:24:17 +000030
Ted Kremeneke09391a2007-11-27 21:46:50 +000031//===----------------------------------------------------------------------===//
32/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner95578782007-08-08 22:51:59 +000033
Ted Kremeneke09391a2007-11-27 21:46:50 +000034namespace {
35 class DeclPrinter {
36 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000037 std::ostream& Out;
Ted Kremeneke09391a2007-11-27 21:46:50 +000038
Chris Lattner216012f2008-01-10 01:43:14 +000039 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000040 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +000041
Chris Lattner1c1aabb2008-01-02 21:04:16 +000042 void PrintDecl(Decl *D);
Ted Kremeneke09391a2007-11-27 21:46:50 +000043 void PrintFunctionDeclStart(FunctionDecl *FD);
44 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattner806a5f52008-01-12 07:05:38 +000045 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremenek42730c52008-01-07 19:49:32 +000046 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
47 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
48 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
49 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
50 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
51 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
52 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Ted Kremeneke09391a2007-11-27 21:46:50 +000053 };
54} // end anonymous namespace
55
Chris Lattner1c1aabb2008-01-02 21:04:16 +000056void DeclPrinter:: PrintDecl(Decl *D) {
57 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
58 PrintFunctionDeclStart(FD);
59
60 if (FD->getBody()) {
61 Out << ' ';
62 FD->getBody()->printPretty(Out);
63 Out << '\n';
64 }
Ted Kremenek42730c52008-01-07 19:49:32 +000065 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000066 // Do nothing, methods definitions are printed in
Ted Kremenek42730c52008-01-07 19:49:32 +000067 // PrintObjCImplementationDecl.
Chris Lattner1c1aabb2008-01-02 21:04:16 +000068 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
69 PrintTypeDefDecl(TD);
Ted Kremenek42730c52008-01-07 19:49:32 +000070 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
71 PrintObjCInterfaceDecl(OID);
72 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
73 PrintObjCProtocolDecl(PID);
74 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattner43b885f2008-02-25 21:04:36 +000075 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000076 Out << "@protocol ";
77 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +000078 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattner1c1aabb2008-01-02 21:04:16 +000079 if (i) Out << ", ";
80 Out << D->getName();
81 }
82 Out << ";\n";
Ted Kremenek42730c52008-01-07 19:49:32 +000083 } else if (ObjCImplementationDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000084 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000085 PrintObjCImplementationDecl(OID);
86 } else if (ObjCCategoryImplDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000087 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000088 PrintObjCCategoryImplDecl(OID);
89 } else if (ObjCCategoryDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000090 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000091 PrintObjCCategoryDecl(OID);
92 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000093 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000094 PrintObjCCompatibleAliasDecl(OID);
95 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000096 Out << "@class [printing todo]\n";
97 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
98 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
99 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
100 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner806a5f52008-01-12 07:05:38 +0000101 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
102 PrintLinkageSpec(LSD);
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000103 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
104 Out << "asm(";
105 AD->getAsmString()->printPretty(Out);
106 Out << ")\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000107 } else {
108 assert(0 && "Unknown decl type!");
109 }
110}
111
Ted Kremeneke09391a2007-11-27 21:46:50 +0000112void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 bool HasBody = FD->getBody();
114
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000115 Out << '\n';
Chris Lattner987058a2007-08-26 04:02:13 +0000116
117 switch (FD->getStorageClass()) {
118 default: assert(0 && "Unknown storage class");
119 case FunctionDecl::None: break;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000120 case FunctionDecl::Extern: Out << "extern "; break;
121 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner987058a2007-08-26 04:02:13 +0000122 }
123
124 if (FD->isInline())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000125 Out << "inline ";
Chris Lattner987058a2007-08-26 04:02:13 +0000126
Chris Lattner4b009652007-07-25 00:24:17 +0000127 std::string Proto = FD->getName();
Chris Lattner934fff62007-12-03 21:43:25 +0000128 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +0000129
Chris Lattner934fff62007-12-03 21:43:25 +0000130 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000131 Proto += "(";
132 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
133 if (i) Proto += ", ";
134 std::string ParamStr;
135 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
136
137 FT->getArgType(i).getAsStringInternal(ParamStr);
138 Proto += ParamStr;
139 }
140
141 if (FT->isVariadic()) {
142 if (FD->getNumParams()) Proto += ", ";
143 Proto += "...";
144 }
145 Proto += ")";
146 } else {
147 assert(isa<FunctionTypeNoProto>(AFT));
148 Proto += "()";
149 }
150
151 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000152 Out << Proto;
Chris Lattner4b009652007-07-25 00:24:17 +0000153
Chris Lattner95578782007-08-08 22:51:59 +0000154 if (!FD->getBody())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000155 Out << ";\n";
Chris Lattner95578782007-08-08 22:51:59 +0000156 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +0000157}
158
Ted Kremeneke09391a2007-11-27 21:46:50 +0000159void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000160 std::string S = TD->getName();
161 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000162 Out << "typedef " << S << ";\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000163}
164
Chris Lattner806a5f52008-01-12 07:05:38 +0000165void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
166 const char *l;
167 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
168 l = "C";
Chris Lattner690c2872008-04-08 05:52:18 +0000169 else {
170 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
171 "unknown language in linkage specification");
Chris Lattner806a5f52008-01-12 07:05:38 +0000172 l = "C++";
Chris Lattner690c2872008-04-08 05:52:18 +0000173 }
Chris Lattner806a5f52008-01-12 07:05:38 +0000174 Out << "extern \"" << l << "\" { ";
175 PrintDecl(LS->getDecl());
176 Out << "}\n";
177}
178
Ted Kremenek42730c52008-01-07 19:49:32 +0000179void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000180 if (OMD->isInstance())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000181 Out << "\n- ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000182 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000183 Out << "\n+ ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000184 if (!OMD->getResultType().isNull())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000185 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000186 // FIXME: just print original selector name!
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000187 Out << OMD->getSelector().getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000188
Chris Lattner685d7922008-03-16 01:07:14 +0000189 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000190 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000191 // FIXME: selector is missing here!
192 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000193 }
194}
195
Ted Kremenek42730c52008-01-07 19:49:32 +0000196void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000197 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000198 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000199
200 if (SID)
201 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000202 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000203 Out << "@implementation " << I;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000204
Ted Kremenek42730c52008-01-07 19:49:32 +0000205 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000206 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000207 ObjCMethodDecl *OMD = *I;
208 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000209 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000210 Out << ' ';
211 OMD->getBody()->printPretty(Out);
212 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000213 }
214 }
215
Ted Kremenek42730c52008-01-07 19:49:32 +0000216 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000217 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000218 ObjCMethodDecl *OMD = *I;
219 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000220 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000221 Out << ' ';
222 OMD->getBody()->printPretty(Out);
223 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000224 }
225 }
226
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000227 Out << "@end\n";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000228}
229
230
Ted Kremenek42730c52008-01-07 19:49:32 +0000231void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000232 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000233 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000234
235 if (SID)
236 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000237 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000238 Out << "@interface " << I;
239
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000240 // Protocols?
241 int count = OID->getNumIntfRefProtocols();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000242
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000243 if (count > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000244 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000245 for (int i = 0; i < count; i++)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000246 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000247 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000248
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000249 if (count > 0)
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000250 Out << ">\n";
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000251 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000252 Out << '\n';
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000253
Chris Lattnerec4979b2008-03-16 21:08:55 +0000254 if (OID->ivar_size() > 0) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000255 Out << '{';
Ted Kremenek42730c52008-01-07 19:49:32 +0000256 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerc7b06752007-12-12 07:56:42 +0000257 E = OID->ivar_end(); I != E; ++I) {
258 Out << '\t' << (*I)->getType().getAsString()
259 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000260 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000261 Out << "}\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000262 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000263
264 int NumProperties = OID->getNumPropertyDecl();
265 if (NumProperties > 0) {
266 for (int i = 0; i < NumProperties; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000267 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000268 Out << "@property";
Ted Kremenek42730c52008-01-07 19:49:32 +0000269 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000270 bool first = true;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000271 Out << " (";
Chris Lattner216012f2008-01-10 01:43:14 +0000272 if (PDecl->getPropertyAttributes() &
273 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000274 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000275 first = false;
276 }
277
Chris Lattner216012f2008-01-10 01:43:14 +0000278 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000279 Out << (first ? ' ' : ',') << "getter = "
280 << PDecl->getGetterName()->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000281 first = false;
282 }
Chris Lattner216012f2008-01-10 01:43:14 +0000283 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000284 Out << (first ? ' ' : ',') << "setter = "
285 << PDecl->getSetterName()->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000286 first = false;
287 }
288
Chris Lattner216012f2008-01-10 01:43:14 +0000289 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000290 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000291 first = false;
292 }
293
Chris Lattner216012f2008-01-10 01:43:14 +0000294 if (PDecl->getPropertyAttributes() &
295 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000296 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000297 first = false;
298 }
299
Chris Lattner216012f2008-01-10 01:43:14 +0000300 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000301 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000302 first = false;
303 }
304
Chris Lattner216012f2008-01-10 01:43:14 +0000305 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000306 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000307 first = false;
308 }
309
Chris Lattner216012f2008-01-10 01:43:14 +0000310 if (PDecl->getPropertyAttributes() &
311 ObjCPropertyDecl::OBJC_PR_nonatomic) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000312 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000313 first = false;
314 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000315 Out << " )";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000316 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000317
Chris Lattner526bf912008-03-17 01:24:41 +0000318 ObjCPropertyDecl::propdecl_iterator
319 I = PDecl->propdecl_begin(), E = PDecl->propdecl_end();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000320
Chris Lattner526bf912008-03-17 01:24:41 +0000321 Out << ' ' << (*I)->getType().getAsString()
322 << ' ' << (*I)->getName();
323
324 for (++I; I != E; ++I)
325 Out << ", " << (*I)->getName();
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000326
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000327 Out << ";\n";
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000328 }
329 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000330
331 Out << "@end\n";
Steve Narofffaed3bf2007-09-10 20:51:04 +0000332 // FIXME: implement the rest...
333}
334
Ted Kremenek42730c52008-01-07 19:49:32 +0000335void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000336 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000337 // FIXME: implement the rest...
338}
339
Ted Kremenek42730c52008-01-07 19:49:32 +0000340void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000341 Out << "@implementation "
342 << PID->getClassInterface()->getName()
343 << '(' << PID->getName() << ");\n";
344
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000345 // FIXME: implement the rest...
346}
347
Ted Kremenek42730c52008-01-07 19:49:32 +0000348void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000349 Out << "@interface "
350 << PID->getClassInterface()->getName()
351 << '(' << PID->getName() << ");\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000352 // FIXME: implement the rest...
353}
354
Ted Kremenek42730c52008-01-07 19:49:32 +0000355void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000356 Out << "@compatibility_alias " << AID->getName()
357 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000358}
359
Ted Kremeneke09391a2007-11-27 21:46:50 +0000360//===----------------------------------------------------------------------===//
361/// ASTPrinter - Pretty-printer of ASTs
362
Chris Lattnerb73abd52007-09-15 23:02:28 +0000363namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000364 class ASTPrinter : public ASTConsumer, public DeclPrinter {
365 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000366 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000367
Chris Lattnerb73abd52007-09-15 23:02:28 +0000368 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000369 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000370 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000371 };
Chris Lattner4b009652007-07-25 00:24:17 +0000372}
Chris Lattner95578782007-08-08 22:51:59 +0000373
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000374ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
375 return new ASTPrinter(out);
376}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000377
378//===----------------------------------------------------------------------===//
379/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000380
381namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000382 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000383 SourceManager *SM;
384 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000385 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000386
Ted Kremenek17861c52007-12-19 22:51:13 +0000387 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000388 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000389 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000390
391 virtual void HandleTopLevelDecl(Decl *D) {
392 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
393 PrintFunctionDeclStart(FD);
394
395 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000396 Out << '\n';
397 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000398 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000399 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000400 }
401 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
402 PrintTypeDefDecl(TD);
403 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000404 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000405 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000406 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000407 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000408 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000409 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000410 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000411 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000412 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000413 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000414 Out << "Read objc fwd class decl\n";
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000415 } else if (isa<FileScopeAsmDecl>(D)) {
416 Out << "Read file scope asm decl\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000417 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
418 Out << "Read objc method decl: '" << MD->getSelector().getName()
419 << "'\n";
420 } else if (isa<ObjCImplementationDecl>(D)) {
421 Out << "Read objc implementation decl\n";
422 }
423 else {
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000424 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000425 }
426 }
427 };
Chris Lattner95578782007-08-08 22:51:59 +0000428}
429
Chris Lattnerb73abd52007-09-15 23:02:28 +0000430ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
431
Ted Kremeneke09391a2007-11-27 21:46:50 +0000432//===----------------------------------------------------------------------===//
433/// ASTViewer - AST Visualization
434
Ted Kremenekb6976a22007-09-19 21:29:43 +0000435namespace {
436 class ASTViewer : public ASTConsumer {
437 SourceManager *SM;
438 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000439 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000440 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000441 }
442
443 virtual void HandleTopLevelDecl(Decl *D) {
444 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000445 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000446
447 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000448 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000449 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000450 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000451 }
452 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000453 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
454 DeclPrinter().PrintObjCMethodDecl(MD);
455
456 if (MD->getBody()) {
457 llvm::cerr << '\n';
458 MD->getBody()->viewAST();
459 llvm::cerr << '\n';
460 }
461 }
Ted Kremenekb6976a22007-09-19 21:29:43 +0000462 }
463 };
464}
465
466ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
467
468
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000469//===----------------------------------------------------------------------===//
470// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
471// the CFGs for all function definitions.
472
473namespace {
474
Chris Lattner52332d02007-09-15 23:21:08 +0000475class CFGVisitor : public ASTConsumer {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000476 std::string FName;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000477public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000478 CFGVisitor(const std::string& fname) : FName(fname) {}
479 CFGVisitor() : FName("") {}
480
Chris Lattner52332d02007-09-15 23:21:08 +0000481 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek5d257d42008-03-14 17:31:00 +0000482 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000483 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000484
485 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000486};
487
488} // end anonymous namespace
489
Chris Lattner52332d02007-09-15 23:21:08 +0000490void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek83390ec2008-02-22 20:00:31 +0000491
Ted Kremenek5d257d42008-03-14 17:31:00 +0000492 CFG *C = NULL;
493
494 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
495
496 if (!FD->getBody())
497 return;
498
499 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
500 return;
Chris Lattner52332d02007-09-15 23:21:08 +0000501
Ted Kremenek5d257d42008-03-14 17:31:00 +0000502 if (printFuncDeclStart()) {
503 DeclPrinter().PrintFunctionDeclStart(FD);
504 llvm::cerr << '\n';
505 }
506
507 C = CFG::buildCFG(FD->getBody());
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000508 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000509 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner52332d02007-09-15 23:21:08 +0000510
Ted Kremenek5d257d42008-03-14 17:31:00 +0000511 if (!MD->getBody())
512 return;
Ted Kremenek2f0c0e12008-03-14 18:14:50 +0000513
514 if (FName.size() > 0 && FName != MD->getSelector().getName())
515 return;
Ted Kremenek5d257d42008-03-14 17:31:00 +0000516
517 if (printFuncDeclStart()) {
518 DeclPrinter().PrintObjCMethodDecl(MD);
519 llvm::cerr << '\n';
520 }
521
522 C = CFG::buildCFG(MD->getBody());
523 }
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000524
525 if (C) {
Ted Kremenek5d257d42008-03-14 17:31:00 +0000526 VisitCFG(*C, *D);
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000527 delete C;
528 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000529}
530
531//===----------------------------------------------------------------------===//
532// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
533
534namespace {
535 class CFGDumper : public CFGVisitor {
536 const bool UseGraphviz;
537 public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000538 CFGDumper(bool use_graphviz, const std::string& fname)
539 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000540
Ted Kremenek5d257d42008-03-14 17:31:00 +0000541 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattner52332d02007-09-15 23:21:08 +0000542 if (UseGraphviz)
543 C.viewCFG();
544 else
545 C.dump();
546 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000547 };
548} // end anonymous namespace
549
Ted Kremenek83390ec2008-02-22 20:00:31 +0000550ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
551 return new CFGDumper(ViewGraphs, FName);
Ted Kremenek97f75312007-08-21 21:42:03 +0000552}
553
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000554//===----------------------------------------------------------------------===//
555// AnalyzeLiveVariables - perform live variable analysis and dump results
556
557namespace {
558 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000559 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000560 public:
Ted Kremenekb278abb2008-02-22 20:13:09 +0000561 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
562
Ted Kremenek17861c52007-12-19 22:51:13 +0000563 virtual void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000564 SM = &Context.getSourceManager();
Chris Lattner52332d02007-09-15 23:21:08 +0000565 }
566
Ted Kremenek5d257d42008-03-14 17:31:00 +0000567 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000568 LiveVariables L(C);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000569 L.runOnCFG(C);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000570 L.dumpBlockLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000571 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000572 };
573} // end anonymous namespace
574
Ted Kremenekb278abb2008-02-22 20:13:09 +0000575ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
576 return new LivenessVisitor(fname);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000577}
578
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000579//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000580// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000581
582namespace {
583 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000584 Diagnostic &Diags;
585 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000586 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000587 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000588 virtual void Initialize(ASTContext &Context) {
Chris Lattner52332d02007-09-15 23:21:08 +0000589 Ctx = &Context;
590 }
591
Ted Kremenek5d257d42008-03-14 17:31:00 +0000592 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000593 CheckDeadStores(C, *Ctx, Diags);
Ted Kremenekd03aece2008-01-29 05:13:23 +0000594 }
595
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000596 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000597 };
598} // end anonymous namespace
599
Chris Lattner52332d02007-09-15 23:21:08 +0000600ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
601 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000602}
Chris Lattner129758d2007-09-16 19:46:59 +0000603
604//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000605// Unitialized Values - run checker to flag potential uses of uninitalized
606// variables.
607
608namespace {
609 class UninitValsVisitor : public CFGVisitor {
610 Diagnostic &Diags;
611 ASTContext *Ctx;
612 public:
613 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek17861c52007-12-19 22:51:13 +0000614 virtual void Initialize(ASTContext &Context) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000615 Ctx = &Context;
616 }
617
Ted Kremenek5d257d42008-03-14 17:31:00 +0000618 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekd03aece2008-01-29 05:13:23 +0000619 CheckUninitializedValues(C, *Ctx, Diags);
620 }
621
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000622 virtual bool printFuncDeclStart() { return false; }
623 };
624} // end anonymous namespace
625
626ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
627 return new UninitValsVisitor(Diags);
628}
629
630//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +0000631// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremenek3b451132008-01-08 18:04:06 +0000632
633namespace {
Ted Kremenek3862eb12008-02-14 22:36:46 +0000634 class GRSimpleValsVisitor : public CFGVisitor {
Ted Kremeneke0866362008-02-07 06:33:19 +0000635 Diagnostic &Diags;
Ted Kremenek54eddae2008-01-24 02:02:54 +0000636 ASTContext* Ctx;
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000637 const std::string& HTMLDir;
Ted Kremenek1da5b142008-02-15 00:35:38 +0000638 bool Visualize;
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000639 bool TrimGraph;
Ted Kremenek3b451132008-01-08 18:04:06 +0000640 public:
Ted Kremenek83390ec2008-02-22 20:00:31 +0000641 GRSimpleValsVisitor(Diagnostic &diags, const std::string& fname,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000642 const std::string& htmldir,
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000643 bool visualize, bool trim)
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000644 : CFGVisitor(fname), Diags(diags), HTMLDir(htmldir),
645 Visualize(visualize), TrimGraph(trim) {}
Ted Kremenek3b451132008-01-08 18:04:06 +0000646
Ted Kremenek7c647412008-01-29 00:33:40 +0000647 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000648 virtual void VisitCFG(CFG& C, Decl&);
Ted Kremenek0118bb52008-02-18 21:21:23 +0000649 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek3b451132008-01-08 18:04:06 +0000650 };
651} // end anonymous namespace
652
Ted Kremenek0118bb52008-02-18 21:21:23 +0000653ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
654 const std::string& FunctionName,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000655 const std::string& HTMLDir,
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000656 bool Visualize, bool TrimGraph) {
Ted Kremenek0118bb52008-02-18 21:21:23 +0000657
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000658 return new GRSimpleValsVisitor(Diags, FunctionName, HTMLDir,
659 Visualize, TrimGraph);
Ted Kremenek3b451132008-01-08 18:04:06 +0000660}
661
Ted Kremenek5d257d42008-03-14 17:31:00 +0000662void GRSimpleValsVisitor::VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek0118bb52008-02-18 21:21:23 +0000663
Ted Kremenekbc3d5562008-03-27 17:14:42 +0000664 if (Diags.hasErrorOccurred())
665 return;
666
Ted Kremenek5d257d42008-03-14 17:31:00 +0000667 SourceLocation Loc = CD.getLocation();
Ted Kremenek2f0c0e12008-03-14 18:14:50 +0000668
Ted Kremeneke060a352008-02-22 19:10:58 +0000669 if (!Loc.isFileID() ||
670 Loc.getFileID() != Ctx->getSourceManager().getMainFileID())
671 return;
Ted Kremenek2f0c0e12008-03-14 18:14:50 +0000672
Ted Kremenek0118bb52008-02-18 21:21:23 +0000673 if (!Visualize) {
Ted Kremenek5d257d42008-03-14 17:31:00 +0000674
675 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
Ted Kremenekcd2eb9b2008-03-31 23:14:05 +0000676 llvm::cerr << "ANALYZE: "
Ted Kremenek5d257d42008-03-14 17:31:00 +0000677 << Ctx->getSourceManager().getSourceName(FD->getLocation())
Ted Kremenekcd2eb9b2008-03-31 23:14:05 +0000678 << ' '
679 << FD->getIdentifier()->getName()
680 << '\n';
Ted Kremenek5d257d42008-03-14 17:31:00 +0000681 }
682 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
Ted Kremenekcd2eb9b2008-03-31 23:14:05 +0000683 llvm::cerr << "ANALYZE (ObjC Method): "
Ted Kremenek5d257d42008-03-14 17:31:00 +0000684 << Ctx->getSourceManager().getSourceName(MD->getLocation())
Ted Kremenekcd2eb9b2008-03-31 23:14:05 +0000685 << " '"
686 << MD->getSelector().getName() << "'\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000687 }
Ted Kremenek0118bb52008-02-18 21:21:23 +0000688
Ted Kremeneka95114d2008-03-31 16:00:32 +0000689#if 0
Ted Kremenek0118bb52008-02-18 21:21:23 +0000690 llvm::Timer T("GRSimpleVals");
691 T.startTimer();
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000692 unsigned size = RunGRSimpleVals(C, CD, *Ctx, Diags, NULL, false, false);
Ted Kremenek0118bb52008-02-18 21:21:23 +0000693 T.stopTimer();
Ted Kremenekbf988d02008-02-19 00:22:37 +0000694 llvm::cerr << size << ' ' << T.getWallTime() << '\n';
Ted Kremeneka95114d2008-03-31 16:00:32 +0000695#else
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000696 llvm::OwningPtr<PathDiagnosticClient> PD;
697
698 if (!HTMLDir.empty())
699 PD.reset(CreateHTMLDiagnosticClient(HTMLDir));
700
701 RunGRSimpleVals(C, CD, *Ctx, Diags, PD.get(), false, false);
Ted Kremeneka95114d2008-03-31 16:00:32 +0000702#endif
Ted Kremenek0118bb52008-02-18 21:21:23 +0000703 }
704 else {
705 llvm::cerr << '\n';
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000706 RunGRSimpleVals(C, CD, *Ctx, Diags, NULL, Visualize, TrimGraph);
Ted Kremenek0118bb52008-02-18 21:21:23 +0000707 }
Ted Kremenek7c647412008-01-29 00:33:40 +0000708}
709
Ted Kremenek827f93b2008-03-06 00:08:09 +0000710
711//===----------------------------------------------------------------------===//
712// Core Foundation Reference Counting Checker
713
714namespace {
715 class CFRefCountCheckerVisitor : public CFGVisitor {
716 Diagnostic &Diags;
717 ASTContext* Ctx;
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000718 const std::string& HTMLDir;
Ted Kremenek827f93b2008-03-06 00:08:09 +0000719
720 public:
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000721 CFRefCountCheckerVisitor(Diagnostic &diags, const std::string& fname,
722 const std::string& htmldir)
723 : CFGVisitor(fname), Diags(diags), HTMLDir(htmldir) {}
Ted Kremenek827f93b2008-03-06 00:08:09 +0000724
725 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000726 virtual void VisitCFG(CFG& C, Decl&);
Ted Kremenek827f93b2008-03-06 00:08:09 +0000727 virtual bool printFuncDeclStart() { return false; }
728 };
729} // end anonymous namespace
730
731
732ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000733 const std::string& FunctionName,
734 const std::string& HTMLDir) {
Ted Kremenek827f93b2008-03-06 00:08:09 +0000735
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000736 return new CFRefCountCheckerVisitor(Diags, FunctionName, HTMLDir);
Ted Kremenek827f93b2008-03-06 00:08:09 +0000737}
738
Ted Kremenek5d257d42008-03-14 17:31:00 +0000739void CFRefCountCheckerVisitor::VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000740
Ted Kremenek5d257d42008-03-14 17:31:00 +0000741 SourceLocation Loc = CD.getLocation();
Ted Kremenek827f93b2008-03-06 00:08:09 +0000742
743 if (!Loc.isFileID() ||
744 Loc.getFileID() != Ctx->getSourceManager().getMainFileID())
745 return;
746
Ted Kremenekdd0126b2008-03-31 18:26:32 +0000747 CheckCFRefCount(C, CD, *Ctx, Diags, NULL);
Ted Kremenek827f93b2008-03-06 00:08:09 +0000748}
749
Ted Kremenek3b451132008-01-08 18:04:06 +0000750//===----------------------------------------------------------------------===//
Ted Kremenek397de012007-12-13 00:37:31 +0000751// AST Serializer
752
753namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000754
755class ASTSerializer : public ASTConsumer {
756protected:
757 Diagnostic &Diags;
758 TranslationUnit TU;
759public:
760 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
761 : Diags(diags), TU(LO) {}
762
763 virtual void Initialize(ASTContext &Context) {
764 TU.setContext(&Context);
765 }
766
767 virtual void HandleTopLevelDecl(Decl *D) {
768 if (Diags.hasErrorOccurred())
769 return;
770
771 TU.AddTopLevelDecl(D);
772 }
773};
774
775class SingleFileSerializer : public ASTSerializer {
776 const llvm::sys::Path FName;
777public:
778 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
779 const LangOptions &LO)
780 : ASTSerializer(diags,LO), FName(F) {}
781
782 ~SingleFileSerializer() {
783 EmitASTBitcodeFile(TU,FName);
784 }
785};
786
787class BuildSerializer : public ASTSerializer {
788 llvm::sys::Path EmitDir;
789public:
790 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremenek397de012007-12-13 00:37:31 +0000791 const LangOptions &LO)
Ted Kremenek21189012007-12-19 23:49:37 +0000792 : ASTSerializer(diags,LO), EmitDir(dir) {}
793
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000794 ~BuildSerializer() {
795 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
796 unsigned ID = SourceMgr.getMainFileID();
797 assert (ID && "MainFileID not set!");
798 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
799 assert (FE && "No FileEntry for main file.");
800
801 // FIXME: This is not portable to Windows.
802 // FIXME: This logic should probably be moved elsewhere later.
803
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000804 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000805
806 std::vector<char> buf;
807 buf.reserve(strlen(FE->getName())+100);
808
809 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000810 FName.appendComponent(&buf[0]);
811 FName.createDirectoryOnDisk(true);
812 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000813 assert (false && "Could not create 'device' serialization directory.");
814 return;
815 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000816
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000817 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000818 FName.appendComponent(&buf[0]);
819 EmitASTBitcodeFile(TU,FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000820
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000821 // Now emit the sources.
822
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000823 }
Ted Kremenek21189012007-12-19 23:49:37 +0000824};
825
826
Ted Kremenek397de012007-12-13 00:37:31 +0000827} // end anonymous namespace
828
829
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000830ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000831 const std::string& OutputFile,
Ted Kremenek397de012007-12-13 00:37:31 +0000832 Diagnostic &Diags,
833 const LangOptions &Features) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000834
Ted Kremenek21189012007-12-19 23:49:37 +0000835 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000836 if (InFile == "-") {
837 llvm::cerr <<
838 "error: Cannot use --serialize with -o for source read from STDIN.\n";
839 return NULL;
840 }
841
Ted Kremenek21189012007-12-19 23:49:37 +0000842 // The user specified an AST-emission directory. Determine if the path
843 // is absolute.
844 llvm::sys::Path EmitDir(OutputFile);
845
846 if (!EmitDir.isAbsolute()) {
847 llvm::cerr <<
848 "error: Output directory for --serialize must be an absolute path.\n";
849
850 return NULL;
851 }
852
853 // Create the directory if it does not exist.
854 EmitDir.createDirectoryOnDisk(true);
855 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
856 llvm::cerr <<
857 "error: Could not create output directory for --serialize.\n";
858
859 return NULL;
860 }
861
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000862 // FIXME: We should probably only allow using BuildSerializer when
863 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenek21189012007-12-19 23:49:37 +0000864 return new BuildSerializer(EmitDir, Diags, Features);
865 }
866
867 // The user did not specify an output directory for serialized ASTs.
868 // Serialize the translation to a single file whose name is the same
869 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000870
Ted Kremenek21189012007-12-19 23:49:37 +0000871 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000872 FName.appendSuffix("ast");
Ted Kremenek21189012007-12-19 23:49:37 +0000873 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremenek397de012007-12-13 00:37:31 +0000874}