blob: 1b25031418e940b1d322a79b8121dab4c8806575 [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 Lattner97e8b6f2007-10-07 06:04:32 +00005// This file was developed by Chris Lattner and is distributed under the
Reid Spencer5f016e22007-07-11 17:01:13 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000017#include "clang/AST/ASTConsumer.h"
Ted Kremenekfddd5182007-08-21 21:42:03 +000018#include "clang/AST/CFG.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000019#include "clang/Analysis/LiveVariables.h"
Ted Kremenek055c2752007-09-06 23:00:42 +000020#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000021#include "llvm/Support/Streams.h"
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000022
Chris Lattner6000dac2007-08-08 22:51:59 +000023using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000024
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000025//===----------------------------------------------------------------------===//
26/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000027
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000028namespace {
29 class DeclPrinter {
30 public:
Ted Kremenekea75c552007-11-28 21:32:21 +000031 std::ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000032
Ted Kremenekea75c552007-11-28 21:32:21 +000033 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
34 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000035
36 void PrintFunctionDeclStart(FunctionDecl *FD);
37 void PrintTypeDefDecl(TypedefDecl *TD);
38 void PrintObjcMethodDecl(ObjcMethodDecl *OMD);
39 void PrintObjcImplementationDecl(ObjcImplementationDecl *OID);
40 void PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID);
41 void PrintObjcProtocolDecl(ObjcProtocolDecl *PID);
42 void PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID);
43 void PrintObjcCategoryDecl(ObjcCategoryDecl *PID);
44 void PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID);
45 };
46} // end anonymous namespace
47
48void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +000049 bool HasBody = FD->getBody();
50
Ted Kremenekea75c552007-11-28 21:32:21 +000051 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +000052
53 switch (FD->getStorageClass()) {
54 default: assert(0 && "Unknown storage class");
55 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +000056 case FunctionDecl::Extern: Out << "extern "; break;
57 case FunctionDecl::Static: Out << "static "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +000058 }
59
60 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +000061 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +000062
Reid Spencer5f016e22007-07-11 17:01:13 +000063 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +000064 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +000065
Chris Lattner0d6ca112007-12-03 21:43:25 +000066 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 Proto += "(";
68 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
69 if (i) Proto += ", ";
70 std::string ParamStr;
71 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
72
73 FT->getArgType(i).getAsStringInternal(ParamStr);
74 Proto += ParamStr;
75 }
76
77 if (FT->isVariadic()) {
78 if (FD->getNumParams()) Proto += ", ";
79 Proto += "...";
80 }
81 Proto += ")";
82 } else {
83 assert(isa<FunctionTypeNoProto>(AFT));
84 Proto += "()";
85 }
86
87 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +000088 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +000089
Chris Lattner6000dac2007-08-08 22:51:59 +000090 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +000091 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +000092 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +000093}
94
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000095void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +000096 std::string S = TD->getName();
97 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +000098 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000099}
100
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000101void DeclPrinter::PrintObjcMethodDecl(ObjcMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000102 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000103 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000104 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000105 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000106 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000107 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000108 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000109 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000110
111 for (int i = 0; i < OMD->getNumParams(); i++) {
112 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000113 // FIXME: selector is missing here!
114 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000115 }
116}
117
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000118void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000119 std::string I = OID->getName();
120 ObjcInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000121
122 if (SID)
123 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000124 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000125 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000126
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000127 for (ObjcImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
128 E = OID->instmeth_end(); I != E; ++I) {
129 ObjcMethodDecl *OMD = *I;
130 PrintObjcMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000131 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000132 Out << ' ';
133 OMD->getBody()->printPretty(Out);
134 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000135 }
136 }
137
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000138 for (ObjcImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
139 E = OID->classmeth_end(); I != E; ++I) {
140 ObjcMethodDecl *OMD = *I;
141 PrintObjcMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000142 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000143 Out << ' ';
144 OMD->getBody()->printPretty(Out);
145 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000146 }
147 }
148
Ted Kremenekea75c552007-11-28 21:32:21 +0000149 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000150}
151
152
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000153void DeclPrinter::PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000154 std::string I = OID->getName();
155 ObjcInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000156
157 if (SID)
158 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000159 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000160 Out << "@interface " << I;
161
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000162 // Protocols?
163 int count = OID->getNumIntfRefProtocols();
Ted Kremenekea75c552007-11-28 21:32:21 +0000164
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000165 if (count > 0) {
166 ObjcProtocolDecl **refProtocols = OID->getReferencedProtocols();
167 for (int i = 0; i < count; i++)
Ted Kremenekea75c552007-11-28 21:32:21 +0000168 Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000169 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000170
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000171 if (count > 0)
Ted Kremenekea75c552007-11-28 21:32:21 +0000172 Out << ">\n";
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000173 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000174 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000175
Chris Lattnerbe6df082007-12-12 07:56:42 +0000176 if (OID->getNumInstanceVariables() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000177 Out << '{';
Chris Lattnerbe6df082007-12-12 07:56:42 +0000178 for (ObjcInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
179 E = OID->ivar_end(); I != E; ++I) {
180 Out << '\t' << (*I)->getType().getAsString()
181 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000182 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000183 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000184 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000185
186 int NumProperties = OID->getNumPropertyDecl();
187 if (NumProperties > 0) {
188 for (int i = 0; i < NumProperties; i++) {
189 ObjcPropertyDecl *PDecl = OID->getPropertyDecl()[i];
Ted Kremenekea75c552007-11-28 21:32:21 +0000190 Out << "@property";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000191 if (PDecl->getPropertyAttributes() != ObjcPropertyDecl::OBJC_PR_noattr) {
192 bool first = true;
Ted Kremenekea75c552007-11-28 21:32:21 +0000193 Out << " (";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000194 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readonly)
195 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000196 Out << (first ? ' ' : ',') << "readonly";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000197 first = false;
198 }
199
200 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_getter)
201 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000202 Out << (first ? ' ' : ',') << "getter = "
203 << PDecl->getGetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000204 first = false;
205 }
206 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_setter)
207 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000208 Out << (first ? ' ' : ',') << "setter = "
209 << PDecl->getSetterName()->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000210 first = false;
211 }
212
213 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_assign)
214 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000215 Out << (first ? ' ' : ',') << "assign";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000216 first = false;
217 }
218
219 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readwrite)
220 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000221 Out << (first ? ' ' : ',') << "readwrite";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000222 first = false;
223 }
224
225 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_retain)
226 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000227 Out << (first ? ' ' : ',') << "retain";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000228 first = false;
229 }
230
231 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_copy)
232 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000233 Out << (first ? ' ' : ',') << "copy";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000234 first = false;
235 }
236
237 if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_nonatomic)
238 {
Ted Kremenekea75c552007-11-28 21:32:21 +0000239 Out << (first ? ' ' : ',') << "nonatomic";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000240 first = false;
241 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000242 Out << " )";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000243 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000244
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000245 ObjcIvarDecl **IDecl = PDecl->getPropertyDecls();
Ted Kremenekea75c552007-11-28 21:32:21 +0000246
247 Out << ' ' << IDecl[0]->getType().getAsString()
248 << ' ' << IDecl[0]->getName();
249
250 for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
251 Out << ", " << IDecl[j]->getName();
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000252
Ted Kremenekea75c552007-11-28 21:32:21 +0000253 Out << ";\n";
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000254 }
255 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000256
257 Out << "@end\n";
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000258 // FIXME: implement the rest...
259}
260
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000261void DeclPrinter::PrintObjcProtocolDecl(ObjcProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000262 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000263 // FIXME: implement the rest...
264}
265
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000266void DeclPrinter::PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000267 Out << "@implementation "
268 << PID->getClassInterface()->getName()
269 << '(' << PID->getName() << ");\n";
270
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000271 // FIXME: implement the rest...
272}
273
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000274void DeclPrinter::PrintObjcCategoryDecl(ObjcCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000275 Out << "@interface "
276 << PID->getClassInterface()->getName()
277 << '(' << PID->getName() << ");\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000278 // FIXME: implement the rest...
279}
280
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000281void DeclPrinter::PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000282 Out << "@compatibility_alias " << AID->getName()
283 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000284}
285
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000286//===----------------------------------------------------------------------===//
287/// ASTPrinter - Pretty-printer of ASTs
288
Chris Lattner3d4997d2007-09-15 23:02:28 +0000289namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000290 class ASTPrinter : public ASTConsumer, public DeclPrinter {
291 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000292 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000293
Chris Lattner3d4997d2007-09-15 23:02:28 +0000294 virtual void HandleTopLevelDecl(Decl *D) {
295 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
296 PrintFunctionDeclStart(FD);
297
298 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000299 Out << ' ';
300 FD->getBody()->printPretty(Out);
301 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000302 }
Steve Naroffaaa3cf82007-11-13 23:48:03 +0000303 } else if (isa<ObjcMethodDecl>(D)) {
304 // Do nothing, methods definitions are printed in
305 // PrintObjcImplementationDecl.
Chris Lattner3d4997d2007-09-15 23:02:28 +0000306 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
307 PrintTypeDefDecl(TD);
Chris Lattner9fa5e652007-10-06 18:52:10 +0000308 } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
309 PrintObjcInterfaceDecl(OID);
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000310 } else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) {
311 PrintObjcProtocolDecl(PID);
Chris Lattner9fa5e652007-10-06 18:52:10 +0000312 } else if (ObjcForwardProtocolDecl *OFPD =
313 dyn_cast<ObjcForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000314 Out << "@protocol ";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000315 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
316 const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000317 if (i) Out << ", ";
318 Out << D->getName();
Chris Lattner9fa5e652007-10-06 18:52:10 +0000319 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000320 Out << ";\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000321 } else if (ObjcImplementationDecl *OID =
322 dyn_cast<ObjcImplementationDecl>(D)) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000323 PrintObjcImplementationDecl(OID);
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000324 } else if (ObjcCategoryImplDecl *OID =
325 dyn_cast<ObjcCategoryImplDecl>(D)) {
326 PrintObjcCategoryImplDecl(OID);
327 } else if (ObjcCategoryDecl *OID =
328 dyn_cast<ObjcCategoryDecl>(D)) {
329 PrintObjcCategoryDecl(OID);
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000330 } else if (ObjcCompatibleAliasDecl *OID =
331 dyn_cast<ObjcCompatibleAliasDecl>(D)) {
332 PrintObjcCompatibleAliasDecl(OID);
Chris Lattner9fa5e652007-10-06 18:52:10 +0000333 } else if (isa<ObjcClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000334 Out << "@class [printing todo]\n";
Steve Naroff1f644322007-11-28 22:54:11 +0000335 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
336 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000337 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000338 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000339 } else {
340 assert(0 && "Unknown decl type!");
Chris Lattner6000dac2007-08-08 22:51:59 +0000341 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000343 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000344}
Chris Lattner6000dac2007-08-08 22:51:59 +0000345
Ted Kremenekea75c552007-11-28 21:32:21 +0000346ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
347 return new ASTPrinter(out);
348}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000349
350//===----------------------------------------------------------------------===//
351/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000352
353namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000354 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000355 SourceManager *SM;
356 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000357 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000358
Chris Lattner3d4997d2007-09-15 23:02:28 +0000359 void Initialize(ASTContext &Context, unsigned MainFileID) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000360 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000361 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000362
363 virtual void HandleTopLevelDecl(Decl *D) {
364 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
365 PrintFunctionDeclStart(FD);
366
367 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000368 Out << '\n';
369 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000370 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000371 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000372 }
373 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
374 PrintTypeDefDecl(TD);
375 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000376 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000377 } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000378 Out << "Read objc interface '" << OID->getName() << "'\n";
Steve Naroff8de28262007-10-14 17:03:01 +0000379 } else if (ObjcProtocolDecl *OPD = dyn_cast<ObjcProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000380 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Steve Naroff8de28262007-10-14 17:03:01 +0000381 } else if (ObjcCategoryDecl *OCD = dyn_cast<ObjcCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000382 Out << "Read objc category '" << OCD->getName() << "'\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000383 } else if (isa<ObjcForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000384 Out << "Read objc fwd protocol decl\n";
Steve Naroff8de28262007-10-14 17:03:01 +0000385 } else if (isa<ObjcClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000386 Out << "Read objc fwd class decl\n";
Chris Lattner9fa5e652007-10-06 18:52:10 +0000387 } else {
388 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000389 }
390 }
391 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000392}
393
Chris Lattner3d4997d2007-09-15 23:02:28 +0000394ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
395
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000396//===----------------------------------------------------------------------===//
397/// ASTViewer - AST Visualization
398
Ted Kremenek80de08f2007-09-19 21:29:43 +0000399namespace {
400 class ASTViewer : public ASTConsumer {
401 SourceManager *SM;
402 public:
403 void Initialize(ASTContext &Context, unsigned MainFileID) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000404 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000405 }
406
407 virtual void HandleTopLevelDecl(Decl *D) {
408 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000409 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000410
411 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000412 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000413 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000414 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000415 }
416 }
417 }
418 };
419}
420
421ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
422
423
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000424//===----------------------------------------------------------------------===//
425// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
426// the CFGs for all function definitions.
427
428namespace {
429
Chris Lattnerc0508f92007-09-15 23:21:08 +0000430class CFGVisitor : public ASTConsumer {
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000431public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000432 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000433 virtual void VisitCFG(CFG& C) = 0;
434 virtual bool printFuncDeclStart() { return true; }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000435
436 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000437};
438
439} // end anonymous namespace
440
Chris Lattnerc0508f92007-09-15 23:21:08 +0000441void CFGVisitor::HandleTopLevelDecl(Decl *D) {
442 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
443 if (!FD || !FD->getBody())
444 return;
445
446 if (printFuncDeclStart()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000447 DeclPrinter().PrintFunctionDeclStart(FD);
448 llvm::cerr << '\n';
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000449 }
Chris Lattnerc0508f92007-09-15 23:21:08 +0000450
Ted Kremenek12259662007-09-17 17:10:02 +0000451 CFG *C = CFG::buildCFG(FD->getBody());
452 VisitCFG(*C);
453 delete C;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000454}
455
456//===----------------------------------------------------------------------===//
457// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
458
459namespace {
460 class CFGDumper : public CFGVisitor {
461 const bool UseGraphviz;
462 public:
463 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
464
Chris Lattnerc0508f92007-09-15 23:21:08 +0000465 virtual void VisitCFG(CFG &C) {
466 if (UseGraphviz)
467 C.viewCFG();
468 else
469 C.dump();
470 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000471 };
472} // end anonymous namespace
473
Chris Lattnerc0508f92007-09-15 23:21:08 +0000474ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
475 return new CFGDumper(ViewGraphs);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000476}
477
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000478//===----------------------------------------------------------------------===//
479// AnalyzeLiveVariables - perform live variable analysis and dump results
480
481namespace {
482 class LivenessVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000483 SourceManager *SM;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000484 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000485 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000486 SM = &Context.getSourceManager();
Chris Lattnerc0508f92007-09-15 23:21:08 +0000487 }
488
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000489 virtual void VisitCFG(CFG& C) {
Ted Kremenek11e72182007-10-01 20:33:52 +0000490 LiveVariables L(C);
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000491 L.runOnCFG(C);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000492 L.dumpBlockLiveness(*SM);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000493 }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000494 };
495} // end anonymous namespace
496
Chris Lattnerc0508f92007-09-15 23:21:08 +0000497ASTConsumer *clang::CreateLiveVarAnalyzer() {
498 return new LivenessVisitor();
Ted Kremeneke4e63342007-09-06 00:17:54 +0000499}
500
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000501//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000502// DeadStores - run checker to locate dead stores in a function
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000503
504namespace {
505 class DeadStoreVisitor : public CFGVisitor {
Chris Lattnerc0508f92007-09-15 23:21:08 +0000506 Diagnostic &Diags;
507 ASTContext *Ctx;
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000508 public:
Chris Lattnerc0508f92007-09-15 23:21:08 +0000509 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
510 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
511 Ctx = &Context;
512 }
513
514 virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
Ted Kremenek567a7e62007-09-07 23:54:15 +0000515 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000516 };
517} // end anonymous namespace
518
Chris Lattnerc0508f92007-09-15 23:21:08 +0000519ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
520 return new DeadStoreVisitor(Diags);
Ted Kremenek055c2752007-09-06 23:00:42 +0000521}
Chris Lattner580980b2007-09-16 19:46:59 +0000522
523//===----------------------------------------------------------------------===//
Ted Kremenek2bf55142007-09-17 20:49:30 +0000524// Unitialized Values - run checker to flag potential uses of uninitalized
525// variables.
526
527namespace {
528 class UninitValsVisitor : public CFGVisitor {
529 Diagnostic &Diags;
530 ASTContext *Ctx;
531 public:
532 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
533 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
534 Ctx = &Context;
535 }
536
537 virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); }
538 virtual bool printFuncDeclStart() { return false; }
539 };
540} // end anonymous namespace
541
542ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
543 return new UninitValsVisitor(Diags);
544}
545
546//===----------------------------------------------------------------------===//
Chris Lattner580980b2007-09-16 19:46:59 +0000547// LLVM Emitter
548
549#include "clang/Basic/Diagnostic.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000550#include "clang/Basic/TargetInfo.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000551#include "clang/CodeGen/ModuleBuilder.h"
552#include "llvm/Module.h"
Devang Patel7a4718e2007-10-31 20:01:01 +0000553#include "llvm/Target/TargetData.h"
554#include "llvm/Target/TargetMachine.h"
Chris Lattner580980b2007-09-16 19:46:59 +0000555
556namespace {
557 class LLVMEmitter : public ASTConsumer {
558 Diagnostic &Diags;
559 llvm::Module *M;
Devang Patel7a4718e2007-10-31 20:01:01 +0000560 const llvm::TargetData *TD;
Chris Lattner580980b2007-09-16 19:46:59 +0000561 ASTContext *Ctx;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000562 const LangOptions &Features;
Chris Lattnera36c4862007-11-13 18:16:41 +0000563 CodeGen::CodeGenModule *Builder;
Chris Lattner580980b2007-09-16 19:46:59 +0000564 public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000565 LLVMEmitter(Diagnostic &diags, const LangOptions &LO)
566 : Diags(diags)
567 , Features(LO) {}
Chris Lattner580980b2007-09-16 19:46:59 +0000568 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
569 Ctx = &Context;
570 M = new llvm::Module("foo");
Devang Patel7a4718e2007-10-31 20:01:01 +0000571 M->setTargetTriple(Ctx->Target.getTargetTriple());
Chris Lattner0404cd82007-12-13 17:34:31 +0000572 M->setDataLayout(Ctx->Target.getTargetDescription());
Devang Patel7a4718e2007-10-31 20:01:01 +0000573 TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
Chris Lattnerfb97b032007-12-02 01:40:18 +0000574 Builder = CodeGen::Init(Context, Features, *M, *TD, Diags);
Chris Lattner580980b2007-09-16 19:46:59 +0000575 }
576
577 virtual void HandleTopLevelDecl(Decl *D) {
578 // If an error occurred, stop code generation, but continue parsing and
579 // semantic analysis (to ensure all warnings and errors are emitted).
580 if (Diags.hasErrorOccurred())
581 return;
582
583 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
584 CodeGen::CodeGenFunction(Builder, FD);
585 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
586 CodeGen::CodeGenGlobalVar(Builder, FVD);
587 } else {
Steve Naroff91578f32007-11-17 21:21:01 +0000588 assert(isa<TypeDecl>(D) && "Only expected type decls here");
Chris Lattner580980b2007-09-16 19:46:59 +0000589 // don't codegen for now, eventually pass down for debug info.
590 //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
591 }
592 }
593
594 ~LLVMEmitter() {
595 CodeGen::Terminate(Builder);
596
597 // Print the generated code.
Ted Kremenek3821d402007-12-13 17:50:11 +0000598 M->print(llvm::cout.stream());
Chris Lattner580980b2007-09-16 19:46:59 +0000599 delete M;
600 }
601 };
602} // end anonymous namespace
603
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000604ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags, const LangOptions &Features) {
605 return new LLVMEmitter(Diags, Features);
Chris Lattner580980b2007-09-16 19:46:59 +0000606}
607
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000608//===----------------------------------------------------------------------===//
609// AST Serializer
610
611namespace {
612 class ASTSerializer : public ASTConsumer {
613 Diagnostic &Diags;
614 TranslationUnit TU;
615 const llvm::sys::Path FName;
616 public:
617 ASTSerializer(const llvm::sys::Path& F, Diagnostic &diags,
618 const LangOptions &LO)
619 : Diags(diags), TU(LO), FName(F) {}
620
621
622 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
623 TU.setContext(&Context);
624 }
625
626 virtual void HandleTopLevelDecl(Decl *D) {
627 // If an error occurred, stop code generation, but continue parsing and
628 // semantic analysis (to ensure all warnings and errors are emitted).
629 if (Diags.hasErrorOccurred())
630 return;
631
632 TU.AddTopLevelDecl(D);
633 }
634
635 ~ASTSerializer() { TU.EmitBitcodeFile(FName); }
636 };
637} // end anonymous namespace
638
639
640ASTConsumer *clang::CreateASTSerializer(const llvm::sys::Path& FName,
641 Diagnostic &Diags,
642 const LangOptions &Features) {
643 return new ASTSerializer(FName, Diags, Features);
644}