blob: 596fefc8e575c7849845df8f1647102b71cf6202 [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 Kremenek4dc41cc2008-03-31 18:26:32 +000015#include "HTMLDiagnostics.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000017#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.h"
Ted Kremenekfddd5182007-08-21 21:42:03 +000022#include "clang/AST/CFG.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000023#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek055c2752007-09-06 23:00:42 +000024#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekd71ed262008-04-10 22:16:52 +000025#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
26#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000027#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000028#include "llvm/Support/Timer.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000029#include "llvm/ADT/OwningPtr.h"
30
Chris Lattner6000dac2007-08-08 22:51:59 +000031using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033//===----------------------------------------------------------------------===//
34/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000035
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000036namespace {
37 class DeclPrinter {
38 public:
Ted Kremenekea75c552007-11-28 21:32:21 +000039 std::ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000040
Chris Lattner4b1daf02008-01-10 01:43:14 +000041 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekea75c552007-11-28 21:32:21 +000042 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000043
Chris Lattneref5a85d2008-01-02 21:04:16 +000044 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000045 void PrintFunctionDeclStart(FunctionDecl *FD);
46 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000047 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000048 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
49 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
50 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
51 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
52 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
53 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
54 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000055 };
56} // end anonymous namespace
57
Chris Lattneref5a85d2008-01-02 21:04:16 +000058void DeclPrinter:: PrintDecl(Decl *D) {
59 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
60 PrintFunctionDeclStart(FD);
61
62 if (FD->getBody()) {
63 Out << ' ';
64 FD->getBody()->printPretty(Out);
65 Out << '\n';
66 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000067 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000068 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000069 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000070 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
71 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000072 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
73 PrintObjCInterfaceDecl(OID);
74 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
75 PrintObjCProtocolDecl(PID);
76 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000077 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000078 Out << "@protocol ";
79 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000080 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000081 if (i) Out << ", ";
82 Out << D->getName();
83 }
84 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000085 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000086 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000087 PrintObjCImplementationDecl(OID);
88 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000089 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000090 PrintObjCCategoryImplDecl(OID);
91 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000092 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000093 PrintObjCCategoryDecl(OID);
94 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000095 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000096 PrintObjCCompatibleAliasDecl(OID);
97 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000098 Out << "@class [printing todo]\n";
99 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
100 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
101 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
102 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000103 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
104 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000105 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
106 Out << "asm(";
107 AD->getAsmString()->printPretty(Out);
108 Out << ")\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000109 } else {
110 assert(0 && "Unknown decl type!");
111 }
112}
113
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000114void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 bool HasBody = FD->getBody();
116
Ted Kremenekea75c552007-11-28 21:32:21 +0000117 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000118
119 switch (FD->getStorageClass()) {
120 default: assert(0 && "Unknown storage class");
121 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000122 case FunctionDecl::Extern: Out << "extern "; break;
123 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000124 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000125 }
126
127 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000128 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000131 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000132
Chris Lattner0d6ca112007-12-03 21:43:25 +0000133 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 Proto += "(";
135 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
136 if (i) Proto += ", ";
137 std::string ParamStr;
138 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
139
140 FT->getArgType(i).getAsStringInternal(ParamStr);
141 Proto += ParamStr;
142 }
143
144 if (FT->isVariadic()) {
145 if (FD->getNumParams()) Proto += ", ";
146 Proto += "...";
147 }
148 Proto += ")";
149 } else {
150 assert(isa<FunctionTypeNoProto>(AFT));
151 Proto += "()";
152 }
153
154 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000155 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000156
Chris Lattner6000dac2007-08-08 22:51:59 +0000157 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000158 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000159 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000160}
161
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000162void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 std::string S = TD->getName();
164 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000165 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000166}
167
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000168void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
169 const char *l;
170 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
171 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000172 else {
173 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
174 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000175 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000176 }
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000177 Out << "extern \"" << l << "\" { ";
178 PrintDecl(LS->getDecl());
179 Out << "}\n";
180}
181
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000182void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000183 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000184 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000185 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000186 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000187 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000188 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000189 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000190 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000191
Chris Lattner58cce3b2008-03-16 01:07:14 +0000192 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000193 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000194 // FIXME: selector is missing here!
195 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000196 }
197}
198
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000199void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000200 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000201 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000202
203 if (SID)
204 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000205 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000206 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000207
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000208 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000209 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000210 ObjCMethodDecl *OMD = *I;
211 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000212 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000213 Out << ' ';
214 OMD->getBody()->printPretty(Out);
215 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000216 }
217 }
218
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000219 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000220 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000221 ObjCMethodDecl *OMD = *I;
222 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000223 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000224 Out << ' ';
225 OMD->getBody()->printPretty(Out);
226 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000227 }
228 }
229
Ted Kremenekea75c552007-11-28 21:32:21 +0000230 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000231}
232
233
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000234void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000235 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000236 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000237
238 if (SID)
239 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000240 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000241 Out << "@interface " << I;
242
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000243 // Protocols?
244 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000245
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000246 if (count > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000247 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000248 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000249 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000250 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000251
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000252 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000253 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000254 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000255 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000256
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000257 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000258 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000259 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000260 E = OID->ivar_end(); I != E; ++I) {
261 Out << '\t' << (*I)->getType().getAsString()
262 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000263 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000264 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000265 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000266
267 int NumProperties = OID->getNumPropertyDecl();
268 if (NumProperties > 0) {
269 for (int i = 0; i < NumProperties; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000270 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekea75c552007-11-28 21:32:21 +0000271 Out << "@property";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000272 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000273 bool first = true;
Ted Kremenekea75c552007-11-28 21:32:21 +0000274 Out << " (";
Chris Lattner4b1daf02008-01-10 01:43:14 +0000275 if (PDecl->getPropertyAttributes() &
276 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000277 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000278 first = false;
279 }
280
Chris Lattner4b1daf02008-01-10 01:43:14 +0000281 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000282 Out << (first ? ' ' : ',') << "getter = "
283 << PDecl->getGetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000284 first = false;
285 }
Chris Lattner4b1daf02008-01-10 01:43:14 +0000286 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000287 Out << (first ? ' ' : ',') << "setter = "
288 << PDecl->getSetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000289 first = false;
290 }
291
Chris Lattner4b1daf02008-01-10 01:43:14 +0000292 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000293 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000294 first = false;
295 }
296
Chris Lattner4b1daf02008-01-10 01:43:14 +0000297 if (PDecl->getPropertyAttributes() &
298 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000299 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000300 first = false;
301 }
302
Chris Lattner4b1daf02008-01-10 01:43:14 +0000303 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000304 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000305 first = false;
306 }
307
Chris Lattner4b1daf02008-01-10 01:43:14 +0000308 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000309 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000310 first = false;
311 }
312
Chris Lattner4b1daf02008-01-10 01:43:14 +0000313 if (PDecl->getPropertyAttributes() &
314 ObjCPropertyDecl::OBJC_PR_nonatomic) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000315 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000316 first = false;
317 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000318 Out << " )";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000319 }
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000320 Out << ' ' << PDecl->getType().getAsString()
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000321 << ' ' << PDecl->getName();
322
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 Jahanian7e7e3872008-04-16 21:08:45 +0000348 // Output property declarations.
349 int NumProperties = PID->getNumPropertyDecl();
350 if (NumProperties > 0) {
351 for (int i = 0; i < NumProperties; i++) {
352 ObjCPropertyDecl *PDecl = PID->getPropertyDecl()[i];
353 Out << "@property";
354 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
355 bool first = true;
356 Out << " (";
357 if (PDecl->getPropertyAttributes() &
358 ObjCPropertyDecl::OBJC_PR_readonly) {
359 Out << (first ? ' ' : ',') << "readonly";
360 first = false;
361 }
362
363 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
364 Out << (first ? ' ' : ',') << "getter = "
365 << PDecl->getGetterName()->getName();
366 first = false;
367 }
368 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
369 Out << (first ? ' ' : ',') << "setter = "
370 << PDecl->getSetterName()->getName();
371 first = false;
372 }
373
374 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
375 Out << (first ? ' ' : ',') << "assign";
376 first = false;
377 }
378
379 if (PDecl->getPropertyAttributes() &
380 ObjCPropertyDecl::OBJC_PR_readwrite) {
381 Out << (first ? ' ' : ',') << "readwrite";
382 first = false;
383 }
384
385 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
386 Out << (first ? ' ' : ',') << "retain";
387 first = false;
388 }
389
390 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
391 Out << (first ? ' ' : ',') << "copy";
392 first = false;
393 }
394
395 if (PDecl->getPropertyAttributes() &
396 ObjCPropertyDecl::OBJC_PR_nonatomic) {
397 Out << (first ? ' ' : ',') << "nonatomic";
398 first = false;
399 }
400 Out << " )";
401 }
402 Out << ' ' << PDecl->getType().getAsString()
403 << ' ' << PDecl->getName();
404
405 Out << ";\n";
406 }
407 }
408
409 Out << "@end\n";
410
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000411 // FIXME: implement the rest...
412}
413
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000414void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000415 Out << "@compatibility_alias " << AID->getName()
416 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000417}
418
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000419//===----------------------------------------------------------------------===//
420/// ASTPrinter - Pretty-printer of ASTs
421
Chris Lattner3d4997d2007-09-15 23:02:28 +0000422namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000423 class ASTPrinter : public ASTConsumer, public DeclPrinter {
424 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000425 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000426
Chris Lattner3d4997d2007-09-15 23:02:28 +0000427 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000428 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000430 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000431}
Chris Lattner6000dac2007-08-08 22:51:59 +0000432
Ted Kremenekea75c552007-11-28 21:32:21 +0000433ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
434 return new ASTPrinter(out);
435}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000436
437//===----------------------------------------------------------------------===//
438/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000439
440namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000441 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000442 SourceManager *SM;
443 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000444 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000445
Ted Kremenek95041a22007-12-19 22:51:13 +0000446 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000447 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000448 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000449
450 virtual void HandleTopLevelDecl(Decl *D) {
451 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
452 PrintFunctionDeclStart(FD);
453
454 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000455 Out << '\n';
456 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000457 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000458 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000459 }
460 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
461 PrintTypeDefDecl(TD);
462 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000463 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000464 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000465 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000466 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000467 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000468 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000469 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000470 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000471 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000472 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000473 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000474 } else if (isa<FileScopeAsmDecl>(D)) {
475 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000476 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
477 Out << "Read objc method decl: '" << MD->getSelector().getName()
478 << "'\n";
479 } else if (isa<ObjCImplementationDecl>(D)) {
480 Out << "Read objc implementation decl\n";
481 }
482 else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000483 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000484 }
485 }
486 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000487}
488
Chris Lattner3d4997d2007-09-15 23:02:28 +0000489ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
490
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000491//===----------------------------------------------------------------------===//
492/// ASTViewer - AST Visualization
493
Ted Kremenek80de08f2007-09-19 21:29:43 +0000494namespace {
495 class ASTViewer : public ASTConsumer {
496 SourceManager *SM;
497 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000498 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000499 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000500 }
501
502 virtual void HandleTopLevelDecl(Decl *D) {
503 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000504 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000505
506 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000507 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000508 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000509 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000510 }
511 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000512 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
513 DeclPrinter().PrintObjCMethodDecl(MD);
514
515 if (MD->getBody()) {
516 llvm::cerr << '\n';
517 MD->getBody()->viewAST();
518 llvm::cerr << '\n';
519 }
520 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000521 }
522 };
523}
524
525ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
526
527
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000528//===----------------------------------------------------------------------===//
529// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
530// the CFGs for all function definitions.
531
532namespace {
533
Chris Lattnerc0508f92007-09-15 23:21:08 +0000534class CFGVisitor : public ASTConsumer {
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000535 std::string FName;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000536public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000537 CFGVisitor(const std::string& fname) : FName(fname) {}
538 CFGVisitor() : FName("") {}
539
Chris Lattnerc0508f92007-09-15 23:21:08 +0000540 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek63bbe532008-03-14 17:31:00 +0000541 virtual void VisitCFG(CFG& C, Decl& CD) = 0;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000542 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000543
544 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000545};
546
547} // end anonymous namespace
548
Chris Lattnerc0508f92007-09-15 23:21:08 +0000549void CFGVisitor::HandleTopLevelDecl(Decl *D) {
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000550
Ted Kremenek63bbe532008-03-14 17:31:00 +0000551 CFG *C = NULL;
552
553 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
554
555 if (!FD->getBody())
556 return;
557
558 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
559 return;
Chris Lattnerc0508f92007-09-15 23:21:08 +0000560
Ted Kremenek63bbe532008-03-14 17:31:00 +0000561 if (printFuncDeclStart()) {
562 DeclPrinter().PrintFunctionDeclStart(FD);
563 llvm::cerr << '\n';
564 }
565
566 C = CFG::buildCFG(FD->getBody());
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000567 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000568 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000569
Ted Kremenek63bbe532008-03-14 17:31:00 +0000570 if (!MD->getBody())
571 return;
Ted Kremenek1b9df4c2008-03-14 18:14:50 +0000572
573 if (FName.size() > 0 && FName != MD->getSelector().getName())
574 return;
Ted Kremenek63bbe532008-03-14 17:31:00 +0000575
576 if (printFuncDeclStart()) {
577 DeclPrinter().PrintObjCMethodDecl(MD);
578 llvm::cerr << '\n';
579 }
580
581 C = CFG::buildCFG(MD->getBody());
582 }
Ted Kremenek4102af92008-03-13 03:04:22 +0000583
584 if (C) {
Ted Kremenek63bbe532008-03-14 17:31:00 +0000585 VisitCFG(*C, *D);
Ted Kremenek4102af92008-03-13 03:04:22 +0000586 delete C;
587 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000588}
589
590//===----------------------------------------------------------------------===//
591// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
592
593namespace {
594 class CFGDumper : public CFGVisitor {
595 const bool UseGraphviz;
596 public:
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000597 CFGDumper(bool use_graphviz, const std::string& fname)
598 : CFGVisitor(fname), UseGraphviz(use_graphviz) {}
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000599
Ted Kremenek63bbe532008-03-14 17:31:00 +0000600 virtual void VisitCFG(CFG& C, Decl&) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000601 if (UseGraphviz)
602 C.viewCFG();
603 else
604 C.dump();
605 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000606 };
607} // end anonymous namespace
608
Ted Kremenek5f39c2d2008-02-22 20:00:31 +0000609ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) {
610 return new CFGDumper(ViewGraphs, FName);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000611}
612
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000613//===----------------------------------------------------------------------===//
614// AnalyzeLiveVariables - perform live variable analysis and dump results
615
616namespace {
617 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000618 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000619 public:
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000620 LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {}
621
Ted Kremenek95041a22007-12-19 22:51:13 +0000622 virtual void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000623 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000624 }
625
Ted Kremenek63bbe532008-03-14 17:31:00 +0000626 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000627 LiveVariables L(C);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000628 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000629 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000630 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000631 };
632} // end anonymous namespace
633
Ted Kremenekbfc10c92008-02-22 20:13:09 +0000634ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) {
635 return new LivenessVisitor(fname);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000636}
637
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000638//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000639// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000640
641namespace {
642 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000643 Diagnostic &Diags;
644 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000645 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000646 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000647 virtual void Initialize(ASTContext &Context) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000648 Ctx = &Context;
649 }
650
Ted Kremenek63bbe532008-03-14 17:31:00 +0000651 virtual void VisitCFG(CFG& C, Decl& CD) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000652 CheckDeadStores(C, *Ctx, Diags);
Ted Kremenekbffaa832008-01-29 05:13:23 +0000653 }
654
Ted Kremenek567a7e62007-09-07 23:54:15 +0000655 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000656 };
657} // end anonymous namespace
658
Chris Lattnerc0508f92007-09-15 23:21:08 +0000659ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
660 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000661}
Chris Lattner580980b2007-09-16 19:46:59 +0000662
663//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000664// Unitialized Values - run checker to flag potential uses of uninitalized
665// variables.
666
667namespace {
668 class UninitValsVisitor : public CFGVisitor {
669 Diagnostic &Diags;
670 ASTContext *Ctx;
671 public:
672 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000673 virtual void Initialize(ASTContext &Context) {
Ted Kremenek2bf55142007-09-17 20:49:30 +0000674 Ctx = &Context;
675 }
676
Ted Kremenek63bbe532008-03-14 17:31:00 +0000677 virtual void VisitCFG(CFG& C, Decl&) {
Ted Kremenekbffaa832008-01-29 05:13:23 +0000678 CheckUninitializedValues(C, *Ctx, Diags);
679 }
680
Ted Kremenek2bf55142007-09-17 20:49:30 +0000681 virtual bool printFuncDeclStart() { return false; }
682 };
683} // end anonymous namespace
684
685ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
686 return new UninitValsVisitor(Diags);
687}
688
689//===----------------------------------------------------------------------===//
Ted Kremenek47abe762008-04-16 16:39:56 +0000690// CheckerConsumer - Generic Driver for running intra-procedural path-sensitive
Ted Kremenekd71ed262008-04-10 22:16:52 +0000691// analyses.
692
693namespace {
694
695class CheckerConsumer : public CFGVisitor {
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000696protected:
Ted Kremenekd71ed262008-04-10 22:16:52 +0000697 Diagnostic &Diags;
698 ASTContext* Ctx;
Ted Kremenek47abe762008-04-16 16:39:56 +0000699 Preprocessor* PP;
Ted Kremenekd71ed262008-04-10 22:16:52 +0000700 const std::string& HTMLDir;
701 bool Visualize;
702 bool TrimGraph;
703 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenek55af98c2008-04-14 18:40:58 +0000704 bool AnalyzeAll;
Ted Kremenekd71ed262008-04-10 22:16:52 +0000705public:
Ted Kremenek47abe762008-04-16 16:39:56 +0000706 CheckerConsumer(Diagnostic &diags, Preprocessor* pp,
707 const std::string& fname,
708 const std::string& htmldir,
709 bool visualize, bool trim, bool analyzeAll)
710 : CFGVisitor(fname), Diags(diags), PP(pp), HTMLDir(htmldir),
Ted Kremenek55af98c2008-04-14 18:40:58 +0000711 Visualize(visualize), TrimGraph(trim), AnalyzeAll(analyzeAll) {}
Ted Kremenekd71ed262008-04-10 22:16:52 +0000712
713 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
714 virtual void VisitCFG(CFG& C, Decl&);
715 virtual bool printFuncDeclStart() { return false; }
716
717 virtual const char* getCheckerName() = 0;
718 virtual GRTransferFuncs* getTransferFunctions() = 0;
719};
720} // end anonymous namespace
721
722void CheckerConsumer::VisitCFG(CFG& C, Decl& CD) {
723
724 if (Diags.hasErrorOccurred())
725 return;
726
727 SourceLocation Loc = CD.getLocation();
728
Ted Kremenek55af98c2008-04-14 18:40:58 +0000729 if (!Loc.isFileID())
730 return;
731
Ted Kremenek080c40b2008-04-14 21:14:41 +0000732 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
Ted Kremenekd71ed262008-04-10 22:16:52 +0000733 return;
734
735 // Lazily create the diagnostic client.
736
737 if (!HTMLDir.empty() && PD.get() == NULL)
Ted Kremenek47abe762008-04-16 16:39:56 +0000738 PD.reset(CreateHTMLDiagnosticClient(HTMLDir, PP));
Ted Kremenekd71ed262008-04-10 22:16:52 +0000739
740
741 if (!Visualize) {
742
743 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) {
744 llvm::cerr << "ANALYZE: "
745 << Ctx->getSourceManager().getSourceName(FD->getLocation())
746 << ' '
747 << FD->getIdentifier()->getName()
748 << '\n';
749 }
750 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
751 llvm::cerr << "ANALYZE (ObjC Method): "
752 << Ctx->getSourceManager().getSourceName(MD->getLocation())
753 << " '"
754 << MD->getSelector().getName() << "'\n";
755 }
756 }
757 else
758 llvm::cerr << '\n';
759
760 // Construct the analysis engine.
761 GRExprEngine Eng(C, CD, *Ctx);
762
763 // Set base transfer functions.
764 llvm::OwningPtr<GRTransferFuncs> TF(getTransferFunctions());
765 Eng.setTransferFunctions(TF.get());
766
767 // Execute the worklist algorithm.
768 Eng.ExecuteWorkList();
769
770 // Display warnings.
771 Eng.EmitWarnings(Diags, PD.get());
772
773#ifndef NDEBUG
774 if (Visualize) Eng.ViewGraph(TrimGraph);
775#endif
776}
777
778//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +0000779// GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremeneke603df42008-01-08 18:04:06 +0000780
781namespace {
Ted Kremenekd71ed262008-04-10 22:16:52 +0000782class GRSimpleValsVisitor : public CheckerConsumer {
783public:
Ted Kremenek47abe762008-04-16 16:39:56 +0000784 GRSimpleValsVisitor(Diagnostic &diags, Preprocessor* pp,
785 const std::string& fname, const std::string& htmldir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000786 bool visualize, bool trim, bool analyzeAll)
Ted Kremenek47abe762008-04-16 16:39:56 +0000787 : CheckerConsumer(diags, pp, fname, htmldir, visualize, trim, analyzeAll) {}
Ted Kremenekd71ed262008-04-10 22:16:52 +0000788
789 virtual const char* getCheckerName() { return "GRSimpleVals"; }
790
791 virtual GRTransferFuncs* getTransferFunctions() {
792 return MakeGRSimpleValsTF();
793 }
794};
Ted Kremeneke603df42008-01-08 18:04:06 +0000795} // end anonymous namespace
796
Ted Kremenekcb330932008-02-18 21:21:23 +0000797ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags,
Ted Kremenek47abe762008-04-16 16:39:56 +0000798 Preprocessor* PP,
Ted Kremenekcb330932008-02-18 21:21:23 +0000799 const std::string& FunctionName,
Ted Kremenek4dc41cc2008-03-31 18:26:32 +0000800 const std::string& HTMLDir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000801 bool Visualize, bool TrimGraph,
802 bool AnalyzeAll) {
Ted Kremenekcb330932008-02-18 21:21:23 +0000803
Ted Kremenek47abe762008-04-16 16:39:56 +0000804 return new GRSimpleValsVisitor(Diags, PP, FunctionName, HTMLDir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000805 Visualize, TrimGraph, AnalyzeAll);
Ted Kremeneke603df42008-01-08 18:04:06 +0000806}
807
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000808
809//===----------------------------------------------------------------------===//
810// Core Foundation Reference Counting Checker
811
812namespace {
Ted Kremenekd71ed262008-04-10 22:16:52 +0000813class CFRefCountCheckerVisitor : public CheckerConsumer {
814public:
Ted Kremenek47abe762008-04-16 16:39:56 +0000815 CFRefCountCheckerVisitor(Diagnostic &diags, Preprocessor* pp,
816 const std::string& fname,
817 const std::string& htmldir,
818 bool visualize, bool trim, bool analyzeAll)
819 : CheckerConsumer(diags, pp, fname, htmldir, visualize, trim, analyzeAll) {}
Ted Kremenekd71ed262008-04-10 22:16:52 +0000820
821 virtual const char* getCheckerName() { return "CFRefCountChecker"; }
822
823 virtual GRTransferFuncs* getTransferFunctions() {
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000824 return MakeCFRefCountTF(*Ctx);
Ted Kremenekd71ed262008-04-10 22:16:52 +0000825 }
826};
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000827} // end anonymous namespace
828
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000829ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags,
Ted Kremenek47abe762008-04-16 16:39:56 +0000830 Preprocessor* PP,
Ted Kremenek4dc41cc2008-03-31 18:26:32 +0000831 const std::string& FunctionName,
Ted Kremenekd71ed262008-04-10 22:16:52 +0000832 const std::string& HTMLDir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000833 bool Visualize, bool TrimGraph,
834 bool AnalyzeAll) {
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000835
Ted Kremenek47abe762008-04-16 16:39:56 +0000836 return new CFRefCountCheckerVisitor(Diags, PP, FunctionName, HTMLDir,
Ted Kremenek55af98c2008-04-14 18:40:58 +0000837 Visualize, TrimGraph, AnalyzeAll);
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000838}
839
Ted Kremeneke603df42008-01-08 18:04:06 +0000840//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000841// AST Serializer
842
843namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000844
845class ASTSerializer : public ASTConsumer {
846protected:
847 Diagnostic &Diags;
848 TranslationUnit TU;
849public:
850 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
851 : Diags(diags), TU(LO) {}
852
853 virtual void Initialize(ASTContext &Context) {
854 TU.setContext(&Context);
855 }
856
857 virtual void HandleTopLevelDecl(Decl *D) {
858 if (Diags.hasErrorOccurred())
859 return;
860
861 TU.AddTopLevelDecl(D);
862 }
863};
864
865class SingleFileSerializer : public ASTSerializer {
866 const llvm::sys::Path FName;
867public:
868 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
869 const LangOptions &LO)
870 : ASTSerializer(diags,LO), FName(F) {}
871
872 ~SingleFileSerializer() {
873 EmitASTBitcodeFile(TU,FName);
874 }
875};
876
877class BuildSerializer : public ASTSerializer {
878 llvm::sys::Path EmitDir;
879public:
880 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000881 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000882 : ASTSerializer(diags,LO), EmitDir(dir) {}
883
Ted Kremenek54117722007-12-20 00:34:58 +0000884 ~BuildSerializer() {
885 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
886 unsigned ID = SourceMgr.getMainFileID();
887 assert (ID && "MainFileID not set!");
888 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
889 assert (FE && "No FileEntry for main file.");
890
891 // FIXME: This is not portable to Windows.
892 // FIXME: This logic should probably be moved elsewhere later.
893
Ted Kremenekee533642007-12-20 19:47:16 +0000894 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000895
896 std::vector<char> buf;
897 buf.reserve(strlen(FE->getName())+100);
898
899 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000900 FName.appendComponent(&buf[0]);
901 FName.createDirectoryOnDisk(true);
902 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000903 assert (false && "Could not create 'device' serialization directory.");
904 return;
905 }
Ted Kremenekee533642007-12-20 19:47:16 +0000906
Ted Kremenek54117722007-12-20 00:34:58 +0000907 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000908 FName.appendComponent(&buf[0]);
909 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000910
Ted Kremenekee533642007-12-20 19:47:16 +0000911 // Now emit the sources.
912
Ted Kremenek54117722007-12-20 00:34:58 +0000913 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000914};
915
916
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000917} // end anonymous namespace
918
919
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000920ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000921 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000922 Diagnostic &Diags,
923 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000924
Ted Kremenekf06c9282007-12-19 23:49:37 +0000925 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000926 if (InFile == "-") {
927 llvm::cerr <<
928 "error: Cannot use --serialize with -o for source read from STDIN.\n";
929 return NULL;
930 }
931
Ted Kremenekf06c9282007-12-19 23:49:37 +0000932 // The user specified an AST-emission directory. Determine if the path
933 // is absolute.
934 llvm::sys::Path EmitDir(OutputFile);
935
936 if (!EmitDir.isAbsolute()) {
937 llvm::cerr <<
938 "error: Output directory for --serialize must be an absolute path.\n";
939
940 return NULL;
941 }
942
943 // Create the directory if it does not exist.
944 EmitDir.createDirectoryOnDisk(true);
945 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
946 llvm::cerr <<
947 "error: Could not create output directory for --serialize.\n";
948
949 return NULL;
950 }
951
Ted Kremenek54117722007-12-20 00:34:58 +0000952 // FIXME: We should probably only allow using BuildSerializer when
953 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000954 return new BuildSerializer(EmitDir, Diags, Features);
955 }
956
957 // The user did not specify an output directory for serialized ASTs.
958 // Serialize the translation to a single file whose name is the same
959 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000960
Ted Kremenekf06c9282007-12-19 23:49:37 +0000961 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000962 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000963 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000964}