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