Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 1 | //===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 5 | // This file was developed by Chris Lattner and is distributed under the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 10 | // AST Consumer Implementations. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 14 | #include "ASTConsumers.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "clang/AST/AST.h" |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 17 | #include "clang/AST/CFG.h" |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/LiveVariables.h" |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 21 | using namespace clang; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | /// DeclPrinter - Utility class for printing top-level decls. |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | class DeclPrinter { |
| 28 | public: |
| 29 | FILE* FP; |
| 30 | |
| 31 | DeclPrinter(FILE* fp) : FP(fp ? fp : stderr) {} |
| 32 | |
| 33 | void PrintFunctionDeclStart(FunctionDecl *FD); |
| 34 | void PrintTypeDefDecl(TypedefDecl *TD); |
| 35 | void PrintObjcMethodDecl(ObjcMethodDecl *OMD); |
| 36 | void PrintObjcImplementationDecl(ObjcImplementationDecl *OID); |
| 37 | void PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID); |
| 38 | void PrintObjcProtocolDecl(ObjcProtocolDecl *PID); |
| 39 | void PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID); |
| 40 | void PrintObjcCategoryDecl(ObjcCategoryDecl *PID); |
| 41 | void PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID); |
| 42 | }; |
| 43 | } // end anonymous namespace |
| 44 | |
| 45 | void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | bool HasBody = FD->getBody(); |
| 47 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 48 | fprintf(FP, "\n"); |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 49 | |
| 50 | switch (FD->getStorageClass()) { |
| 51 | default: assert(0 && "Unknown storage class"); |
| 52 | case FunctionDecl::None: break; |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 53 | case FunctionDecl::Extern: fprintf(FP, "extern "); break; |
| 54 | case FunctionDecl::Static: fprintf(FP, "static "); break; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (FD->isInline()) |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 58 | fprintf(FP, "inline "); |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 59 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | std::string Proto = FD->getName(); |
| 61 | FunctionType *AFT = cast<FunctionType>(FD->getType()); |
| 62 | |
| 63 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) { |
| 64 | Proto += "("; |
| 65 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 66 | if (i) Proto += ", "; |
| 67 | std::string ParamStr; |
| 68 | if (HasBody) ParamStr = FD->getParamDecl(i)->getName(); |
| 69 | |
| 70 | FT->getArgType(i).getAsStringInternal(ParamStr); |
| 71 | Proto += ParamStr; |
| 72 | } |
| 73 | |
| 74 | if (FT->isVariadic()) { |
| 75 | if (FD->getNumParams()) Proto += ", "; |
| 76 | Proto += "..."; |
| 77 | } |
| 78 | Proto += ")"; |
| 79 | } else { |
| 80 | assert(isa<FunctionTypeNoProto>(AFT)); |
| 81 | Proto += "()"; |
| 82 | } |
| 83 | |
| 84 | AFT->getResultType().getAsStringInternal(Proto); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 85 | fprintf(FP, "%s", Proto.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 87 | if (!FD->getBody()) |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 88 | fprintf(FP, ";\n"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 89 | // Doesn't print the body. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 92 | void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | std::string S = TD->getName(); |
| 94 | TD->getUnderlyingType().getAsStringInternal(S); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 95 | fprintf(FP, "typedef %s;\n", S.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 98 | void DeclPrinter::PrintObjcMethodDecl(ObjcMethodDecl *OMD) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 99 | if (OMD->isInstance()) |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 100 | fprintf(FP, "\n- "); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 101 | else |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 102 | fprintf(FP, "\n+ "); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 103 | if (!OMD->getResultType().isNull()) |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 104 | fprintf(FP, "(%s) ", OMD->getResultType().getAsString().c_str()); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 105 | // FIXME: just print original selector name! |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 106 | fprintf(FP, "%s ", OMD->getSelector().getName().c_str()); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 107 | |
| 108 | for (int i = 0; i < OMD->getNumParams(); i++) { |
| 109 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
| 110 | // FIXME: selector is missing here! |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 111 | fprintf(FP, " :(%s) %s", PDecl->getType().getAsString().c_str(), |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 112 | PDecl->getName()); |
| 113 | } |
| 114 | } |
| 115 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 116 | void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 117 | std::string I = OID->getName(); |
| 118 | ObjcInterfaceDecl *SID = OID->getSuperClass(); |
| 119 | if (SID) { |
| 120 | std::string S = SID->getName(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 121 | fprintf(FP, "@implementation %s : %s", I.c_str(), S.c_str()); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 122 | } |
| 123 | else |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 124 | fprintf(FP, "@implementation %s", I.c_str()); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 125 | |
| 126 | for (int i = 0; i < OID->getNumInstanceMethods(); i++) { |
| 127 | PrintObjcMethodDecl(OID->getInstanceMethods()[i]); |
| 128 | ObjcMethodDecl *OMD = OID->getInstanceMethods()[i]; |
| 129 | if (OMD->getBody()) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 130 | fprintf(FP, " "); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 131 | OMD->getBody()->dumpPretty(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 132 | fprintf(FP, "\n"); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | for (int i = 0; i < OID->getNumClassMethods(); i++) { |
| 137 | PrintObjcMethodDecl(OID->getClassMethods()[i]); |
| 138 | ObjcMethodDecl *OMD = OID->getClassMethods()[i]; |
| 139 | if (OMD->getBody()) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 140 | fprintf(FP, " "); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 141 | OMD->getBody()->dumpPretty(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 142 | fprintf(FP, "\n"); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 146 | fprintf(FP,"@end\n"); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 150 | void DeclPrinter::PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) { |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 151 | std::string I = OID->getName(); |
| 152 | ObjcInterfaceDecl *SID = OID->getSuperClass(); |
| 153 | if (SID) { |
| 154 | std::string S = SID->getName(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 155 | fprintf(FP, "@interface %s : %s", I.c_str(), S.c_str()); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 156 | } |
| 157 | else |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 158 | fprintf(FP, "@interface %s", I.c_str()); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 159 | // Protocols? |
| 160 | int count = OID->getNumIntfRefProtocols(); |
| 161 | if (count > 0) { |
| 162 | ObjcProtocolDecl **refProtocols = OID->getReferencedProtocols(); |
| 163 | for (int i = 0; i < count; i++) |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 164 | fprintf(FP, "%c%s", (i == 0 ? '<' : ','), |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 165 | refProtocols[i]->getName()); |
| 166 | } |
| 167 | if (count > 0) |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 168 | fprintf(FP, ">\n"); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 169 | else |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 170 | fprintf(FP, "\n"); |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 171 | |
Steve Naroff | 0330071 | 2007-11-12 13:56:41 +0000 | [diff] [blame] | 172 | int NumIvars = OID->getNumInstanceVariables(); |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 173 | if (NumIvars > 0) { |
Steve Naroff | 0330071 | 2007-11-12 13:56:41 +0000 | [diff] [blame] | 174 | ObjcIvarDecl **Ivars = OID->getInstanceVariables(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 175 | fprintf(FP,"{"); |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 176 | for (int i = 0; i < NumIvars; i++) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 177 | fprintf(FP, "\t%s %s;\n", Ivars[i]->getType().getAsString().c_str(), |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 178 | Ivars[i]->getName()); |
| 179 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 180 | fprintf(FP, "}\n"); |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 181 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 182 | |
| 183 | int NumProperties = OID->getNumPropertyDecl(); |
| 184 | if (NumProperties > 0) { |
| 185 | for (int i = 0; i < NumProperties; i++) { |
| 186 | ObjcPropertyDecl *PDecl = OID->getPropertyDecl()[i]; |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 187 | fprintf(FP, "@property"); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 188 | if (PDecl->getPropertyAttributes() != ObjcPropertyDecl::OBJC_PR_noattr) { |
| 189 | bool first = true; |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 190 | fprintf(FP, " ("); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 191 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readonly) |
| 192 | { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 193 | fprintf(FP, "%creadonly", first ? ' ' : ','); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 194 | first = false; |
| 195 | } |
| 196 | |
| 197 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_getter) |
| 198 | { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 199 | fprintf(FP, "%cgetter = %s", first ? ' ' : ',' |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 200 | , PDecl->getGetterName()->getName()); |
| 201 | first = false; |
| 202 | } |
| 203 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_setter) |
| 204 | { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 205 | fprintf(FP, "%csetter = %s:", first ? ' ' : ',' |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 206 | , PDecl->getSetterName()->getName()); |
| 207 | first = false; |
| 208 | } |
| 209 | |
| 210 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_assign) |
| 211 | { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 212 | fprintf(FP, "%cassign", first ? ' ' : ','); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 213 | first = false; |
| 214 | } |
| 215 | |
| 216 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readwrite) |
| 217 | { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 218 | fprintf(FP, "%creadwrite", first ? ' ' : ','); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 219 | first = false; |
| 220 | } |
| 221 | |
| 222 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_retain) |
| 223 | { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 224 | fprintf(FP, "%cretain", first ? ' ' : ','); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 225 | first = false; |
| 226 | } |
| 227 | |
| 228 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_copy) |
| 229 | { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 230 | fprintf(FP, "%ccopy", first ? ' ' : ','); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 231 | first = false; |
| 232 | } |
| 233 | |
| 234 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_nonatomic) |
| 235 | { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 236 | fprintf(FP, "%cnonatomic", first ? ' ' : ','); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 237 | first = false; |
| 238 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 239 | fprintf(FP, " )"); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 240 | } |
| 241 | ObjcIvarDecl **IDecl = PDecl->getPropertyDecls(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 242 | fprintf(FP, " %s %s", IDecl[0]->getType().getAsString().c_str(), |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 243 | IDecl[0]->getName()); |
| 244 | |
| 245 | for (int j = 1; j < PDecl->getNumPropertyDecls(); j++) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 246 | fprintf(FP, ", %s", IDecl[j]->getName()); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 247 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 248 | fprintf(FP, ";\n"); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 251 | fprintf(FP,"@end\n"); |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 252 | // FIXME: implement the rest... |
| 253 | } |
| 254 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 255 | void DeclPrinter::PrintObjcProtocolDecl(ObjcProtocolDecl *PID) { |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 256 | std::string S = PID->getName(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 257 | fprintf(FP, "@protocol %s;\n", S.c_str()); |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 258 | // FIXME: implement the rest... |
| 259 | } |
| 260 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 261 | void DeclPrinter::PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID) { |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 262 | std::string S = PID->getName(); |
| 263 | std::string I = PID->getClassInterface()->getName(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 264 | fprintf(FP, "@implementation %s(%s);\n", I.c_str(), S.c_str()); |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 265 | // FIXME: implement the rest... |
| 266 | } |
| 267 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 268 | void DeclPrinter::PrintObjcCategoryDecl(ObjcCategoryDecl *PID) { |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 269 | std::string S = PID->getName(); |
| 270 | std::string I = PID->getClassInterface()->getName(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 271 | fprintf(FP, "@interface %s(%s);\n", I.c_str(), S.c_str()); |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 272 | // FIXME: implement the rest... |
| 273 | } |
| 274 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 275 | void DeclPrinter::PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) { |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 276 | std::string A = AID->getName(); |
| 277 | std::string I = AID->getClassInterface()->getName(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 278 | fprintf(FP, "@compatibility_alias %s %s;\n", A.c_str(), I.c_str()); |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 281 | //===----------------------------------------------------------------------===// |
| 282 | /// ASTPrinter - Pretty-printer of ASTs |
| 283 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 284 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 285 | class ASTPrinter : public ASTConsumer, public DeclPrinter { |
| 286 | public: |
| 287 | ASTPrinter(FILE* F = NULL) : DeclPrinter(F) {} |
| 288 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 289 | virtual void HandleTopLevelDecl(Decl *D) { |
| 290 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 291 | PrintFunctionDeclStart(FD); |
| 292 | |
| 293 | if (FD->getBody()) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 294 | fprintf(FP, " "); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 295 | FD->getBody()->dumpPretty(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 296 | fprintf(FP, "\n"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 297 | } |
Steve Naroff | aaa3cf8 | 2007-11-13 23:48:03 +0000 | [diff] [blame] | 298 | } else if (isa<ObjcMethodDecl>(D)) { |
| 299 | // Do nothing, methods definitions are printed in |
| 300 | // PrintObjcImplementationDecl. |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 301 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 302 | PrintTypeDefDecl(TD); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 303 | } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) { |
| 304 | PrintObjcInterfaceDecl(OID); |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 305 | } else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) { |
| 306 | PrintObjcProtocolDecl(PID); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 307 | } else if (ObjcForwardProtocolDecl *OFPD = |
| 308 | dyn_cast<ObjcForwardProtocolDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 309 | fprintf(FP, "@protocol "); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 310 | for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) { |
| 311 | const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 312 | if (i) fprintf(FP, ", "); |
| 313 | fprintf(FP, "%s", D->getName()); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 314 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 315 | fprintf(FP, ";\n"); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 316 | } else if (ObjcImplementationDecl *OID = |
| 317 | dyn_cast<ObjcImplementationDecl>(D)) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 318 | PrintObjcImplementationDecl(OID); |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 319 | } else if (ObjcCategoryImplDecl *OID = |
| 320 | dyn_cast<ObjcCategoryImplDecl>(D)) { |
| 321 | PrintObjcCategoryImplDecl(OID); |
| 322 | } else if (ObjcCategoryDecl *OID = |
| 323 | dyn_cast<ObjcCategoryDecl>(D)) { |
| 324 | PrintObjcCategoryDecl(OID); |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 325 | } else if (ObjcCompatibleAliasDecl *OID = |
| 326 | dyn_cast<ObjcCompatibleAliasDecl>(D)) { |
| 327 | PrintObjcCompatibleAliasDecl(OID); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 328 | } else if (isa<ObjcClassDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 329 | fprintf(FP, "@class [printing todo]\n"); |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 330 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 331 | fprintf(FP, "Read top-level variable decl: '%s'\n", SD->getName()); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 332 | } else { |
| 333 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 334 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 336 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 338 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 339 | ASTConsumer *clang::CreateASTPrinter(FILE* fp) { return new ASTPrinter(fp); } |
| 340 | |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | /// ASTDumper - Low-level dumper of ASTs |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 343 | |
| 344 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 345 | class ASTDumper : public ASTConsumer, public DeclPrinter { |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 346 | SourceManager *SM; |
| 347 | public: |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 348 | ASTDumper(FILE* fp = NULL) : DeclPrinter(fp) {} |
| 349 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 350 | void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 351 | SM = &Context.SourceMgr; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 352 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 353 | |
| 354 | virtual void HandleTopLevelDecl(Decl *D) { |
| 355 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 356 | PrintFunctionDeclStart(FD); |
| 357 | |
| 358 | if (FD->getBody()) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 359 | fprintf(FP, "\n"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 360 | FD->getBody()->dumpAll(*SM); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 361 | fprintf(FP, "\n"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 362 | } |
| 363 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 364 | PrintTypeDefDecl(TD); |
| 365 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 366 | fprintf(FP, "Read top-level variable decl: '%s'\n", SD->getName()); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 367 | } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 368 | fprintf(FP, "Read objc interface '%s'\n", OID->getName()); |
Steve Naroff | 8de2826 | 2007-10-14 17:03:01 +0000 | [diff] [blame] | 369 | } else if (ObjcProtocolDecl *OPD = dyn_cast<ObjcProtocolDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 370 | fprintf(FP, "Read objc protocol '%s'\n", OPD->getName()); |
Steve Naroff | 8de2826 | 2007-10-14 17:03:01 +0000 | [diff] [blame] | 371 | } else if (ObjcCategoryDecl *OCD = dyn_cast<ObjcCategoryDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 372 | fprintf(FP, "Read objc category '%s'\n", OCD->getName()); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 373 | } else if (isa<ObjcForwardProtocolDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 374 | fprintf(FP, "Read objc fwd protocol decl\n"); |
Steve Naroff | 8de2826 | 2007-10-14 17:03:01 +0000 | [diff] [blame] | 375 | } else if (isa<ObjcClassDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 376 | fprintf(FP, "Read objc fwd class decl\n"); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 377 | } else { |
| 378 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | }; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 384 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 385 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 386 | //===----------------------------------------------------------------------===// |
| 387 | /// ASTViewer - AST Visualization |
| 388 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 389 | namespace { |
| 390 | class ASTViewer : public ASTConsumer { |
| 391 | SourceManager *SM; |
| 392 | public: |
| 393 | void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 394 | SM = &Context.SourceMgr; |
| 395 | } |
| 396 | |
| 397 | virtual void HandleTopLevelDecl(Decl *D) { |
| 398 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 399 | DeclPrinter(stderr).PrintFunctionDeclStart(FD); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 400 | |
| 401 | if (FD->getBody()) { |
| 402 | fprintf(stderr, "\n"); |
| 403 | FD->getBody()->viewAST(); |
| 404 | fprintf(stderr, "\n"); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | }; |
| 409 | } |
| 410 | |
| 411 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 412 | |
| 413 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 414 | //===----------------------------------------------------------------------===// |
| 415 | // CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit |
| 416 | // the CFGs for all function definitions. |
| 417 | |
| 418 | namespace { |
| 419 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 420 | class CFGVisitor : public ASTConsumer { |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 421 | public: |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 422 | // CFG Visitor interface to be implemented by subclass. |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 423 | virtual void VisitCFG(CFG& C) = 0; |
| 424 | virtual bool printFuncDeclStart() { return true; } |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 425 | |
| 426 | virtual void HandleTopLevelDecl(Decl *D); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | } // end anonymous namespace |
| 430 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 431 | void CFGVisitor::HandleTopLevelDecl(Decl *D) { |
| 432 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 433 | if (!FD || !FD->getBody()) |
| 434 | return; |
| 435 | |
| 436 | if (printFuncDeclStart()) { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 437 | DeclPrinter(stderr).PrintFunctionDeclStart(FD); |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 438 | fprintf(stderr,"\n"); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 439 | } |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 440 | |
Ted Kremenek | 1225966 | 2007-09-17 17:10:02 +0000 | [diff] [blame] | 441 | CFG *C = CFG::buildCFG(FD->getBody()); |
| 442 | VisitCFG(*C); |
| 443 | delete C; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | //===----------------------------------------------------------------------===// |
| 447 | // DumpCFGs - Dump CFGs to stderr or visualize with Graphviz |
| 448 | |
| 449 | namespace { |
| 450 | class CFGDumper : public CFGVisitor { |
| 451 | const bool UseGraphviz; |
| 452 | public: |
| 453 | CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {} |
| 454 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 455 | virtual void VisitCFG(CFG &C) { |
| 456 | if (UseGraphviz) |
| 457 | C.viewCFG(); |
| 458 | else |
| 459 | C.dump(); |
| 460 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 461 | }; |
| 462 | } // end anonymous namespace |
| 463 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 464 | ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) { |
| 465 | return new CFGDumper(ViewGraphs); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 468 | //===----------------------------------------------------------------------===// |
| 469 | // AnalyzeLiveVariables - perform live variable analysis and dump results |
| 470 | |
| 471 | namespace { |
| 472 | class LivenessVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 473 | SourceManager *SM; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 474 | public: |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 475 | virtual void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 476 | SM = &Context.SourceMgr; |
| 477 | } |
| 478 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 479 | virtual void VisitCFG(CFG& C) { |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 480 | LiveVariables L(C); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 481 | L.runOnCFG(C); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 482 | L.dumpBlockLiveness(*SM); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 483 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 484 | }; |
| 485 | } // end anonymous namespace |
| 486 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 487 | ASTConsumer *clang::CreateLiveVarAnalyzer() { |
| 488 | return new LivenessVisitor(); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 491 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 492 | // DeadStores - run checker to locate dead stores in a function |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 493 | |
| 494 | namespace { |
| 495 | class DeadStoreVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 496 | Diagnostic &Diags; |
| 497 | ASTContext *Ctx; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 498 | public: |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 499 | DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {} |
| 500 | virtual void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 501 | Ctx = &Context; |
| 502 | } |
| 503 | |
| 504 | virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); } |
Ted Kremenek | 567a7e6 | 2007-09-07 23:54:15 +0000 | [diff] [blame] | 505 | virtual bool printFuncDeclStart() { return false; } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 506 | }; |
| 507 | } // end anonymous namespace |
| 508 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 509 | ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) { |
| 510 | return new DeadStoreVisitor(Diags); |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 511 | } |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 512 | |
| 513 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 514 | // Unitialized Values - run checker to flag potential uses of uninitalized |
| 515 | // variables. |
| 516 | |
| 517 | namespace { |
| 518 | class UninitValsVisitor : public CFGVisitor { |
| 519 | Diagnostic &Diags; |
| 520 | ASTContext *Ctx; |
| 521 | public: |
| 522 | UninitValsVisitor(Diagnostic &diags) : Diags(diags) {} |
| 523 | virtual void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 524 | Ctx = &Context; |
| 525 | } |
| 526 | |
| 527 | virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); } |
| 528 | virtual bool printFuncDeclStart() { return false; } |
| 529 | }; |
| 530 | } // end anonymous namespace |
| 531 | |
| 532 | ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) { |
| 533 | return new UninitValsVisitor(Diags); |
| 534 | } |
| 535 | |
| 536 | //===----------------------------------------------------------------------===// |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 537 | // LLVM Emitter |
| 538 | |
| 539 | #include "clang/Basic/Diagnostic.h" |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 540 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 541 | #include "clang/CodeGen/ModuleBuilder.h" |
| 542 | #include "llvm/Module.h" |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 543 | #include "llvm/Target/TargetData.h" |
| 544 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 545 | #include <iostream> |
| 546 | |
| 547 | namespace { |
| 548 | class LLVMEmitter : public ASTConsumer { |
| 549 | Diagnostic &Diags; |
| 550 | llvm::Module *M; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 551 | const llvm::TargetData *TD; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 552 | ASTContext *Ctx; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame^] | 553 | const LangOptions &Features; |
Chris Lattner | a36c486 | 2007-11-13 18:16:41 +0000 | [diff] [blame] | 554 | CodeGen::CodeGenModule *Builder; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 555 | public: |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame^] | 556 | LLVMEmitter(Diagnostic &diags, const LangOptions &LO) |
| 557 | : Diags(diags) |
| 558 | , Features(LO) {} |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 559 | virtual void Initialize(ASTContext &Context, unsigned MainFileID) { |
| 560 | Ctx = &Context; |
| 561 | M = new llvm::Module("foo"); |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 562 | M->setTargetTriple(Ctx->Target.getTargetTriple()); |
| 563 | TD = new llvm::TargetData(Ctx->Target.getTargetDescription()); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame^] | 564 | Builder = CodeGen::Init(Context, Features, *M, *TD); |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | virtual void HandleTopLevelDecl(Decl *D) { |
| 568 | // If an error occurred, stop code generation, but continue parsing and |
| 569 | // semantic analysis (to ensure all warnings and errors are emitted). |
| 570 | if (Diags.hasErrorOccurred()) |
| 571 | return; |
| 572 | |
| 573 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 574 | CodeGen::CodeGenFunction(Builder, FD); |
| 575 | } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) { |
| 576 | CodeGen::CodeGenGlobalVar(Builder, FVD); |
| 577 | } else { |
Steve Naroff | 91578f3 | 2007-11-17 21:21:01 +0000 | [diff] [blame] | 578 | assert(isa<TypeDecl>(D) && "Only expected type decls here"); |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 579 | // don't codegen for now, eventually pass down for debug info. |
| 580 | //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n"; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | ~LLVMEmitter() { |
| 585 | CodeGen::Terminate(Builder); |
| 586 | |
| 587 | // Print the generated code. |
| 588 | M->print(std::cout); |
| 589 | delete M; |
| 590 | } |
| 591 | }; |
| 592 | } // end anonymous namespace |
| 593 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame^] | 594 | ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags, const LangOptions &Features) { |
| 595 | return new LLVMEmitter(Diags, Features); |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 596 | } |
| 597 | |