blob: 3868e7d75c3836535e35eef77ce44dd5e1cc0161 [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 Kremenekee985462008-01-16 18:18:48 +000023#include "clang/Analysis/Analyses/GRConstants.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"
Chris Lattner6000dac2007-08-08 22:51:59 +000026using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000027
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000028//===----------------------------------------------------------------------===//
29/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000030
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000031namespace {
32 class DeclPrinter {
33 public:
Ted Kremenekea75c552007-11-28 21:32:21 +000034 std::ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000035
Chris Lattner4b1daf02008-01-10 01:43:14 +000036 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekea75c552007-11-28 21:32:21 +000037 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000038
Chris Lattneref5a85d2008-01-02 21:04:16 +000039 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000040 void PrintFunctionDeclStart(FunctionDecl *FD);
41 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000042 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000043 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
44 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
45 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
46 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
47 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
48 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
49 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000050 };
51} // end anonymous namespace
52
Chris Lattneref5a85d2008-01-02 21:04:16 +000053void DeclPrinter:: PrintDecl(Decl *D) {
54 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
55 PrintFunctionDeclStart(FD);
56
57 if (FD->getBody()) {
58 Out << ' ';
59 FD->getBody()->printPretty(Out);
60 Out << '\n';
61 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000062 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000063 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000064 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000065 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
66 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000067 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
68 PrintObjCInterfaceDecl(OID);
69 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
70 PrintObjCProtocolDecl(PID);
71 } else if (ObjCForwardProtocolDecl *OFPD =
72 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000073 Out << "@protocol ";
74 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000075 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000076 if (i) Out << ", ";
77 Out << D->getName();
78 }
79 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000080 } else if (ObjCImplementationDecl *OID =
81 dyn_cast<ObjCImplementationDecl>(D)) {
82 PrintObjCImplementationDecl(OID);
83 } else if (ObjCCategoryImplDecl *OID =
84 dyn_cast<ObjCCategoryImplDecl>(D)) {
85 PrintObjCCategoryImplDecl(OID);
86 } else if (ObjCCategoryDecl *OID =
87 dyn_cast<ObjCCategoryDecl>(D)) {
88 PrintObjCCategoryDecl(OID);
89 } else if (ObjCCompatibleAliasDecl *OID =
90 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
91 PrintObjCCompatibleAliasDecl(OID);
92 } else if (isa<ObjCClassDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000093 Out << "@class [printing todo]\n";
94 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
95 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
96 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
97 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +000098 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
99 PrintLinkageSpec(LSD);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000100 } else {
101 assert(0 && "Unknown decl type!");
102 }
103}
104
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000105void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 bool HasBody = FD->getBody();
107
Ted Kremenekea75c552007-11-28 21:32:21 +0000108 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000109
110 switch (FD->getStorageClass()) {
111 default: assert(0 && "Unknown storage class");
112 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000113 case FunctionDecl::Extern: Out << "extern "; break;
114 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000115 }
116
117 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000118 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000121 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000122
Chris Lattner0d6ca112007-12-03 21:43:25 +0000123 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 Proto += "(";
125 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
126 if (i) Proto += ", ";
127 std::string ParamStr;
128 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
129
130 FT->getArgType(i).getAsStringInternal(ParamStr);
131 Proto += ParamStr;
132 }
133
134 if (FT->isVariadic()) {
135 if (FD->getNumParams()) Proto += ", ";
136 Proto += "...";
137 }
138 Proto += ")";
139 } else {
140 assert(isa<FunctionTypeNoProto>(AFT));
141 Proto += "()";
142 }
143
144 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000145 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000146
Chris Lattner6000dac2007-08-08 22:51:59 +0000147 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000148 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000149 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000150}
151
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000152void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 std::string S = TD->getName();
154 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000155 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000156}
157
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000158void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
159 const char *l;
160 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
161 l = "C";
162 else if (LS->getLanguage() == LinkageSpecDecl::lang_cxx)
163 l = "C++";
164 else assert(0 && "unknown language in linkage specification");
165 Out << "extern \"" << l << "\" { ";
166 PrintDecl(LS->getDecl());
167 Out << "}\n";
168}
169
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000170void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000171 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000172 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000173 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000174 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000175 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000176 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000177 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000178 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000179
180 for (int i = 0; i < OMD->getNumParams(); i++) {
181 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000182 // FIXME: selector is missing here!
183 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000184 }
185}
186
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000187void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000188 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000189 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000190
191 if (SID)
192 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000193 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000194 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000195
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000197 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000198 ObjCMethodDecl *OMD = *I;
199 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000200 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000201 Out << ' ';
202 OMD->getBody()->printPretty(Out);
203 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000204 }
205 }
206
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000207 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000208 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000209 ObjCMethodDecl *OMD = *I;
210 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000211 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000212 Out << ' ';
213 OMD->getBody()->printPretty(Out);
214 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000215 }
216 }
217
Ted Kremenekea75c552007-11-28 21:32:21 +0000218 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000219}
220
221
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000222void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000223 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000224 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000225
226 if (SID)
227 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000228 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000229 Out << "@interface " << I;
230
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000231 // Protocols?
232 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000233
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000234 if (count > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000235 ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000236 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000237 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000238 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000239
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000240 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000241 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000242 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000243 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000244
Chris Lattnerbe6df082007-12-12 07:56:42 +0000245 if (OID->getNumInstanceVariables() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000246 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000247 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000248 E = OID->ivar_end(); I != E; ++I) {
249 Out << '\t' << (*I)->getType().getAsString()
250 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000251 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000252 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000253 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000254
255 int NumProperties = OID->getNumPropertyDecl();
256 if (NumProperties > 0) {
257 for (int i = 0; i < NumProperties; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000258 ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekea75c552007-11-28 21:32:21 +0000259 Out << "@property";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000260 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000261 bool first = true;
Ted Kremenekea75c552007-11-28 21:32:21 +0000262 Out << " (";
Chris Lattner4b1daf02008-01-10 01:43:14 +0000263 if (PDecl->getPropertyAttributes() &
264 ObjCPropertyDecl::OBJC_PR_readonly) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000265 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000266 first = false;
267 }
268
Chris Lattner4b1daf02008-01-10 01:43:14 +0000269 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000270 Out << (first ? ' ' : ',') << "getter = "
271 << PDecl->getGetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000272 first = false;
273 }
Chris Lattner4b1daf02008-01-10 01:43:14 +0000274 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000275 Out << (first ? ' ' : ',') << "setter = "
276 << PDecl->getSetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000277 first = false;
278 }
279
Chris Lattner4b1daf02008-01-10 01:43:14 +0000280 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000281 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000282 first = false;
283 }
284
Chris Lattner4b1daf02008-01-10 01:43:14 +0000285 if (PDecl->getPropertyAttributes() &
286 ObjCPropertyDecl::OBJC_PR_readwrite) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000287 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000288 first = false;
289 }
290
Chris Lattner4b1daf02008-01-10 01:43:14 +0000291 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000292 Out << (first ? ' ' : ',') << "retain";
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_copy) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000297 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000298 first = false;
299 }
300
Chris Lattner4b1daf02008-01-10 01:43:14 +0000301 if (PDecl->getPropertyAttributes() &
302 ObjCPropertyDecl::OBJC_PR_nonatomic) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000303 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000304 first = false;
305 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000306 Out << " )";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000307 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000308
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000309 ObjCIvarDecl **IDecl = PDecl->getPropertyDecls();
Ted Kremenekea75c552007-11-28 21:32:21 +0000310
311 Out << ' ' << IDecl[0]->getType().getAsString()
312 << ' ' << IDecl[0]->getName();
313
314 for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
315 Out << ", " << IDecl[j]->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000316
Ted Kremenekea75c552007-11-28 21:32:21 +0000317 Out << ";\n";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000318 }
319 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000320
321 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000322 // FIXME: implement the rest...
323}
324
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000325void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000326 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000327 // FIXME: implement the rest...
328}
329
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000330void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000331 Out << "@implementation "
332 << PID->getClassInterface()->getName()
333 << '(' << PID->getName() << ");\n";
334
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000335 // FIXME: implement the rest...
336}
337
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000338void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000339 Out << "@interface "
340 << PID->getClassInterface()->getName()
341 << '(' << PID->getName() << ");\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000342 // FIXME: implement the rest...
343}
344
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000345void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000346 Out << "@compatibility_alias " << AID->getName()
347 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000348}
349
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000350//===----------------------------------------------------------------------===//
351/// ASTPrinter - Pretty-printer of ASTs
352
Chris Lattner3d4997d2007-09-15 23:02:28 +0000353namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000354 class ASTPrinter : public ASTConsumer, public DeclPrinter {
355 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000356 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000357
Chris Lattner3d4997d2007-09-15 23:02:28 +0000358 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000359 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000361 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000362}
Chris Lattner6000dac2007-08-08 22:51:59 +0000363
Ted Kremenekea75c552007-11-28 21:32:21 +0000364ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
365 return new ASTPrinter(out);
366}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000367
368//===----------------------------------------------------------------------===//
369/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000370
371namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000372 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000373 SourceManager *SM;
374 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000375 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000376
Ted Kremenek95041a22007-12-19 22:51:13 +0000377 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000378 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000379 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000380
381 virtual void HandleTopLevelDecl(Decl *D) {
382 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
383 PrintFunctionDeclStart(FD);
384
385 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000386 Out << '\n';
387 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000388 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000389 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000390 }
391 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
392 PrintTypeDefDecl(TD);
393 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000394 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000395 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000396 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000397 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000398 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000399 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000400 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000401 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000402 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000403 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000404 Out << "Read objc fwd class decl\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000405 } else {
406 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000407 }
408 }
409 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000410}
411
Chris Lattner3d4997d2007-09-15 23:02:28 +0000412ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
413
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000414//===----------------------------------------------------------------------===//
415/// ASTViewer - AST Visualization
416
Ted Kremenek80de08f2007-09-19 21:29:43 +0000417namespace {
418 class ASTViewer : public ASTConsumer {
419 SourceManager *SM;
420 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000421 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000422 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000423 }
424
425 virtual void HandleTopLevelDecl(Decl *D) {
426 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000427 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000428
429 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000430 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000431 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000432 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000433 }
434 }
435 }
436 };
437}
438
439ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
440
441
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000442//===----------------------------------------------------------------------===//
443// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
444// the CFGs for all function definitions.
445
446namespace {
447
Chris Lattnerc0508f92007-09-15 23:21:08 +0000448class CFGVisitor : public ASTConsumer {
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000449public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000450 // CFG Visitor interface to be implemented by subclass.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000451 virtual void VisitCFG(CFG& C, FunctionDecl& FD) = 0;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000452 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000453
454 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000455};
456
457} // end anonymous namespace
458
Chris Lattnerc0508f92007-09-15 23:21:08 +0000459void CFGVisitor::HandleTopLevelDecl(Decl *D) {
460 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
461 if (!FD || !FD->getBody())
462 return;
463
464 if (printFuncDeclStart()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000465 DeclPrinter().PrintFunctionDeclStart(FD);
466 llvm::cerr << '\n';
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000467 }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000468
Ted Kremenek12259662007-09-17 17:10:02 +0000469 CFG *C = CFG::buildCFG(FD->getBody());
Ted Kremenekbffaa832008-01-29 05:13:23 +0000470 VisitCFG(*C, *FD);
Ted Kremenek12259662007-09-17 17:10:02 +0000471 delete C;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000472}
473
474//===----------------------------------------------------------------------===//
475// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
476
477namespace {
478 class CFGDumper : public CFGVisitor {
479 const bool UseGraphviz;
480 public:
481 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
482
Ted Kremenekbffaa832008-01-29 05:13:23 +0000483 virtual void VisitCFG(CFG& C, FunctionDecl&) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000484 if (UseGraphviz)
485 C.viewCFG();
486 else
487 C.dump();
488 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000489 };
490} // end anonymous namespace
491
Chris Lattnerc0508f92007-09-15 23:21:08 +0000492ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
493 return new CFGDumper(ViewGraphs);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000494}
495
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000496//===----------------------------------------------------------------------===//
497// AnalyzeLiveVariables - perform live variable analysis and dump results
498
499namespace {
500 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000501 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000502 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000503 virtual void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000504 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000505 }
506
Ted Kremenekbffaa832008-01-29 05:13:23 +0000507 virtual void VisitCFG(CFG& C, FunctionDecl& FD) {
508 LiveVariables L(C, FD);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000509 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000510 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000511 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000512 };
513} // end anonymous namespace
514
Chris Lattnerc0508f92007-09-15 23:21:08 +0000515ASTConsumer *clang::CreateLiveVarAnalyzer() {
516 return new LivenessVisitor();
Ted Kremeneke4e63342007-09-06 00:17:54 +0000517}
518
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000519//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000520// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000521
522namespace {
523 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000524 Diagnostic &Diags;
525 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000526 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000527 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000528 virtual void Initialize(ASTContext &Context) {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000529 Ctx = &Context;
530 }
531
Ted Kremenekbffaa832008-01-29 05:13:23 +0000532 virtual void VisitCFG(CFG& C, FunctionDecl& FD) {
533 CheckDeadStores(C, FD, *Ctx, Diags);
534 }
535
Ted Kremenek567a7e62007-09-07 23:54:15 +0000536 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000537 };
538} // end anonymous namespace
539
Chris Lattnerc0508f92007-09-15 23:21:08 +0000540ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
541 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000542}
Chris Lattner580980b2007-09-16 19:46:59 +0000543
544//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000545// Unitialized Values - run checker to flag potential uses of uninitalized
546// variables.
547
548namespace {
549 class UninitValsVisitor : public CFGVisitor {
550 Diagnostic &Diags;
551 ASTContext *Ctx;
552 public:
553 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremenek95041a22007-12-19 22:51:13 +0000554 virtual void Initialize(ASTContext &Context) {
Ted Kremenek2bf55142007-09-17 20:49:30 +0000555 Ctx = &Context;
556 }
557
Ted Kremenekbffaa832008-01-29 05:13:23 +0000558 virtual void VisitCFG(CFG& C, FunctionDecl&) {
559 CheckUninitializedValues(C, *Ctx, Diags);
560 }
561
Ted Kremenek2bf55142007-09-17 20:49:30 +0000562 virtual bool printFuncDeclStart() { return false; }
563 };
564} // end anonymous namespace
565
566ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
567 return new UninitValsVisitor(Diags);
568}
569
570//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +0000571// GRConstants - Perform intra-procedural, path-sensitive constant propagation.
Ted Kremeneke603df42008-01-08 18:04:06 +0000572
573namespace {
Ted Kremenekbffaa832008-01-29 05:13:23 +0000574 class GRConstantsVisitor : public CFGVisitor {
Ted Kremenek19227e32008-02-07 06:33:19 +0000575 Diagnostic &Diags;
Ted Kremenek874d63f2008-01-24 02:02:54 +0000576 ASTContext* Ctx;
Ted Kremeneke603df42008-01-08 18:04:06 +0000577 public:
Ted Kremenek19227e32008-02-07 06:33:19 +0000578 GRConstantsVisitor(Diagnostic &diags) : Diags(diags) {}
Ted Kremeneke603df42008-01-08 18:04:06 +0000579
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000580 virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
Ted Kremenekbffaa832008-01-29 05:13:23 +0000581 virtual void VisitCFG(CFG& C, FunctionDecl&);
Ted Kremeneke603df42008-01-08 18:04:06 +0000582 };
583} // end anonymous namespace
584
Ted Kremenek19227e32008-02-07 06:33:19 +0000585ASTConsumer* clang::CreateGRConstants(Diagnostic &Diags) {
586 return new GRConstantsVisitor(Diags);
Ted Kremeneke603df42008-01-08 18:04:06 +0000587}
588
Ted Kremenekbffaa832008-01-29 05:13:23 +0000589void GRConstantsVisitor::VisitCFG(CFG& C, FunctionDecl& FD) {
Ted Kremenek19227e32008-02-07 06:33:19 +0000590 RunGRConstants(C, FD, *Ctx, Diags);
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000591}
592
Ted Kremeneke603df42008-01-08 18:04:06 +0000593//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000594// AST Serializer
595
596namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000597
598class ASTSerializer : public ASTConsumer {
599protected:
600 Diagnostic &Diags;
601 TranslationUnit TU;
602public:
603 ASTSerializer(Diagnostic& diags, const LangOptions& LO)
604 : Diags(diags), TU(LO) {}
605
606 virtual void Initialize(ASTContext &Context) {
607 TU.setContext(&Context);
608 }
609
610 virtual void HandleTopLevelDecl(Decl *D) {
611 if (Diags.hasErrorOccurred())
612 return;
613
614 TU.AddTopLevelDecl(D);
615 }
616};
617
618class SingleFileSerializer : public ASTSerializer {
619 const llvm::sys::Path FName;
620public:
621 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
622 const LangOptions &LO)
623 : ASTSerializer(diags,LO), FName(F) {}
624
625 ~SingleFileSerializer() {
626 EmitASTBitcodeFile(TU,FName);
627 }
628};
629
630class BuildSerializer : public ASTSerializer {
631 llvm::sys::Path EmitDir;
632public:
633 BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000634 const LangOptions &LO)
Ted Kremenekf06c9282007-12-19 23:49:37 +0000635 : ASTSerializer(diags,LO), EmitDir(dir) {}
636
Ted Kremenek54117722007-12-20 00:34:58 +0000637 ~BuildSerializer() {
638 SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
639 unsigned ID = SourceMgr.getMainFileID();
640 assert (ID && "MainFileID not set!");
641 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
642 assert (FE && "No FileEntry for main file.");
643
644 // FIXME: This is not portable to Windows.
645 // FIXME: This logic should probably be moved elsewhere later.
646
Ted Kremenekee533642007-12-20 19:47:16 +0000647 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000648
649 std::vector<char> buf;
650 buf.reserve(strlen(FE->getName())+100);
651
652 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000653 FName.appendComponent(&buf[0]);
654 FName.createDirectoryOnDisk(true);
655 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000656 assert (false && "Could not create 'device' serialization directory.");
657 return;
658 }
Ted Kremenekee533642007-12-20 19:47:16 +0000659
Ted Kremenek54117722007-12-20 00:34:58 +0000660 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000661 FName.appendComponent(&buf[0]);
662 EmitASTBitcodeFile(TU,FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000663
Ted Kremenekee533642007-12-20 19:47:16 +0000664 // Now emit the sources.
665
Ted Kremenek54117722007-12-20 00:34:58 +0000666 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000667};
668
669
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000670} // end anonymous namespace
671
672
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000673ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000674 const std::string& OutputFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000675 Diagnostic &Diags,
676 const LangOptions &Features) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000677
Ted Kremenekf06c9282007-12-19 23:49:37 +0000678 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000679 if (InFile == "-") {
680 llvm::cerr <<
681 "error: Cannot use --serialize with -o for source read from STDIN.\n";
682 return NULL;
683 }
684
Ted Kremenekf06c9282007-12-19 23:49:37 +0000685 // The user specified an AST-emission directory. Determine if the path
686 // is absolute.
687 llvm::sys::Path EmitDir(OutputFile);
688
689 if (!EmitDir.isAbsolute()) {
690 llvm::cerr <<
691 "error: Output directory for --serialize must be an absolute path.\n";
692
693 return NULL;
694 }
695
696 // Create the directory if it does not exist.
697 EmitDir.createDirectoryOnDisk(true);
698 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
699 llvm::cerr <<
700 "error: Could not create output directory for --serialize.\n";
701
702 return NULL;
703 }
704
Ted Kremenek54117722007-12-20 00:34:58 +0000705 // FIXME: We should probably only allow using BuildSerializer when
706 // the ASTs come from parsed source files, and not from .ast files.
Ted Kremenekf06c9282007-12-19 23:49:37 +0000707 return new BuildSerializer(EmitDir, Diags, Features);
708 }
709
710 // The user did not specify an output directory for serialized ASTs.
711 // Serialize the translation to a single file whose name is the same
712 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000713
Ted Kremenekf06c9282007-12-19 23:49:37 +0000714 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000715 FName.appendSuffix("ast");
Ted Kremenekf06c9282007-12-19 23:49:37 +0000716 return new SingleFileSerializer(FName, Diags, Features);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000717}