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 | |
Eli Friedman | 39d7c4d | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTConsumers.h" |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/AST.h" |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 22 | #include "clang/AST/PrettyPrinter.h" |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Timer.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 27 | using namespace clang; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 30 | /// ASTPrinter - Pretty-printer and dumper of ASTs |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 32 | namespace { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 33 | class ASTPrinter : public ASTConsumer { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 34 | raw_ostream &Out; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 35 | bool Dump; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 37 | public: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 38 | ASTPrinter(raw_ostream* o = NULL, bool Dump = false) |
Argyrios Kyrtzidis | bc1e146 | 2010-08-03 17:29:57 +0000 | [diff] [blame] | 39 | : Out(o? *o : llvm::outs()), Dump(Dump) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 41 | virtual void HandleTranslationUnit(ASTContext &Context) { |
| 42 | PrintingPolicy Policy = Context.PrintingPolicy; |
| 43 | Policy.Dump = Dump; |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 44 | Context.getTranslationUnitDecl()->print(Out, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 46 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 47 | } // end anonymous namespace |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 49 | ASTConsumer *clang::CreateASTPrinter(raw_ostream* out) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 50 | return new ASTPrinter(out); |
| 51 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 52 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | ASTConsumer *clang::CreateASTDumper() { |
| 54 | return new ASTPrinter(0, true); |
Douglas Gregor | 609e72f | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 55 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
| 58 | /// ASTViewer - AST Visualization |
| 59 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 60 | namespace { |
| 61 | class ASTViewer : public ASTConsumer { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 62 | ASTContext *Context; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 63 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 64 | void Initialize(ASTContext &Context) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 65 | this->Context = &Context; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 67 | |
| 68 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 69 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 70 | HandleTopLevelSingleDecl(*I); |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 73 | void HandleTopLevelSingleDecl(Decl *D); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 74 | }; |
| 75 | } |
| 76 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 77 | void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { |
Argyrios Kyrtzidis | 9d96f92 | 2010-07-07 11:31:23 +0000 | [diff] [blame] | 78 | if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 79 | D->print(llvm::errs()); |
| 80 | |
| 81 | if (Stmt *Body = D->getBody()) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 82 | llvm::errs() << '\n'; |
Douglas Gregor | af3280f | 2009-09-12 00:08:48 +0000 | [diff] [blame] | 83 | Body->viewAST(); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 84 | llvm::errs() << '\n'; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 90 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 91 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 92 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 93 | /// DeclContextPrinter - Decl and DeclContext Visualization |
| 94 | |
| 95 | namespace { |
| 96 | |
| 97 | class DeclContextPrinter : public ASTConsumer { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 98 | raw_ostream& Out; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 99 | public: |
| 100 | DeclContextPrinter() : Out(llvm::errs()) {} |
| 101 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 102 | void HandleTranslationUnit(ASTContext &C) { |
| 103 | PrintDeclContext(C.getTranslationUnitDecl(), 4); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void PrintDeclContext(const DeclContext* DC, unsigned Indentation); |
| 107 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 108 | } // end anonymous namespace |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 109 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | void DeclContextPrinter::PrintDeclContext(const DeclContext* DC, |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 111 | unsigned Indentation) { |
| 112 | // Print DeclContext name. |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 113 | switch (DC->getDeclKind()) { |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 114 | case Decl::TranslationUnit: |
| 115 | Out << "[translation unit] " << DC; |
| 116 | break; |
| 117 | case Decl::Namespace: { |
| 118 | Out << "[namespace] "; |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 119 | const NamespaceDecl* ND = cast<NamespaceDecl>(DC); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 120 | Out << ND; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 121 | break; |
| 122 | } |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 123 | case Decl::Enum: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 124 | const EnumDecl* ED = cast<EnumDecl>(DC); |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 125 | if (ED->isDefinition()) |
| 126 | Out << "[enum] "; |
| 127 | else |
| 128 | Out << "<enum> "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 129 | Out << ED; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 130 | break; |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 131 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 132 | case Decl::Record: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 133 | const RecordDecl* RD = cast<RecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 134 | if (RD->isDefinition()) |
| 135 | Out << "[struct] "; |
| 136 | else |
| 137 | Out << "<struct> "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 138 | Out << RD; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | case Decl::CXXRecord: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 142 | const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 143 | if (RD->isDefinition()) |
| 144 | Out << "[class] "; |
| 145 | else |
| 146 | Out << "<class> "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 147 | Out << RD << ' ' << DC; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | case Decl::ObjCMethod: |
| 151 | Out << "[objc method]"; |
| 152 | break; |
| 153 | case Decl::ObjCInterface: |
| 154 | Out << "[objc interface]"; |
| 155 | break; |
| 156 | case Decl::ObjCCategory: |
| 157 | Out << "[objc category]"; |
| 158 | break; |
| 159 | case Decl::ObjCProtocol: |
| 160 | Out << "[objc protocol]"; |
| 161 | break; |
| 162 | case Decl::ObjCImplementation: |
| 163 | Out << "[objc implementation]"; |
| 164 | break; |
| 165 | case Decl::ObjCCategoryImpl: |
| 166 | Out << "[objc categoryimpl]"; |
| 167 | break; |
| 168 | case Decl::LinkageSpec: |
| 169 | Out << "[linkage spec]"; |
| 170 | break; |
| 171 | case Decl::Block: |
| 172 | Out << "[block]"; |
| 173 | break; |
| 174 | case Decl::Function: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 175 | const FunctionDecl* FD = cast<FunctionDecl>(DC); |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 176 | if (FD->doesThisDeclarationHaveABody()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 177 | Out << "[function] "; |
| 178 | else |
| 179 | Out << "<function> "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 180 | Out << FD; |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 181 | // Print the parameters. |
| 182 | Out << "("; |
| 183 | bool PrintComma = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 185 | E = FD->param_end(); I != E; ++I) { |
| 186 | if (PrintComma) |
| 187 | Out << ", "; |
| 188 | else |
| 189 | PrintComma = true; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 190 | Out << *I; |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 191 | } |
| 192 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | case Decl::CXXMethod: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 196 | const CXXMethodDecl* D = cast<CXXMethodDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 197 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 198 | Out << "[c++ method] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 199 | else if (D->isImplicit()) |
| 200 | Out << "(c++ method) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 201 | else |
| 202 | Out << "<c++ method> "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 203 | Out << D; |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 204 | // Print the parameters. |
| 205 | Out << "("; |
| 206 | bool PrintComma = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 208 | E = D->param_end(); I != E; ++I) { |
| 209 | if (PrintComma) |
| 210 | Out << ", "; |
| 211 | else |
| 212 | PrintComma = true; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 213 | Out << *I; |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 214 | } |
| 215 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 216 | |
| 217 | // Check the semantic DeclContext. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 218 | const DeclContext* SemaDC = D->getDeclContext(); |
| 219 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 220 | if (SemaDC != LexicalDC) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 221 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 222 | |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | case Decl::CXXConstructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 226 | const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 227 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 228 | Out << "[c++ ctor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 229 | else if (D->isImplicit()) |
| 230 | Out << "(c++ ctor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 231 | else |
| 232 | Out << "<c++ ctor> "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 233 | Out << D; |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 234 | // Print the parameters. |
| 235 | Out << "("; |
| 236 | bool PrintComma = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 238 | E = D->param_end(); I != E; ++I) { |
| 239 | if (PrintComma) |
| 240 | Out << ", "; |
| 241 | else |
| 242 | PrintComma = true; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 243 | Out << *I; |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 244 | } |
| 245 | Out << ")"; |
| 246 | |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 247 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 248 | const DeclContext* SemaDC = D->getDeclContext(); |
| 249 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 250 | if (SemaDC != LexicalDC) |
| 251 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 252 | break; |
| 253 | } |
| 254 | case Decl::CXXDestructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 255 | const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 256 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 257 | Out << "[c++ dtor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 258 | else if (D->isImplicit()) |
| 259 | Out << "(c++ dtor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 260 | else |
| 261 | Out << "<c++ dtor> "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 262 | Out << D; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 263 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 264 | const DeclContext* SemaDC = D->getDeclContext(); |
| 265 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 266 | if (SemaDC != LexicalDC) |
| 267 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 268 | break; |
| 269 | } |
| 270 | case Decl::CXXConversion: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 271 | const CXXConversionDecl* D = cast<CXXConversionDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 272 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 273 | Out << "[c++ conversion] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 274 | else if (D->isImplicit()) |
| 275 | Out << "(c++ conversion) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 276 | else |
| 277 | Out << "<c++ conversion> "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 278 | Out << D; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 279 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 280 | const DeclContext* SemaDC = D->getDeclContext(); |
| 281 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 282 | if (SemaDC != LexicalDC) |
| 283 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | |
| 287 | default: |
| 288 | assert(0 && "a decl that inherits DeclContext isn't handled"); |
| 289 | } |
| 290 | |
| 291 | Out << "\n"; |
| 292 | |
| 293 | // Print decls in the DeclContext. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 294 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 295 | I != E; ++I) { |
| 296 | for (unsigned i = 0; i < Indentation; ++i) |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 297 | Out << " "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 298 | |
| 299 | Decl::Kind DK = I->getKind(); |
| 300 | switch (DK) { |
| 301 | case Decl::Namespace: |
| 302 | case Decl::Enum: |
| 303 | case Decl::Record: |
| 304 | case Decl::CXXRecord: |
| 305 | case Decl::ObjCMethod: |
| 306 | case Decl::ObjCInterface: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | case Decl::ObjCCategory: |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 308 | case Decl::ObjCProtocol: |
| 309 | case Decl::ObjCImplementation: |
| 310 | case Decl::ObjCCategoryImpl: |
| 311 | case Decl::LinkageSpec: |
| 312 | case Decl::Block: |
| 313 | case Decl::Function: |
| 314 | case Decl::CXXMethod: |
| 315 | case Decl::CXXConstructor: |
| 316 | case Decl::CXXDestructor: |
| 317 | case Decl::CXXConversion: |
| 318 | { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 319 | DeclContext* DC = cast<DeclContext>(*I); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 320 | PrintDeclContext(DC, Indentation+2); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 321 | break; |
| 322 | } |
Francois Pichet | a3d39c0 | 2010-12-21 03:08:02 +0000 | [diff] [blame] | 323 | case Decl::IndirectField: { |
| 324 | IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(*I); |
| 325 | Out << "<IndirectField> " << IFD << '\n'; |
| 326 | break; |
| 327 | } |
Chris Lattner | 21ac10d | 2011-02-18 00:52:55 +0000 | [diff] [blame] | 328 | case Decl::Label: { |
| 329 | LabelDecl *LD = cast<LabelDecl>(*I); |
| 330 | Out << "<Label> " << LD << '\n'; |
| 331 | break; |
| 332 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 333 | case Decl::Field: { |
Chris Lattner | 21ac10d | 2011-02-18 00:52:55 +0000 | [diff] [blame] | 334 | FieldDecl *FD = cast<FieldDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 335 | Out << "<field> " << FD << '\n'; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 336 | break; |
| 337 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 338 | case Decl::Typedef: |
| 339 | case Decl::TypeAlias: { |
| 340 | TypedefNameDecl* TD = cast<TypedefNameDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 341 | Out << "<typedef> " << TD << '\n'; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 342 | break; |
| 343 | } |
| 344 | case Decl::EnumConstant: { |
| 345 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 346 | Out << "<enum constant> " << ECD << '\n'; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 347 | break; |
| 348 | } |
| 349 | case Decl::Var: { |
| 350 | VarDecl* VD = cast<VarDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 351 | Out << "<var> " << VD << '\n'; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 352 | break; |
| 353 | } |
| 354 | case Decl::ImplicitParam: { |
| 355 | ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 356 | Out << "<implicit parameter> " << IPD << '\n'; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 357 | break; |
| 358 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 359 | case Decl::ParmVar: { |
| 360 | ParmVarDecl* PVD = cast<ParmVarDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 361 | Out << "<parameter> " << PVD << '\n'; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | case Decl::ObjCProperty: { |
| 365 | ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 366 | Out << "<objc property> " << OPD << '\n'; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 367 | break; |
| 368 | } |
Eli Friedman | 7ac1c9e | 2009-12-08 06:22:37 +0000 | [diff] [blame] | 369 | case Decl::FunctionTemplate: { |
| 370 | FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 371 | Out << "<function template> " << FTD << '\n'; |
Eli Friedman | 7ac1c9e | 2009-12-08 06:22:37 +0000 | [diff] [blame] | 372 | break; |
| 373 | } |
Eli Friedman | 368a55d | 2010-01-03 02:01:11 +0000 | [diff] [blame] | 374 | case Decl::FileScopeAsm: { |
| 375 | Out << "<file-scope asm>\n"; |
| 376 | break; |
| 377 | } |
| 378 | case Decl::UsingDirective: { |
| 379 | Out << "<using directive>\n"; |
| 380 | break; |
| 381 | } |
| 382 | case Decl::NamespaceAlias: { |
| 383 | NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 384 | Out << "<namespace alias> " << NAD << '\n'; |
Eli Friedman | 368a55d | 2010-01-03 02:01:11 +0000 | [diff] [blame] | 385 | break; |
| 386 | } |
Zhongxing Xu | 2f3cd9c | 2010-01-20 03:21:28 +0000 | [diff] [blame] | 387 | case Decl::ClassTemplate: { |
| 388 | ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 389 | Out << "<class template> " << CTD << '\n'; |
Zhongxing Xu | 2f3cd9c | 2010-01-20 03:21:28 +0000 | [diff] [blame] | 390 | break; |
| 391 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 392 | default: |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 393 | Out << "DeclKind: " << DK << '"' << *I << "\"\n"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 394 | assert(0 && "decl unhandled"); |
| 395 | } |
| 396 | } |
| 397 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | ASTConsumer *clang::CreateDeclContextPrinter() { |
| 399 | return new DeclContextPrinter(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | //===----------------------------------------------------------------------===// |
John McCall | f351424 | 2010-11-24 11:21:45 +0000 | [diff] [blame] | 403 | /// ASTDumperXML - In-depth XML dumping. |
| 404 | |
| 405 | namespace { |
| 406 | class ASTDumpXML : public ASTConsumer { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 407 | raw_ostream &OS; |
John McCall | f351424 | 2010-11-24 11:21:45 +0000 | [diff] [blame] | 408 | |
| 409 | public: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 410 | ASTDumpXML(raw_ostream &OS) : OS(OS) {} |
John McCall | f351424 | 2010-11-24 11:21:45 +0000 | [diff] [blame] | 411 | |
| 412 | void HandleTranslationUnit(ASTContext &C) { |
| 413 | C.getTranslationUnitDecl()->dumpXML(OS); |
| 414 | } |
| 415 | }; |
| 416 | } |
| 417 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 418 | ASTConsumer *clang::CreateASTDumperXML(raw_ostream &OS) { |
John McCall | f351424 | 2010-11-24 11:21:45 +0000 | [diff] [blame] | 419 | return new ASTDumpXML(OS); |
| 420 | } |