blob: 3c67d9524a84391c13a2dddbc700ace1ac2413db [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
161 if (CFG *C = CFG::buildCFG(FD->getBody())) {
162 VisitCFG(*C);
163 delete C;
164 } else
165 fprintf(stderr, " Error processing CFG.\n");
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000166}
167
168//===----------------------------------------------------------------------===//
169// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
170
171namespace {
172 class CFGDumper : public CFGVisitor {
173 const bool UseGraphviz;
174 public:
175 CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
176
Chris Lattner52332d02007-09-15 23:21:08 +0000177 virtual void VisitCFG(CFG &C) {
178 if (UseGraphviz)
179 C.viewCFG();
180 else
181 C.dump();
182 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000183 };
184} // end anonymous namespace
185
Chris Lattner52332d02007-09-15 23:21:08 +0000186ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
187 return new CFGDumper(ViewGraphs);
Ted Kremenek97f75312007-08-21 21:42:03 +0000188}
189
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000190//===----------------------------------------------------------------------===//
191// AnalyzeLiveVariables - perform live variable analysis and dump results
192
193namespace {
194 class LivenessVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000195 SourceManager *SM;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000196 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000197 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
198 SM = &Context.SourceMgr;
199 }
200
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000201 virtual void VisitCFG(CFG& C) {
202 LiveVariables L;
203 L.runOnCFG(C);
Chris Lattner52332d02007-09-15 23:21:08 +0000204 L.dumpBlockLiveness(*SM);
205 L.dumpVarLiveness(*SM);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000206 }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000207 };
208} // end anonymous namespace
209
Chris Lattner52332d02007-09-15 23:21:08 +0000210ASTConsumer *clang::CreateLiveVarAnalyzer() {
211 return new LivenessVisitor();
Ted Kremenekaa04c512007-09-06 00:17:54 +0000212}
213
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000214//===----------------------------------------------------------------------===//
215// RunDeadStores - run checker to locate dead stores in a function
216
217namespace {
218 class DeadStoreVisitor : public CFGVisitor {
Chris Lattner52332d02007-09-15 23:21:08 +0000219 Diagnostic &Diags;
220 ASTContext *Ctx;
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000221 public:
Chris Lattner52332d02007-09-15 23:21:08 +0000222 DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
223 virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
224 Ctx = &Context;
225 }
226
227 virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
Ted Kremenek39b8c4b2007-09-07 23:54:15 +0000228 virtual bool printFuncDeclStart() { return false; }
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000229 };
230} // end anonymous namespace
231
Chris Lattner52332d02007-09-15 23:21:08 +0000232ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
233 return new DeadStoreVisitor(Diags);
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000234}