blob: 0dc669294564a9e825af33e76455f738e1a3359d [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
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000314 ObjCIvarDecl **IDecl = PDecl->getPropertyDecls();
Ted Kremenekea75c552007-11-28 21:32:21 +0000315
316 Out << ' ' << IDecl[0]->getType().getAsString()
317 << ' ' << IDecl[0]->getName();
318
319 for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
320 Out << ", " << IDecl[j]->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000321
Ted Kremenekea75c552007-11-28 21:32:21 +0000322 Out << ";\n";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000323 }
324 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000325
326 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000327 // FIXME: implement the rest...
328}
329
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000330void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000331 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000332 // FIXME: implement the rest...
333}
334
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000335void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000336 Out << "@implementation "
337 << PID->getClassInterface()->getName()
338 << '(' << PID->getName() << ");\n";
339
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000340 // FIXME: implement the rest...
341}
342
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000343void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000344 Out << "@interface "
345 << PID->getClassInterface()->getName()
346 << '(' << PID->getName() << ");\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000347 // FIXME: implement the rest...
348}
349
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000350void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000351 Out << "@compatibility_alias " << AID->getName()
352 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000353}
354
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000355//===----------------------------------------------------------------------===//
356/// ASTPrinter - Pretty-printer of ASTs
357
Chris Lattner3d4997d2007-09-15 23:02:28 +0000358namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000359 class ASTPrinter : public ASTConsumer, public DeclPrinter {
360 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000361 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000362
Chris Lattner3d4997d2007-09-15 23:02:28 +0000363 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000364 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000366 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000367}
Chris Lattner6000dac2007-08-08 22:51:59 +0000368
Ted Kremenekea75c552007-11-28 21:32:21 +0000369ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
370 return new ASTPrinter(out);
371}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000372
373//===----------------------------------------------------------------------===//
374/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000375
376namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000377 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000378 SourceManager *SM;
379 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000380 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000381
Ted Kremenek95041a22007-12-19 22:51:13 +0000382 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000383 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000384 }
Chris Lattner3d4997d2007-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 Kremenekea75c552007-11-28 21:32:21 +0000391 Out << '\n';
392 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000393 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000394 Out << '\n';
Chris Lattner3d4997d2007-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 Kremenekea75c552007-11-28 21:32:21 +0000399 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000400 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000401 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000402 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000403 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000404 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000405 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000406 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000407 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000408 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000409 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000410 } else if (isa<FileScopeAsmDecl>(D)) {
411 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-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 Lattner9fa5e652007-10-06 18:52:10 +0000419 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000420 }
421 }
422 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000423}
424
Chris Lattner3d4997d2007-09-15 23:02:28 +0000425ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
426
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000427//===----------------------------------------------------------------------===//
428/// ASTViewer - AST Visualization
429
Ted Kremenek80de08f2007-09-19 21:29:43 +0000430namespace {
431 class ASTViewer : public ASTConsumer {
432 SourceManager *SM;
433 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000434 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000435 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000436 }
437
438 virtual void HandleTopLevelDecl(Decl *D) {
439 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000440 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000441
442 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000443 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000444 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000445 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000446 }
447 }
Ted Kremenek63bbe532008-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 Kremenek80de08f2007-09-19 21:29:43 +0000457 }
458 };
459}
460
461ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
462
463
Ted Kremenek74bf2c92007-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 Lattnerc0508f92007-09-15 23:21:08 +0000470class CFGVisitor : public ASTConsumer {
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000471 std::string FName;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000472public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000473 CFGVisitor(const std::string& fname) : FName(fname) {}
474 CFGVisitor() : FName("") {}
475
Chris Lattnerc0508f92007-09-15 23:21:08 +0000476 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek63bbe532008-03-14 17:31:00 +0000477 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000478 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000479
480 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000481};
482
483} // end anonymous namespace
484
Chris Lattnerc0508f92007-09-15 23:21:08 +0000485void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000486
Ted Kremenek63bbe532008-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 Lattnerc0508f92007-09-15 23:21:08 +0000496
Ted Kremenek63bbe532008-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 Kremenek74bf2c92007-09-07 23:47:56 +0000503 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000504 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000505
Ted Kremenek63bbe532008-03-14 17:31:00 +0000506 if (!MD->getBody())
507 return;
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000508
509 if (FName.size() > 0 && FName != MD->getSelector().getName())
510 return;
Ted Kremenek63bbe532008-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 Kremenek4102af92008-03-13 03:04:22 +0000519
520 if (C) {
Ted Kremenek63bbe532008-03-14 17:31:00 +0000521 VisitCFG(*C, *D);
Ted Kremenek4102af92008-03-13 03:04:22 +0000522 delete C;
523 }
Ted Kremenek74bf2c92007-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 Kremenek5f39c2d2008-02-22 20:00:31 +0000533 CFGDumper(bool use_graphviz, const std::string& fname)
534 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000535
Ted Kremenek63bbe532008-03-14 17:31:00 +0000536 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000537 if (UseGraphviz)
538 C.viewCFG();
539 else
540 C.dump();
541 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000542 };
543} // end anonymous namespace
544
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000545ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
546 return new CFGDumper(ViewGraphs, FName);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000547}
548
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000549//===----------------------------------------------------------------------===//
550// AnalyzeLiveVariables - perform live variable analysis and dump results
551
552namespace {
553 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000554 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000555 public:
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000556 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
557
Ted Kremenek95041a22007-12-19 22:51:13 +0000558 virtual void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000559 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000560 }
561
Ted Kremenek63bbe532008-03-14 17:31:00 +0000562 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000563 LiveVariables L(C);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000564 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000565 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000566 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000567 };
568} // end anonymous namespace
569
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000570ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
571 return new LivenessVisitor(fname);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000572}
573
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000574//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000575// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000576
577namespace {
578 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000579 Diagnostic &Diags;
580 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000581 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000582 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000583 virtual void Initialize(ASTContext &Context) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000584 Ctx = &Context;
585 }
586
Ted Kremenek63bbe532008-03-14 17:31:00 +0000587 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000588 CheckDeadStores(C, *Ctx, Diags);
Ted Kremenekbffaa832008-01-29 05:13:23 +0000589 }
590
Ted Kremenek567a7e62007-09-07 23:54:15 +0000591 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000592 };
593} // end anonymous namespace
594
Chris Lattnerc0508f92007-09-15 23:21:08 +0000595ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
596 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000597}
Chris Lattner580980b2007-09-16 19:46:59 +0000598
599//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-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 Kremenek95041a22007-12-19 22:51:13 +0000609 virtual void Initialize(ASTContext &Context) {
Ted Kremenek2bf55142007-09-17 20:49:30 +0000610 Ctx = &Context;
611 }
612
Ted Kremenek63bbe532008-03-14 17:31:00 +0000613 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekbffaa832008-01-29 05:13:23 +0000614 CheckUninitializedValues(C, *Ctx, Diags);
615 }
616
Ted Kremenek2bf55142007-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 Kremeneke01c9872008-02-14 22:36:46 +0000626// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremeneke603df42008-01-08 18:04:06 +0000627
628namespace {
Ted Kremeneke01c9872008-02-14 22:36:46 +0000629 class GRSimpleValsVisitor : public CFGVisitor {
Ted Kremenek19227e32008-02-07 06:33:19 +0000630 Diagnostic &Diags;
Ted Kremenek874d63f2008-01-24 02:02:54 +0000631 ASTContext* Ctx;
Ted Kremenekd55fe522008-02-15 00:35:38 +0000632 bool Visualize;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000633 bool TrimGraph;
Ted Kremeneke603df42008-01-08 18:04:06 +0000634 public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000635 GRSimpleValsVisitor(Diagnostic &diags, const std::string& fname,
Ted Kremenekffe0f432008-03-07 22:58:01 +0000636 bool visualize, bool trim)
637 : CFGVisitor(fname), Diags(diags), Visualize(visualize), TrimGraph(trim){}
Ted Kremeneke603df42008-01-08 18:04:06 +0000638
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000639 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000640 virtual void VisitCFG(CFG& C, Decl&);
Ted Kremenekcb330932008-02-18 21:21:23 +0000641 virtual bool printFuncDeclStart() { return false; }
Ted Kremeneke603df42008-01-08 18:04:06 +0000642 };
643} // end anonymous namespace
644
Ted Kremenekcb330932008-02-18 21:21:23 +0000645ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
646 const std::string& FunctionName,
Ted Kremenekffe0f432008-03-07 22:58:01 +0000647 bool Visualize, bool TrimGraph) {
Ted Kremenekcb330932008-02-18 21:21:23 +0000648
Ted Kremenekffe0f432008-03-07 22:58:01 +0000649 return new GRSimpleValsVisitor(Diags, FunctionName, Visualize, TrimGraph);
Ted Kremeneke603df42008-01-08 18:04:06 +0000650}
651
Ted Kremenek63bbe532008-03-14 17:31:00 +0000652void GRSimpleValsVisitor::VisitCFG(CFG& C, Decl& CD) {
Ted Kremenekcb330932008-02-18 21:21:23 +0000653
Ted Kremenek63bbe532008-03-14 17:31:00 +0000654 SourceLocation Loc = CD.getLocation();
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000655
Ted Kremenek71ac9c42008-02-22 19:10:58 +0000656 if (!Loc.isFileID() ||
657 Loc.getFileID() != Ctx->getSourceManager().getMainFileID())
658 return;
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000659
Ted Kremenekcb330932008-02-18 21:21:23 +0000660 if (!Visualize) {
Ted Kremenek63bbe532008-03-14 17:31:00 +0000661
662 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
663 llvm::cerr << "ANALYZE: " << FD->getIdentifier()->getName() << ' '
664 << Ctx->getSourceManager().getSourceName(FD->getLocation())
665 << ' ';
666 }
667 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000668 llvm::cerr << "ANALYZE (ObjC Method): '"
669 << MD->getSelector().getName() << "' "
Ted Kremenek63bbe532008-03-14 17:31:00 +0000670 << Ctx->getSourceManager().getSourceName(MD->getLocation())
671 << ' ';
672 }
Ted Kremenekcb330932008-02-18 21:21:23 +0000673
674 llvm::Timer T("GRSimpleVals");
675 T.startTimer();
Ted Kremenek63bbe532008-03-14 17:31:00 +0000676 unsigned size = RunGRSimpleVals(C, CD, *Ctx, Diags, false, false);
Ted Kremenekcb330932008-02-18 21:21:23 +0000677 T.stopTimer();
Ted Kremenek9dca0622008-02-19 00:22:37 +0000678 llvm::cerr << size << ' ' << T.getWallTime() << '\n';
Ted Kremenekcb330932008-02-18 21:21:23 +0000679 }
680 else {
681 llvm::cerr << '\n';
Ted Kremenek63bbe532008-03-14 17:31:00 +0000682 RunGRSimpleVals(C, CD, *Ctx, Diags, Visualize, TrimGraph);
Ted Kremenekcb330932008-02-18 21:21:23 +0000683 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000684}
685
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000686
687//===----------------------------------------------------------------------===//
688// Core Foundation Reference Counting Checker
689
690namespace {
691 class CFRefCountCheckerVisitor : public CFGVisitor {
692 Diagnostic &Diags;
693 ASTContext* Ctx;
694
695 public:
696 CFRefCountCheckerVisitor(Diagnostic &diags, const std::string& fname)
697 : CFGVisitor(fname), Diags(diags) {}
698
699 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000700 virtual void VisitCFG(CFG& C, Decl&);
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000701 virtual bool printFuncDeclStart() { return false; }
702 };
703} // end anonymous namespace
704
705
706ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
707 const std::string& FunctionName) {
708
709 return new CFRefCountCheckerVisitor(Diags, FunctionName);
710}
711
Ted Kremenek63bbe532008-03-14 17:31:00 +0000712void CFRefCountCheckerVisitor::VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000713
Ted Kremenek63bbe532008-03-14 17:31:00 +0000714 SourceLocation Loc = CD.getLocation();
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000715
716 if (!Loc.isFileID() ||
717 Loc.getFileID() != Ctx->getSourceManager().getMainFileID())
718 return;
719
Ted Kremenek63bbe532008-03-14 17:31:00 +0000720 CheckCFRefCount(C, CD, *Ctx, Diags);
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000721}
722
Ted Kremeneke603df42008-01-08 18:04:06 +0000723//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000724// AST Serializer
725
726namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000727
728class ASTSerializer : public ASTConsumer {
729protected:
730 Diagnostic &Diags;
731 TranslationUnit TU;
732public:
733 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
734 : Diags(diags), TU(LO) {}
735
736 virtual void Initialize(ASTContext &Context) {
737 TU.setContext(&Context);
738 }
739
740 virtual void HandleTopLevelDecl(Decl *D) {
741 if (Diags.hasErrorOccurred())
742 return;
743
744 TU.AddTopLevelDecl(D);
745 }
746};
747
748class SingleFileSerializer : public ASTSerializer {
749 const llvm::sys::Path FName;
750public:
751 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
752 const LangOptions &LO)
753 : ASTSerializer(diags,LO), FName(F) {}
754
755 ~SingleFileSerializer() {
756 EmitASTBitcodeFile(TU,FName);
757 }
758};
759
760class BuildSerializer : public ASTSerializer {
761 llvm::sys::Path EmitDir;
762public:
763 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000764 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000765 : ASTSerializer(diags,LO), EmitDir(dir) {}
766
Ted Kremenek54117722007-12-20 00:34:58 +0000767 ~BuildSerializer() {
768 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
769 unsigned ID = SourceMgr.getMainFileID();
770 assert (ID && "MainFileID not set!");
771 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
772 assert (FE && "No FileEntry for main file.");
773
774 // FIXME: This is not portable to Windows.
775 // FIXME: This logic should probably be moved elsewhere later.
776
Ted Kremenekee533642007-12-20 19:47:16 +0000777 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000778
779 std::vector<char> buf;
780 buf.reserve(strlen(FE->getName())+100);
781
782 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000783 FName.appendComponent(&buf[0]);
784 FName.createDirectoryOnDisk(true);
785 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000786 assert (false && "Could not create 'device' serialization directory.");
787 return;
788 }
Ted Kremenekee533642007-12-20 19:47:16 +0000789
Ted Kremenek54117722007-12-20 00:34:58 +0000790 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000791 FName.appendComponent(&buf[0]);
792 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000793
Ted Kremenekee533642007-12-20 19:47:16 +0000794 // Now emit the sources.
795
Ted Kremenek54117722007-12-20 00:34:58 +0000796 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000797};
798
799
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000800} // end anonymous namespace
801
802
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000803ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000804 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000805 Diagnostic &Diags,
806 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000807
Ted Kremenekf06c9282007-12-19 23:49:37 +0000808 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000809 if (InFile == "-") {
810 llvm::cerr <<
811 "error: Cannot use --serialize with -o for source read from STDIN.\n";
812 return NULL;
813 }
814
Ted Kremenekf06c9282007-12-19 23:49:37 +0000815 // The user specified an AST-emission directory. Determine if the path
816 // is absolute.
817 llvm::sys::Path EmitDir(OutputFile);
818
819 if (!EmitDir.isAbsolute()) {
820 llvm::cerr <<
821 "error: Output directory for --serialize must be an absolute path.\n";
822
823 return NULL;
824 }
825
826 // Create the directory if it does not exist.
827 EmitDir.createDirectoryOnDisk(true);
828 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
829 llvm::cerr <<
830 "error: Could not create output directory for --serialize.\n";
831
832 return NULL;
833 }
834
Ted Kremenek54117722007-12-20 00:34:58 +0000835 // FIXME: We should probably only allow using BuildSerializer when
836 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000837 return new BuildSerializer(EmitDir, Diags, Features);
838 }
839
840 // The user did not specify an output directory for serialized ASTs.
841 // Serialize the translation to a single file whose name is the same
842 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000843
Ted Kremenekf06c9282007-12-19 23:49:37 +0000844 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000845 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000846 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000847}