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