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" |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/PathDiagnosticClients.h" |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/AST.h" |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.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" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 27 | #include "llvm/System/Path.h" |
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; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 37 | unsigned Indentation; |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 38 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 39 | DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()), |
| 40 | Indentation(0) {} |
| 41 | DeclPrinter() : Out(llvm::errs()), Indentation(0) {} |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 42 | virtual ~DeclPrinter(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 43 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 44 | void ChangeIndent(int I) { |
| 45 | Indentation += I; |
| 46 | } |
| 47 | |
| 48 | llvm::raw_ostream& Indent() { |
| 49 | for (unsigned i = 0; i < Indentation; ++i) |
| 50 | Out << " "; |
| 51 | return Out; |
| 52 | } |
| 53 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 54 | void PrintDecl(Decl *D); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 55 | void Print(NamedDecl *ND); |
| 56 | void Print(NamespaceDecl *NS); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 57 | void PrintFunctionDeclStart(FunctionDecl *FD); |
| 58 | void PrintTypeDefDecl(TypedefDecl *TD); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 59 | void PrintLinkageSpec(LinkageSpecDecl *LS); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 60 | void PrintObjCMethodDecl(ObjCMethodDecl *OMD); |
| 61 | void PrintObjCImplementationDecl(ObjCImplementationDecl *OID); |
| 62 | void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID); |
| 63 | void PrintObjCProtocolDecl(ObjCProtocolDecl *PID); |
| 64 | void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID); |
| 65 | void PrintObjCCategoryDecl(ObjCCategoryDecl *PID); |
| 66 | void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 67 | void PrintObjCPropertyDecl(ObjCPropertyDecl *PD); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 68 | void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 69 | |
| 70 | void PrintTemplateDecl(TemplateDecl *TD); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 71 | }; |
| 72 | } // end anonymous namespace |
| 73 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 74 | DeclPrinter::~DeclPrinter() { |
| 75 | Out.flush(); |
| 76 | } |
| 77 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 78 | void DeclPrinter:: PrintDecl(Decl *D) { |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 79 | Indent(); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 80 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 81 | PrintFunctionDeclStart(FD); |
| 82 | |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 83 | // FIXME: Pass a context here so we can use getBody() |
| 84 | if (FD->getBodyIfAvailable()) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 85 | Out << ' '; |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 86 | FD->getBodyIfAvailable()->printPretty(Out, 0, Indentation, true); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 87 | Out << '\n'; |
| 88 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 89 | } else if (isa<ObjCMethodDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 90 | // Do nothing, methods definitions are printed in |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 91 | // PrintObjCImplementationDecl. |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 92 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 93 | PrintTypeDefDecl(TD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 94 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 95 | PrintObjCInterfaceDecl(OID); |
| 96 | } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) { |
| 97 | PrintObjCProtocolDecl(PID); |
| 98 | } else if (ObjCForwardProtocolDecl *OFPD = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 99 | dyn_cast<ObjCForwardProtocolDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 100 | Out << "@protocol "; |
Steve Naroff | 30833f8 | 2009-04-21 15:12:33 +0000 | [diff] [blame^] | 101 | for (ObjCForwardProtocolDecl::protocol_iterator I = OFPD->protocol_begin(), |
| 102 | E = OFPD->protocol_end(); |
Chris Lattner | 07fa774 | 2009-02-20 18:10:37 +0000 | [diff] [blame] | 103 | I != E; ++I) { |
Steve Naroff | 30833f8 | 2009-04-21 15:12:33 +0000 | [diff] [blame^] | 104 | if (I != OFPD->protocol_begin()) Out << ", "; |
Chris Lattner | 07fa774 | 2009-02-20 18:10:37 +0000 | [diff] [blame] | 105 | Out << (*I)->getNameAsString(); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 106 | } |
| 107 | Out << ";\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 108 | } else if (ObjCImplementationDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 109 | dyn_cast<ObjCImplementationDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 110 | PrintObjCImplementationDecl(OID); |
| 111 | } else if (ObjCCategoryImplDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 112 | dyn_cast<ObjCCategoryImplDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 113 | PrintObjCCategoryImplDecl(OID); |
| 114 | } else if (ObjCCategoryDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 115 | dyn_cast<ObjCCategoryDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 116 | PrintObjCCategoryDecl(OID); |
| 117 | } else if (ObjCCompatibleAliasDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 118 | dyn_cast<ObjCCompatibleAliasDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 119 | PrintObjCCompatibleAliasDecl(OID); |
Chris Lattner | 23a0e45 | 2008-06-21 21:40:20 +0000 | [diff] [blame] | 120 | } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) { |
| 121 | Out << "@class "; |
Chris Lattner | 6795605 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 122 | for (ObjCClassDecl::iterator I = OFCD->begin(), E = OFCD->end(); |
| 123 | I != E; ++I) { |
| 124 | if (I != OFCD->begin()) Out << ", "; |
| 125 | Out << (*I)->getNameAsString(); |
Chris Lattner | 23a0e45 | 2008-06-21 21:40:20 +0000 | [diff] [blame] | 126 | } |
| 127 | Out << ";\n"; |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 128 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 129 | Out << "enum " << ED->getNameAsString() << " {\n"; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 130 | // FIXME: Shouldn't pass a NULL context |
| 131 | ASTContext *Context = 0; |
| 132 | for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(*Context), |
| 133 | EEnd = ED->enumerator_end(*Context); |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 134 | E != EEnd; ++E) |
| 135 | Out << " " << (*E)->getNameAsString() << ",\n"; |
| 136 | Out << "};\n"; |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 137 | } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 138 | // print a free standing tag decl (e.g. "struct x;"). |
| 139 | Out << TD->getKindName(); |
| 140 | Out << " "; |
| 141 | if (const IdentifierInfo *II = TD->getIdentifier()) |
| 142 | Out << II->getName(); |
| 143 | |
| 144 | Out << " {\n"; |
| 145 | ChangeIndent(1); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 146 | // FIXME: Shouldn't pass a NULL context |
| 147 | ASTContext *Context = 0; |
| 148 | for (DeclContext::decl_iterator i = TD->decls_begin(*Context); |
| 149 | i != TD->decls_end(*Context); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 150 | ++i) |
| 151 | PrintDecl(*i); |
| 152 | ChangeIndent(-1); |
| 153 | Indent(); |
| 154 | Out << "}"; |
| 155 | |
| 156 | Out << "\n"; |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 157 | } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) { |
| 158 | PrintTemplateDecl(TempD); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 159 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 160 | PrintLinkageSpec(LSD); |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 161 | } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) { |
| 162 | Out << "asm("; |
| 163 | AD->getAsmString()->printPretty(Out); |
| 164 | Out << ")\n"; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 165 | } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 166 | Print(ND); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 167 | } else { |
| 168 | assert(0 && "Unknown decl type!"); |
| 169 | } |
| 170 | } |
| 171 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 172 | void DeclPrinter::Print(NamedDecl *ND) { |
| 173 | switch (ND->getKind()) { |
| 174 | default: |
| 175 | // FIXME: Handle the rest of the NamedDecls. |
| 176 | Out << "### NamedDecl " << ND->getNameAsString() << "\n"; |
| 177 | break; |
| 178 | case Decl::Field: |
| 179 | case Decl::Var: { |
| 180 | // Emit storage class for vardecls. |
| 181 | if (VarDecl *V = dyn_cast<VarDecl>(ND)) { |
| 182 | switch (V->getStorageClass()) { |
| 183 | default: assert(0 && "Unknown storage class!"); |
Mike Stump | c5840c0 | 2009-02-10 23:49:50 +0000 | [diff] [blame] | 184 | case VarDecl::None: break; |
| 185 | case VarDecl::Auto: Out << "auto "; break; |
| 186 | case VarDecl::Register: Out << "register "; break; |
| 187 | case VarDecl::Extern: Out << "extern "; break; |
| 188 | case VarDecl::Static: Out << "static "; break; |
Daniel Dunbar | 7ab41f7 | 2009-02-13 22:49:34 +0000 | [diff] [blame] | 189 | case VarDecl::PrivateExtern: Out << "__private_extern__ "; break; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | std::string Name = ND->getNameAsString(); |
| 193 | // This forms: "int a". |
| 194 | dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 195 | Out << Name; |
| 196 | if (VarDecl *Var = dyn_cast<VarDecl>(ND)) { |
| 197 | if (Var->getInit()) { |
| 198 | Out << " = "; |
| 199 | Var->getInit()->printPretty(Out); |
| 200 | } |
| 201 | } |
| 202 | Out << ";\n"; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | case Decl::Namespace: |
| 206 | Print(dyn_cast<NamespaceDecl>(ND)); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void DeclPrinter::Print(NamespaceDecl *NS) { |
| 212 | Out << "namespace " << NS->getNameAsString() << " {\n"; |
| 213 | ChangeIndent(1); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 214 | // FIXME: Shouldn't pass a NULL context |
| 215 | ASTContext *Context = 0; |
| 216 | for (DeclContext::decl_iterator i = NS->decls_begin(*Context); |
| 217 | i != NS->decls_end(*Context); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 218 | ++i) |
| 219 | PrintDecl(*i); |
| 220 | ChangeIndent(-1); |
| 221 | Indent(); |
| 222 | Out << "}\n"; |
| 223 | } |
| 224 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 225 | void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 226 | // FIXME: pass a context so that we can use getBody. |
| 227 | bool HasBody = FD->getBodyIfAvailable(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 229 | Out << '\n'; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 230 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 231 | Indent(); |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 232 | switch (FD->getStorageClass()) { |
| 233 | default: assert(0 && "Unknown storage class"); |
| 234 | case FunctionDecl::None: break; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 235 | case FunctionDecl::Extern: Out << "extern "; break; |
| 236 | case FunctionDecl::Static: Out << "static "; break; |
Ted Kremenek | 24bd3c4 | 2008-04-15 03:57:09 +0000 | [diff] [blame] | 237 | case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if (FD->isInline()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 241 | Out << "inline "; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 242 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 243 | std::string Proto = FD->getNameAsString(); |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 244 | const FunctionType *AFT = FD->getType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 246 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | Proto += "("; |
| 248 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 249 | if (i) Proto += ", "; |
| 250 | std::string ParamStr; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 251 | if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 252 | |
| 253 | FT->getArgType(i).getAsStringInternal(ParamStr); |
| 254 | Proto += ParamStr; |
| 255 | } |
| 256 | |
| 257 | if (FT->isVariadic()) { |
| 258 | if (FD->getNumParams()) Proto += ", "; |
| 259 | Proto += "..."; |
| 260 | } |
| 261 | Proto += ")"; |
| 262 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 263 | assert(isa<FunctionNoProtoType>(AFT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | Proto += "()"; |
| 265 | } |
| 266 | |
| 267 | AFT->getResultType().getAsStringInternal(Proto); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 268 | Out << Proto; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 270 | if (!FD->getBodyIfAvailable()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 271 | Out << ";\n"; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 272 | // Doesn't print the body. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 275 | void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 276 | std::string S = TD->getNameAsString(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | TD->getUnderlyingType().getAsStringInternal(S); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 278 | Out << "typedef " << S << ";\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 281 | void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) { |
| 282 | const char *l; |
| 283 | if (LS->getLanguage() == LinkageSpecDecl::lang_c) |
| 284 | l = "C"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 285 | else { |
| 286 | assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx && |
| 287 | "unknown language in linkage specification"); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 288 | l = "C++"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 289 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 290 | |
| 291 | Out << "extern \"" << l << "\" "; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 292 | if (LS->hasBraces()) { |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 293 | Out << "{\n"; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 294 | ChangeIndent(1); |
| 295 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 296 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 297 | // FIXME: Should not use a NULL DeclContext! |
| 298 | ASTContext *Context = 0; |
| 299 | for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context), |
| 300 | DEnd = LS->decls_end(*Context); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 301 | D != DEnd; ++D) |
| 302 | PrintDecl(*D); |
| 303 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 304 | if (LS->hasBraces()) { |
| 305 | ChangeIndent(-1); |
| 306 | Indent() << "}"; |
| 307 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 308 | Out << "\n"; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 311 | void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 312 | if (OMD->isInstanceMethod()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 313 | Out << "\n- "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 314 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 315 | Out << "\n+ "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 316 | if (!OMD->getResultType().isNull()) |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 317 | Out << '(' << OMD->getResultType().getAsString() << ")"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 319 | std::string name = OMD->getSelector().getAsString(); |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 320 | std::string::size_type pos, lastPos = 0; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 321 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 322 | E = OMD->param_end(); PI != E; ++PI) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 323 | // FIXME: selector is missing here! |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 324 | pos = name.find_first_of(":", lastPos); |
| 325 | Out << " " << name.substr(lastPos, pos - lastPos); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 326 | Out << ":(" << (*PI)->getType().getAsString() << ")" |
| 327 | << (*PI)->getNameAsString(); |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 328 | lastPos = pos + 1; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 331 | if (OMD->param_begin() == OMD->param_end()) |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 332 | Out << " " << name; |
| 333 | |
| 334 | if (OMD->isVariadic()) |
| 335 | Out << ", ..."; |
| 336 | |
| 337 | Out << ";"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 340 | void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 341 | std::string I = OID->getNameAsString(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 342 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 343 | |
| 344 | if (SID) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 345 | Out << "@implementation " << I << " : " << SID->getNameAsString(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 346 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 347 | Out << "@implementation " << I; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 349 | for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 350 | E = OID->instmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 351 | ObjCMethodDecl *OMD = *I; |
| 352 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 353 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 354 | Out << ' '; |
| 355 | OMD->getBody()->printPretty(Out); |
| 356 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 360 | for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 361 | E = OID->classmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 362 | ObjCMethodDecl *OMD = *I; |
| 363 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 364 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 365 | Out << ' '; |
| 366 | OMD->getBody()->printPretty(Out); |
| 367 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 371 | for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(), |
| 372 | E = OID->propimpl_end(); I != E; ++I) |
| 373 | PrintObjCPropertyImplDecl(*I); |
| 374 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 375 | Out << "@end\n"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 379 | void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 380 | std::string I = OID->getNameAsString(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 381 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 382 | |
| 383 | if (SID) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 384 | Out << "@interface " << I << " : " << SID->getNameAsString(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 385 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 386 | Out << "@interface " << I; |
| 387 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 388 | // Protocols? |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 389 | const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); |
| 390 | if (!Protocols.empty()) { |
| 391 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 392 | E = Protocols.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 393 | Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 394 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 396 | if (!Protocols.empty()) |
| 397 | Out << ">"; |
| 398 | Out << '\n'; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 399 | |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 400 | if (OID->ivar_size() > 0) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 401 | Out << '{'; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 402 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 403 | E = OID->ivar_end(); I != E; ++I) { |
| 404 | Out << '\t' << (*I)->getType().getAsString() |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 405 | << ' ' << (*I)->getNameAsString() << ";\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 406 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 407 | Out << "}\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 408 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 410 | // FIXME: Should not use a NULL DeclContext! |
| 411 | ASTContext *Context = 0; |
| 412 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context), |
| 413 | E = OID->prop_end(*Context); I != E; ++I) |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 414 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 415 | bool eol_needed = false; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 416 | for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context), |
| 417 | E = OID->classmeth_end(*Context); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 418 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 419 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 420 | for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context), |
| 421 | E = OID->instmeth_end(*Context); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 422 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 423 | |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 424 | Out << (eol_needed ? "\n@end\n" : "@end\n"); |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 425 | // FIXME: implement the rest... |
| 426 | } |
| 427 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 428 | void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 429 | Out << "@protocol " << PID->getNameAsString() << '\n'; |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 430 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 431 | // FIXME: Should not use a NULL DeclContext! |
| 432 | ASTContext *Context = 0; |
| 433 | for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context), |
| 434 | E = PID->prop_end(*Context); I != E; ++I) |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 435 | PrintObjCPropertyDecl(*I); |
| 436 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 437 | // FIXME: implement the rest... |
| 438 | } |
| 439 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 440 | void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 441 | Out << "@implementation " |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 442 | << PID->getClassInterface()->getNameAsString() |
| 443 | << '(' << PID->getNameAsString() << ");\n"; |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 444 | for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(), |
| 445 | E = PID->propimpl_end(); I != E; ++I) |
| 446 | PrintObjCPropertyImplDecl(*I); |
| 447 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 448 | // FIXME: implement the rest... |
| 449 | } |
| 450 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 451 | void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 452 | // FIXME: Should not use a NULL DeclContext! |
| 453 | ASTContext *Context = 0; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 454 | Out << "@interface " |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 455 | << PID->getClassInterface()->getNameAsString() |
| 456 | << '(' << PID->getNameAsString() << ");\n"; |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 457 | // Output property declarations. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 458 | for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context), |
| 459 | E = PID->prop_end(*Context); I != E; ++I) |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 460 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 461 | Out << "@end\n"; |
| 462 | |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 463 | // FIXME: implement the rest... |
| 464 | } |
| 465 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 466 | void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 467 | Out << "@compatibility_alias " << AID->getNameAsString() |
| 468 | << ' ' << AID->getClassInterface()->getNameAsString() << ";\n"; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 471 | /// PrintObjCPropertyDecl - print a property declaration. |
| 472 | /// |
| 473 | void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 474 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 475 | Out << "@required\n"; |
| 476 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 477 | Out << "@optional\n"; |
| 478 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 479 | Out << "@property"; |
| 480 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 481 | bool first = true; |
| 482 | Out << " ("; |
| 483 | if (PDecl->getPropertyAttributes() & |
| 484 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 485 | Out << (first ? ' ' : ',') << "readonly"; |
| 486 | first = false; |
| 487 | } |
| 488 | |
| 489 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 490 | Out << (first ? ' ' : ',') << "getter = " |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 491 | << PDecl->getGetterName().getAsString(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 492 | first = false; |
| 493 | } |
| 494 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 495 | Out << (first ? ' ' : ',') << "setter = " |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 496 | << PDecl->getSetterName().getAsString(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 497 | first = false; |
| 498 | } |
| 499 | |
| 500 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 501 | Out << (first ? ' ' : ',') << "assign"; |
| 502 | first = false; |
| 503 | } |
| 504 | |
| 505 | if (PDecl->getPropertyAttributes() & |
| 506 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 507 | Out << (first ? ' ' : ',') << "readwrite"; |
| 508 | first = false; |
| 509 | } |
| 510 | |
| 511 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 512 | Out << (first ? ' ' : ',') << "retain"; |
| 513 | first = false; |
| 514 | } |
| 515 | |
| 516 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 517 | Out << (first ? ' ' : ',') << "copy"; |
| 518 | first = false; |
| 519 | } |
| 520 | |
| 521 | if (PDecl->getPropertyAttributes() & |
| 522 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| 523 | Out << (first ? ' ' : ',') << "nonatomic"; |
| 524 | first = false; |
| 525 | } |
| 526 | Out << " )"; |
| 527 | } |
| 528 | Out << ' ' << PDecl->getType().getAsString() |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 529 | << ' ' << PDecl->getNameAsString(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 530 | |
| 531 | Out << ";\n"; |
| 532 | } |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 533 | |
| 534 | /// PrintObjCPropertyImplDecl - Print an objective-c property implementation |
| 535 | /// declaration syntax. |
| 536 | /// |
| 537 | void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 538 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 539 | Out << "\n@synthesize "; |
| 540 | else |
| 541 | Out << "\n@dynamic "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 542 | Out << PID->getPropertyDecl()->getNameAsString(); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 543 | if (PID->getPropertyIvarDecl()) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 544 | Out << "=" << PID->getPropertyIvarDecl()->getNameAsString(); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 545 | Out << ";\n"; |
| 546 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 547 | |
| 548 | /// PrintTemplateParams - Print a template parameter list and recursively print |
| 549 | /// it's underlying top-level definition. |
| 550 | void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) { |
| 551 | // TODO: Write template parameters. |
| 552 | Out << "template <...> "; |
| 553 | PrintDecl(TD->getTemplatedDecl()); |
| 554 | } |
| 555 | |
| 556 | |
| 557 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 558 | //===----------------------------------------------------------------------===// |
| 559 | /// ASTPrinter - Pretty-printer of ASTs |
| 560 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 561 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 562 | class ASTPrinter : public ASTConsumer, public DeclPrinter { |
| 563 | public: |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 564 | ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 566 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 567 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 568 | PrintDecl(*I); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 569 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 570 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 571 | } // end anonymous namespace |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 572 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 573 | ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 574 | return new ASTPrinter(out); |
| 575 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 576 | |
| 577 | //===----------------------------------------------------------------------===// |
| 578 | /// ASTDumper - Low-level dumper of ASTs |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 579 | |
| 580 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 581 | class ASTDumper : public ASTConsumer, public DeclPrinter { |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 582 | SourceManager *SM; |
| 583 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 584 | ASTDumper() : DeclPrinter() {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 585 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 586 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 587 | SM = &Context.getSourceManager(); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 588 | } |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 590 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 591 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 592 | HandleTopLevelSingleDecl(*I); |
| 593 | } |
| 594 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 595 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 596 | } // end anonymous namespace |
| 597 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 598 | void ASTDumper::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 599 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 600 | PrintFunctionDeclStart(FD); |
| 601 | |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 602 | if (FD->getBodyIfAvailable()) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 603 | Out << '\n'; |
| 604 | // FIXME: convert dumper to use std::ostream? |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 605 | FD->getBodyIfAvailable()->dumpAll(*SM); |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 606 | Out << '\n'; |
| 607 | } |
| 608 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 609 | PrintTypeDefDecl(TD); |
| 610 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 611 | Out << "Read objc interface '" << OID->getNameAsString() << "'\n"; |
| 612 | } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 613 | Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n"; |
| 614 | } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 615 | Out << "Read objc category '" << OCD->getNameAsString() << "'\n"; |
| 616 | } else if (isa<ObjCForwardProtocolDecl>(D)) { |
| 617 | Out << "Read objc fwd protocol decl\n"; |
| 618 | } else if (isa<ObjCClassDecl>(D)) { |
| 619 | Out << "Read objc fwd class decl\n"; |
| 620 | } else if (isa<FileScopeAsmDecl>(D)) { |
| 621 | Out << "Read file scope asm decl\n"; |
| 622 | } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 623 | Out << "Read objc method decl: '" << MD->getSelector().getAsString() |
| 624 | << "'\n"; |
| 625 | if (MD->getBody()) { |
| 626 | // FIXME: convert dumper to use std::ostream? |
| 627 | MD->getBody()->dumpAll(*SM); |
| 628 | Out << '\n'; |
| 629 | } |
| 630 | } else if (isa<ObjCImplementationDecl>(D)) { |
| 631 | Out << "Read objc implementation decl\n"; |
| 632 | } else if (isa<ObjCCategoryImplDecl>(D)) { |
| 633 | Out << "Read objc category implementation decl\n"; |
| 634 | } else if (isa<LinkageSpecDecl>(D)) { |
| 635 | Out << "Read linkage spec decl\n"; |
| 636 | } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 637 | Out << "Read top-level variable decl: '" << ND->getNameAsString() |
| 638 | << "'\n"; |
| 639 | } else { |
| 640 | assert(0 && "Unknown decl type!"); |
| 641 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 644 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 645 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 646 | //===----------------------------------------------------------------------===// |
| 647 | /// ASTViewer - AST Visualization |
| 648 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 649 | namespace { |
| 650 | class ASTViewer : public ASTConsumer { |
| 651 | SourceManager *SM; |
| 652 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 653 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 654 | SM = &Context.getSourceManager(); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 655 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 656 | |
| 657 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 658 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 659 | HandleTopLevelSingleDecl(*I); |
| 660 | } |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 662 | void HandleTopLevelSingleDecl(Decl *D); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 663 | }; |
| 664 | } |
| 665 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 666 | void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 667 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 668 | DeclPrinter().PrintFunctionDeclStart(FD); |
| 669 | |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 670 | if (FD->getBodyIfAvailable()) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 671 | llvm::cerr << '\n'; |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 672 | FD->getBodyIfAvailable()->viewAST(); |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 673 | llvm::cerr << '\n'; |
| 674 | } |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 679 | DeclPrinter().PrintObjCMethodDecl(MD); |
| 680 | |
| 681 | if (MD->getBody()) { |
| 682 | llvm::cerr << '\n'; |
| 683 | MD->getBody()->viewAST(); |
| 684 | llvm::cerr << '\n'; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 690 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 691 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 692 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 693 | /// DeclContextPrinter - Decl and DeclContext Visualization |
| 694 | |
| 695 | namespace { |
| 696 | |
| 697 | class DeclContextPrinter : public ASTConsumer { |
| 698 | llvm::raw_ostream& Out; |
| 699 | public: |
| 700 | DeclContextPrinter() : Out(llvm::errs()) {} |
| 701 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 702 | void HandleTranslationUnit(ASTContext &C) { |
| 703 | PrintDeclContext(C.getTranslationUnitDecl(), 4); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | void PrintDeclContext(const DeclContext* DC, unsigned Indentation); |
| 707 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 708 | } // end anonymous namespace |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 709 | |
| 710 | void DeclContextPrinter::PrintDeclContext(const DeclContext* DC, |
| 711 | unsigned Indentation) { |
| 712 | // Print DeclContext name. |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 713 | switch (DC->getDeclKind()) { |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 714 | case Decl::TranslationUnit: |
| 715 | Out << "[translation unit] " << DC; |
| 716 | break; |
| 717 | case Decl::Namespace: { |
| 718 | Out << "[namespace] "; |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 719 | const NamespaceDecl* ND = cast<NamespaceDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 720 | Out << ND->getNameAsString(); |
| 721 | break; |
| 722 | } |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 723 | case Decl::Enum: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 724 | const EnumDecl* ED = cast<EnumDecl>(DC); |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 725 | if (ED->isDefinition()) |
| 726 | Out << "[enum] "; |
| 727 | else |
| 728 | Out << "<enum> "; |
| 729 | Out << ED->getNameAsString(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 730 | break; |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 731 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 732 | case Decl::Record: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 733 | const RecordDecl* RD = cast<RecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 734 | if (RD->isDefinition()) |
| 735 | Out << "[struct] "; |
| 736 | else |
| 737 | Out << "<struct> "; |
| 738 | Out << RD->getNameAsString(); |
| 739 | break; |
| 740 | } |
| 741 | case Decl::CXXRecord: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 742 | const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 743 | if (RD->isDefinition()) |
| 744 | Out << "[class] "; |
| 745 | else |
| 746 | Out << "<class> "; |
| 747 | Out << RD->getNameAsString() << " " << DC; |
| 748 | break; |
| 749 | } |
| 750 | case Decl::ObjCMethod: |
| 751 | Out << "[objc method]"; |
| 752 | break; |
| 753 | case Decl::ObjCInterface: |
| 754 | Out << "[objc interface]"; |
| 755 | break; |
| 756 | case Decl::ObjCCategory: |
| 757 | Out << "[objc category]"; |
| 758 | break; |
| 759 | case Decl::ObjCProtocol: |
| 760 | Out << "[objc protocol]"; |
| 761 | break; |
| 762 | case Decl::ObjCImplementation: |
| 763 | Out << "[objc implementation]"; |
| 764 | break; |
| 765 | case Decl::ObjCCategoryImpl: |
| 766 | Out << "[objc categoryimpl]"; |
| 767 | break; |
| 768 | case Decl::LinkageSpec: |
| 769 | Out << "[linkage spec]"; |
| 770 | break; |
| 771 | case Decl::Block: |
| 772 | Out << "[block]"; |
| 773 | break; |
| 774 | case Decl::Function: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 775 | const FunctionDecl* FD = cast<FunctionDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 776 | if (FD->isThisDeclarationADefinition()) |
| 777 | Out << "[function] "; |
| 778 | else |
| 779 | Out << "<function> "; |
| 780 | Out << FD->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 781 | // Print the parameters. |
| 782 | Out << "("; |
| 783 | bool PrintComma = false; |
| 784 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
| 785 | E = FD->param_end(); I != E; ++I) { |
| 786 | if (PrintComma) |
| 787 | Out << ", "; |
| 788 | else |
| 789 | PrintComma = true; |
| 790 | Out << (*I)->getNameAsString(); |
| 791 | } |
| 792 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 793 | break; |
| 794 | } |
| 795 | case Decl::CXXMethod: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 796 | const CXXMethodDecl* D = cast<CXXMethodDecl>(DC); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 797 | if (D->isOutOfLineDefinition()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 798 | Out << "[c++ method] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 799 | else if (D->isImplicit()) |
| 800 | Out << "(c++ method) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 801 | else |
| 802 | Out << "<c++ method> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 803 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 804 | // Print the parameters. |
| 805 | Out << "("; |
| 806 | bool PrintComma = false; |
| 807 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
| 808 | E = D->param_end(); I != E; ++I) { |
| 809 | if (PrintComma) |
| 810 | Out << ", "; |
| 811 | else |
| 812 | PrintComma = true; |
| 813 | Out << (*I)->getNameAsString(); |
| 814 | } |
| 815 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 816 | |
| 817 | // Check the semantic DeclContext. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 818 | const DeclContext* SemaDC = D->getDeclContext(); |
| 819 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 820 | if (SemaDC != LexicalDC) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 821 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 822 | |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 823 | break; |
| 824 | } |
| 825 | case Decl::CXXConstructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 826 | const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 827 | if (D->isOutOfLineDefinition()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 828 | Out << "[c++ ctor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 829 | else if (D->isImplicit()) |
| 830 | Out << "(c++ ctor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 831 | else |
| 832 | Out << "<c++ ctor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 833 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 834 | // Print the parameters. |
| 835 | Out << "("; |
| 836 | bool PrintComma = false; |
| 837 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
| 838 | E = D->param_end(); I != E; ++I) { |
| 839 | if (PrintComma) |
| 840 | Out << ", "; |
| 841 | else |
| 842 | PrintComma = true; |
| 843 | Out << (*I)->getNameAsString(); |
| 844 | } |
| 845 | Out << ")"; |
| 846 | |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 847 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 848 | const DeclContext* SemaDC = D->getDeclContext(); |
| 849 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 850 | if (SemaDC != LexicalDC) |
| 851 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 852 | break; |
| 853 | } |
| 854 | case Decl::CXXDestructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 855 | const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 856 | if (D->isOutOfLineDefinition()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 857 | Out << "[c++ dtor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 858 | else if (D->isImplicit()) |
| 859 | Out << "(c++ dtor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 860 | else |
| 861 | Out << "<c++ dtor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 862 | Out << D->getNameAsString(); |
| 863 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 864 | const DeclContext* SemaDC = D->getDeclContext(); |
| 865 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 866 | if (SemaDC != LexicalDC) |
| 867 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 868 | break; |
| 869 | } |
| 870 | case Decl::CXXConversion: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 871 | const CXXConversionDecl* D = cast<CXXConversionDecl>(DC); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 872 | if (D->isOutOfLineDefinition()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 873 | Out << "[c++ conversion] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 874 | else if (D->isImplicit()) |
| 875 | Out << "(c++ conversion) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 876 | else |
| 877 | Out << "<c++ conversion> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 878 | Out << D->getNameAsString(); |
| 879 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 880 | const DeclContext* SemaDC = D->getDeclContext(); |
| 881 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 882 | if (SemaDC != LexicalDC) |
| 883 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 884 | break; |
| 885 | } |
| 886 | |
| 887 | default: |
| 888 | assert(0 && "a decl that inherits DeclContext isn't handled"); |
| 889 | } |
| 890 | |
| 891 | Out << "\n"; |
| 892 | |
| 893 | // Print decls in the DeclContext. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 894 | // FIXME: Should not use a NULL DeclContext! |
| 895 | ASTContext *Context = 0; |
| 896 | for (DeclContext::decl_iterator I = DC->decls_begin(*Context), |
| 897 | E = DC->decls_end(*Context); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 898 | I != E; ++I) { |
| 899 | for (unsigned i = 0; i < Indentation; ++i) |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 900 | Out << " "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 901 | |
| 902 | Decl::Kind DK = I->getKind(); |
| 903 | switch (DK) { |
| 904 | case Decl::Namespace: |
| 905 | case Decl::Enum: |
| 906 | case Decl::Record: |
| 907 | case Decl::CXXRecord: |
| 908 | case Decl::ObjCMethod: |
| 909 | case Decl::ObjCInterface: |
| 910 | case Decl::ObjCCategory: |
| 911 | case Decl::ObjCProtocol: |
| 912 | case Decl::ObjCImplementation: |
| 913 | case Decl::ObjCCategoryImpl: |
| 914 | case Decl::LinkageSpec: |
| 915 | case Decl::Block: |
| 916 | case Decl::Function: |
| 917 | case Decl::CXXMethod: |
| 918 | case Decl::CXXConstructor: |
| 919 | case Decl::CXXDestructor: |
| 920 | case Decl::CXXConversion: |
| 921 | { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 922 | DeclContext* DC = cast<DeclContext>(*I); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 923 | PrintDeclContext(DC, Indentation+2); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 924 | break; |
| 925 | } |
| 926 | case Decl::Field: { |
| 927 | FieldDecl* FD = cast<FieldDecl>(*I); |
| 928 | Out << "<field> " << FD->getNameAsString() << "\n"; |
| 929 | break; |
| 930 | } |
| 931 | case Decl::Typedef: { |
| 932 | TypedefDecl* TD = cast<TypedefDecl>(*I); |
| 933 | Out << "<typedef> " << TD->getNameAsString() << "\n"; |
| 934 | break; |
| 935 | } |
| 936 | case Decl::EnumConstant: { |
| 937 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I); |
| 938 | Out << "<enum constant> " << ECD->getNameAsString() << "\n"; |
| 939 | break; |
| 940 | } |
| 941 | case Decl::Var: { |
| 942 | VarDecl* VD = cast<VarDecl>(*I); |
| 943 | Out << "<var> " << VD->getNameAsString() << "\n"; |
| 944 | break; |
| 945 | } |
| 946 | case Decl::ImplicitParam: { |
| 947 | ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I); |
| 948 | Out << "<implicit parameter> " << IPD->getNameAsString() << "\n"; |
| 949 | break; |
| 950 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 951 | case Decl::ParmVar: { |
| 952 | ParmVarDecl* PVD = cast<ParmVarDecl>(*I); |
| 953 | Out << "<parameter> " << PVD->getNameAsString() << "\n"; |
| 954 | break; |
| 955 | } |
Zhongxing Xu | ba16be9 | 2009-04-05 02:04:38 +0000 | [diff] [blame] | 956 | case Decl::OriginalParmVar: { |
| 957 | OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I); |
| 958 | Out << "<original parameter> " << OPVD->getNameAsString() << "\n"; |
| 959 | break; |
| 960 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 961 | case Decl::ObjCProperty: { |
| 962 | ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I); |
| 963 | Out << "<objc property> " << OPD->getNameAsString() << "\n"; |
| 964 | break; |
| 965 | } |
| 966 | default: |
Zhongxing Xu | ba16be9 | 2009-04-05 02:04:38 +0000 | [diff] [blame] | 967 | fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName()); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 968 | assert(0 && "decl unhandled"); |
| 969 | } |
| 970 | } |
| 971 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 972 | ASTConsumer *clang::CreateDeclContextPrinter() { |
| 973 | return new DeclContextPrinter(); |
| 974 | } |
| 975 | |
| 976 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 977 | /// InheritanceViewer - C++ Inheritance Visualization |
| 978 | |
| 979 | namespace { |
| 980 | class InheritanceViewer : public ASTConsumer { |
| 981 | const std::string clsname; |
| 982 | public: |
| 983 | InheritanceViewer(const std::string& cname) : clsname(cname) {} |
| 984 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 985 | void HandleTranslationUnit(ASTContext &C) { |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 986 | for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I) |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 987 | if (RecordType *T = dyn_cast<RecordType>(*I)) { |
| 988 | if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) { |
| 989 | // FIXME: This lookup needs to be generalized to handle namespaces and |
| 990 | // (when we support them) templates. |
| 991 | if (D->getNameAsString() == clsname) { |
| 992 | D->viewInheritance(C); |
| 993 | } |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | } |
| 997 | }; |
| 998 | } |
| 999 | |
| 1000 | ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) { |
| 1001 | return new InheritanceViewer(clsname); |
| 1002 | } |
| 1003 | |
| 1004 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1005 | // AST Serializer |
| 1006 | |
| 1007 | namespace { |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1008 | |
| 1009 | class ASTSerializer : public ASTConsumer { |
| 1010 | protected: |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 1011 | Diagnostic& Diags; |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1012 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1013 | public: |
Nico Weber | be1eb5c | 2008-08-10 19:20:05 +0000 | [diff] [blame] | 1014 | ASTSerializer(Diagnostic& diags) : Diags(diags) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1015 | }; |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame] | 1016 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1017 | class SingleFileSerializer : public ASTSerializer { |
| 1018 | const llvm::sys::Path FName; |
| 1019 | public: |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 1020 | SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags) |
| 1021 | : ASTSerializer(diags), FName(F) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 1023 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 1024 | if (Diags.hasErrorOccurred()) |
| 1025 | return; |
Chris Lattner | 80a0333 | 2009-03-28 03:53:02 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Reserve 256K for bitstream buffer. |
| 1028 | std::vector<unsigned char> Buffer; |
| 1029 | Buffer.reserve(256*1024); |
| 1030 | |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 1031 | Ctx.EmitASTBitcodeBuffer(Buffer); |
Chris Lattner | 80a0333 | 2009-03-28 03:53:02 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Write the bits to disk. |
| 1034 | if (FILE* fp = fopen(FName.c_str(),"wb")) { |
| 1035 | fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp); |
| 1036 | fclose(fp); |
| 1037 | } |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1038 | } |
| 1039 | }; |
| 1040 | |
| 1041 | class BuildSerializer : public ASTSerializer { |
| 1042 | llvm::sys::Path EmitDir; |
| 1043 | public: |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 1044 | BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags) |
| 1045 | : ASTSerializer(diags), EmitDir(dir) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1046 | |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 1047 | virtual void HandleTranslationUnit(ASTContext &Ctx); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1048 | }; |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1049 | } // end anonymous namespace |
| 1050 | |
| 1051 | |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 1052 | void BuildSerializer::HandleTranslationUnit(ASTContext &Ctx) { |
| 1053 | if (Diags.hasErrorOccurred()) |
| 1054 | return; |
| 1055 | |
| 1056 | SourceManager& SourceMgr = Ctx.getSourceManager(); |
| 1057 | FileID ID = SourceMgr.getMainFileID(); |
| 1058 | assert(!ID.isInvalid() && "MainFileID not set!"); |
| 1059 | const FileEntry* FE = SourceMgr.getFileEntryForID(ID); |
| 1060 | assert(FE && "No FileEntry for main file."); |
| 1061 | |
| 1062 | // FIXME: This is not portable to Windows. |
| 1063 | // FIXME: This logic should probably be moved elsewhere later. |
| 1064 | |
| 1065 | llvm::sys::Path FName(EmitDir); |
| 1066 | |
| 1067 | std::vector<char> buf; |
| 1068 | buf.reserve(strlen(FE->getName())+100); |
| 1069 | |
| 1070 | sprintf(&buf[0], "dev_%llx", (unsigned long long) FE->getDevice()); |
| 1071 | FName.appendComponent(&buf[0]); |
| 1072 | FName.createDirectoryOnDisk(true); |
| 1073 | if (!FName.canWrite() || !FName.isDirectory()) { |
| 1074 | assert (false && "Could not create 'device' serialization directory."); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | sprintf(&buf[0], "%s-%llX.ast", FE->getName(), |
| 1079 | (unsigned long long) FE->getInode()); |
| 1080 | FName.appendComponent(&buf[0]); |
| 1081 | |
| 1082 | |
| 1083 | // Reserve 256K for bitstream buffer. |
| 1084 | std::vector<unsigned char> Buffer; |
| 1085 | Buffer.reserve(256*1024); |
| 1086 | |
| 1087 | Ctx.EmitASTBitcodeBuffer(Buffer); |
| 1088 | |
| 1089 | // Write the bits to disk. |
| 1090 | if (FILE* fp = fopen(FName.c_str(),"wb")) { |
| 1091 | fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp); |
| 1092 | fclose(fp); |
| 1093 | } |
| 1094 | |
| 1095 | // Now emit the sources. |
| 1096 | |
| 1097 | } |
| 1098 | |
| 1099 | |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1100 | ASTConsumer* clang::CreateASTSerializer(const std::string& InFile, |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1101 | const std::string& OutputFile, |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1102 | Diagnostic &Diags) { |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 1103 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1104 | if (OutputFile.size()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 1105 | if (InFile == "-") { |
| 1106 | llvm::cerr << |
| 1107 | "error: Cannot use --serialize with -o for source read from STDIN.\n"; |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1111 | // The user specified an AST-emission directory. Determine if the path |
| 1112 | // is absolute. |
| 1113 | llvm::sys::Path EmitDir(OutputFile); |
| 1114 | |
| 1115 | if (!EmitDir.isAbsolute()) { |
| 1116 | llvm::cerr << |
| 1117 | "error: Output directory for --serialize must be an absolute path.\n"; |
| 1118 | |
| 1119 | return NULL; |
| 1120 | } |
| 1121 | |
| 1122 | // Create the directory if it does not exist. |
| 1123 | EmitDir.createDirectoryOnDisk(true); |
| 1124 | if (!EmitDir.canWrite() || !EmitDir.isDirectory()) { |
| 1125 | llvm::cerr << |
| 1126 | "error: Could not create output directory for --serialize.\n"; |
| 1127 | |
| 1128 | return NULL; |
| 1129 | } |
| 1130 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 1131 | // FIXME: We should probably only allow using BuildSerializer when |
| 1132 | // the ASTs come from parsed source files, and not from .ast files. |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 1133 | return new BuildSerializer(EmitDir, Diags); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | // The user did not specify an output directory for serialized ASTs. |
| 1137 | // Serialize the translation to a single file whose name is the same |
| 1138 | // as the input file with the ".ast" extension appended. |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 1139 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 1140 | llvm::sys::Path FName(InFile.c_str()); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 1141 | FName.appendSuffix("ast"); |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 1142 | return new SingleFileSerializer(FName, Diags); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1143 | } |