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