blob: 1ec2c72d522dc9fb7b22cd3f83f3cc83614b954b [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)
94 fprintf(stderr, ">;\n");
95 else
96 fprintf(stderr, ";\n");
Steve Narofffaed3bf2007-09-10 20:51:04 +000097 // FIXME: implement the rest...
98}
99
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000100static void PrintObjcProtocolDecl(ObjcProtocolDecl *PID) {
101 std::string S = PID->getName();
102 fprintf(stderr, "@protocol %s;\n", S.c_str());
103 // FIXME: implement the rest...
104}
105
106static void PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID) {
107 std::string S = PID->getName();
108 std::string I = PID->getClassInterface()->getName();
109 fprintf(stderr, "@implementation %s(%s);\n", I.c_str(), S.c_str());
110 // FIXME: implement the rest...
111}
112
113static void PrintObjcCategoryDecl(ObjcCategoryDecl *PID) {
114 std::string S = PID->getName();
115 std::string I = PID->getClassInterface()->getName();
116 fprintf(stderr, "@interface %s(%s);\n", I.c_str(), S.c_str());
117 // FIXME: implement the rest...
118}
119
Chris Lattnerb73abd52007-09-15 23:02:28 +0000120namespace {
121 class ASTPrinter : public ASTConsumer {
122 virtual void HandleTopLevelDecl(Decl *D) {
123 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
124 PrintFunctionDeclStart(FD);
125
126 if (FD->getBody()) {
127 fprintf(stderr, " ");
128 FD->getBody()->dumpPretty();
129 fprintf(stderr, "\n");
130 }
131 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
132 PrintTypeDefDecl(TD);
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000133 } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
134 PrintObjcInterfaceDecl(OID);
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000135 } else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) {
136 PrintObjcProtocolDecl(PID);
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000137 } else if (ObjcForwardProtocolDecl *OFPD =
138 dyn_cast<ObjcForwardProtocolDecl>(D)) {
139 fprintf(stderr, "@protocol ");
140 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
141 const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
142 if (i) fprintf(stderr, ", ");
143 fprintf(stderr, "%s", D->getName());
144 }
Chris Lattnera845bbe2007-10-06 19:08:22 +0000145 fprintf(stderr, ";\n");
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000146 } else if (ObjcImplementationDecl *OID =
147 dyn_cast<ObjcImplementationDecl>(D)) {
148 fprintf(stderr, "@implementation %s [printing todo]\n",
149 OID->getName());
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000150 } else if (ObjcCategoryImplDecl *OID =
151 dyn_cast<ObjcCategoryImplDecl>(D)) {
152 PrintObjcCategoryImplDecl(OID);
153 } else if (ObjcCategoryDecl *OID =
154 dyn_cast<ObjcCategoryDecl>(D)) {
155 PrintObjcCategoryDecl(OID);
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000156 } else if (isa<ObjcClassDecl>(D)) {
157 fprintf(stderr, "@class [printing todo]\n");
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000158 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
159 fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000160 } else {
161 assert(0 && "Unknown decl type!");
Chris Lattner95578782007-08-08 22:51:59 +0000162 }
Chris Lattner4b009652007-07-25 00:24:17 +0000163 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000164 };
Chris Lattner4b009652007-07-25 00:24:17 +0000165}
Chris Lattner95578782007-08-08 22:51:59 +0000166
Chris Lattnerb73abd52007-09-15 23:02:28 +0000167ASTConsumer *clang::CreateASTPrinter() { return new ASTPrinter(); }
168
169namespace {
170 class ASTDumper : public ASTConsumer {
171 SourceManager *SM;
172 public:
173 void Initialize(ASTContext &Context, unsigned MainFileID) {
174 SM = &Context.SourceMgr;
Chris Lattner95578782007-08-08 22:51:59 +0000175 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000176
177 virtual void HandleTopLevelDecl(Decl *D) {
178 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
179 PrintFunctionDeclStart(FD);
180
181 if (FD->getBody()) {
182 fprintf(stderr, "\n");
183 FD->getBody()->dumpAll(*SM);
184 fprintf(stderr, "\n");
185 }
186 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
187 PrintTypeDefDecl(TD);
188 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
189 fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000190 } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
191 fprintf(stderr, "Read objc interface '%s'\n", OID->getName());
192 } else if (isa<ObjcForwardProtocolDecl>(D)) {
193 fprintf(stderr, "Read objc fwd protocol decl\n");
194 } else {
195 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000196 }
197 }
198 };
Chris Lattner95578782007-08-08 22:51:59 +0000199}
200
Chris Lattnerb73abd52007-09-15 23:02:28 +0000201ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
202
Ted Kremenekb6976a22007-09-19 21:29:43 +0000203namespace {
204 class ASTViewer : public ASTConsumer {
205 SourceManager *SM;
206 public:
207 void Initialize(ASTContext &Context, unsigned MainFileID) {
208 SM = &Context.SourceMgr;
209 }
210
211 virtual void HandleTopLevelDecl(Decl *D) {
212 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
213 PrintFunctionDeclStart(FD);
214
215 if (FD->getBody()) {
216 fprintf(stderr, "\n");
217 FD->getBody()->viewAST();
218 fprintf(stderr, "\n");
219 }
220 }
221 }
222 };
223}
224
225ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
226
227
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000228//===----------------------------------------------------------------------===//
229// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
230// the CFGs for all function definitions.
231
232namespace {
233
Chris Lattner52332d02007-09-15 23:21:08 +0000234class CFGVisitor : public ASTConsumer {
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000235public:
Chris Lattner52332d02007-09-15 23:21:08 +0000236 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000237 virtual void VisitCFG(CFG& C) = 0;
238 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000239
240 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000241};
242
243} // end anonymous namespace
244
Chris Lattner52332d02007-09-15 23:21:08 +0000245void CFGVisitor::HandleTopLevelDecl(Decl *D) {
246 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
247 if (!FD || !FD->getBody())
248 return;
249
250 if (printFuncDeclStart()) {
251 PrintFunctionDeclStart(FD);
252 fprintf(stderr,"\n");
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000253 }
Chris Lattner52332d02007-09-15 23:21:08 +0000254
Ted Kremenek3e88d752007-09-17 17:10:02 +0000255 CFG *C = CFG::buildCFG(FD->getBody());
256 VisitCFG(*C);
257 delete C;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000258}
259
260//===----------------------------------------------------------------------===//
261// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
262
263namespace {
264 class CFGDumper : public CFGVisitor {
265 const bool UseGraphviz;
266 public:
267 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
268
Chris Lattner52332d02007-09-15 23:21:08 +0000269 virtual void VisitCFG(CFG &C) {
270 if (UseGraphviz)
271 C.viewCFG();
272 else
273 C.dump();
274 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000275 };
276} // end anonymous namespace
277
Chris Lattner52332d02007-09-15 23:21:08 +0000278ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
279 return new CFGDumper(ViewGraphs);
Ted Kremenek97f75312007-08-21 21:42:03 +0000280}
281
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000282//===----------------------------------------------------------------------===//
283// AnalyzeLiveVariables - perform live variable analysis and dump results
284
285namespace {
286 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000287 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000288 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000289 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
290 SM = &Context.SourceMgr;
291 }
292
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000293 virtual void VisitCFG(CFG& C) {
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000294 LiveVariables L(C);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000295 L.runOnCFG(C);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000296 L.dumpBlockLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000297 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000298 };
299} // end anonymous namespace
300
Chris Lattner52332d02007-09-15 23:21:08 +0000301ASTConsumer *clang::CreateLiveVarAnalyzer() {
302 return new LivenessVisitor();
Ted Kremenekaa04c512007-09-06 00:17:54 +0000303}
304
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000305//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000306// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000307
308namespace {
309 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000310 Diagnostic &Diags;
311 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000312 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000313 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
314 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
315 Ctx = &Context;
316 }
317
318 virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000319 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000320 };
321} // end anonymous namespace
322
Chris Lattner52332d02007-09-15 23:21:08 +0000323ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
324 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000325}
Chris Lattner129758d2007-09-16 19:46:59 +0000326
327//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000328// Unitialized Values - run checker to flag potential uses of uninitalized
329// variables.
330
331namespace {
332 class UninitValsVisitor : public CFGVisitor {
333 Diagnostic &Diags;
334 ASTContext *Ctx;
335 public:
336 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
337 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
338 Ctx = &Context;
339 }
340
341 virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); }
342 virtual bool printFuncDeclStart() { return false; }
343 };
344} // end anonymous namespace
345
346ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
347 return new UninitValsVisitor(Diags);
348}
349
350//===----------------------------------------------------------------------===//
Chris Lattner129758d2007-09-16 19:46:59 +0000351// LLVM Emitter
352
353#include "clang/Basic/Diagnostic.h"
354#include "clang/CodeGen/ModuleBuilder.h"
355#include "llvm/Module.h"
356#include <iostream>
357
358namespace {
359 class LLVMEmitter : public ASTConsumer {
360 Diagnostic &Diags;
361 llvm::Module *M;
362 ASTContext *Ctx;
363 CodeGen::BuilderTy *Builder;
364 public:
365 LLVMEmitter(Diagnostic &diags) : Diags(diags) {}
366 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
367 Ctx = &Context;
368 M = new llvm::Module("foo");
369 Builder = CodeGen::Init(Context, *M);
370 }
371
372 virtual void HandleTopLevelDecl(Decl *D) {
373 // If an error occurred, stop code generation, but continue parsing and
374 // semantic analysis (to ensure all warnings and errors are emitted).
375 if (Diags.hasErrorOccurred())
376 return;
377
378 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
379 CodeGen::CodeGenFunction(Builder, FD);
380 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
381 CodeGen::CodeGenGlobalVar(Builder, FVD);
382 } else {
383 assert(isa<TypedefDecl>(D) && "Only expected typedefs here");
384 // don't codegen for now, eventually pass down for debug info.
385 //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
386 }
387 }
388
389 ~LLVMEmitter() {
390 CodeGen::Terminate(Builder);
391
392 // Print the generated code.
393 M->print(std::cout);
394 delete M;
395 }
396 };
397} // end anonymous namespace
398
399ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags) {
400 return new LLVMEmitter(Diags);
401}
402