blob: 124b9e2e51e61bbc7220c4973aff7fac8b963ec1 [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//===----------------------------------------------------------------------===//
213// RunDeadStores - run checker to locate dead stores in a function
214
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//===----------------------------------------------------------------------===//
235// LLVM Emitter
236
237#include "clang/Basic/Diagnostic.h"
238#include "clang/CodeGen/ModuleBuilder.h"
239#include "llvm/Module.h"
240#include <iostream>
241
242namespace {
243 class LLVMEmitter : public ASTConsumer {
244 Diagnostic &Diags;
245 llvm::Module *M;
246 ASTContext *Ctx;
247 CodeGen::BuilderTy *Builder;
248 public:
249 LLVMEmitter(Diagnostic &diags) : Diags(diags) {}
250 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
251 Ctx = &Context;
252 M = new llvm::Module("foo");
253 Builder = CodeGen::Init(Context, *M);
254 }
255
256 virtual void HandleTopLevelDecl(Decl *D) {
257 // If an error occurred, stop code generation, but continue parsing and
258 // semantic analysis (to ensure all warnings and errors are emitted).
259 if (Diags.hasErrorOccurred())
260 return;
261
262 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
263 CodeGen::CodeGenFunction(Builder, FD);
264 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
265 CodeGen::CodeGenGlobalVar(Builder, FVD);
266 } else {
267 assert(isa<TypedefDecl>(D) && "Only expected typedefs here");
268 // don't codegen for now, eventually pass down for debug info.
269 //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
270 }
271 }
272
273 ~LLVMEmitter() {
274 CodeGen::Terminate(Builder);
275
276 // Print the generated code.
277 M->print(std::cout);
278 delete M;
279 }
280 };
281} // end anonymous namespace
282
283ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags) {
284 return new LLVMEmitter(Diags);
285}
286