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