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" |
Ted Kremenek | ad99dbf | 2008-11-03 22:31:48 +0000 | [diff] [blame] | 15 | #include "clang/Driver/PathDiagnosticClients.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 << ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 86 | Out << D->getNameAsString(); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 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 << ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 107 | Out << D->getNameAsString(); |
Chris Lattner | 23a0e45 | 2008-06-21 21:40:20 +0000 | [diff] [blame] | 108 | } |
| 109 | Out << ";\n"; |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 110 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 111 | Out << "enum " << ED->getNameAsString() << " {\n"; |
| 112 | for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(), |
| 113 | EEnd = ED->enumerator_end(); |
| 114 | E != EEnd; ++E) |
| 115 | Out << " " << (*E)->getNameAsString() << ",\n"; |
| 116 | Out << "};\n"; |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 117 | } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 118 | Out << "Read top-level tag decl: '" << TD->getNameAsString() << "'\n"; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 119 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 120 | PrintLinkageSpec(LSD); |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 121 | } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) { |
| 122 | Out << "asm("; |
| 123 | AD->getAsmString()->printPretty(Out); |
| 124 | Out << ")\n"; |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 125 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
| 126 | Out << "Read top-level variable decl: '" << SD->getNameAsString() << "'\n"; |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 127 | } else { |
| 128 | assert(0 && "Unknown decl type!"); |
| 129 | } |
| 130 | } |
| 131 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 132 | void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | bool HasBody = FD->getBody(); |
| 134 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 135 | Out << '\n'; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 136 | |
| 137 | switch (FD->getStorageClass()) { |
| 138 | default: assert(0 && "Unknown storage class"); |
| 139 | case FunctionDecl::None: break; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 140 | case FunctionDecl::Extern: Out << "extern "; break; |
| 141 | case FunctionDecl::Static: Out << "static "; break; |
Ted Kremenek | 24bd3c4 | 2008-04-15 03:57:09 +0000 | [diff] [blame] | 142 | case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (FD->isInline()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 146 | Out << "inline "; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 147 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 148 | std::string Proto = FD->getNameAsString(); |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 149 | const FunctionType *AFT = FD->getType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 151 | if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | Proto += "("; |
| 153 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 154 | if (i) Proto += ", "; |
| 155 | std::string ParamStr; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 156 | if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | |
| 158 | FT->getArgType(i).getAsStringInternal(ParamStr); |
| 159 | Proto += ParamStr; |
| 160 | } |
| 161 | |
| 162 | if (FT->isVariadic()) { |
| 163 | if (FD->getNumParams()) Proto += ", "; |
| 164 | Proto += "..."; |
| 165 | } |
| 166 | Proto += ")"; |
| 167 | } else { |
| 168 | assert(isa<FunctionTypeNoProto>(AFT)); |
| 169 | Proto += "()"; |
| 170 | } |
| 171 | |
| 172 | AFT->getResultType().getAsStringInternal(Proto); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 173 | Out << Proto; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 175 | if (!FD->getBody()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 176 | Out << ";\n"; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 177 | // Doesn't print the body. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 180 | void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 181 | std::string S = TD->getNameAsString(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | TD->getUnderlyingType().getAsStringInternal(S); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 183 | Out << "typedef " << S << ";\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 186 | void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) { |
| 187 | const char *l; |
| 188 | if (LS->getLanguage() == LinkageSpecDecl::lang_c) |
| 189 | l = "C"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 190 | else { |
| 191 | assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx && |
| 192 | "unknown language in linkage specification"); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 193 | l = "C++"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 194 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 195 | |
| 196 | Out << "extern \"" << l << "\" "; |
| 197 | if (LS->hasBraces()) |
| 198 | Out << "{\n"; |
| 199 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 200 | for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(), |
| 201 | DEnd = LS->decls_end(); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 202 | D != DEnd; ++D) |
| 203 | PrintDecl(*D); |
| 204 | |
| 205 | if (LS->hasBraces()) |
| 206 | Out << "}"; |
| 207 | Out << "\n"; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 210 | void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 211 | if (OMD->isInstanceMethod()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 212 | Out << "\n- "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 213 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 214 | Out << "\n+ "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 215 | if (!OMD->getResultType().isNull()) |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 216 | Out << '(' << OMD->getResultType().getAsString() << ")"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 218 | std::string name = OMD->getSelector().getAsString(); |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 219 | std::string::size_type pos, lastPos = 0; |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 220 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 221 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 222 | // FIXME: selector is missing here! |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 223 | pos = name.find_first_of(":", lastPos); |
| 224 | Out << " " << name.substr(lastPos, pos - lastPos); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 225 | Out << ":(" << PDecl->getType().getAsString() << ")" |
| 226 | << PDecl->getNameAsString(); |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 227 | lastPos = pos + 1; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 229 | |
| 230 | if (OMD->getNumParams() == 0) |
| 231 | Out << " " << name; |
| 232 | |
| 233 | if (OMD->isVariadic()) |
| 234 | Out << ", ..."; |
| 235 | |
| 236 | Out << ";"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 239 | void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 240 | std::string I = OID->getNameAsString(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 241 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 242 | |
| 243 | if (SID) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 244 | Out << "@implementation " << I << " : " << SID->getNameAsString(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 245 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 246 | Out << "@implementation " << I; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 248 | for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 249 | E = OID->instmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 250 | ObjCMethodDecl *OMD = *I; |
| 251 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 252 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 253 | Out << ' '; |
| 254 | OMD->getBody()->printPretty(Out); |
| 255 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 259 | for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 260 | E = OID->classmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 261 | ObjCMethodDecl *OMD = *I; |
| 262 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 263 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 264 | Out << ' '; |
| 265 | OMD->getBody()->printPretty(Out); |
| 266 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 270 | for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(), |
| 271 | E = OID->propimpl_end(); I != E; ++I) |
| 272 | PrintObjCPropertyImplDecl(*I); |
| 273 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 274 | Out << "@end\n"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 278 | void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 279 | std::string I = OID->getNameAsString(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 280 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 281 | |
| 282 | if (SID) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 283 | Out << "@interface " << I << " : " << SID->getNameAsString(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 284 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 285 | Out << "@interface " << I; |
| 286 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 287 | // Protocols? |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 288 | const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); |
| 289 | if (!Protocols.empty()) { |
| 290 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 291 | E = Protocols.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 292 | Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 293 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 295 | if (!Protocols.empty()) |
| 296 | Out << ">"; |
| 297 | Out << '\n'; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 298 | |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 299 | if (OID->ivar_size() > 0) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 300 | Out << '{'; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 301 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 302 | E = OID->ivar_end(); I != E; ++I) { |
| 303 | Out << '\t' << (*I)->getType().getAsString() |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 304 | << ' ' << (*I)->getNameAsString() << ";\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 305 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 306 | Out << "}\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 307 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 308 | |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 309 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(), |
| 310 | E = OID->prop_end(); I != E; ++I) |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 311 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 312 | bool eol_needed = false; |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 313 | for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(), |
| 314 | E = OID->classmeth_end(); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 315 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 316 | |
| 317 | for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(), |
| 318 | E = OID->instmeth_end(); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 319 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 320 | |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 321 | Out << (eol_needed ? "\n@end\n" : "@end\n"); |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 322 | // FIXME: implement the rest... |
| 323 | } |
| 324 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 325 | void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 326 | Out << "@protocol " << PID->getNameAsString() << '\n'; |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 327 | |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 328 | for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(), |
| 329 | E = PID->prop_end(); I != E; ++I) |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 330 | PrintObjCPropertyDecl(*I); |
| 331 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 332 | // FIXME: implement the rest... |
| 333 | } |
| 334 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 335 | void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 336 | Out << "@implementation " |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 337 | << PID->getClassInterface()->getNameAsString() |
| 338 | << '(' << PID->getNameAsString() << ");\n"; |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 339 | for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(), |
| 340 | E = PID->propimpl_end(); I != E; ++I) |
| 341 | PrintObjCPropertyImplDecl(*I); |
| 342 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 343 | // FIXME: implement the rest... |
| 344 | } |
| 345 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 346 | void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 347 | Out << "@interface " |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 348 | << PID->getClassInterface()->getNameAsString() |
| 349 | << '(' << PID->getNameAsString() << ");\n"; |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 350 | // Output property declarations. |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 351 | for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(), |
| 352 | E = PID->prop_end(); I != E; ++I) |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 353 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 354 | Out << "@end\n"; |
| 355 | |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 356 | // FIXME: implement the rest... |
| 357 | } |
| 358 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 359 | void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 360 | Out << "@compatibility_alias " << AID->getNameAsString() |
| 361 | << ' ' << AID->getClassInterface()->getNameAsString() << ";\n"; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 364 | /// PrintObjCPropertyDecl - print a property declaration. |
| 365 | /// |
| 366 | void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 367 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 368 | Out << "@required\n"; |
| 369 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 370 | Out << "@optional\n"; |
| 371 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 372 | Out << "@property"; |
| 373 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 374 | bool first = true; |
| 375 | Out << " ("; |
| 376 | if (PDecl->getPropertyAttributes() & |
| 377 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 378 | Out << (first ? ' ' : ',') << "readonly"; |
| 379 | first = false; |
| 380 | } |
| 381 | |
| 382 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 383 | Out << (first ? ' ' : ',') << "getter = " |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 384 | << PDecl->getGetterName().getAsString(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 385 | first = false; |
| 386 | } |
| 387 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 388 | Out << (first ? ' ' : ',') << "setter = " |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 389 | << PDecl->getSetterName().getAsString(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 390 | first = false; |
| 391 | } |
| 392 | |
| 393 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 394 | Out << (first ? ' ' : ',') << "assign"; |
| 395 | first = false; |
| 396 | } |
| 397 | |
| 398 | if (PDecl->getPropertyAttributes() & |
| 399 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 400 | Out << (first ? ' ' : ',') << "readwrite"; |
| 401 | first = false; |
| 402 | } |
| 403 | |
| 404 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 405 | Out << (first ? ' ' : ',') << "retain"; |
| 406 | first = false; |
| 407 | } |
| 408 | |
| 409 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 410 | Out << (first ? ' ' : ',') << "copy"; |
| 411 | first = false; |
| 412 | } |
| 413 | |
| 414 | if (PDecl->getPropertyAttributes() & |
| 415 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| 416 | Out << (first ? ' ' : ',') << "nonatomic"; |
| 417 | first = false; |
| 418 | } |
| 419 | Out << " )"; |
| 420 | } |
| 421 | Out << ' ' << PDecl->getType().getAsString() |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 422 | << ' ' << PDecl->getNameAsString(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 423 | |
| 424 | Out << ";\n"; |
| 425 | } |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 426 | |
| 427 | /// PrintObjCPropertyImplDecl - Print an objective-c property implementation |
| 428 | /// declaration syntax. |
| 429 | /// |
| 430 | void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 431 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 432 | Out << "\n@synthesize "; |
| 433 | else |
| 434 | Out << "\n@dynamic "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 435 | Out << PID->getPropertyDecl()->getNameAsString(); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 436 | if (PID->getPropertyIvarDecl()) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 437 | Out << "=" << PID->getPropertyIvarDecl()->getNameAsString(); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 438 | Out << ";\n"; |
| 439 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 440 | //===----------------------------------------------------------------------===// |
| 441 | /// ASTPrinter - Pretty-printer of ASTs |
| 442 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 443 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 444 | class ASTPrinter : public ASTConsumer, public DeclPrinter { |
| 445 | public: |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 446 | ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 448 | virtual void HandleTopLevelDecl(Decl *D) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 449 | PrintDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 450 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 451 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 452 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 453 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 454 | ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 455 | return new ASTPrinter(out); |
| 456 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 457 | |
| 458 | //===----------------------------------------------------------------------===// |
| 459 | /// ASTDumper - Low-level dumper of ASTs |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 460 | |
| 461 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 462 | class ASTDumper : public ASTConsumer, public DeclPrinter { |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 463 | SourceManager *SM; |
| 464 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 465 | ASTDumper() : DeclPrinter() {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 466 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 467 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 468 | SM = &Context.getSourceManager(); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 469 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 470 | |
| 471 | virtual void HandleTopLevelDecl(Decl *D) { |
| 472 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 473 | PrintFunctionDeclStart(FD); |
| 474 | |
| 475 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 476 | Out << '\n'; |
| 477 | // FIXME: convert dumper to use std::ostream? |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 478 | FD->getBody()->dumpAll(*SM); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 479 | Out << '\n'; |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 480 | } |
| 481 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 482 | PrintTypeDefDecl(TD); |
| 483 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 484 | Out << "Read top-level variable decl: '" << SD->getNameAsString() |
| 485 | << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 486 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 487 | Out << "Read objc interface '" << OID->getNameAsString() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 488 | } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 489 | Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 490 | } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 491 | Out << "Read objc category '" << OCD->getNameAsString() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 492 | } else if (isa<ObjCForwardProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 493 | Out << "Read objc fwd protocol decl\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 494 | } else if (isa<ObjCClassDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 495 | Out << "Read objc fwd class decl\n"; |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 496 | } else if (isa<FileScopeAsmDecl>(D)) { |
| 497 | Out << "Read file scope asm decl\n"; |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 498 | } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 499 | Out << "Read objc method decl: '" << MD->getSelector().getAsString() |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 500 | << "'\n"; |
Steve Naroff | 1a2b90d | 2008-05-23 18:50:58 +0000 | [diff] [blame] | 501 | if (MD->getBody()) { |
| 502 | // FIXME: convert dumper to use std::ostream? |
| 503 | MD->getBody()->dumpAll(*SM); |
| 504 | Out << '\n'; |
| 505 | } |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 506 | } else if (isa<ObjCImplementationDecl>(D)) { |
| 507 | Out << "Read objc implementation decl\n"; |
Daniel Dunbar | 539ced1 | 2008-10-05 00:31:15 +0000 | [diff] [blame] | 508 | } else if (isa<ObjCCategoryImplDecl>(D)) { |
| 509 | Out << "Read objc category implementation decl\n"; |
Eli Friedman | f595eb0 | 2008-12-16 22:14:15 +0000 | [diff] [blame] | 510 | } else if (isa<LinkageSpecDecl>(D)) { |
| 511 | Out << "Read linkage spec decl\n"; |
| 512 | } else { |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 513 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | }; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 519 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 520 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 521 | //===----------------------------------------------------------------------===// |
| 522 | /// ASTViewer - AST Visualization |
| 523 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 524 | namespace { |
| 525 | class ASTViewer : public ASTConsumer { |
| 526 | SourceManager *SM; |
| 527 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 528 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 529 | SM = &Context.getSourceManager(); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | virtual void HandleTopLevelDecl(Decl *D) { |
| 533 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 534 | DeclPrinter().PrintFunctionDeclStart(FD); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 535 | |
| 536 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 537 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 538 | FD->getBody()->viewAST(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 539 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 542 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 543 | DeclPrinter().PrintObjCMethodDecl(MD); |
| 544 | |
| 545 | if (MD->getBody()) { |
| 546 | llvm::cerr << '\n'; |
| 547 | MD->getBody()->viewAST(); |
| 548 | llvm::cerr << '\n'; |
| 549 | } |
| 550 | } |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 551 | } |
| 552 | }; |
| 553 | } |
| 554 | |
| 555 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 556 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 557 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 558 | /// DeclContextPrinter - Decl and DeclContext Visualization |
| 559 | |
| 560 | namespace { |
| 561 | |
| 562 | class DeclContextPrinter : public ASTConsumer { |
| 563 | llvm::raw_ostream& Out; |
| 564 | public: |
| 565 | DeclContextPrinter() : Out(llvm::errs()) {} |
| 566 | |
| 567 | void HandleTranslationUnit(TranslationUnit& TU) { |
| 568 | TranslationUnitDecl* TUD = TU.getContext().getTranslationUnitDecl(); |
| 569 | PrintDeclContext(TUD, 4); |
| 570 | } |
| 571 | |
| 572 | void PrintDeclContext(const DeclContext* DC, unsigned Indentation); |
| 573 | }; |
| 574 | |
| 575 | void DeclContextPrinter::PrintDeclContext(const DeclContext* DC, |
| 576 | unsigned Indentation) { |
| 577 | // Print DeclContext name. |
| 578 | Decl::Kind DK = DeclContext::KindTrait<DeclContext>::getKind(DC); |
| 579 | switch (DK) { |
| 580 | case Decl::TranslationUnit: |
| 581 | Out << "[translation unit] " << DC; |
| 582 | break; |
| 583 | case Decl::Namespace: { |
| 584 | Out << "[namespace] "; |
| 585 | NamespaceDecl* ND = NamespaceDecl::castFromDeclContext(DC); |
| 586 | Out << ND->getNameAsString(); |
| 587 | break; |
| 588 | } |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame^] | 589 | case Decl::Enum: { |
| 590 | EnumDecl* ED = EnumDecl::castFromDeclContext(DC); |
| 591 | if (ED->isDefinition()) |
| 592 | Out << "[enum] "; |
| 593 | else |
| 594 | Out << "<enum> "; |
| 595 | Out << ED->getNameAsString(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 596 | break; |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame^] | 597 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 598 | case Decl::Record: { |
| 599 | RecordDecl* RD = RecordDecl::castFromDeclContext(DC); |
| 600 | if (RD->isDefinition()) |
| 601 | Out << "[struct] "; |
| 602 | else |
| 603 | Out << "<struct> "; |
| 604 | Out << RD->getNameAsString(); |
| 605 | break; |
| 606 | } |
| 607 | case Decl::CXXRecord: { |
| 608 | CXXRecordDecl* RD = CXXRecordDecl::castFromDeclContext(DC); |
| 609 | if (RD->isDefinition()) |
| 610 | Out << "[class] "; |
| 611 | else |
| 612 | Out << "<class> "; |
| 613 | Out << RD->getNameAsString() << " " << DC; |
| 614 | break; |
| 615 | } |
| 616 | case Decl::ObjCMethod: |
| 617 | Out << "[objc method]"; |
| 618 | break; |
| 619 | case Decl::ObjCInterface: |
| 620 | Out << "[objc interface]"; |
| 621 | break; |
| 622 | case Decl::ObjCCategory: |
| 623 | Out << "[objc category]"; |
| 624 | break; |
| 625 | case Decl::ObjCProtocol: |
| 626 | Out << "[objc protocol]"; |
| 627 | break; |
| 628 | case Decl::ObjCImplementation: |
| 629 | Out << "[objc implementation]"; |
| 630 | break; |
| 631 | case Decl::ObjCCategoryImpl: |
| 632 | Out << "[objc categoryimpl]"; |
| 633 | break; |
| 634 | case Decl::LinkageSpec: |
| 635 | Out << "[linkage spec]"; |
| 636 | break; |
| 637 | case Decl::Block: |
| 638 | Out << "[block]"; |
| 639 | break; |
| 640 | case Decl::Function: { |
| 641 | FunctionDecl* FD = FunctionDecl::castFromDeclContext(DC); |
| 642 | if (FD->isThisDeclarationADefinition()) |
| 643 | Out << "[function] "; |
| 644 | else |
| 645 | Out << "<function> "; |
| 646 | Out << FD->getNameAsString(); |
| 647 | break; |
| 648 | } |
| 649 | case Decl::CXXMethod: { |
| 650 | CXXMethodDecl* CMD = CXXMethodDecl::castFromDeclContext(DC); |
| 651 | if (CMD->isOutOfLineDefinition()) |
| 652 | Out << "[c++ method] "; |
| 653 | else |
| 654 | Out << "<c++ method> "; |
| 655 | Out << CMD->getNameAsString(); |
| 656 | |
| 657 | // Check the semantic DeclContext. |
| 658 | DeclContext* SemaDC = CMD->getDeclContext(); |
| 659 | DeclContext* LexicalDC = CMD->getLexicalDeclContext(); |
| 660 | if (SemaDC != LexicalDC) { |
| 661 | Out << " [[" << SemaDC << "]]"; |
| 662 | } |
| 663 | break; |
| 664 | } |
| 665 | case Decl::CXXConstructor: { |
| 666 | CXXConstructorDecl* CMD = CXXConstructorDecl::castFromDeclContext(DC); |
| 667 | if (CMD->isOutOfLineDefinition()) |
| 668 | Out << "[c++ ctor] "; |
| 669 | else |
| 670 | Out << "<c++ ctor> "; |
| 671 | Out << CMD->getNameAsString(); |
| 672 | break; |
| 673 | } |
| 674 | case Decl::CXXDestructor: { |
| 675 | CXXDestructorDecl* CMD = CXXDestructorDecl::castFromDeclContext(DC); |
| 676 | if (CMD->isOutOfLineDefinition()) |
| 677 | Out << "[c++ dtor] "; |
| 678 | else |
| 679 | Out << "<c++ dtor> "; |
| 680 | Out << CMD->getNameAsString(); |
| 681 | break; |
| 682 | } |
| 683 | case Decl::CXXConversion: { |
| 684 | CXXConversionDecl* CMD = CXXConversionDecl::castFromDeclContext(DC); |
| 685 | if (CMD->isOutOfLineDefinition()) |
| 686 | Out << "[c++ conversion] "; |
| 687 | else |
| 688 | Out << "<c++ conversion> "; |
| 689 | Out << CMD->getNameAsString(); |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | default: |
| 694 | assert(0 && "a decl that inherits DeclContext isn't handled"); |
| 695 | } |
| 696 | |
| 697 | Out << "\n"; |
| 698 | |
| 699 | // Print decls in the DeclContext. |
| 700 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 701 | I != E; ++I) { |
| 702 | for (unsigned i = 0; i < Indentation; ++i) |
| 703 | Out << " "; |
| 704 | |
| 705 | Decl::Kind DK = I->getKind(); |
| 706 | switch (DK) { |
| 707 | case Decl::Namespace: |
| 708 | case Decl::Enum: |
| 709 | case Decl::Record: |
| 710 | case Decl::CXXRecord: |
| 711 | case Decl::ObjCMethod: |
| 712 | case Decl::ObjCInterface: |
| 713 | case Decl::ObjCCategory: |
| 714 | case Decl::ObjCProtocol: |
| 715 | case Decl::ObjCImplementation: |
| 716 | case Decl::ObjCCategoryImpl: |
| 717 | case Decl::LinkageSpec: |
| 718 | case Decl::Block: |
| 719 | case Decl::Function: |
| 720 | case Decl::CXXMethod: |
| 721 | case Decl::CXXConstructor: |
| 722 | case Decl::CXXDestructor: |
| 723 | case Decl::CXXConversion: |
| 724 | { |
| 725 | DeclContext* DC = Decl::castToDeclContext(*I); |
| 726 | PrintDeclContext(DC, Indentation+4); |
| 727 | break; |
| 728 | } |
| 729 | case Decl::Field: { |
| 730 | FieldDecl* FD = cast<FieldDecl>(*I); |
| 731 | Out << "<field> " << FD->getNameAsString() << "\n"; |
| 732 | break; |
| 733 | } |
| 734 | case Decl::Typedef: { |
| 735 | TypedefDecl* TD = cast<TypedefDecl>(*I); |
| 736 | Out << "<typedef> " << TD->getNameAsString() << "\n"; |
| 737 | break; |
| 738 | } |
| 739 | case Decl::EnumConstant: { |
| 740 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I); |
| 741 | Out << "<enum constant> " << ECD->getNameAsString() << "\n"; |
| 742 | break; |
| 743 | } |
| 744 | case Decl::Var: { |
| 745 | VarDecl* VD = cast<VarDecl>(*I); |
| 746 | Out << "<var> " << VD->getNameAsString() << "\n"; |
| 747 | break; |
| 748 | } |
| 749 | case Decl::ImplicitParam: { |
| 750 | ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I); |
| 751 | Out << "<implicit parameter> " << IPD->getNameAsString() << "\n"; |
| 752 | break; |
| 753 | } |
| 754 | case Decl::CXXClassVar: { |
| 755 | CXXClassVarDecl* CVD = cast<CXXClassVarDecl>(*I); |
| 756 | Out << "<static member var> " << CVD->getNameAsString() << "\n"; |
| 757 | break; |
| 758 | } |
| 759 | case Decl::ParmVar: { |
| 760 | ParmVarDecl* PVD = cast<ParmVarDecl>(*I); |
| 761 | Out << "<parameter> " << PVD->getNameAsString() << "\n"; |
| 762 | break; |
| 763 | } |
| 764 | case Decl::ObjCProperty: { |
| 765 | ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I); |
| 766 | Out << "<objc property> " << OPD->getNameAsString() << "\n"; |
| 767 | break; |
| 768 | } |
| 769 | default: |
| 770 | fprintf(stderr, "DeclKind: %d\n", DK); |
| 771 | assert(0 && "decl unhandled"); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | } |
| 777 | |
| 778 | ASTConsumer *clang::CreateDeclContextPrinter() { |
| 779 | return new DeclContextPrinter(); |
| 780 | } |
| 781 | |
| 782 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 783 | /// InheritanceViewer - C++ Inheritance Visualization |
| 784 | |
| 785 | namespace { |
| 786 | class InheritanceViewer : public ASTConsumer { |
| 787 | const std::string clsname; |
| 788 | public: |
| 789 | InheritanceViewer(const std::string& cname) : clsname(cname) {} |
| 790 | |
| 791 | void HandleTranslationUnit(TranslationUnit& TU) { |
| 792 | ASTContext& C = TU.getContext(); |
| 793 | for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I) |
| 794 | if (CXXRecordType *T = dyn_cast<CXXRecordType>(*I)) { |
| 795 | CXXRecordDecl* D = T->getDecl(); |
| 796 | // FIXME: This lookup needs to be generalized to handle namespaces and |
| 797 | // (when we support them) templates. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 798 | if (D->getNameAsString() == clsname) { |
Douglas Gregor | 1f81230 | 2008-10-24 19:53:54 +0000 | [diff] [blame] | 799 | D->viewInheritance(C); |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
| 803 | }; |
| 804 | } |
| 805 | |
| 806 | ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) { |
| 807 | return new InheritanceViewer(clsname); |
| 808 | } |
| 809 | |
| 810 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 811 | // AST Serializer |
| 812 | |
| 813 | namespace { |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 814 | |
| 815 | class ASTSerializer : public ASTConsumer { |
| 816 | protected: |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 817 | Diagnostic& Diags; |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 818 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 819 | public: |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 820 | ASTSerializer(Diagnostic& diags) : Diags(diags) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 821 | }; |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame] | 822 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 823 | class SingleFileSerializer : public ASTSerializer { |
| 824 | const llvm::sys::Path FName; |
| 825 | public: |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 826 | SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags) |
| 827 | : ASTSerializer(diags), FName(F) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 828 | |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 829 | virtual void HandleTranslationUnit(TranslationUnit& TU) { |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 830 | if (Diags.hasErrorOccurred()) |
| 831 | return; |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 832 | EmitASTBitcodeFile(&TU, FName); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 833 | } |
| 834 | }; |
| 835 | |
| 836 | class BuildSerializer : public ASTSerializer { |
| 837 | llvm::sys::Path EmitDir; |
| 838 | public: |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 839 | BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags) |
| 840 | : ASTSerializer(diags), EmitDir(dir) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 841 | |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 842 | virtual void HandleTranslationUnit(TranslationUnit& TU) { |
| 843 | if (Diags.hasErrorOccurred()) |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 844 | return; |
| 845 | |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 846 | SourceManager& SourceMgr = TU.getContext().getSourceManager(); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 847 | unsigned ID = SourceMgr.getMainFileID(); |
| 848 | assert (ID && "MainFileID not set!"); |
| 849 | const FileEntry* FE = SourceMgr.getFileEntryForID(ID); |
| 850 | assert (FE && "No FileEntry for main file."); |
| 851 | |
| 852 | // FIXME: This is not portable to Windows. |
| 853 | // FIXME: This logic should probably be moved elsewhere later. |
| 854 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 855 | llvm::sys::Path FName(EmitDir); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 856 | |
| 857 | std::vector<char> buf; |
| 858 | buf.reserve(strlen(FE->getName())+100); |
| 859 | |
| 860 | sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 861 | FName.appendComponent(&buf[0]); |
| 862 | FName.createDirectoryOnDisk(true); |
| 863 | if (!FName.canWrite() || !FName.isDirectory()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 864 | assert (false && "Could not create 'device' serialization directory."); |
| 865 | return; |
| 866 | } |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 867 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 868 | sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 869 | FName.appendComponent(&buf[0]); |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 870 | EmitASTBitcodeFile(&TU, FName); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 871 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 872 | // Now emit the sources. |
| 873 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 874 | } |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 875 | }; |
| 876 | |
| 877 | |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 878 | } // end anonymous namespace |
| 879 | |
| 880 | |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 881 | ASTConsumer* clang::CreateASTSerializer(const std::string& InFile, |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 882 | const std::string& OutputFile, |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 883 | Diagnostic &Diags) { |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 884 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 885 | if (OutputFile.size()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 886 | if (InFile == "-") { |
| 887 | llvm::cerr << |
| 888 | "error: Cannot use --serialize with -o for source read from STDIN.\n"; |
| 889 | return NULL; |
| 890 | } |
| 891 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 892 | // The user specified an AST-emission directory. Determine if the path |
| 893 | // is absolute. |
| 894 | llvm::sys::Path EmitDir(OutputFile); |
| 895 | |
| 896 | if (!EmitDir.isAbsolute()) { |
| 897 | llvm::cerr << |
| 898 | "error: Output directory for --serialize must be an absolute path.\n"; |
| 899 | |
| 900 | return NULL; |
| 901 | } |
| 902 | |
| 903 | // Create the directory if it does not exist. |
| 904 | EmitDir.createDirectoryOnDisk(true); |
| 905 | if (!EmitDir.canWrite() || !EmitDir.isDirectory()) { |
| 906 | llvm::cerr << |
| 907 | "error: Could not create output directory for --serialize.\n"; |
| 908 | |
| 909 | return NULL; |
| 910 | } |
| 911 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 912 | // FIXME: We should probably only allow using BuildSerializer when |
| 913 | // the ASTs come from parsed source files, and not from .ast files. |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 914 | return new BuildSerializer(EmitDir, Diags); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | // The user did not specify an output directory for serialized ASTs. |
| 918 | // Serialize the translation to a single file whose name is the same |
| 919 | // as the input file with the ".ast" extension appended. |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 920 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 921 | llvm::sys::Path FName(InFile.c_str()); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 922 | FName.appendSuffix("ast"); |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 923 | return new SingleFileSerializer(FName, Diags); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 924 | } |