Chris Lattner | eb8c963 | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 1 | //===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | eb8c963 | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 5 | // This file was developed by Chris Lattner and is distributed under the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | eb8c963 | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 10 | // AST Consumer Implementations. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | eb8c963 | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 14 | #include "ASTConsumers.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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) { |
Fariborz Jahanian | c04aff1 | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 77 | 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 Naroff | faed3bf | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 97 | // FIXME: implement the rest... |
| 98 | } |
| 99 | |
Fariborz Jahanian | ac20be2 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 100 | static 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 | |
| 106 | static 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 | |
| 113 | static 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 | |
Fariborz Jahanian | 05d212a | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 120 | static void PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) { |
| 121 | std::string A = AID->getName(); |
| 122 | std::string I = AID->getClassInterface()->getName(); |
| 123 | fprintf(stderr, "@compatibility_alias %s %s;\n", A.c_str(), I.c_str()); |
| 124 | } |
| 125 | |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 126 | namespace { |
| 127 | class ASTPrinter : public ASTConsumer { |
| 128 | virtual void HandleTopLevelDecl(Decl *D) { |
| 129 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 130 | PrintFunctionDeclStart(FD); |
| 131 | |
| 132 | if (FD->getBody()) { |
| 133 | fprintf(stderr, " "); |
| 134 | FD->getBody()->dumpPretty(); |
| 135 | fprintf(stderr, "\n"); |
| 136 | } |
| 137 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 138 | PrintTypeDefDecl(TD); |
Chris Lattner | d5c9d3d | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 139 | } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) { |
| 140 | PrintObjcInterfaceDecl(OID); |
Fariborz Jahanian | ac20be2 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 141 | } else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) { |
| 142 | PrintObjcProtocolDecl(PID); |
Chris Lattner | d5c9d3d | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 143 | } else if (ObjcForwardProtocolDecl *OFPD = |
| 144 | dyn_cast<ObjcForwardProtocolDecl>(D)) { |
| 145 | fprintf(stderr, "@protocol "); |
| 146 | for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) { |
| 147 | const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i); |
| 148 | if (i) fprintf(stderr, ", "); |
| 149 | fprintf(stderr, "%s", D->getName()); |
| 150 | } |
Chris Lattner | a845bbe | 2007-10-06 19:08:22 +0000 | [diff] [blame] | 151 | fprintf(stderr, ";\n"); |
Chris Lattner | d5c9d3d | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 152 | } else if (ObjcImplementationDecl *OID = |
| 153 | dyn_cast<ObjcImplementationDecl>(D)) { |
| 154 | fprintf(stderr, "@implementation %s [printing todo]\n", |
| 155 | OID->getName()); |
Fariborz Jahanian | ac20be2 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 156 | } else if (ObjcCategoryImplDecl *OID = |
| 157 | dyn_cast<ObjcCategoryImplDecl>(D)) { |
| 158 | PrintObjcCategoryImplDecl(OID); |
| 159 | } else if (ObjcCategoryDecl *OID = |
| 160 | dyn_cast<ObjcCategoryDecl>(D)) { |
| 161 | PrintObjcCategoryDecl(OID); |
Fariborz Jahanian | 05d212a | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 162 | } else if (ObjcCompatibleAliasDecl *OID = |
| 163 | dyn_cast<ObjcCompatibleAliasDecl>(D)) { |
| 164 | PrintObjcCompatibleAliasDecl(OID); |
Chris Lattner | d5c9d3d | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 165 | } else if (isa<ObjcClassDecl>(D)) { |
| 166 | fprintf(stderr, "@class [printing todo]\n"); |
Fariborz Jahanian | ac20be2 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 167 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
| 168 | fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName()); |
Chris Lattner | d5c9d3d | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 169 | } else { |
| 170 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 171 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 172 | } |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 173 | }; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 174 | } |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 175 | |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 176 | ASTConsumer *clang::CreateASTPrinter() { return new ASTPrinter(); } |
| 177 | |
| 178 | namespace { |
| 179 | class ASTDumper : public ASTConsumer { |
| 180 | SourceManager *SM; |
| 181 | public: |
| 182 | void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 183 | SM = &Context.SourceMgr; |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 184 | } |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 185 | |
| 186 | virtual void HandleTopLevelDecl(Decl *D) { |
| 187 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 188 | PrintFunctionDeclStart(FD); |
| 189 | |
| 190 | if (FD->getBody()) { |
| 191 | fprintf(stderr, "\n"); |
| 192 | FD->getBody()->dumpAll(*SM); |
| 193 | fprintf(stderr, "\n"); |
| 194 | } |
| 195 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 196 | PrintTypeDefDecl(TD); |
| 197 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
| 198 | fprintf(stderr, "Read top-level variable decl: '%s'\n", SD->getName()); |
Chris Lattner | d5c9d3d | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 199 | } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) { |
| 200 | fprintf(stderr, "Read objc interface '%s'\n", OID->getName()); |
| 201 | } else if (isa<ObjcForwardProtocolDecl>(D)) { |
| 202 | fprintf(stderr, "Read objc fwd protocol decl\n"); |
| 203 | } else { |
| 204 | assert(0 && "Unknown decl type!"); |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | }; |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Chris Lattner | b73abd5 | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 210 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 211 | |
Ted Kremenek | b6976a2 | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 212 | namespace { |
| 213 | class ASTViewer : public ASTConsumer { |
| 214 | SourceManager *SM; |
| 215 | public: |
| 216 | void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 217 | SM = &Context.SourceMgr; |
| 218 | } |
| 219 | |
| 220 | virtual void HandleTopLevelDecl(Decl *D) { |
| 221 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 222 | PrintFunctionDeclStart(FD); |
| 223 | |
| 224 | if (FD->getBody()) { |
| 225 | fprintf(stderr, "\n"); |
| 226 | FD->getBody()->viewAST(); |
| 227 | fprintf(stderr, "\n"); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | }; |
| 232 | } |
| 233 | |
| 234 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 235 | |
| 236 | |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 237 | //===----------------------------------------------------------------------===// |
| 238 | // CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit |
| 239 | // the CFGs for all function definitions. |
| 240 | |
| 241 | namespace { |
| 242 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 243 | class CFGVisitor : public ASTConsumer { |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 244 | public: |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 245 | // CFG Visitor interface to be implemented by subclass. |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 246 | virtual void VisitCFG(CFG& C) = 0; |
| 247 | virtual bool printFuncDeclStart() { return true; } |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 248 | |
| 249 | virtual void HandleTopLevelDecl(Decl *D); |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | } // end anonymous namespace |
| 253 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 254 | void CFGVisitor::HandleTopLevelDecl(Decl *D) { |
| 255 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 256 | if (!FD || !FD->getBody()) |
| 257 | return; |
| 258 | |
| 259 | if (printFuncDeclStart()) { |
| 260 | PrintFunctionDeclStart(FD); |
| 261 | fprintf(stderr,"\n"); |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 262 | } |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 3e88d75 | 2007-09-17 17:10:02 +0000 | [diff] [blame] | 264 | CFG *C = CFG::buildCFG(FD->getBody()); |
| 265 | VisitCFG(*C); |
| 266 | delete C; |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | //===----------------------------------------------------------------------===// |
| 270 | // DumpCFGs - Dump CFGs to stderr or visualize with Graphviz |
| 271 | |
| 272 | namespace { |
| 273 | class CFGDumper : public CFGVisitor { |
| 274 | const bool UseGraphviz; |
| 275 | public: |
| 276 | CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {} |
| 277 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 278 | virtual void VisitCFG(CFG &C) { |
| 279 | if (UseGraphviz) |
| 280 | C.viewCFG(); |
| 281 | else |
| 282 | C.dump(); |
| 283 | } |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 284 | }; |
| 285 | } // end anonymous namespace |
| 286 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 287 | ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) { |
| 288 | return new CFGDumper(ViewGraphs); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 291 | //===----------------------------------------------------------------------===// |
| 292 | // AnalyzeLiveVariables - perform live variable analysis and dump results |
| 293 | |
| 294 | namespace { |
| 295 | class LivenessVisitor : public CFGVisitor { |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 296 | SourceManager *SM; |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 297 | public: |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 298 | virtual void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 299 | SM = &Context.SourceMgr; |
| 300 | } |
| 301 | |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 302 | virtual void VisitCFG(CFG& C) { |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 303 | LiveVariables L(C); |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 304 | L.runOnCFG(C); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 305 | L.dumpBlockLiveness(*SM); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 306 | } |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 307 | }; |
| 308 | } // end anonymous namespace |
| 309 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 310 | ASTConsumer *clang::CreateLiveVarAnalyzer() { |
| 311 | return new LivenessVisitor(); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 314 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 315 | // DeadStores - run checker to locate dead stores in a function |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 316 | |
| 317 | namespace { |
| 318 | class DeadStoreVisitor : public CFGVisitor { |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 319 | Diagnostic &Diags; |
| 320 | ASTContext *Ctx; |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 321 | public: |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 322 | DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {} |
| 323 | virtual void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 324 | Ctx = &Context; |
| 325 | } |
| 326 | |
| 327 | virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); } |
Ted Kremenek | 39b8c4b | 2007-09-07 23:54:15 +0000 | [diff] [blame] | 328 | virtual bool printFuncDeclStart() { return false; } |
Ted Kremenek | 1e3c202 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 329 | }; |
| 330 | } // end anonymous namespace |
| 331 | |
Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 332 | ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) { |
| 333 | return new DeadStoreVisitor(Diags); |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | 129758d | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 335 | |
| 336 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 337 | // Unitialized Values - run checker to flag potential uses of uninitalized |
| 338 | // variables. |
| 339 | |
| 340 | namespace { |
| 341 | class UninitValsVisitor : public CFGVisitor { |
| 342 | Diagnostic &Diags; |
| 343 | ASTContext *Ctx; |
| 344 | public: |
| 345 | UninitValsVisitor(Diagnostic &diags) : Diags(diags) {} |
| 346 | virtual void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 347 | Ctx = &Context; |
| 348 | } |
| 349 | |
| 350 | virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); } |
| 351 | virtual bool printFuncDeclStart() { return false; } |
| 352 | }; |
| 353 | } // end anonymous namespace |
| 354 | |
| 355 | ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) { |
| 356 | return new UninitValsVisitor(Diags); |
| 357 | } |
| 358 | |
| 359 | //===----------------------------------------------------------------------===// |
Chris Lattner | 129758d | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 360 | // LLVM Emitter |
| 361 | |
| 362 | #include "clang/Basic/Diagnostic.h" |
| 363 | #include "clang/CodeGen/ModuleBuilder.h" |
| 364 | #include "llvm/Module.h" |
| 365 | #include <iostream> |
| 366 | |
| 367 | namespace { |
| 368 | class LLVMEmitter : public ASTConsumer { |
| 369 | Diagnostic &Diags; |
| 370 | llvm::Module *M; |
| 371 | ASTContext *Ctx; |
| 372 | CodeGen::BuilderTy *Builder; |
| 373 | public: |
| 374 | LLVMEmitter(Diagnostic &diags) : Diags(diags) {} |
| 375 | virtual void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 376 | Ctx = &Context; |
| 377 | M = new llvm::Module("foo"); |
| 378 | Builder = CodeGen::Init(Context, *M); |
| 379 | } |
| 380 | |
| 381 | virtual void HandleTopLevelDecl(Decl *D) { |
| 382 | // If an error occurred, stop code generation, but continue parsing and |
| 383 | // semantic analysis (to ensure all warnings and errors are emitted). |
| 384 | if (Diags.hasErrorOccurred()) |
| 385 | return; |
| 386 | |
| 387 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 388 | CodeGen::CodeGenFunction(Builder, FD); |
| 389 | } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) { |
| 390 | CodeGen::CodeGenGlobalVar(Builder, FVD); |
| 391 | } else { |
| 392 | assert(isa<TypedefDecl>(D) && "Only expected typedefs here"); |
| 393 | // don't codegen for now, eventually pass down for debug info. |
| 394 | //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n"; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | ~LLVMEmitter() { |
| 399 | CodeGen::Terminate(Builder); |
| 400 | |
| 401 | // Print the generated code. |
| 402 | M->print(std::cout); |
| 403 | delete M; |
| 404 | } |
| 405 | }; |
| 406 | } // end anonymous namespace |
| 407 | |
| 408 | ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags) { |
| 409 | return new LLVMEmitter(Diags); |
| 410 | } |
| 411 | |