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" |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/AST.h" |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 20 | #include "clang/AST/CFG.h" |
Ted Kremenek | cf6e41b | 2007-12-21 21:42:19 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Streams.h" |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 24 | #include <fstream> |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 26 | using namespace clang; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | /// DeclPrinter - Utility class for printing top-level decls. |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | class DeclPrinter { |
| 33 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 34 | std::ostream& Out; |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 36 | DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {} |
| 37 | DeclPrinter() : Out(*llvm::cerr.stream()) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 38 | |
| 39 | void PrintFunctionDeclStart(FunctionDecl *FD); |
| 40 | void PrintTypeDefDecl(TypedefDecl *TD); |
| 41 | void PrintObjcMethodDecl(ObjcMethodDecl *OMD); |
| 42 | void PrintObjcImplementationDecl(ObjcImplementationDecl *OID); |
| 43 | void PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID); |
| 44 | void PrintObjcProtocolDecl(ObjcProtocolDecl *PID); |
| 45 | void PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID); |
| 46 | void PrintObjcCategoryDecl(ObjcCategoryDecl *PID); |
| 47 | void PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID); |
| 48 | }; |
| 49 | } // end anonymous namespace |
| 50 | |
| 51 | void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | bool HasBody = FD->getBody(); |
| 53 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 54 | Out << '\n'; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 55 | |
| 56 | switch (FD->getStorageClass()) { |
| 57 | default: assert(0 && "Unknown storage class"); |
| 58 | case FunctionDecl::None: break; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 59 | case FunctionDecl::Extern: Out << "extern "; break; |
| 60 | case FunctionDecl::Static: Out << "static "; break; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (FD->isInline()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 64 | Out << "inline "; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 65 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | std::string Proto = FD->getName(); |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 67 | const FunctionType *AFT = FD->getType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 69 | if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | Proto += "("; |
| 71 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 72 | if (i) Proto += ", "; |
| 73 | std::string ParamStr; |
| 74 | if (HasBody) ParamStr = FD->getParamDecl(i)->getName(); |
| 75 | |
| 76 | FT->getArgType(i).getAsStringInternal(ParamStr); |
| 77 | Proto += ParamStr; |
| 78 | } |
| 79 | |
| 80 | if (FT->isVariadic()) { |
| 81 | if (FD->getNumParams()) Proto += ", "; |
| 82 | Proto += "..."; |
| 83 | } |
| 84 | Proto += ")"; |
| 85 | } else { |
| 86 | assert(isa<FunctionTypeNoProto>(AFT)); |
| 87 | Proto += "()"; |
| 88 | } |
| 89 | |
| 90 | AFT->getResultType().getAsStringInternal(Proto); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 91 | Out << Proto; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 93 | if (!FD->getBody()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 94 | Out << ";\n"; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 95 | // Doesn't print the body. |
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::PrintTypeDefDecl(TypedefDecl *TD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | std::string S = TD->getName(); |
| 100 | TD->getUnderlyingType().getAsStringInternal(S); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 101 | Out << "typedef " << S << ";\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 104 | void DeclPrinter::PrintObjcMethodDecl(ObjcMethodDecl *OMD) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 105 | if (OMD->isInstance()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 106 | Out << "\n- "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 107 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 108 | Out << "\n+ "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 109 | if (!OMD->getResultType().isNull()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 110 | Out << '(' << OMD->getResultType().getAsString() << ") "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 111 | // FIXME: just print original selector name! |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 112 | Out << OMD->getSelector().getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 113 | |
| 114 | for (int i = 0; i < OMD->getNumParams(); i++) { |
| 115 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 116 | // FIXME: selector is missing here! |
| 117 | Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 121 | void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 122 | std::string I = OID->getName(); |
| 123 | ObjcInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 124 | |
| 125 | if (SID) |
| 126 | Out << "@implementation " << I << " : " << SID->getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 127 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 128 | Out << "@implementation " << I; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 129 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 130 | for (ObjcImplementationDecl::instmeth_iterator I = OID->instmeth_begin(), |
| 131 | E = OID->instmeth_end(); I != E; ++I) { |
| 132 | ObjcMethodDecl *OMD = *I; |
| 133 | PrintObjcMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 134 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 135 | Out << ' '; |
| 136 | OMD->getBody()->printPretty(Out); |
| 137 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 141 | for (ObjcImplementationDecl::classmeth_iterator I = OID->classmeth_begin(), |
| 142 | E = OID->classmeth_end(); I != E; ++I) { |
| 143 | ObjcMethodDecl *OMD = *I; |
| 144 | PrintObjcMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 145 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 146 | Out << ' '; |
| 147 | OMD->getBody()->printPretty(Out); |
| 148 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 152 | Out << "@end\n"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 156 | void DeclPrinter::PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) { |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 157 | std::string I = OID->getName(); |
| 158 | ObjcInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 159 | |
| 160 | if (SID) |
| 161 | Out << "@interface " << I << " : " << SID->getName(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 162 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 163 | Out << "@interface " << I; |
| 164 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 165 | // Protocols? |
| 166 | int count = OID->getNumIntfRefProtocols(); |
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) { |
| 169 | ObjcProtocolDecl **refProtocols = OID->getReferencedProtocols(); |
| 170 | for (int i = 0; i < count; i++) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 171 | Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 172 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 173 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 174 | if (count > 0) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 175 | Out << ">\n"; |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 176 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 177 | Out << '\n'; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 179 | if (OID->getNumInstanceVariables() > 0) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 180 | Out << '{'; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 181 | for (ObjcInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 182 | E = OID->ivar_end(); I != E; ++I) { |
| 183 | Out << '\t' << (*I)->getType().getAsString() |
| 184 | << ' ' << (*I)->getName() << ";\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 185 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 186 | Out << "}\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 187 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 188 | |
| 189 | int NumProperties = OID->getNumPropertyDecl(); |
| 190 | if (NumProperties > 0) { |
| 191 | for (int i = 0; i < NumProperties; i++) { |
| 192 | ObjcPropertyDecl *PDecl = OID->getPropertyDecl()[i]; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 193 | Out << "@property"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 194 | if (PDecl->getPropertyAttributes() != ObjcPropertyDecl::OBJC_PR_noattr) { |
| 195 | bool first = true; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 196 | Out << " ("; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 197 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readonly) |
| 198 | { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 199 | Out << (first ? ' ' : ',') << "readonly"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 200 | first = false; |
| 201 | } |
| 202 | |
| 203 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_getter) |
| 204 | { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 205 | Out << (first ? ' ' : ',') << "getter = " |
| 206 | << PDecl->getGetterName()->getName(); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 207 | first = false; |
| 208 | } |
| 209 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_setter) |
| 210 | { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 211 | Out << (first ? ' ' : ',') << "setter = " |
| 212 | << PDecl->getSetterName()->getName(); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 213 | first = false; |
| 214 | } |
| 215 | |
| 216 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_assign) |
| 217 | { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 218 | Out << (first ? ' ' : ',') << "assign"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 219 | first = false; |
| 220 | } |
| 221 | |
| 222 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readwrite) |
| 223 | { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 224 | Out << (first ? ' ' : ',') << "readwrite"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 225 | first = false; |
| 226 | } |
| 227 | |
| 228 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_retain) |
| 229 | { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 230 | Out << (first ? ' ' : ',') << "retain"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 231 | first = false; |
| 232 | } |
| 233 | |
| 234 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_copy) |
| 235 | { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 236 | Out << (first ? ' ' : ',') << "copy"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 237 | first = false; |
| 238 | } |
| 239 | |
| 240 | if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_nonatomic) |
| 241 | { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 242 | Out << (first ? ' ' : ',') << "nonatomic"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 243 | first = false; |
| 244 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 245 | Out << " )"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 246 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 247 | |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 248 | ObjcIvarDecl **IDecl = PDecl->getPropertyDecls(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 249 | |
| 250 | Out << ' ' << IDecl[0]->getType().getAsString() |
| 251 | << ' ' << IDecl[0]->getName(); |
| 252 | |
| 253 | for (int j = 1; j < PDecl->getNumPropertyDecls(); j++) |
| 254 | Out << ", " << IDecl[j]->getName(); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 256 | Out << ";\n"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 259 | |
| 260 | Out << "@end\n"; |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 261 | // FIXME: implement the rest... |
| 262 | } |
| 263 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 264 | void DeclPrinter::PrintObjcProtocolDecl(ObjcProtocolDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 265 | Out << "@protocol " << PID->getName() << '\n'; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 266 | // FIXME: implement the rest... |
| 267 | } |
| 268 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 269 | void DeclPrinter::PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 270 | Out << "@implementation " |
| 271 | << PID->getClassInterface()->getName() |
| 272 | << '(' << PID->getName() << ");\n"; |
| 273 | |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 274 | // FIXME: implement the rest... |
| 275 | } |
| 276 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 277 | void DeclPrinter::PrintObjcCategoryDecl(ObjcCategoryDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 278 | Out << "@interface " |
| 279 | << PID->getClassInterface()->getName() |
| 280 | << '(' << PID->getName() << ");\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 281 | // FIXME: implement the rest... |
| 282 | } |
| 283 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 284 | void DeclPrinter::PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 285 | Out << "@compatibility_alias " << AID->getName() |
| 286 | << ' ' << AID->getClassInterface()->getName() << ";\n"; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 289 | //===----------------------------------------------------------------------===// |
| 290 | /// ASTPrinter - Pretty-printer of ASTs |
| 291 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 292 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 293 | class ASTPrinter : public ASTConsumer, public DeclPrinter { |
| 294 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 295 | ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 297 | virtual void HandleTopLevelDecl(Decl *D) { |
| 298 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 299 | PrintFunctionDeclStart(FD); |
| 300 | |
| 301 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 302 | Out << ' '; |
| 303 | FD->getBody()->printPretty(Out); |
| 304 | Out << '\n'; |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 305 | } |
Steve Naroff | aaa3cf8 | 2007-11-13 23:48:03 +0000 | [diff] [blame] | 306 | } else if (isa<ObjcMethodDecl>(D)) { |
| 307 | // Do nothing, methods definitions are printed in |
| 308 | // PrintObjcImplementationDecl. |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 309 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 310 | PrintTypeDefDecl(TD); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 311 | } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) { |
| 312 | PrintObjcInterfaceDecl(OID); |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 313 | } else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) { |
| 314 | PrintObjcProtocolDecl(PID); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 315 | } else if (ObjcForwardProtocolDecl *OFPD = |
| 316 | dyn_cast<ObjcForwardProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 317 | Out << "@protocol "; |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 318 | for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) { |
| 319 | const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 320 | if (i) Out << ", "; |
| 321 | Out << D->getName(); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 322 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 323 | Out << ";\n"; |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 324 | } else if (ObjcImplementationDecl *OID = |
| 325 | dyn_cast<ObjcImplementationDecl>(D)) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 326 | PrintObjcImplementationDecl(OID); |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 327 | } else if (ObjcCategoryImplDecl *OID = |
| 328 | dyn_cast<ObjcCategoryImplDecl>(D)) { |
| 329 | PrintObjcCategoryImplDecl(OID); |
| 330 | } else if (ObjcCategoryDecl *OID = |
| 331 | dyn_cast<ObjcCategoryDecl>(D)) { |
| 332 | PrintObjcCategoryDecl(OID); |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 333 | } else if (ObjcCompatibleAliasDecl *OID = |
| 334 | dyn_cast<ObjcCompatibleAliasDecl>(D)) { |
| 335 | PrintObjcCompatibleAliasDecl(OID); |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 336 | } else if (isa<ObjcClassDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 337 | Out << "@class [printing todo]\n"; |
Steve Naroff | 1f64432 | 2007-11-28 22:54:11 +0000 | [diff] [blame] | 338 | } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 339 | Out << "Read top-level tag decl: '" << TD->getName() << "'\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 340 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 341 | Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 342 | } else { |
| 343 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 344 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 346 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 349 | ASTConsumer *clang::CreateASTPrinter(std::ostream* out) { |
| 350 | return new ASTPrinter(out); |
| 351 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 352 | |
| 353 | //===----------------------------------------------------------------------===// |
| 354 | /// ASTDumper - Low-level dumper of ASTs |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 355 | |
| 356 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 357 | class ASTDumper : public ASTConsumer, public DeclPrinter { |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 358 | SourceManager *SM; |
| 359 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 360 | ASTDumper() : DeclPrinter() {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 361 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 362 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 363 | SM = &Context.getSourceManager(); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 364 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 365 | |
| 366 | virtual void HandleTopLevelDecl(Decl *D) { |
| 367 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 368 | PrintFunctionDeclStart(FD); |
| 369 | |
| 370 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 371 | Out << '\n'; |
| 372 | // FIXME: convert dumper to use std::ostream? |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 373 | FD->getBody()->dumpAll(*SM); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 374 | Out << '\n'; |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 375 | } |
| 376 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 377 | PrintTypeDefDecl(TD); |
| 378 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 379 | Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 380 | } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 381 | Out << "Read objc interface '" << OID->getName() << "'\n"; |
Steve Naroff | 8de2826 | 2007-10-14 17:03:01 +0000 | [diff] [blame] | 382 | } else if (ObjcProtocolDecl *OPD = dyn_cast<ObjcProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 383 | Out << "Read objc protocol '" << OPD->getName() << "'\n"; |
Steve Naroff | 8de2826 | 2007-10-14 17:03:01 +0000 | [diff] [blame] | 384 | } else if (ObjcCategoryDecl *OCD = dyn_cast<ObjcCategoryDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 385 | Out << "Read objc category '" << OCD->getName() << "'\n"; |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 386 | } else if (isa<ObjcForwardProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 387 | Out << "Read objc fwd protocol decl\n"; |
Steve Naroff | 8de2826 | 2007-10-14 17:03:01 +0000 | [diff] [blame] | 388 | } else if (isa<ObjcClassDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 389 | Out << "Read objc fwd class decl\n"; |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 390 | } else { |
| 391 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | }; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 397 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 398 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
| 400 | /// ASTViewer - AST Visualization |
| 401 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 402 | namespace { |
| 403 | class ASTViewer : public ASTConsumer { |
| 404 | SourceManager *SM; |
| 405 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 406 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 407 | SM = &Context.getSourceManager(); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | virtual void HandleTopLevelDecl(Decl *D) { |
| 411 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 412 | DeclPrinter().PrintFunctionDeclStart(FD); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 413 | |
| 414 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 415 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 416 | FD->getBody()->viewAST(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 417 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
| 421 | }; |
| 422 | } |
| 423 | |
| 424 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 425 | |
| 426 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 427 | //===----------------------------------------------------------------------===// |
| 428 | // CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit |
| 429 | // the CFGs for all function definitions. |
| 430 | |
| 431 | namespace { |
| 432 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 433 | class CFGVisitor : public ASTConsumer { |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 434 | public: |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 435 | // CFG Visitor interface to be implemented by subclass. |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 436 | virtual void VisitCFG(CFG& C) = 0; |
| 437 | virtual bool printFuncDeclStart() { return true; } |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 438 | |
| 439 | virtual void HandleTopLevelDecl(Decl *D); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
| 442 | } // end anonymous namespace |
| 443 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 444 | void CFGVisitor::HandleTopLevelDecl(Decl *D) { |
| 445 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 446 | if (!FD || !FD->getBody()) |
| 447 | return; |
| 448 | |
| 449 | if (printFuncDeclStart()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 450 | DeclPrinter().PrintFunctionDeclStart(FD); |
| 451 | llvm::cerr << '\n'; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 452 | } |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 453 | |
Ted Kremenek | 1225966 | 2007-09-17 17:10:02 +0000 | [diff] [blame] | 454 | CFG *C = CFG::buildCFG(FD->getBody()); |
| 455 | VisitCFG(*C); |
| 456 | delete C; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | //===----------------------------------------------------------------------===// |
| 460 | // DumpCFGs - Dump CFGs to stderr or visualize with Graphviz |
| 461 | |
| 462 | namespace { |
| 463 | class CFGDumper : public CFGVisitor { |
| 464 | const bool UseGraphviz; |
| 465 | public: |
| 466 | CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {} |
| 467 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 468 | virtual void VisitCFG(CFG &C) { |
| 469 | if (UseGraphviz) |
| 470 | C.viewCFG(); |
| 471 | else |
| 472 | C.dump(); |
| 473 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 474 | }; |
| 475 | } // end anonymous namespace |
| 476 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 477 | ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) { |
| 478 | return new CFGDumper(ViewGraphs); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 481 | //===----------------------------------------------------------------------===// |
| 482 | // AnalyzeLiveVariables - perform live variable analysis and dump results |
| 483 | |
| 484 | namespace { |
| 485 | class LivenessVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 486 | SourceManager *SM; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 487 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 488 | virtual void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 489 | SM = &Context.getSourceManager(); |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 492 | virtual void VisitCFG(CFG& C) { |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 493 | LiveVariables L(C); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 494 | L.runOnCFG(C); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 495 | L.dumpBlockLiveness(*SM); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 496 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 497 | }; |
| 498 | } // end anonymous namespace |
| 499 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 500 | ASTConsumer *clang::CreateLiveVarAnalyzer() { |
| 501 | return new LivenessVisitor(); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 504 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 505 | // DeadStores - run checker to locate dead stores in a function |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 506 | |
| 507 | namespace { |
| 508 | class DeadStoreVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 509 | Diagnostic &Diags; |
| 510 | ASTContext *Ctx; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 511 | public: |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 512 | DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {} |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 513 | virtual void Initialize(ASTContext &Context) { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 514 | Ctx = &Context; |
| 515 | } |
| 516 | |
| 517 | virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); } |
Ted Kremenek | 567a7e6 | 2007-09-07 23:54:15 +0000 | [diff] [blame] | 518 | virtual bool printFuncDeclStart() { return false; } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 519 | }; |
| 520 | } // end anonymous namespace |
| 521 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 522 | ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) { |
| 523 | return new DeadStoreVisitor(Diags); |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 524 | } |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 525 | |
| 526 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 527 | // Unitialized Values - run checker to flag potential uses of uninitalized |
| 528 | // variables. |
| 529 | |
| 530 | namespace { |
| 531 | class UninitValsVisitor : public CFGVisitor { |
| 532 | Diagnostic &Diags; |
| 533 | ASTContext *Ctx; |
| 534 | public: |
| 535 | UninitValsVisitor(Diagnostic &diags) : Diags(diags) {} |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 536 | virtual void Initialize(ASTContext &Context) { |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 537 | Ctx = &Context; |
| 538 | } |
| 539 | |
| 540 | virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); } |
| 541 | virtual bool printFuncDeclStart() { return false; } |
| 542 | }; |
| 543 | } // end anonymous namespace |
| 544 | |
| 545 | ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) { |
| 546 | return new UninitValsVisitor(Diags); |
| 547 | } |
| 548 | |
| 549 | //===----------------------------------------------------------------------===// |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 550 | // LLVM Emitter |
| 551 | |
| 552 | #include "clang/Basic/Diagnostic.h" |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 553 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 554 | #include "clang/CodeGen/ModuleBuilder.h" |
| 555 | #include "llvm/Module.h" |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 556 | #include "llvm/Target/TargetData.h" |
| 557 | #include "llvm/Target/TargetMachine.h" |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 558 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 559 | |
| 560 | namespace { |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 561 | class CodeGenerator : public ASTConsumer { |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 562 | Diagnostic &Diags; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 563 | const llvm::TargetData *TD; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 564 | ASTContext *Ctx; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 565 | const LangOptions &Features; |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 566 | protected: |
| 567 | llvm::Module *M; |
Chris Lattner | a36c486 | 2007-11-13 18:16:41 +0000 | [diff] [blame] | 568 | CodeGen::CodeGenModule *Builder; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 569 | public: |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 570 | CodeGenerator(Diagnostic &diags, const LangOptions &LO) |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 571 | : Diags(diags) |
| 572 | , Features(LO) {} |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 573 | virtual void Initialize(ASTContext &Context) { |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 574 | Ctx = &Context; |
| 575 | M = new llvm::Module("foo"); |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 576 | M->setTargetTriple(Ctx->Target.getTargetTriple()); |
Chris Lattner | 0404cd8 | 2007-12-13 17:34:31 +0000 | [diff] [blame] | 577 | M->setDataLayout(Ctx->Target.getTargetDescription()); |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 578 | TD = new llvm::TargetData(Ctx->Target.getTargetDescription()); |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 579 | Builder = CodeGen::Init(Context, Features, *M, *TD, Diags); |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | virtual void HandleTopLevelDecl(Decl *D) { |
| 583 | // If an error occurred, stop code generation, but continue parsing and |
| 584 | // semantic analysis (to ensure all warnings and errors are emitted). |
| 585 | if (Diags.hasErrorOccurred()) |
| 586 | return; |
| 587 | |
| 588 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 589 | CodeGen::CodeGenFunction(Builder, FD); |
| 590 | } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) { |
| 591 | CodeGen::CodeGenGlobalVar(Builder, FVD); |
| 592 | } else { |
Steve Naroff | 91578f3 | 2007-11-17 21:21:01 +0000 | [diff] [blame] | 593 | assert(isa<TypeDecl>(D) && "Only expected type decls here"); |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 594 | // don't codegen for now, eventually pass down for debug info. |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 595 | //std::cerr << "Read top-level typedef decl: '" |
| 596 | // << D->getName() << "'\n"; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 599 | }; |
| 600 | } |
| 601 | |
| 602 | namespace { |
| 603 | class LLVMEmitter : public CodeGenerator { |
| 604 | public: |
| 605 | LLVMEmitter(Diagnostic &diags, const LangOptions &LO) |
| 606 | : CodeGenerator(diags,LO) {} |
| 607 | |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 608 | ~LLVMEmitter() { |
| 609 | CodeGen::Terminate(Builder); |
| 610 | |
| 611 | // Print the generated code. |
Ted Kremenek | 3821d40 | 2007-12-13 17:50:11 +0000 | [diff] [blame] | 612 | M->print(llvm::cout.stream()); |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 613 | delete M; |
| 614 | } |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 615 | }; |
| 616 | } |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 617 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 618 | ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags, |
| 619 | const LangOptions &Features) { |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 620 | return new LLVMEmitter(Diags, Features); |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 623 | namespace { |
| 624 | class BCWriter : public CodeGenerator { |
| 625 | public: |
| 626 | std::ostream& Out; |
| 627 | |
| 628 | BCWriter(std::ostream* out, Diagnostic &diags, const LangOptions &LO) |
| 629 | : CodeGenerator(diags,LO) |
| 630 | , Out(*out) {} |
| 631 | |
| 632 | ~BCWriter() { |
| 633 | CodeGen::Terminate(Builder); |
| 634 | llvm::WriteBitcodeToFile(M, Out); |
| 635 | delete M; |
| 636 | } |
| 637 | }; |
| 638 | } |
| 639 | |
| 640 | ASTConsumer *clang::CreateBCWriter(const std::string& InFile, |
| 641 | const std::string& OutputFile, |
| 642 | Diagnostic &Diags, |
| 643 | const LangOptions &Features) { |
| 644 | std::string FileName = OutputFile; |
Christopher Lamb | 2d6c065 | 2007-12-24 03:23:55 +0000 | [diff] [blame] | 645 | |
| 646 | std::ostream *Out; |
Christopher Lamb | 8bd848f | 2007-12-24 20:56:07 +0000 | [diff] [blame^] | 647 | if (OutputFile == "-" || InFile == "-") |
| 648 | Out = llvm::cout.stream(); |
| 649 | else if (!OutputFile.size()) { |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 650 | llvm::sys::Path Path(InFile); |
| 651 | Path.eraseSuffix(); |
| 652 | Path.appendSuffix("bc"); |
| 653 | FileName = Path.toString(); |
Christopher Lamb | 2d6c065 | 2007-12-24 03:23:55 +0000 | [diff] [blame] | 654 | Out = new std::ofstream(FileName.c_str()); |
Christopher Lamb | 2d6c065 | 2007-12-24 03:23:55 +0000 | [diff] [blame] | 655 | } else { |
| 656 | Out = new std::ofstream(FileName.c_str()); |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 659 | return new BCWriter(Out, Diags, Features); |
| 660 | } |
| 661 | |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 662 | //===----------------------------------------------------------------------===// |
| 663 | // AST Serializer |
| 664 | |
| 665 | namespace { |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 666 | |
| 667 | class ASTSerializer : public ASTConsumer { |
| 668 | protected: |
| 669 | Diagnostic &Diags; |
| 670 | TranslationUnit TU; |
| 671 | public: |
| 672 | ASTSerializer(Diagnostic& diags, const LangOptions& LO) |
| 673 | : Diags(diags), TU(LO) {} |
| 674 | |
| 675 | virtual void Initialize(ASTContext &Context) { |
| 676 | TU.setContext(&Context); |
| 677 | } |
| 678 | |
| 679 | virtual void HandleTopLevelDecl(Decl *D) { |
| 680 | if (Diags.hasErrorOccurred()) |
| 681 | return; |
| 682 | |
| 683 | TU.AddTopLevelDecl(D); |
| 684 | } |
| 685 | }; |
| 686 | |
| 687 | class SingleFileSerializer : public ASTSerializer { |
| 688 | const llvm::sys::Path FName; |
| 689 | public: |
| 690 | SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags, |
| 691 | const LangOptions &LO) |
| 692 | : ASTSerializer(diags,LO), FName(F) {} |
| 693 | |
| 694 | ~SingleFileSerializer() { |
| 695 | EmitASTBitcodeFile(TU,FName); |
| 696 | } |
| 697 | }; |
| 698 | |
| 699 | class BuildSerializer : public ASTSerializer { |
| 700 | llvm::sys::Path EmitDir; |
| 701 | public: |
| 702 | BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 703 | const LangOptions &LO) |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 704 | : ASTSerializer(diags,LO), EmitDir(dir) {} |
| 705 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 706 | ~BuildSerializer() { |
| 707 | SourceManager& SourceMgr = TU.getASTContext()->getSourceManager(); |
| 708 | unsigned ID = SourceMgr.getMainFileID(); |
| 709 | assert (ID && "MainFileID not set!"); |
| 710 | const FileEntry* FE = SourceMgr.getFileEntryForID(ID); |
| 711 | assert (FE && "No FileEntry for main file."); |
| 712 | |
| 713 | // FIXME: This is not portable to Windows. |
| 714 | // FIXME: This logic should probably be moved elsewhere later. |
| 715 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 716 | llvm::sys::Path FName(EmitDir); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 717 | |
| 718 | std::vector<char> buf; |
| 719 | buf.reserve(strlen(FE->getName())+100); |
| 720 | |
| 721 | sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 722 | FName.appendComponent(&buf[0]); |
| 723 | FName.createDirectoryOnDisk(true); |
| 724 | if (!FName.canWrite() || !FName.isDirectory()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 725 | assert (false && "Could not create 'device' serialization directory."); |
| 726 | return; |
| 727 | } |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 728 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 729 | sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 730 | FName.appendComponent(&buf[0]); |
| 731 | EmitASTBitcodeFile(TU,FName); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 732 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 733 | // Now emit the sources. |
| 734 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 735 | } |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 736 | }; |
| 737 | |
| 738 | |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 739 | } // end anonymous namespace |
| 740 | |
| 741 | |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 742 | ASTConsumer* clang::CreateASTSerializer(const std::string& InFile, |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 743 | const std::string& OutputFile, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 744 | Diagnostic &Diags, |
| 745 | const LangOptions &Features) { |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 746 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 747 | if (OutputFile.size()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 748 | if (InFile == "-") { |
| 749 | llvm::cerr << |
| 750 | "error: Cannot use --serialize with -o for source read from STDIN.\n"; |
| 751 | return NULL; |
| 752 | } |
| 753 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 754 | // The user specified an AST-emission directory. Determine if the path |
| 755 | // is absolute. |
| 756 | llvm::sys::Path EmitDir(OutputFile); |
| 757 | |
| 758 | if (!EmitDir.isAbsolute()) { |
| 759 | llvm::cerr << |
| 760 | "error: Output directory for --serialize must be an absolute path.\n"; |
| 761 | |
| 762 | return NULL; |
| 763 | } |
| 764 | |
| 765 | // Create the directory if it does not exist. |
| 766 | EmitDir.createDirectoryOnDisk(true); |
| 767 | if (!EmitDir.canWrite() || !EmitDir.isDirectory()) { |
| 768 | llvm::cerr << |
| 769 | "error: Could not create output directory for --serialize.\n"; |
| 770 | |
| 771 | return NULL; |
| 772 | } |
| 773 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 774 | // FIXME: We should probably only allow using BuildSerializer when |
| 775 | // the ASTs come from parsed source files, and not from .ast files. |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 776 | return new BuildSerializer(EmitDir, Diags, Features); |
| 777 | } |
| 778 | |
| 779 | // The user did not specify an output directory for serialized ASTs. |
| 780 | // Serialize the translation to a single file whose name is the same |
| 781 | // as the input file with the ".ast" extension appended. |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 782 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 783 | llvm::sys::Path FName(InFile.c_str()); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 784 | FName.appendSuffix("ast"); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 785 | return new SingleFileSerializer(FName, Diags, Features); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 786 | } |