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