blob: cce19e795aa293d6509db6e43f01b06200b13a0a [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTStreamers.cpp - ASTStreamer Drivers ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Bill Wendling and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// ASTStreamer drivers.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ASTStreamers.h"
15#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) {
77 std::string S = OID->getName();
78 fprintf(stderr, "@interface %s;\n", S.c_str());
79 // FIXME: implement the rest...
80}
81
Chris Lattnerb73abd52007-09-15 23:02:28 +000082namespace {
83 class ASTPrinter : public ASTConsumer {
84 virtual void HandleTopLevelDecl(Decl *D) {
85 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
86 PrintFunctionDeclStart(FD);
87
88 if (FD->getBody()) {
89 fprintf(stderr, " ");
90 FD->getBody()->dumpPretty();
91 fprintf(stderr, "\n");
92 }
93 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
94 PrintTypeDefDecl(TD);
95 } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
96 PrintObjcInterfaceDecl(OID);
97 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
98 fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
Chris Lattner95578782007-08-08 22:51:59 +000099 }
Chris Lattner4b009652007-07-25 00:24:17 +0000100 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000101 };
Chris Lattner4b009652007-07-25 00:24:17 +0000102}
Chris Lattner95578782007-08-08 22:51:59 +0000103
Chris Lattnerb73abd52007-09-15 23:02:28 +0000104ASTConsumer *clang::CreateASTPrinter() { return new ASTPrinter(); }
105
106namespace {
107 class ASTDumper : public ASTConsumer {
108 SourceManager *SM;
109 public:
110 void Initialize(ASTContext &Context, unsigned MainFileID) {
111 SM = &Context.SourceMgr;
Chris Lattner95578782007-08-08 22:51:59 +0000112 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000113
114 virtual void HandleTopLevelDecl(Decl *D) {
115 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
116 PrintFunctionDeclStart(FD);
117
118 if (FD->getBody()) {
119 fprintf(stderr, "\n");
120 FD->getBody()->dumpAll(*SM);
121 fprintf(stderr, "\n");
122 }
123 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
124 PrintTypeDefDecl(TD);
125 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
126 fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName());
127 }
128 }
129 };
Chris Lattner95578782007-08-08 22:51:59 +0000130}
131
Chris Lattnerb73abd52007-09-15 23:02:28 +0000132ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
133
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000134//===----------------------------------------------------------------------===//
135// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
136// the CFGs for all function definitions.
137
138namespace {
139
Chris Lattner52332d02007-09-15 23:21:08 +0000140class CFGVisitor : public ASTConsumer {
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000141public:
Chris Lattner52332d02007-09-15 23:21:08 +0000142 // CFG Visitor interface to be implemented by subclass.
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000143 virtual void VisitCFG(CFG& C) = 0;
144 virtual bool printFuncDeclStart() { return true; }
Chris Lattner52332d02007-09-15 23:21:08 +0000145
146 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000147};
148
149} // end anonymous namespace
150
Chris Lattner52332d02007-09-15 23:21:08 +0000151void CFGVisitor::HandleTopLevelDecl(Decl *D) {
152 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
153 if (!FD || !FD->getBody())
154 return;
155
156 if (printFuncDeclStart()) {
157 PrintFunctionDeclStart(FD);
158 fprintf(stderr,"\n");
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000159 }
Chris Lattner52332d02007-09-15 23:21:08 +0000160
Ted Kremenek3e88d752007-09-17 17:10:02 +0000161 CFG *C = CFG::buildCFG(FD->getBody());
162 VisitCFG(*C);
163 delete C;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000164}
165
166//===----------------------------------------------------------------------===//
167// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
168
169namespace {
170 class CFGDumper : public CFGVisitor {
171 const bool UseGraphviz;
172 public:
173 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
174
Chris Lattner52332d02007-09-15 23:21:08 +0000175 virtual void VisitCFG(CFG &C) {
176 if (UseGraphviz)
177 C.viewCFG();
178 else
179 C.dump();
180 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000181 };
182} // end anonymous namespace
183
Chris Lattner52332d02007-09-15 23:21:08 +0000184ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
185 return new CFGDumper(ViewGraphs);
Ted Kremenek97f75312007-08-21 21:42:03 +0000186}
187
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000188//===----------------------------------------------------------------------===//
189// AnalyzeLiveVariables - perform live variable analysis and dump results
190
191namespace {
192 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000193 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000194 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000195 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
196 SM = &Context.SourceMgr;
197 }
198
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000199 virtual void VisitCFG(CFG& C) {
200 LiveVariables L;
201 L.runOnCFG(C);
Chris Lattner52332d02007-09-15 23:21:08 +0000202 L.dumpBlockLiveness(*SM);
203 L.dumpVarLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000204 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000205 };
206} // end anonymous namespace
207
Chris Lattner52332d02007-09-15 23:21:08 +0000208ASTConsumer *clang::CreateLiveVarAnalyzer() {
209 return new LivenessVisitor();
Ted Kremenekaa04c512007-09-06 00:17:54 +0000210}
211
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000212//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000213// DeadStores - run checker to locate dead stores in a function
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000214
215namespace {
216 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000217 Diagnostic &Diags;
218 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000219 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000220 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
221 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
222 Ctx = &Context;
223 }
224
225 virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000226 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000227 };
228} // end anonymous namespace
229
Chris Lattner52332d02007-09-15 23:21:08 +0000230ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
231 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000232}
Chris Lattner129758d2007-09-16 19:46:59 +0000233
234//===----------------------------------------------------------------------===//
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000235// Unitialized Values - run checker to flag potential uses of uninitalized
236// variables.
237
238namespace {
239 class UninitValsVisitor : public CFGVisitor {
240 Diagnostic &Diags;
241 ASTContext *Ctx;
242 public:
243 UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
244 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
245 Ctx = &Context;
246 }
247
248 virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); }
249 virtual bool printFuncDeclStart() { return false; }
250 };
251} // end anonymous namespace
252
253ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
254 return new UninitValsVisitor(Diags);
255}
256
257//===----------------------------------------------------------------------===//
Chris Lattner129758d2007-09-16 19:46:59 +0000258// LLVM Emitter
259
260#include "clang/Basic/Diagnostic.h"
261#include "clang/CodeGen/ModuleBuilder.h"
262#include "llvm/Module.h"
263#include <iostream>
264
265namespace {
266 class LLVMEmitter : public ASTConsumer {
267 Diagnostic &Diags;
268 llvm::Module *M;
269 ASTContext *Ctx;
270 CodeGen::BuilderTy *Builder;
271 public:
272 LLVMEmitter(Diagnostic &diags) : Diags(diags) {}
273 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
274 Ctx = &Context;
275 M = new llvm::Module("foo");
276 Builder = CodeGen::Init(Context, *M);
277 }
278
279 virtual void HandleTopLevelDecl(Decl *D) {
280 // If an error occurred, stop code generation, but continue parsing and
281 // semantic analysis (to ensure all warnings and errors are emitted).
282 if (Diags.hasErrorOccurred())
283 return;
284
285 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
286 CodeGen::CodeGenFunction(Builder, FD);
287 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
288 CodeGen::CodeGenGlobalVar(Builder, FVD);
289 } else {
290 assert(isa<TypedefDecl>(D) && "Only expected typedefs here");
291 // don't codegen for now, eventually pass down for debug info.
292 //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
293 }
294 }
295
296 ~LLVMEmitter() {
297 CodeGen::Terminate(Builder);
298
299 // Print the generated code.
300 M->print(std::cout);
301 delete M;
302 }
303 };
304} // end anonymous namespace
305
306ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags) {
307 return new LLVMEmitter(Diags);
308}
309