blob: 5868a28b70ab19d5d542630efdcb741b35b0fabe [file] [log] [blame]
Chris Lattner97e8b6f2007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner97e8b6f2007-10-07 06:04:32 +000010// AST Consumer Implementations.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner97e8b6f2007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000015#include "clang/AST/TranslationUnit.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000016#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000020#include "clang/AST/ASTConsumer.h"
Ted Kremenekfddd5182007-08-21 21:42:03 +000021#include "clang/AST/CFG.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000022#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000023#include "clang/Analysis/Analyses/GRSimpleVals.h"
Ted Kremenek055c2752007-09-06 23:00:42 +000024#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000025#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000026#include "llvm/Support/Timer.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000027using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000028
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000029//===----------------------------------------------------------------------===//
30/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000031
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000032namespace {
33 class DeclPrinter {
34 public:
Ted Kremenekea75c552007-11-28 21:32:21 +000035 std::ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000036
Chris Lattner4b1daf02008-01-10 01:43:14 +000037 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekea75c552007-11-28 21:32:21 +000038 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000039
Chris Lattneref5a85d2008-01-02 21:04:16 +000040 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000041 void PrintFunctionDeclStart(FunctionDecl *FD);
42 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000043 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000044 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
45 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
46 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
47 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
48 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
49 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
50 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000051 };
52} // end anonymous namespace
53
Chris Lattneref5a85d2008-01-02 21:04:16 +000054void DeclPrinter:: PrintDecl(Decl *D) {
55 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
56 PrintFunctionDeclStart(FD);
57
58 if (FD->getBody()) {
59 Out << ' ';
60 FD->getBody()->printPretty(Out);
61 Out << '\n';
62 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000063 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000064 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000065 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000066 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
67 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000068 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
69 PrintObjCInterfaceDecl(OID);
70 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
71 PrintObjCProtocolDecl(PID);
72 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000073 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000074 Out << "@protocol ";
75 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000077 if (i) Out << ", ";
78 Out << D->getName();
79 }
80 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000081 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000082 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000083 PrintObjCImplementationDecl(OID);
84 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000085 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000086 PrintObjCCategoryImplDecl(OID);
87 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000088 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000089 PrintObjCCategoryDecl(OID);
90 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000091 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000092 PrintObjCCompatibleAliasDecl(OID);
93 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000094 Out << "@class [printing todo]\n";
95 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
96 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
97 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
98 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +000099 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
100 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000101 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
102 Out << "asm(";
103 AD->getAsmString()->printPretty(Out);
104 Out << ")\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000105 } else {
106 assert(0 && "Unknown decl type!");
107 }
108}
109
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000110void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 bool HasBody = FD->getBody();
112
Ted Kremenekea75c552007-11-28 21:32:21 +0000113 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000114
115 switch (FD->getStorageClass()) {
116 default: assert(0 && "Unknown storage class");
117 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000118 case FunctionDecl::Extern: Out << "extern "; break;
119 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000120 }
121
122 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000123 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000126 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000127
Chris Lattner0d6ca112007-12-03 21:43:25 +0000128 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 Proto += "(";
130 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
131 if (i) Proto += ", ";
132 std::string ParamStr;
133 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
134
135 FT->getArgType(i).getAsStringInternal(ParamStr);
136 Proto += ParamStr;
137 }
138
139 if (FT->isVariadic()) {
140 if (FD->getNumParams()) Proto += ", ";
141 Proto += "...";
142 }
143 Proto += ")";
144 } else {
145 assert(isa<FunctionTypeNoProto>(AFT));
146 Proto += "()";
147 }
148
149 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000150 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000151
Chris Lattner6000dac2007-08-08 22:51:59 +0000152 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000153 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000154 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000155}
156
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000157void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 std::string S = TD->getName();
159 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000160 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000161}
162
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000163void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
164 const char *l;
165 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
166 l = "C";
167 else if (LS->getLanguage() == LinkageSpecDecl::lang_cxx)
168 l = "C++";
169 else assert(0 && "unknown language in linkage specification");
170 Out << "extern \"" << l << "\" { ";
171 PrintDecl(LS->getDecl());
172 Out << "}\n";
173}
174
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000176 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000177 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000178 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000179 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000180 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000181 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000182 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000183 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000184
Chris Lattner58cce3b2008-03-16 01:07:14 +0000185 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000186 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000187 // FIXME: selector is missing here!
188 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000189 }
190}
191
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000192void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000193 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000195
196 if (SID)
197 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000198 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000199 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000200
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000201 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000202 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000203 ObjCMethodDecl *OMD = *I;
204 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000205 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000206 Out << ' ';
207 OMD->getBody()->printPretty(Out);
208 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000209 }
210 }
211
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000212 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000213 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000214 ObjCMethodDecl *OMD = *I;
215 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000216 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000217 Out << ' ';
218 OMD->getBody()->printPretty(Out);
219 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000220 }
221 }
222
Ted Kremenekea75c552007-11-28 21:32:21 +0000223 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000224}
225
226
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000227void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000228 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000229 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000230
231 if (SID)
232 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000233 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000234 Out << "@interface " << I;
235
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000236 // Protocols?
237 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000238
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000239 if (count > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000240 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000241 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000242 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000243 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000244
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000245 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000246 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000247 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000248 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000249
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000250 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000251 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000252 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000253 E = OID->ivar_end(); I != E; ++I) {
254 Out << '\t' << (*I)->getType().getAsString()
255 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000256 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000257 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000258 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000259
260 int NumProperties = OID->getNumPropertyDecl();
261 if (NumProperties > 0) {
262 for (int i = 0; i < NumProperties; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000263 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekea75c552007-11-28 21:32:21 +0000264 Out << "@property";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000265 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000266 bool first = true;
Ted Kremenekea75c552007-11-28 21:32:21 +0000267 Out << " (";
Chris Lattner4b1daf02008-01-10 01:43:14 +0000268 if (PDecl->getPropertyAttributes() &
269 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000270 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000271 first = false;
272 }
273
Chris Lattner4b1daf02008-01-10 01:43:14 +0000274 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000275 Out << (first ? ' ' : ',') << "getter = "
276 << PDecl->getGetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000277 first = false;
278 }
Chris Lattner4b1daf02008-01-10 01:43:14 +0000279 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000280 Out << (first ? ' ' : ',') << "setter = "
281 << PDecl->getSetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000282 first = false;
283 }
284
Chris Lattner4b1daf02008-01-10 01:43:14 +0000285 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000286 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000287 first = false;
288 }
289
Chris Lattner4b1daf02008-01-10 01:43:14 +0000290 if (PDecl->getPropertyAttributes() &
291 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000292 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000293 first = false;
294 }
295
Chris Lattner4b1daf02008-01-10 01:43:14 +0000296 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000297 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000298 first = false;
299 }
300
Chris Lattner4b1daf02008-01-10 01:43:14 +0000301 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000302 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000303 first = false;
304 }
305
Chris Lattner4b1daf02008-01-10 01:43:14 +0000306 if (PDecl->getPropertyAttributes() &
307 ObjCPropertyDecl::OBJC_PR_nonatomic) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000308 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000309 first = false;
310 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000311 Out << " )";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000312 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000313
Chris Lattnera5674252008-03-17 01:24:41 +0000314 ObjCPropertyDecl::propdecl_iterator
315 I = PDecl->propdecl_begin(), E = PDecl->propdecl_end();
Ted Kremenekea75c552007-11-28 21:32:21 +0000316
Chris Lattnera5674252008-03-17 01:24:41 +0000317 Out << ' ' << (*I)->getType().getAsString()
318 << ' ' << (*I)->getName();
319
320 for (++I; I != E; ++I)
321 Out << ", " << (*I)->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000322
Ted Kremenekea75c552007-11-28 21:32:21 +0000323 Out << ";\n";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000324 }
325 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000326
327 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000328 // FIXME: implement the rest...
329}
330
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000331void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000332 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000333 // FIXME: implement the rest...
334}
335
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000336void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000337 Out << "@implementation "
338 << PID->getClassInterface()->getName()
339 << '(' << PID->getName() << ");\n";
340
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000341 // FIXME: implement the rest...
342}
343
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000344void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000345 Out << "@interface "
346 << PID->getClassInterface()->getName()
347 << '(' << PID->getName() << ");\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000348 // FIXME: implement the rest...
349}
350
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000351void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000352 Out << "@compatibility_alias " << AID->getName()
353 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000354}
355
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000356//===----------------------------------------------------------------------===//
357/// ASTPrinter - Pretty-printer of ASTs
358
Chris Lattner3d4997d2007-09-15 23:02:28 +0000359namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000360 class ASTPrinter : public ASTConsumer, public DeclPrinter {
361 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000362 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000363
Chris Lattner3d4997d2007-09-15 23:02:28 +0000364 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000365 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000367 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000368}
Chris Lattner6000dac2007-08-08 22:51:59 +0000369
Ted Kremenekea75c552007-11-28 21:32:21 +0000370ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
371 return new ASTPrinter(out);
372}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000373
374//===----------------------------------------------------------------------===//
375/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000376
377namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000378 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000379 SourceManager *SM;
380 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000381 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000382
Ted Kremenek95041a22007-12-19 22:51:13 +0000383 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000384 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000385 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000386
387 virtual void HandleTopLevelDecl(Decl *D) {
388 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
389 PrintFunctionDeclStart(FD);
390
391 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000392 Out << '\n';
393 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000394 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000395 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000396 }
397 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
398 PrintTypeDefDecl(TD);
399 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000400 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000401 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000402 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000403 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000404 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000405 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000406 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000407 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000408 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000409 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000410 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000411 } else if (isa<FileScopeAsmDecl>(D)) {
412 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000413 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
414 Out << "Read objc method decl: '" << MD->getSelector().getName()
415 << "'\n";
416 } else if (isa<ObjCImplementationDecl>(D)) {
417 Out << "Read objc implementation decl\n";
418 }
419 else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000420 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000421 }
422 }
423 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000424}
425
Chris Lattner3d4997d2007-09-15 23:02:28 +0000426ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
427
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000428//===----------------------------------------------------------------------===//
429/// ASTViewer - AST Visualization
430
Ted Kremenek80de08f2007-09-19 21:29:43 +0000431namespace {
432 class ASTViewer : public ASTConsumer {
433 SourceManager *SM;
434 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000435 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000436 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000437 }
438
439 virtual void HandleTopLevelDecl(Decl *D) {
440 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000441 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000442
443 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000444 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000445 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000446 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000447 }
448 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000449 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
450 DeclPrinter().PrintObjCMethodDecl(MD);
451
452 if (MD->getBody()) {
453 llvm::cerr << '\n';
454 MD->getBody()->viewAST();
455 llvm::cerr << '\n';
456 }
457 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000458 }
459 };
460}
461
462ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
463
464
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000465//===----------------------------------------------------------------------===//
466// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
467// the CFGs for all function definitions.
468
469namespace {
470
Chris Lattnerc0508f92007-09-15 23:21:08 +0000471class CFGVisitor : public ASTConsumer {
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000472 std::string FName;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000473public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000474 CFGVisitor(const std::string& fname) : FName(fname) {}
475 CFGVisitor() : FName("") {}
476
Chris Lattnerc0508f92007-09-15 23:21:08 +0000477 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek63bbe532008-03-14 17:31:00 +0000478 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000479 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000480
481 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000482};
483
484} // end anonymous namespace
485
Chris Lattnerc0508f92007-09-15 23:21:08 +0000486void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000487
Ted Kremenek63bbe532008-03-14 17:31:00 +0000488 CFG *C = NULL;
489
490 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
491
492 if (!FD->getBody())
493 return;
494
495 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
496 return;
Chris Lattnerc0508f92007-09-15 23:21:08 +0000497
Ted Kremenek63bbe532008-03-14 17:31:00 +0000498 if (printFuncDeclStart()) {
499 DeclPrinter().PrintFunctionDeclStart(FD);
500 llvm::cerr << '\n';
501 }
502
503 C = CFG::buildCFG(FD->getBody());
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000504 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000505 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000506
Ted Kremenek63bbe532008-03-14 17:31:00 +0000507 if (!MD->getBody())
508 return;
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000509
510 if (FName.size() > 0 && FName != MD->getSelector().getName())
511 return;
Ted Kremenek63bbe532008-03-14 17:31:00 +0000512
513 if (printFuncDeclStart()) {
514 DeclPrinter().PrintObjCMethodDecl(MD);
515 llvm::cerr << '\n';
516 }
517
518 C = CFG::buildCFG(MD->getBody());
519 }
Ted Kremenek4102af92008-03-13 03:04:22 +0000520
521 if (C) {
Ted Kremenek63bbe532008-03-14 17:31:00 +0000522 VisitCFG(*C, *D);
Ted Kremenek4102af92008-03-13 03:04:22 +0000523 delete C;
524 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000525}
526
527//===----------------------------------------------------------------------===//
528// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
529
530namespace {
531 class CFGDumper : public CFGVisitor {
532 const bool UseGraphviz;
533 public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000534 CFGDumper(bool use_graphviz, const std::string& fname)
535 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000536
Ted Kremenek63bbe532008-03-14 17:31:00 +0000537 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000538 if (UseGraphviz)
539 C.viewCFG();
540 else
541 C.dump();
542 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000543 };
544} // end anonymous namespace
545
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000546ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
547 return new CFGDumper(ViewGraphs, FName);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000548}
549
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000550//===----------------------------------------------------------------------===//
551// AnalyzeLiveVariables - perform live variable analysis and dump results
552
553namespace {
554 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000555 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000556 public:
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000557 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
558
Ted Kremenek95041a22007-12-19 22:51:13 +0000559 virtual void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000560 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000561 }
562
Ted Kremenek63bbe532008-03-14 17:31:00 +0000563 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000564 LiveVariables L(C);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000565 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000566 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000567 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000568 };
569} // end anonymous namespace
570
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000571ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
572 return new LivenessVisitor(fname);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000573}
574
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000575//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000576// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000577
578namespace {
579 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000580 Diagnostic &Diags;
581 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000582 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000583 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000584 virtual void Initialize(ASTContext &Context) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000585 Ctx = &Context;
586 }
587
Ted Kremenek63bbe532008-03-14 17:31:00 +0000588 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000589 CheckDeadStores(C, *Ctx, Diags);
Ted Kremenekbffaa832008-01-29 05:13:23 +0000590 }
591
Ted Kremenek567a7e62007-09-07 23:54:15 +0000592 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000593 };
594} // end anonymous namespace
595
Chris Lattnerc0508f92007-09-15 23:21:08 +0000596ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
597 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000598}
Chris Lattner580980b2007-09-16 19:46:59 +0000599
600//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000601// Unitialized Values - run checker to flag potential uses of uninitalized
602// variables.
603
604namespace {
605 class UninitValsVisitor : public CFGVisitor {
606 Diagnostic &Diags;
607 ASTContext *Ctx;
608 public:
609 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000610 virtual void Initialize(ASTContext &Context) {
Ted Kremenek2bf55142007-09-17 20:49:30 +0000611 Ctx = &Context;
612 }
613
Ted Kremenek63bbe532008-03-14 17:31:00 +0000614 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekbffaa832008-01-29 05:13:23 +0000615 CheckUninitializedValues(C, *Ctx, Diags);
616 }
617
Ted Kremenek2bf55142007-09-17 20:49:30 +0000618 virtual bool printFuncDeclStart() { return false; }
619 };
620} // end anonymous namespace
621
622ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
623 return new UninitValsVisitor(Diags);
624}
625
626//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +0000627// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremeneke603df42008-01-08 18:04:06 +0000628
629namespace {
Ted Kremeneke01c9872008-02-14 22:36:46 +0000630 class GRSimpleValsVisitor : public CFGVisitor {
Ted Kremenek19227e32008-02-07 06:33:19 +0000631 Diagnostic &Diags;
Ted Kremenek874d63f2008-01-24 02:02:54 +0000632 ASTContext* Ctx;
Ted Kremenekd55fe522008-02-15 00:35:38 +0000633 bool Visualize;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000634 bool TrimGraph;
Ted Kremeneke603df42008-01-08 18:04:06 +0000635 public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000636 GRSimpleValsVisitor(Diagnostic &diags, const std::string& fname,
Ted Kremenekffe0f432008-03-07 22:58:01 +0000637 bool visualize, bool trim)
638 : CFGVisitor(fname), Diags(diags), Visualize(visualize), TrimGraph(trim){}
Ted Kremeneke603df42008-01-08 18:04:06 +0000639
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000640 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000641 virtual void VisitCFG(CFG& C, Decl&);
Ted Kremenekcb330932008-02-18 21:21:23 +0000642 virtual bool printFuncDeclStart() { return false; }
Ted Kremeneke603df42008-01-08 18:04:06 +0000643 };
644} // end anonymous namespace
645
Ted Kremenekcb330932008-02-18 21:21:23 +0000646ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
647 const std::string& FunctionName,
Ted Kremenekffe0f432008-03-07 22:58:01 +0000648 bool Visualize, bool TrimGraph) {
Ted Kremenekcb330932008-02-18 21:21:23 +0000649
Ted Kremenekffe0f432008-03-07 22:58:01 +0000650 return new GRSimpleValsVisitor(Diags, FunctionName, Visualize, TrimGraph);
Ted Kremeneke603df42008-01-08 18:04:06 +0000651}
652
Ted Kremenek63bbe532008-03-14 17:31:00 +0000653void GRSimpleValsVisitor::VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekcb330932008-02-18 21:21:23 +0000654
Ted Kremenek1c339752008-03-27 17:14:42 +0000655 if (Diags.hasErrorOccurred())
656 return;
657
Ted Kremenek63bbe532008-03-14 17:31:00 +0000658 SourceLocation Loc = CD.getLocation();
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000659
Ted Kremenek71ac9c42008-02-22 19:10:58 +0000660 if (!Loc.isFileID() ||
661 Loc.getFileID() != Ctx->getSourceManager().getMainFileID())
662 return;
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000663
Ted Kremenekcb330932008-02-18 21:21:23 +0000664 if (!Visualize) {
Ted Kremenek63bbe532008-03-14 17:31:00 +0000665
666 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
667 llvm::cerr << "ANALYZE: " << FD->getIdentifier()->getName() << ' '
668 << Ctx->getSourceManager().getSourceName(FD->getLocation())
669 << ' ';
670 }
671 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000672 llvm::cerr << "ANALYZE (ObjC Method): '"
673 << MD->getSelector().getName() << "' "
Ted Kremenek63bbe532008-03-14 17:31:00 +0000674 << Ctx->getSourceManager().getSourceName(MD->getLocation())
675 << ' ';
676 }
Ted Kremenekcb330932008-02-18 21:21:23 +0000677
Ted Kremenekf186f302008-03-31 16:00:32 +0000678#if 0
Ted Kremenekcb330932008-02-18 21:21:23 +0000679 llvm::Timer T("GRSimpleVals");
680 T.startTimer();
Ted Kremenek63bbe532008-03-14 17:31:00 +0000681 unsigned size = RunGRSimpleVals(C, CD, *Ctx, Diags, false, false);
Ted Kremenekcb330932008-02-18 21:21:23 +0000682 T.stopTimer();
Ted Kremenek9dca0622008-02-19 00:22:37 +0000683 llvm::cerr << size << ' ' << T.getWallTime() << '\n';
Ted Kremenekf186f302008-03-31 16:00:32 +0000684#else
685 RunGRSimpleVals(C, CD, *Ctx, Diags, false, false);
686#endif
Ted Kremenekcb330932008-02-18 21:21:23 +0000687 }
688 else {
689 llvm::cerr << '\n';
Ted Kremenek63bbe532008-03-14 17:31:00 +0000690 RunGRSimpleVals(C, CD, *Ctx, Diags, Visualize, TrimGraph);
Ted Kremenekcb330932008-02-18 21:21:23 +0000691 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000692}
693
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000694
695//===----------------------------------------------------------------------===//
696// Core Foundation Reference Counting Checker
697
698namespace {
699 class CFRefCountCheckerVisitor : public CFGVisitor {
700 Diagnostic &Diags;
701 ASTContext* Ctx;
702
703 public:
704 CFRefCountCheckerVisitor(Diagnostic &diags, const std::string& fname)
705 : CFGVisitor(fname), Diags(diags) {}
706
707 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000708 virtual void VisitCFG(CFG& C, Decl&);
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000709 virtual bool printFuncDeclStart() { return false; }
710 };
711} // end anonymous namespace
712
713
714ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
715 const std::string& FunctionName) {
716
717 return new CFRefCountCheckerVisitor(Diags, FunctionName);
718}
719
Ted Kremenek63bbe532008-03-14 17:31:00 +0000720void CFRefCountCheckerVisitor::VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000721
Ted Kremenek63bbe532008-03-14 17:31:00 +0000722 SourceLocation Loc = CD.getLocation();
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000723
724 if (!Loc.isFileID() ||
725 Loc.getFileID() != Ctx->getSourceManager().getMainFileID())
726 return;
727
Ted Kremenek63bbe532008-03-14 17:31:00 +0000728 CheckCFRefCount(C, CD, *Ctx, Diags);
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000729}
730
Ted Kremeneke603df42008-01-08 18:04:06 +0000731//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000732// AST Serializer
733
734namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000735
736class ASTSerializer : public ASTConsumer {
737protected:
738 Diagnostic &Diags;
739 TranslationUnit TU;
740public:
741 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
742 : Diags(diags), TU(LO) {}
743
744 virtual void Initialize(ASTContext &Context) {
745 TU.setContext(&Context);
746 }
747
748 virtual void HandleTopLevelDecl(Decl *D) {
749 if (Diags.hasErrorOccurred())
750 return;
751
752 TU.AddTopLevelDecl(D);
753 }
754};
755
756class SingleFileSerializer : public ASTSerializer {
757 const llvm::sys::Path FName;
758public:
759 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
760 const LangOptions &LO)
761 : ASTSerializer(diags,LO), FName(F) {}
762
763 ~SingleFileSerializer() {
764 EmitASTBitcodeFile(TU,FName);
765 }
766};
767
768class BuildSerializer : public ASTSerializer {
769 llvm::sys::Path EmitDir;
770public:
771 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000772 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000773 : ASTSerializer(diags,LO), EmitDir(dir) {}
774
Ted Kremenek54117722007-12-20 00:34:58 +0000775 ~BuildSerializer() {
776 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
777 unsigned ID = SourceMgr.getMainFileID();
778 assert (ID && "MainFileID not set!");
779 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
780 assert (FE && "No FileEntry for main file.");
781
782 // FIXME: This is not portable to Windows.
783 // FIXME: This logic should probably be moved elsewhere later.
784
Ted Kremenekee533642007-12-20 19:47:16 +0000785 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000786
787 std::vector<char> buf;
788 buf.reserve(strlen(FE->getName())+100);
789
790 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000791 FName.appendComponent(&buf[0]);
792 FName.createDirectoryOnDisk(true);
793 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000794 assert (false && "Could not create 'device' serialization directory.");
795 return;
796 }
Ted Kremenekee533642007-12-20 19:47:16 +0000797
Ted Kremenek54117722007-12-20 00:34:58 +0000798 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000799 FName.appendComponent(&buf[0]);
800 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000801
Ted Kremenekee533642007-12-20 19:47:16 +0000802 // Now emit the sources.
803
Ted Kremenek54117722007-12-20 00:34:58 +0000804 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000805};
806
807
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000808} // end anonymous namespace
809
810
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000811ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000812 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000813 Diagnostic &Diags,
814 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000815
Ted Kremenekf06c9282007-12-19 23:49:37 +0000816 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000817 if (InFile == "-") {
818 llvm::cerr <<
819 "error: Cannot use --serialize with -o for source read from STDIN.\n";
820 return NULL;
821 }
822
Ted Kremenekf06c9282007-12-19 23:49:37 +0000823 // The user specified an AST-emission directory. Determine if the path
824 // is absolute.
825 llvm::sys::Path EmitDir(OutputFile);
826
827 if (!EmitDir.isAbsolute()) {
828 llvm::cerr <<
829 "error: Output directory for --serialize must be an absolute path.\n";
830
831 return NULL;
832 }
833
834 // Create the directory if it does not exist.
835 EmitDir.createDirectoryOnDisk(true);
836 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
837 llvm::cerr <<
838 "error: Could not create output directory for --serialize.\n";
839
840 return NULL;
841 }
842
Ted Kremenek54117722007-12-20 00:34:58 +0000843 // FIXME: We should probably only allow using BuildSerializer when
844 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000845 return new BuildSerializer(EmitDir, Diags, Features);
846 }
847
848 // The user did not specify an output directory for serialized ASTs.
849 // Serialize the translation to a single file whose name is the same
850 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000851
Ted Kremenekf06c9282007-12-19 23:49:37 +0000852 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000853 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000854 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000855}