Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 17 | #include "clang/AST/CFG.h" |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/LiveVariables.h" |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/LocalCheckers.h" |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 20 | using namespace clang; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 22 | |
| 23 | static void PrintFunctionDeclStart(FunctionDecl *FD) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 24 | bool HasBody = FD->getBody(); |
| 25 | |
Chris Lattner | 987058a | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 26 | 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 Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 38 | 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 Lattner | 987058a | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 63 | fprintf(stderr, "%s", Proto.c_str()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 65 | if (!FD->getBody()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 66 | fprintf(stderr, ";\n"); |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 67 | // Doesn't print the body. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 70 | static void PrintTypeDefDecl(TypedefDecl *TD) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 71 | std::string S = TD->getName(); |
| 72 | TD->getUnderlyingType().getAsStringInternal(S); |
| 73 | fprintf(stderr, "typedef %s;\n", S.c_str()); |
| 74 | } |
| 75 | |
Steve Naroff | faed3bf | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 76 | static 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 Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 82 | namespace { |
| 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 Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 99 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 100 | } |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 101 | }; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 103 | |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 104 | ASTConsumer *clang::CreateASTPrinter() { return new ASTPrinter(); } |
| 105 | |
| 106 | namespace { |
| 107 | class ASTDumper : public ASTConsumer { |
| 108 | SourceManager *SM; |
| 109 | public: |
| 110 | void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 111 | SM = &Context.SourceMgr; |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 113 | |
| 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 Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 132 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 133 | |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 134 | //===----------------------------------------------------------------------===// |
| 135 | // CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit |
| 136 | // the CFGs for all function definitions. |
| 137 | |
| 138 | namespace { |
| 139 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 140 | class CFGVisitor : public ASTConsumer { |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 141 | public: |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 142 | // CFG Visitor interface to be implemented by subclass. |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 143 | virtual void VisitCFG(CFG& C) = 0; |
| 144 | virtual bool printFuncDeclStart() { return true; } |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 145 | |
| 146 | virtual void HandleTopLevelDecl(Decl *D); |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | } // end anonymous namespace |
| 150 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 151 | void 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 Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 159 | } |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | 3e88d75 | 2007-09-17 17:10:02 +0000 | [diff] [blame^] | 161 | CFG *C = CFG::buildCFG(FD->getBody()); |
| 162 | VisitCFG(*C); |
| 163 | delete C; |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | // DumpCFGs - Dump CFGs to stderr or visualize with Graphviz |
| 168 | |
| 169 | namespace { |
| 170 | class CFGDumper : public CFGVisitor { |
| 171 | const bool UseGraphviz; |
| 172 | public: |
| 173 | CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {} |
| 174 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 175 | virtual void VisitCFG(CFG &C) { |
| 176 | if (UseGraphviz) |
| 177 | C.viewCFG(); |
| 178 | else |
| 179 | C.dump(); |
| 180 | } |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 181 | }; |
| 182 | } // end anonymous namespace |
| 183 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 184 | ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) { |
| 185 | return new CFGDumper(ViewGraphs); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
| 189 | // AnalyzeLiveVariables - perform live variable analysis and dump results |
| 190 | |
| 191 | namespace { |
| 192 | class LivenessVisitor : public CFGVisitor { |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 193 | SourceManager *SM; |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 194 | public: |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 195 | virtual void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 196 | SM = &Context.SourceMgr; |
| 197 | } |
| 198 | |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 199 | virtual void VisitCFG(CFG& C) { |
| 200 | LiveVariables L; |
| 201 | L.runOnCFG(C); |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 202 | L.dumpBlockLiveness(*SM); |
| 203 | L.dumpVarLiveness(*SM); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 204 | } |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 205 | }; |
| 206 | } // end anonymous namespace |
| 207 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 208 | ASTConsumer *clang::CreateLiveVarAnalyzer() { |
| 209 | return new LivenessVisitor(); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 212 | //===----------------------------------------------------------------------===// |
| 213 | // RunDeadStores - run checker to locate dead stores in a function |
| 214 | |
| 215 | namespace { |
| 216 | class DeadStoreVisitor : public CFGVisitor { |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 217 | Diagnostic &Diags; |
| 218 | ASTContext *Ctx; |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 219 | public: |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 220 | 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 Kremenek | 39b8c4b | 2007-09-07 23:54:15 +0000 | [diff] [blame] | 226 | virtual bool printFuncDeclStart() { return false; } |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 227 | }; |
| 228 | } // end anonymous namespace |
| 229 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 230 | ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) { |
| 231 | return new DeadStoreVisitor(Diags); |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 232 | } |
Chris Lattner | 129758d | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 233 | |
| 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 | |
| 242 | namespace { |
| 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 | |
| 283 | ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags) { |
| 284 | return new LLVMEmitter(Diags); |
| 285 | } |
| 286 | |