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