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