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 | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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" |
Zhongxing Xu | 8148839 | 2008-08-24 02:33:36 +0000 | [diff] [blame] | 15 | #include "clang/Driver/HTMLDiagnostics.h" |
Ted Kremenek | 77cda50 | 2007-12-18 21:34:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/TranslationUnit.h" |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/AST.h" |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 22 | #include "clang/CodeGen/ModuleBuilder.h" |
| 23 | #include "llvm/Module.h" |
Daniel Dunbar | d46075f | 2008-10-21 23:54:00 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Streams.h" |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Timer.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 28 | using namespace clang; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | /// DeclPrinter - Utility class for printing top-level decls. |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | class DeclPrinter { |
| 35 | public: |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 36 | llvm::raw_ostream& Out; |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 38 | DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()) {} |
| 39 | DeclPrinter() : Out(llvm::errs()) {} |
| 40 | virtual ~DeclPrinter(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 41 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 42 | void PrintDecl(Decl *D); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 43 | void PrintFunctionDeclStart(FunctionDecl *FD); |
| 44 | void PrintTypeDefDecl(TypedefDecl *TD); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 45 | void PrintLinkageSpec(LinkageSpecDecl *LS); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 46 | void PrintObjCMethodDecl(ObjCMethodDecl *OMD); |
| 47 | void PrintObjCImplementationDecl(ObjCImplementationDecl *OID); |
| 48 | void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID); |
| 49 | void PrintObjCProtocolDecl(ObjCProtocolDecl *PID); |
| 50 | void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID); |
| 51 | void PrintObjCCategoryDecl(ObjCCategoryDecl *PID); |
| 52 | void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 53 | void PrintObjCPropertyDecl(ObjCPropertyDecl *PD); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 54 | void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 55 | }; |
| 56 | } // end anonymous namespace |
| 57 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 58 | DeclPrinter::~DeclPrinter() { |
| 59 | Out.flush(); |
| 60 | } |
| 61 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 62 | void DeclPrinter:: PrintDecl(Decl *D) { |
| 63 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 64 | PrintFunctionDeclStart(FD); |
| 65 | |
| 66 | if (FD->getBody()) { |
| 67 | Out << ' '; |
| 68 | FD->getBody()->printPretty(Out); |
| 69 | Out << '\n'; |
| 70 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 71 | } else if (isa<ObjCMethodDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 72 | // Do nothing, methods definitions are printed in |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 73 | // PrintObjCImplementationDecl. |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 74 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 75 | PrintTypeDefDecl(TD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 76 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 77 | PrintObjCInterfaceDecl(OID); |
| 78 | } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) { |
| 79 | PrintObjCProtocolDecl(PID); |
| 80 | } else if (ObjCForwardProtocolDecl *OFPD = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 81 | dyn_cast<ObjCForwardProtocolDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 82 | Out << "@protocol "; |
| 83 | for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 84 | const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 85 | if (i) Out << ", "; |
| 86 | Out << D->getName(); |
| 87 | } |
| 88 | Out << ";\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 89 | } else if (ObjCImplementationDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 90 | dyn_cast<ObjCImplementationDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 91 | PrintObjCImplementationDecl(OID); |
| 92 | } else if (ObjCCategoryImplDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 93 | dyn_cast<ObjCCategoryImplDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 94 | PrintObjCCategoryImplDecl(OID); |
| 95 | } else if (ObjCCategoryDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 96 | dyn_cast<ObjCCategoryDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 97 | PrintObjCCategoryDecl(OID); |
| 98 | } else if (ObjCCompatibleAliasDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 99 | dyn_cast<ObjCCompatibleAliasDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 100 | PrintObjCCompatibleAliasDecl(OID); |
Chris Lattner | 23a0e45 | 2008-06-21 21:40:20 +0000 | [diff] [blame] | 101 | } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) { |
| 102 | Out << "@class "; |
| 103 | ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls(); |
| 104 | for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) { |
| 105 | const ObjCInterfaceDecl *D = ForwardDecls[i]; |
| 106 | if (i) Out << ", "; |
| 107 | Out << D->getName(); |
| 108 | } |
| 109 | Out << ";\n"; |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 110 | } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 111 | Out << "Read top-level tag decl: '" << TD->getName() << "'\n"; |
| 112 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
| 113 | Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 114 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 115 | PrintLinkageSpec(LSD); |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 116 | } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) { |
| 117 | Out << "asm("; |
| 118 | AD->getAsmString()->printPretty(Out); |
| 119 | Out << ")\n"; |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 120 | } else { |
| 121 | assert(0 && "Unknown decl type!"); |
| 122 | } |
| 123 | } |
| 124 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 125 | void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | bool HasBody = FD->getBody(); |
| 127 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 128 | Out << '\n'; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 129 | |
| 130 | switch (FD->getStorageClass()) { |
| 131 | default: assert(0 && "Unknown storage class"); |
| 132 | case FunctionDecl::None: break; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 133 | case FunctionDecl::Extern: Out << "extern "; break; |
| 134 | case FunctionDecl::Static: Out << "static "; break; |
Ted Kremenek | 24bd3c4 | 2008-04-15 03:57:09 +0000 | [diff] [blame] | 135 | case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (FD->isInline()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 139 | Out << "inline "; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 140 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | std::string Proto = FD->getName(); |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 142 | const FunctionType *AFT = FD->getType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 144 | if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | Proto += "("; |
| 146 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 147 | if (i) Proto += ", "; |
| 148 | std::string ParamStr; |
| 149 | if (HasBody) ParamStr = FD->getParamDecl(i)->getName(); |
| 150 | |
| 151 | FT->getArgType(i).getAsStringInternal(ParamStr); |
| 152 | Proto += ParamStr; |
| 153 | } |
| 154 | |
| 155 | if (FT->isVariadic()) { |
| 156 | if (FD->getNumParams()) Proto += ", "; |
| 157 | Proto += "..."; |
| 158 | } |
| 159 | Proto += ")"; |
| 160 | } else { |
| 161 | assert(isa<FunctionTypeNoProto>(AFT)); |
| 162 | Proto += "()"; |
| 163 | } |
| 164 | |
| 165 | AFT->getResultType().getAsStringInternal(Proto); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 166 | Out << Proto; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 168 | if (!FD->getBody()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 169 | Out << ";\n"; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 170 | // Doesn't print the body. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 173 | void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | std::string S = TD->getName(); |
| 175 | TD->getUnderlyingType().getAsStringInternal(S); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 176 | Out << "typedef " << S << ";\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 179 | void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) { |
| 180 | const char *l; |
| 181 | if (LS->getLanguage() == LinkageSpecDecl::lang_c) |
| 182 | l = "C"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 183 | else { |
| 184 | assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx && |
| 185 | "unknown language in linkage specification"); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 186 | l = "C++"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 188 | Out << "extern \"" << l << "\" { "; |
| 189 | PrintDecl(LS->getDecl()); |
| 190 | Out << "}\n"; |
| 191 | } |
| 192 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 193 | void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 194 | if (OMD->isInstance()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 195 | Out << "\n- "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 196 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 197 | Out << "\n+ "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 198 | if (!OMD->getResultType().isNull()) |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 199 | Out << '(' << OMD->getResultType().getAsString() << ")"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 200 | |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 201 | std::string name = OMD->getSelector().getName(); |
| 202 | std::string::size_type pos, lastPos = 0; |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 203 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 204 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 205 | // FIXME: selector is missing here! |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 206 | pos = name.find_first_of(":", lastPos); |
| 207 | Out << " " << name.substr(lastPos, pos - lastPos); |
| 208 | Out << ":(" << PDecl->getType().getAsString() << ")" << PDecl->getName(); |
| 209 | lastPos = pos + 1; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 211 | |
| 212 | if (OMD->getNumParams() == 0) |
| 213 | Out << " " << name; |
| 214 | |
| 215 | if (OMD->isVariadic()) |
| 216 | Out << ", ..."; |
| 217 | |
| 218 | Out << ";"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 221 | void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 222 | std::string I = OID->getName(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 223 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 224 | |
| 225 | if (SID) |
| 226 | Out << "@implementation " << I << " : " << SID->getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 227 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 228 | Out << "@implementation " << I; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 230 | for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 231 | E = OID->instmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 232 | ObjCMethodDecl *OMD = *I; |
| 233 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 234 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 235 | Out << ' '; |
| 236 | OMD->getBody()->printPretty(Out); |
| 237 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 241 | for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 242 | E = OID->classmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 243 | ObjCMethodDecl *OMD = *I; |
| 244 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 245 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 246 | Out << ' '; |
| 247 | OMD->getBody()->printPretty(Out); |
| 248 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 252 | for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(), |
| 253 | E = OID->propimpl_end(); I != E; ++I) |
| 254 | PrintObjCPropertyImplDecl(*I); |
| 255 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 256 | Out << "@end\n"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 260 | void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 261 | std::string I = OID->getName(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 262 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 263 | |
| 264 | if (SID) |
| 265 | Out << "@interface " << I << " : " << SID->getName(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 266 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 267 | Out << "@interface " << I; |
| 268 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 269 | // Protocols? |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 270 | const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); |
| 271 | if (!Protocols.empty()) { |
| 272 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 273 | E = Protocols.end(); I != E; ++I) |
| 274 | Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getName(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 275 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 277 | if (!Protocols.empty()) |
| 278 | Out << ">"; |
| 279 | Out << '\n'; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 280 | |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 281 | if (OID->ivar_size() > 0) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 282 | Out << '{'; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 283 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 284 | E = OID->ivar_end(); I != E; ++I) { |
| 285 | Out << '\t' << (*I)->getType().getAsString() |
| 286 | << ' ' << (*I)->getName() << ";\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 287 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 288 | Out << "}\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 289 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 290 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 291 | for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(), |
| 292 | E = OID->classprop_end(); I != E; ++I) |
| 293 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 294 | bool eol_needed = false; |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 295 | for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(), |
| 296 | E = OID->classmeth_end(); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 297 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 298 | |
| 299 | for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(), |
| 300 | E = OID->instmeth_end(); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 301 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 302 | |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 303 | Out << (eol_needed ? "\n@end\n" : "@end\n"); |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 304 | // FIXME: implement the rest... |
| 305 | } |
| 306 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 307 | void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 308 | Out << "@protocol " << PID->getName() << '\n'; |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 309 | |
| 310 | for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(), |
| 311 | E = PID->classprop_end(); I != E; ++I) |
| 312 | PrintObjCPropertyDecl(*I); |
| 313 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 314 | // FIXME: implement the rest... |
| 315 | } |
| 316 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 317 | void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 318 | Out << "@implementation " |
| 319 | << PID->getClassInterface()->getName() |
| 320 | << '(' << PID->getName() << ");\n"; |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 321 | for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(), |
| 322 | E = PID->propimpl_end(); I != E; ++I) |
| 323 | PrintObjCPropertyImplDecl(*I); |
| 324 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 325 | // FIXME: implement the rest... |
| 326 | } |
| 327 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 328 | void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 329 | Out << "@interface " |
| 330 | << PID->getClassInterface()->getName() |
| 331 | << '(' << PID->getName() << ");\n"; |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 332 | // Output property declarations. |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 333 | for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(), |
| 334 | E = PID->classprop_end(); I != E; ++I) |
| 335 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 336 | Out << "@end\n"; |
| 337 | |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 338 | // FIXME: implement the rest... |
| 339 | } |
| 340 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 341 | void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 342 | Out << "@compatibility_alias " << AID->getName() |
| 343 | << ' ' << AID->getClassInterface()->getName() << ";\n"; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 346 | /// PrintObjCPropertyDecl - print a property declaration. |
| 347 | /// |
| 348 | void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 349 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 350 | Out << "@required\n"; |
| 351 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 352 | Out << "@optional\n"; |
| 353 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 354 | Out << "@property"; |
| 355 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 356 | bool first = true; |
| 357 | Out << " ("; |
| 358 | if (PDecl->getPropertyAttributes() & |
| 359 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 360 | Out << (first ? ' ' : ',') << "readonly"; |
| 361 | first = false; |
| 362 | } |
| 363 | |
| 364 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 365 | Out << (first ? ' ' : ',') << "getter = " |
Fariborz Jahanian | 5251e13 | 2008-05-06 18:09:04 +0000 | [diff] [blame] | 366 | << PDecl->getGetterName().getName(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 367 | first = false; |
| 368 | } |
| 369 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 370 | Out << (first ? ' ' : ',') << "setter = " |
Fariborz Jahanian | 5251e13 | 2008-05-06 18:09:04 +0000 | [diff] [blame] | 371 | << PDecl->getSetterName().getName(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 372 | first = false; |
| 373 | } |
| 374 | |
| 375 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 376 | Out << (first ? ' ' : ',') << "assign"; |
| 377 | first = false; |
| 378 | } |
| 379 | |
| 380 | if (PDecl->getPropertyAttributes() & |
| 381 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 382 | Out << (first ? ' ' : ',') << "readwrite"; |
| 383 | first = false; |
| 384 | } |
| 385 | |
| 386 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 387 | Out << (first ? ' ' : ',') << "retain"; |
| 388 | first = false; |
| 389 | } |
| 390 | |
| 391 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 392 | Out << (first ? ' ' : ',') << "copy"; |
| 393 | first = false; |
| 394 | } |
| 395 | |
| 396 | if (PDecl->getPropertyAttributes() & |
| 397 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| 398 | Out << (first ? ' ' : ',') << "nonatomic"; |
| 399 | first = false; |
| 400 | } |
| 401 | Out << " )"; |
| 402 | } |
| 403 | Out << ' ' << PDecl->getType().getAsString() |
| 404 | << ' ' << PDecl->getName(); |
| 405 | |
| 406 | Out << ";\n"; |
| 407 | } |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 408 | |
| 409 | /// PrintObjCPropertyImplDecl - Print an objective-c property implementation |
| 410 | /// declaration syntax. |
| 411 | /// |
| 412 | void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 413 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 414 | Out << "\n@synthesize "; |
| 415 | else |
| 416 | Out << "\n@dynamic "; |
| 417 | Out << PID->getPropertyDecl()->getName(); |
| 418 | if (PID->getPropertyIvarDecl()) |
| 419 | Out << "=" << PID->getPropertyIvarDecl()->getName(); |
| 420 | Out << ";\n"; |
| 421 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 422 | //===----------------------------------------------------------------------===// |
| 423 | /// ASTPrinter - Pretty-printer of ASTs |
| 424 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 425 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 426 | class ASTPrinter : public ASTConsumer, public DeclPrinter { |
| 427 | public: |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 428 | ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 430 | virtual void HandleTopLevelDecl(Decl *D) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 431 | PrintDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 432 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 433 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 434 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 435 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 436 | ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 437 | return new ASTPrinter(out); |
| 438 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 439 | |
| 440 | //===----------------------------------------------------------------------===// |
| 441 | /// ASTDumper - Low-level dumper of ASTs |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 442 | |
| 443 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 444 | class ASTDumper : public ASTConsumer, public DeclPrinter { |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 445 | SourceManager *SM; |
| 446 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 447 | ASTDumper() : DeclPrinter() {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 448 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 449 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 450 | SM = &Context.getSourceManager(); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 451 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 452 | |
| 453 | virtual void HandleTopLevelDecl(Decl *D) { |
| 454 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 455 | PrintFunctionDeclStart(FD); |
| 456 | |
| 457 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 458 | Out << '\n'; |
| 459 | // FIXME: convert dumper to use std::ostream? |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 460 | FD->getBody()->dumpAll(*SM); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 461 | Out << '\n'; |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 462 | } |
| 463 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 464 | PrintTypeDefDecl(TD); |
| 465 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 466 | Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 467 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 468 | Out << "Read objc interface '" << OID->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 469 | } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 470 | Out << "Read objc protocol '" << OPD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 471 | } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 472 | Out << "Read objc category '" << OCD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 473 | } else if (isa<ObjCForwardProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 474 | Out << "Read objc fwd protocol decl\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 475 | } else if (isa<ObjCClassDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 476 | Out << "Read objc fwd class decl\n"; |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 477 | } else if (isa<FileScopeAsmDecl>(D)) { |
| 478 | Out << "Read file scope asm decl\n"; |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 479 | } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 480 | Out << "Read objc method decl: '" << MD->getSelector().getName() |
| 481 | << "'\n"; |
Steve Naroff | 1a2b90d | 2008-05-23 18:50:58 +0000 | [diff] [blame] | 482 | if (MD->getBody()) { |
| 483 | // FIXME: convert dumper to use std::ostream? |
| 484 | MD->getBody()->dumpAll(*SM); |
| 485 | Out << '\n'; |
| 486 | } |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 487 | } else if (isa<ObjCImplementationDecl>(D)) { |
| 488 | Out << "Read objc implementation decl\n"; |
Daniel Dunbar | 539ced1 | 2008-10-05 00:31:15 +0000 | [diff] [blame] | 489 | } else if (isa<ObjCCategoryImplDecl>(D)) { |
| 490 | Out << "Read objc category implementation decl\n"; |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 491 | } |
| 492 | else { |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 493 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | }; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 499 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 500 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 501 | //===----------------------------------------------------------------------===// |
| 502 | /// ASTViewer - AST Visualization |
| 503 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 504 | namespace { |
| 505 | class ASTViewer : public ASTConsumer { |
| 506 | SourceManager *SM; |
| 507 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 508 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 509 | SM = &Context.getSourceManager(); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | virtual void HandleTopLevelDecl(Decl *D) { |
| 513 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 514 | DeclPrinter().PrintFunctionDeclStart(FD); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 515 | |
| 516 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 517 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 518 | FD->getBody()->viewAST(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 519 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 522 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 523 | DeclPrinter().PrintObjCMethodDecl(MD); |
| 524 | |
| 525 | if (MD->getBody()) { |
| 526 | llvm::cerr << '\n'; |
| 527 | MD->getBody()->viewAST(); |
| 528 | llvm::cerr << '\n'; |
| 529 | } |
| 530 | } |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 531 | } |
| 532 | }; |
| 533 | } |
| 534 | |
| 535 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 536 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 537 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 538 | /// InheritanceViewer - C++ Inheritance Visualization |
| 539 | |
| 540 | namespace { |
| 541 | class InheritanceViewer : public ASTConsumer { |
| 542 | const std::string clsname; |
| 543 | public: |
| 544 | InheritanceViewer(const std::string& cname) : clsname(cname) {} |
| 545 | |
| 546 | void HandleTranslationUnit(TranslationUnit& TU) { |
| 547 | ASTContext& C = TU.getContext(); |
| 548 | for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I) |
| 549 | if (CXXRecordType *T = dyn_cast<CXXRecordType>(*I)) { |
| 550 | CXXRecordDecl* D = T->getDecl(); |
| 551 | // FIXME: This lookup needs to be generalized to handle namespaces and |
| 552 | // (when we support them) templates. |
| 553 | if (D->getName() == clsname) { |
Douglas Gregor | 1f81230 | 2008-10-24 19:53:54 +0000 | [diff] [blame^] | 554 | D->viewInheritance(C); |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
| 558 | }; |
| 559 | } |
| 560 | |
| 561 | ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) { |
| 562 | return new InheritanceViewer(clsname); |
| 563 | } |
| 564 | |
| 565 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 566 | // AST Serializer |
| 567 | |
| 568 | namespace { |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 569 | |
| 570 | class ASTSerializer : public ASTConsumer { |
| 571 | protected: |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 572 | Diagnostic& Diags; |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 573 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 574 | public: |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 575 | ASTSerializer(Diagnostic& diags) : Diags(diags) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 576 | }; |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame] | 577 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 578 | class SingleFileSerializer : public ASTSerializer { |
| 579 | const llvm::sys::Path FName; |
| 580 | public: |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 581 | SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags) |
| 582 | : ASTSerializer(diags), FName(F) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 583 | |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 584 | virtual void HandleTranslationUnit(TranslationUnit& TU) { |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 585 | if (Diags.hasErrorOccurred()) |
| 586 | return; |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 587 | EmitASTBitcodeFile(&TU, FName); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 588 | } |
| 589 | }; |
| 590 | |
| 591 | class BuildSerializer : public ASTSerializer { |
| 592 | llvm::sys::Path EmitDir; |
| 593 | public: |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 594 | BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags) |
| 595 | : ASTSerializer(diags), EmitDir(dir) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 596 | |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 597 | virtual void HandleTranslationUnit(TranslationUnit& TU) { |
| 598 | if (Diags.hasErrorOccurred()) |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 599 | return; |
| 600 | |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 601 | SourceManager& SourceMgr = TU.getContext().getSourceManager(); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 602 | unsigned ID = SourceMgr.getMainFileID(); |
| 603 | assert (ID && "MainFileID not set!"); |
| 604 | const FileEntry* FE = SourceMgr.getFileEntryForID(ID); |
| 605 | assert (FE && "No FileEntry for main file."); |
| 606 | |
| 607 | // FIXME: This is not portable to Windows. |
| 608 | // FIXME: This logic should probably be moved elsewhere later. |
| 609 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 610 | llvm::sys::Path FName(EmitDir); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 611 | |
| 612 | std::vector<char> buf; |
| 613 | buf.reserve(strlen(FE->getName())+100); |
| 614 | |
| 615 | sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 616 | FName.appendComponent(&buf[0]); |
| 617 | FName.createDirectoryOnDisk(true); |
| 618 | if (!FName.canWrite() || !FName.isDirectory()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 619 | assert (false && "Could not create 'device' serialization directory."); |
| 620 | return; |
| 621 | } |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 622 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 623 | sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 624 | FName.appendComponent(&buf[0]); |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 625 | EmitASTBitcodeFile(&TU, FName); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 626 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 627 | // Now emit the sources. |
| 628 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 629 | } |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 630 | }; |
| 631 | |
| 632 | |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 633 | } // end anonymous namespace |
| 634 | |
| 635 | |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 636 | ASTConsumer* clang::CreateASTSerializer(const std::string& InFile, |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 637 | const std::string& OutputFile, |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 638 | Diagnostic &Diags) { |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 640 | if (OutputFile.size()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 641 | if (InFile == "-") { |
| 642 | llvm::cerr << |
| 643 | "error: Cannot use --serialize with -o for source read from STDIN.\n"; |
| 644 | return NULL; |
| 645 | } |
| 646 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 647 | // The user specified an AST-emission directory. Determine if the path |
| 648 | // is absolute. |
| 649 | llvm::sys::Path EmitDir(OutputFile); |
| 650 | |
| 651 | if (!EmitDir.isAbsolute()) { |
| 652 | llvm::cerr << |
| 653 | "error: Output directory for --serialize must be an absolute path.\n"; |
| 654 | |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | // Create the directory if it does not exist. |
| 659 | EmitDir.createDirectoryOnDisk(true); |
| 660 | if (!EmitDir.canWrite() || !EmitDir.isDirectory()) { |
| 661 | llvm::cerr << |
| 662 | "error: Could not create output directory for --serialize.\n"; |
| 663 | |
| 664 | return NULL; |
| 665 | } |
| 666 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 667 | // FIXME: We should probably only allow using BuildSerializer when |
| 668 | // the ASTs come from parsed source files, and not from .ast files. |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 669 | return new BuildSerializer(EmitDir, Diags); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | // The user did not specify an output directory for serialized ASTs. |
| 673 | // Serialize the translation to a single file whose name is the same |
| 674 | // as the input file with the ".ast" extension appended. |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 675 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 676 | llvm::sys::Path FName(InFile.c_str()); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 677 | FName.appendSuffix("ast"); |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 678 | return new SingleFileSerializer(FName, Diags); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 679 | } |