blob: 04c238287a9717ff9494b76f0b863cc5d069a1e5 [file] [log] [blame]
Chris Lattnereb8c9632007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnereb8c9632007-10-07 06:04:32 +00005// This file was developed by Chris Lattner and is distributed under the
Chris Lattner4b009652007-07-25 00:24:17 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnereb8c9632007-10-07 06:04:32 +000010// AST Consumer Implementations.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnereb8c9632007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "clang/AST/AST.h"
Chris Lattnerb73abd52007-09-15 23:02:28 +000016#include "clang/AST/ASTConsumer.h"
Ted Kremenek97f75312007-08-21 21:42:03 +000017#include "clang/AST/CFG.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000018#include "clang/Analysis/LiveVariables.h"
Ted Kremeneke805c4a2007-09-06 23:00:42 +000019#include "clang/Analysis/LocalCheckers.h"
Chris Lattner95578782007-08-08 22:51:59 +000020using namespace clang;
Chris Lattner4b009652007-07-25 00:24:17 +000021
Chris Lattner95578782007-08-08 22:51:59 +000022
23static void PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +000024 bool HasBody = FD->getBody();
25
Chris Lattner987058a2007-08-26 04:02:13 +000026 fprintf(stderr, "\n");
27
28 switch (FD->getStorageClass()) {
29 default: assert(0 && "Unknown storage class");
30 case FunctionDecl::None: break;
31 case FunctionDecl::Extern: fprintf(stderr, "extern "); break;
32 case FunctionDecl::Static: fprintf(stderr, "static "); break;
33 }
34
35 if (FD->isInline())
36 fprintf(stderr, "inline ");
37
Chris Lattner4b009652007-07-25 00:24:17 +000038 std::string Proto = FD->getName();
39 FunctionType *AFT = cast<FunctionType>(FD->getType());
40
41 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
42 Proto += "(";
43 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
44 if (i) Proto += ", ";
45 std::string ParamStr;
46 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
47
48 FT->getArgType(i).getAsStringInternal(ParamStr);
49 Proto += ParamStr;
50 }
51
52 if (FT->isVariadic()) {
53 if (FD->getNumParams()) Proto += ", ";
54 Proto += "...";
55 }
56 Proto += ")";
57 } else {
58 assert(isa<FunctionTypeNoProto>(AFT));
59 Proto += "()";
60 }
61
62 AFT->getResultType().getAsStringInternal(Proto);
Chris Lattner987058a2007-08-26 04:02:13 +000063 fprintf(stderr, "%s", Proto.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +000064
Chris Lattner95578782007-08-08 22:51:59 +000065 if (!FD->getBody())
Chris Lattner4b009652007-07-25 00:24:17 +000066 fprintf(stderr, ";\n");
Chris Lattner95578782007-08-08 22:51:59 +000067 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +000068}
69
Chris Lattner95578782007-08-08 22:51:59 +000070static void PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +000071 std::string S = TD->getName();
72 TD->getUnderlyingType().getAsStringInternal(S);
73 fprintf(stderr, "typedef %s;\n", S.c_str());
74}
75
Steve Narofffaed3bf2007-09-10 20:51:04 +000076static void PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +000077 std::string I = OID->getName();
78 ObjcInterfaceDecl *SID = OID->getSuperClass();
79 if (SID) {
80 std::string S = SID->getName();
81 fprintf(stderr, "@interface %s : %s", I.c_str(), S.c_str());
82 }
83 else
84 fprintf(stderr, "@interface %s", I.c_str());
85 // Protocols?
86 int count = OID->getNumIntfRefProtocols();
87 if (count > 0) {
88 ObjcProtocolDecl **refProtocols = OID->getReferencedProtocols();
89 for (int i = 0; i < count; i++)
90 fprintf(stderr, "%c%s", (i == 0 ? '<' : ','),
91 refProtocols[i]->getName());
92 }
93 if (count > 0)
Fariborz Jahanianf468b312007-10-26 16:29:12 +000094 fprintf(stderr, ">\n");
Fariborz Jahanianc04aff12007-10-08 23:06:41 +000095 else
Fariborz Jahanianf468b312007-10-26 16:29:12 +000096 fprintf(stderr, "\n");
97
98 int NumIvars = OID->getIntfDeclNumIvars();
99 if (NumIvars > 0) {
100 ObjcIvarDecl **Ivars = OID->getIntfDeclIvars();
101 fprintf(stderr,"{");
102 for (int i = 0; i < NumIvars; i++) {
103 fprintf(stderr, "\t%s %s;\n", Ivars[i]->getType().getAsString().c_str(),
104 Ivars[i]->getName());
105 }
106 fprintf(stderr, "}\n@end\n");
107 }
108 else
109 fprintf(stderr,"@end\n");
Steve Narofffaed3bf2007-09-10 20:51:04 +0000110 // FIXME: implement the rest...
111}
112
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000113static void PrintObjcProtocolDecl(ObjcProtocolDecl *PID) {
114 std::string S = PID->getName();
115 fprintf(stderr, "@protocol %s;\n", S.c_str());
116 // FIXME: implement the rest...
117}
118
119static void PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID) {
120 std::string S = PID->getName();
121 std::string I = PID->getClassInterface()->getName();
122 fprintf(stderr, "@implementation %s(%s);\n", I.c_str(), S.c_str());
123 // FIXME: implement the rest...
124}
125
126static void PrintObjcCategoryDecl(ObjcCategoryDecl *PID) {
127 std::string S = PID->getName();
128 std::string I = PID->getClassInterface()->getName();
129 fprintf(stderr, "@interface %s(%s);\n", I.c_str(), S.c_str());
130 // FIXME: implement the rest...
131}
132
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000133static void PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) {
134 std::string A = AID->getName();
135 std::string I = AID->getClassInterface()->getName();
136 fprintf(stderr, "@compatibility_alias %s %s;\n", A.c_str(), I.c_str());
137}
138
Chris Lattnerb73abd52007-09-15 23:02:28 +0000139namespace {
140 class ASTPrinter : public ASTConsumer {
141 virtual void HandleTopLevelDecl(Decl *D) {
142 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
143 PrintFunctionDeclStart(FD);
144
145 if (FD->getBody()) {
146 fprintf(stderr, " ");
147 FD->getBody()->dumpPretty();
148 fprintf(stderr, "\n");
149 }
150 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
151 PrintTypeDefDecl(TD);
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000152 } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
153 PrintObjcInterfaceDecl(OID);
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000154 } else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) {
155 PrintObjcProtocolDecl(PID);
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000156 } else if (ObjcForwardProtocolDecl *OFPD =
157 dyn_cast<ObjcForwardProtocolDecl>(D)) {
158 fprintf(stderr, "@protocol ");
159 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
160 const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
161 if (i) fprintf(stderr, ", ");
162 fprintf(stderr, "%s", D->getName());
163 }
Chris Lattnera845bbe2007-10-06 19:08:22 +0000164 fprintf(stderr, ";\n");
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000165 } else if (ObjcImplementationDecl *OID =
166 dyn_cast<ObjcImplementationDecl>(D)) {
167 fprintf(stderr, "@implementation %s [printing todo]\n",
168 OID->getName());
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000169 } else if (ObjcCategoryImplDecl *OID =
170 dyn_cast<ObjcCategoryImplDecl>(D)) {
171 PrintObjcCategoryImplDecl(OID);
172 } else if (ObjcCategoryDecl *OID =
173 dyn_cast<ObjcCategoryDecl>(D)) {
174 PrintObjcCategoryDecl(OID);
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000175 } else if (ObjcCompatibleAliasDecl *OID =
176 dyn_cast<ObjcCompatibleAliasDecl>(D)) {
177 PrintObjcCompatibleAliasDecl(OID);
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000178 } else if (isa<ObjcClassDecl>(D)) {
179 fprintf(stderr, "@class [printing todo]\n");
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000180 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
181 fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000182 } else {
183 assert(0 && "Unknown decl type!");
Chris Lattner95578782007-08-08 22:51:59 +0000184 }
Chris Lattner4b009652007-07-25 00:24:17 +0000185 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000186 };
Chris Lattner4b009652007-07-25 00:24:17 +0000187}
Chris Lattner95578782007-08-08 22:51:59 +0000188
Chris Lattnerb73abd52007-09-15 23:02:28 +0000189ASTConsumer *clang::CreateASTPrinter() { return new ASTPrinter(); }
190
191namespace {
192 class ASTDumper : public ASTConsumer {
193 SourceManager *SM;
194 public:
195 void Initialize(ASTContext &Context, unsigned MainFileID) {
196 SM = &Context.SourceMgr;
Chris Lattner95578782007-08-08 22:51:59 +0000197 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000198
199 virtual void HandleTopLevelDecl(Decl *D) {
200 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
201 PrintFunctionDeclStart(FD);
202
203 if (FD->getBody()) {
204 fprintf(stderr, "\n");
205 FD->getBody()->dumpAll(*SM);
206 fprintf(stderr, "\n");
207 }
208 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
209 PrintTypeDefDecl(TD);
210 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
211 fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000212 } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
213 fprintf(stderr, "Read objc interface '%s'\n", OID->getName());
Steve Naroffd5d18732007-10-14 17:03:01 +0000214 } else if (ObjcProtocolDecl *OPD = dyn_cast<ObjcProtocolDecl>(D)) {
215 fprintf(stderr, "Read objc protocol '%s'\n", OPD->getName());
216 } else if (ObjcCategoryDecl *OCD = dyn_cast<ObjcCategoryDecl>(D)) {
217 fprintf(stderr, "Read objc category '%s'\n", OCD->getName());
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000218 } else if (isa<ObjcForwardProtocolDecl>(D)) {
219 fprintf(stderr, "Read objc fwd protocol decl\n");
Steve Naroffd5d18732007-10-14 17:03:01 +0000220 } else if (isa<ObjcClassDecl>(D)) {
221 fprintf(stderr, "Read objc fwd class decl\n");
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000222 } else {
223 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000224 }
225 }
226 };
Chris Lattner95578782007-08-08 22:51:59 +0000227}
228
Chris Lattnerb73abd52007-09-15 23:02:28 +0000229ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
230
Ted Kremenekb6976a22007-09-19 21:29:43 +0000231namespace {
232 class ASTViewer : public ASTConsumer {
233 SourceManager *SM;
234 public:
235 void Initialize(ASTContext &Context, unsigned MainFileID) {
236 SM = &Context.SourceMgr;
237 }
238
239 virtual void HandleTopLevelDecl(Decl *D) {
240 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
241 PrintFunctionDeclStart(FD);
242
243 if (FD->getBody()) {
244 fprintf(stderr, "\n");
245 FD->getBody()->viewAST();
246 fprintf(stderr, "\n");
247 }
248 }
249 }
250 };
251}
252
253ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
254
255
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000256//===----------------------------------------------------------------------===//
257// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
258// the CFGs for all function definitions.
259
260namespace {
261
Chris Lattner52332d02007-09-15 23:21:08 +0000262class CFGVisitor : public ASTConsumer {
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000263public:
Chris Lattner52332d02007-09-15 23:21:08 +0000264 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000265 virtual void VisitCFG(CFG& C) = 0;
266 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000267
268 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000269};
270
271} // end anonymous namespace
272
Chris Lattner52332d02007-09-15 23:21:08 +0000273void CFGVisitor::HandleTopLevelDecl(Decl *D) {
274 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
275 if (!FD || !FD->getBody())
276 return;
277
278 if (printFuncDeclStart()) {
279 PrintFunctionDeclStart(FD);
280 fprintf(stderr,"\n");
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000281 }
Chris Lattner52332d02007-09-15 23:21:08 +0000282
Ted Kremenek3e88d752007-09-17 17:10:02 +0000283 CFG *C = CFG::buildCFG(FD->getBody());
284 VisitCFG(*C);
285 delete C;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000286}
287
288//===----------------------------------------------------------------------===//
289// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
290
291namespace {
292 class CFGDumper : public CFGVisitor {
293 const bool UseGraphviz;
294 public:
295 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
296
Chris Lattner52332d02007-09-15 23:21:08 +0000297 virtual void VisitCFG(CFG &C) {
298 if (UseGraphviz)
299 C.viewCFG();
300 else
301 C.dump();
302 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000303 };
304} // end anonymous namespace
305
Chris Lattner52332d02007-09-15 23:21:08 +0000306ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
307 return new CFGDumper(ViewGraphs);
Ted Kremenek97f75312007-08-21 21:42:03 +0000308}
309
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000310//===----------------------------------------------------------------------===//
311// AnalyzeLiveVariables - perform live variable analysis and dump results
312
313namespace {
314 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000315 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000316 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000317 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
318 SM = &Context.SourceMgr;
319 }
320
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000321 virtual void VisitCFG(CFG& C) {
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000322 LiveVariables L(C);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000323 L.runOnCFG(C);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000324 L.dumpBlockLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000325 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000326 };
327} // end anonymous namespace
328
Chris Lattner52332d02007-09-15 23:21:08 +0000329ASTConsumer *clang::CreateLiveVarAnalyzer() {
330 return new LivenessVisitor();
Ted Kremenekaa04c512007-09-06 00:17:54 +0000331}
332
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000333//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000334// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000335
336namespace {
337 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000338 Diagnostic &Diags;
339 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000340 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000341 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
342 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
343 Ctx = &Context;
344 }
345
346 virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000347 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000348 };
349} // end anonymous namespace
350
Chris Lattner52332d02007-09-15 23:21:08 +0000351ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
352 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000353}
Chris Lattner129758d2007-09-16 19:46:59 +0000354
355//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000356// Unitialized Values - run checker to flag potential uses of uninitalized
357// variables.
358
359namespace {
360 class UninitValsVisitor : public CFGVisitor {
361 Diagnostic &Diags;
362 ASTContext *Ctx;
363 public:
364 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
365 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
366 Ctx = &Context;
367 }
368
369 virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); }
370 virtual bool printFuncDeclStart() { return false; }
371 };
372} // end anonymous namespace
373
374ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
375 return new UninitValsVisitor(Diags);
376}
377
378//===----------------------------------------------------------------------===//
Chris Lattner129758d2007-09-16 19:46:59 +0000379// LLVM Emitter
380
381#include "clang/Basic/Diagnostic.h"
Devang Patela8fccb82007-10-31 20:01:01 +0000382#include "clang/Basic/TargetInfo.h"
Chris Lattner129758d2007-09-16 19:46:59 +0000383#include "clang/CodeGen/ModuleBuilder.h"
384#include "llvm/Module.h"
Devang Patela8fccb82007-10-31 20:01:01 +0000385#include "llvm/Target/TargetData.h"
386#include "llvm/Target/TargetMachine.h"
Chris Lattner129758d2007-09-16 19:46:59 +0000387#include <iostream>
388
389namespace {
390 class LLVMEmitter : public ASTConsumer {
391 Diagnostic &Diags;
392 llvm::Module *M;
Devang Patela8fccb82007-10-31 20:01:01 +0000393 const llvm::TargetData *TD;
Chris Lattner129758d2007-09-16 19:46:59 +0000394 ASTContext *Ctx;
395 CodeGen::BuilderTy *Builder;
396 public:
397 LLVMEmitter(Diagnostic &diags) : Diags(diags) {}
398 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
399 Ctx = &Context;
400 M = new llvm::Module("foo");
Devang Patela8fccb82007-10-31 20:01:01 +0000401 M->setTargetTriple(Ctx->Target.getTargetTriple());
402 TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
403 Builder = CodeGen::Init(Context, *M, *TD);
Chris Lattner129758d2007-09-16 19:46:59 +0000404 }
405
406 virtual void HandleTopLevelDecl(Decl *D) {
407 // If an error occurred, stop code generation, but continue parsing and
408 // semantic analysis (to ensure all warnings and errors are emitted).
409 if (Diags.hasErrorOccurred())
410 return;
411
412 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
413 CodeGen::CodeGenFunction(Builder, FD);
414 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
415 CodeGen::CodeGenGlobalVar(Builder, FVD);
416 } else {
417 assert(isa<TypedefDecl>(D) && "Only expected typedefs here");
418 // don't codegen for now, eventually pass down for debug info.
419 //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
420 }
421 }
422
423 ~LLVMEmitter() {
424 CodeGen::Terminate(Builder);
425
426 // Print the generated code.
427 M->print(std::cout);
428 delete M;
429 }
430 };
431} // end anonymous namespace
432
433ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags) {
434 return new LLVMEmitter(Diags);
435}
436