blob: f53c614b0a30ada3cf3b31417f4bdd780539e987 [file] [log] [blame]
Chris Lattnerd1cdee72007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Bill Wendlingaec64c32007-06-23 00:39:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendlingaec64c32007-06-23 00:39:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerd1cdee72007-10-07 06:04:32 +000010// AST Consumer Implementations.
Bill Wendlingaec64c32007-06-23 00:39:57 +000011//
12//===----------------------------------------------------------------------===//
13
Eli Friedman9f30fc32009-05-18 22:50:54 +000014#include "clang/Frontend/ASTConsumers.h"
Bill Wendlingaec64c32007-06-23 00:39:57 +000015#include "clang/AST/AST.h"
Chris Lattner09c39db2007-09-15 23:02:28 +000016#include "clang/AST/ASTConsumer.h"
Chris Lattnera5adead2009-03-28 04:27:18 +000017#include "clang/AST/ASTContext.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000018#include "clang/AST/PrettyPrinter.h"
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000019#include "clang/AST/RecordLayout.h"
20#include "clang/AST/RecursiveASTVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Basic/Diagnostic.h"
22#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000024#include "llvm/IR/Module.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000025#include "llvm/Support/Path.h"
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000026#include "llvm/Support/Timer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnercbe4f772007-08-08 22:51:59 +000028using namespace clang;
Bill Wendlingaec64c32007-06-23 00:39:57 +000029
Ted Kremenek7c81bdd2007-11-27 21:46:50 +000030//===----------------------------------------------------------------------===//
Douglas Gregor278f52e2009-05-30 00:08:05 +000031/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattnercbe4f772007-08-08 22:51:59 +000032
Ted Kremenek7c81bdd2007-11-27 21:46:50 +000033namespace {
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000034 class ASTPrinter : public ASTConsumer,
35 public RecursiveASTVisitor<ASTPrinter> {
36 typedef RecursiveASTVisitor<ASTPrinter> base;
Mike Stump11289f42009-09-09 15:08:12 +000037
Ted Kremenek7c81bdd2007-11-27 21:46:50 +000038 public:
Craig Topper49a27902014-05-22 04:46:25 +000039 ASTPrinter(raw_ostream *Out = nullptr, bool Dump = false,
Richard Smith6ea05822013-06-24 01:45:33 +000040 StringRef FilterString = "", bool DumpLookups = false)
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000041 : Out(Out ? *Out : llvm::outs()), Dump(Dump),
Richard Smith6ea05822013-06-24 01:45:33 +000042 FilterString(FilterString), DumpLookups(DumpLookups) {}
Mike Stump11289f42009-09-09 15:08:12 +000043
Craig Topperafa7cb32014-03-13 06:07:04 +000044 void HandleTranslationUnit(ASTContext &Context) override {
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000045 TranslationUnitDecl *D = Context.getTranslationUnitDecl();
46
Richard Smith6ea05822013-06-24 01:45:33 +000047 if (FilterString.empty())
48 return print(D);
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000049
50 TraverseDecl(D);
Bill Wendlingaec64c32007-06-23 00:39:57 +000051 }
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000052
53 bool shouldWalkTypesOfTypeLocs() const { return false; }
54
55 bool TraverseDecl(Decl *D) {
Craig Topper49a27902014-05-22 04:46:25 +000056 if (D && filterMatches(D)) {
Dmitri Gribenko32333912012-11-21 10:54:55 +000057 bool ShowColors = Out.has_colors();
58 if (ShowColors)
Dmitri Gribenkof8579502013-01-12 19:30:44 +000059 Out.changeColor(raw_ostream::BLUE);
Richard Smith35f986d2014-08-11 22:11:07 +000060 Out << ((Dump || DumpLookups) ? "Dumping " : "Printing ") << getName(D)
61 << ":\n";
Dmitri Gribenko32333912012-11-21 10:54:55 +000062 if (ShowColors)
63 Out.resetColor();
Richard Smith6ea05822013-06-24 01:45:33 +000064 print(D);
Alexander Kornienko20186182012-08-17 17:38:39 +000065 Out << "\n";
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000066 // Don't traverse child nodes to avoid output duplication.
67 return true;
68 }
69 return base::TraverseDecl(D);
70 }
71
72 private:
73 std::string getName(Decl *D) {
74 if (isa<NamedDecl>(D))
75 return cast<NamedDecl>(D)->getQualifiedNameAsString();
76 return "";
77 }
78 bool filterMatches(Decl *D) {
79 return getName(D).find(FilterString) != std::string::npos;
80 }
Richard Smith6ea05822013-06-24 01:45:33 +000081 void print(Decl *D) {
82 if (DumpLookups) {
Richard Smith35f986d2014-08-11 22:11:07 +000083 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
84 if (DC == DC->getPrimaryContext())
85 DC->dumpLookups(Out, Dump);
86 else
87 Out << "Lookup map is in primary DeclContext "
88 << DC->getPrimaryContext() << "\n";
89 } else
Richard Smith6ea05822013-06-24 01:45:33 +000090 Out << "Not a DeclContext\n";
91 } else if (Dump)
92 D->dump(Out);
93 else
94 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
95 }
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000096
97 raw_ostream &Out;
98 bool Dump;
99 std::string FilterString;
Richard Smith6ea05822013-06-24 01:45:33 +0000100 bool DumpLookups;
Chris Lattner09c39db2007-09-15 23:02:28 +0000101 };
Alexander Kornienko4de03592012-07-31 09:37:40 +0000102
103 class ASTDeclNodeLister : public ASTConsumer,
104 public RecursiveASTVisitor<ASTDeclNodeLister> {
Alexander Kornienko4de03592012-07-31 09:37:40 +0000105 public:
Craig Topper49a27902014-05-22 04:46:25 +0000106 ASTDeclNodeLister(raw_ostream *Out = nullptr)
Alexander Kornienko4de03592012-07-31 09:37:40 +0000107 : Out(Out ? *Out : llvm::outs()) {}
108
Craig Topperafa7cb32014-03-13 06:07:04 +0000109 void HandleTranslationUnit(ASTContext &Context) override {
Alexander Kornienko4de03592012-07-31 09:37:40 +0000110 TraverseDecl(Context.getTranslationUnitDecl());
111 }
112
113 bool shouldWalkTypesOfTypeLocs() const { return false; }
114
Craig Topper45670532014-03-13 07:14:47 +0000115 bool VisitNamedDecl(NamedDecl *D) {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000116 D->printQualifiedName(Out);
117 Out << '\n';
Alexander Kornienko4de03592012-07-31 09:37:40 +0000118 return true;
119 }
120
121 private:
122 raw_ostream &Out;
123 };
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000124} // end anonymous namespace
Chris Lattnercbe4f772007-08-08 22:51:59 +0000125
David Blaikie6beb6aa2014-08-10 19:56:51 +0000126std::unique_ptr<ASTConsumer> clang::CreateASTPrinter(raw_ostream *Out,
127 StringRef FilterString) {
128 return llvm::make_unique<ASTPrinter>(Out, /*Dump=*/false, FilterString);
Ted Kremenek59337682007-11-28 21:32:21 +0000129}
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000130
David Blaikie6beb6aa2014-08-10 19:56:51 +0000131std::unique_ptr<ASTConsumer> clang::CreateASTDumper(StringRef FilterString,
Richard Smith35f986d2014-08-11 22:11:07 +0000132 bool DumpDecls,
David Blaikie6beb6aa2014-08-10 19:56:51 +0000133 bool DumpLookups) {
Richard Smith35f986d2014-08-11 22:11:07 +0000134 assert((DumpDecls || DumpLookups) && "nothing to dump");
135 return llvm::make_unique<ASTPrinter>(nullptr, DumpDecls, FilterString,
David Blaikie6beb6aa2014-08-10 19:56:51 +0000136 DumpLookups);
Douglas Gregor32887f02009-04-26 02:02:08 +0000137}
Chris Lattner09c39db2007-09-15 23:02:28 +0000138
David Blaikie6beb6aa2014-08-10 19:56:51 +0000139std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
140 return llvm::make_unique<ASTDeclNodeLister>(nullptr);
Alexander Kornienko4de03592012-07-31 09:37:40 +0000141}
142
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000143//===----------------------------------------------------------------------===//
144/// ASTViewer - AST Visualization
145
Ted Kremenek66d130a2007-09-19 21:29:43 +0000146namespace {
147 class ASTViewer : public ASTConsumer {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000148 ASTContext *Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000149 public:
Craig Topperafa7cb32014-03-13 06:07:04 +0000150 void Initialize(ASTContext &Context) override {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000151 this->Context = &Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000152 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000153
Craig Topperafa7cb32014-03-13 06:07:04 +0000154 bool HandleTopLevelDecl(DeclGroupRef D) override {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000155 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
156 HandleTopLevelSingleDecl(*I);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000157 return true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000158 }
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000160 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek66d130a2007-09-19 21:29:43 +0000161 };
162}
163
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000164void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Argyrios Kyrtzidis46f556d2010-07-07 11:31:23 +0000165 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
166 D->print(llvm::errs());
167
168 if (Stmt *Body = D->getBody()) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000169 llvm::errs() << '\n';
Douglas Gregore2350a32009-09-12 00:08:48 +0000170 Body->viewAST();
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000171 llvm::errs() << '\n';
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000172 }
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000173 }
174}
175
David Blaikie6beb6aa2014-08-10 19:56:51 +0000176std::unique_ptr<ASTConsumer> clang::CreateASTViewer() {
177 return llvm::make_unique<ASTViewer>();
178}
Ted Kremenek66d130a2007-09-19 21:29:43 +0000179
Ted Kremenek45c9e962007-09-07 23:47:56 +0000180//===----------------------------------------------------------------------===//
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000181/// DeclContextPrinter - Decl and DeclContext Visualization
182
183namespace {
184
185class DeclContextPrinter : public ASTConsumer {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000186 raw_ostream& Out;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000187public:
188 DeclContextPrinter() : Out(llvm::errs()) {}
189
Craig Topperafa7cb32014-03-13 06:07:04 +0000190 void HandleTranslationUnit(ASTContext &C) override {
Chris Lattnercf169832009-03-28 04:11:33 +0000191 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000192 }
193
194 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
195};
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000196} // end anonymous namespace
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000197
Mike Stump11289f42009-09-09 15:08:12 +0000198void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000199 unsigned Indentation) {
200 // Print DeclContext name.
Argyrios Kyrtzidisbe40b7e2009-01-13 13:11:58 +0000201 switch (DC->getDeclKind()) {
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000202 case Decl::TranslationUnit:
203 Out << "[translation unit] " << DC;
204 break;
205 case Decl::Namespace: {
206 Out << "[namespace] ";
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000207 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000208 Out << *ND;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000209 break;
210 }
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000211 case Decl::Enum: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000212 const EnumDecl* ED = cast<EnumDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000213 if (ED->isCompleteDefinition())
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000214 Out << "[enum] ";
215 else
216 Out << "<enum> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000217 Out << *ED;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000218 break;
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000219 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000220 case Decl::Record: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000221 const RecordDecl* RD = cast<RecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000222 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000223 Out << "[struct] ";
224 else
225 Out << "<struct> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000226 Out << *RD;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000227 break;
228 }
229 case Decl::CXXRecord: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000230 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000231 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000232 Out << "[class] ";
233 else
234 Out << "<class> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000235 Out << *RD << ' ' << DC;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000236 break;
237 }
238 case Decl::ObjCMethod:
239 Out << "[objc method]";
240 break;
241 case Decl::ObjCInterface:
242 Out << "[objc interface]";
243 break;
244 case Decl::ObjCCategory:
245 Out << "[objc category]";
246 break;
247 case Decl::ObjCProtocol:
248 Out << "[objc protocol]";
249 break;
250 case Decl::ObjCImplementation:
251 Out << "[objc implementation]";
252 break;
253 case Decl::ObjCCategoryImpl:
254 Out << "[objc categoryimpl]";
255 break;
256 case Decl::LinkageSpec:
257 Out << "[linkage spec]";
258 break;
259 case Decl::Block:
260 Out << "[block]";
261 break;
262 case Decl::Function: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000263 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000264 if (FD->doesThisDeclarationHaveABody())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000265 Out << "[function] ";
266 else
267 Out << "<function> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000268 Out << *FD;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000269 // Print the parameters.
270 Out << "(";
271 bool PrintComma = false;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000272 for (auto I : FD->params()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000273 if (PrintComma)
274 Out << ", ";
275 else
276 PrintComma = true;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000277 Out << *I;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000278 }
279 Out << ")";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000280 break;
281 }
282 case Decl::CXXMethod: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000283 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000284 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000285 Out << "[c++ method] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000286 else if (D->isImplicit())
287 Out << "(c++ method) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000288 else
289 Out << "<c++ method> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000290 Out << *D;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000291 // Print the parameters.
292 Out << "(";
293 bool PrintComma = false;
Mike Stump11289f42009-09-09 15:08:12 +0000294 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000295 E = D->param_end(); I != E; ++I) {
296 if (PrintComma)
297 Out << ", ";
298 else
299 PrintComma = true;
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000300 Out << **I;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000301 }
302 Out << ")";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000303
304 // Check the semantic DeclContext.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000305 const DeclContext* SemaDC = D->getDeclContext();
306 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000307 if (SemaDC != LexicalDC)
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000308 Out << " [[" << SemaDC << "]]";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000309
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000310 break;
311 }
312 case Decl::CXXConstructor: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000313 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000314 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000315 Out << "[c++ ctor] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000316 else if (D->isImplicit())
317 Out << "(c++ ctor) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000318 else
319 Out << "<c++ ctor> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000320 Out << *D;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000321 // Print the parameters.
322 Out << "(";
323 bool PrintComma = false;
Mike Stump11289f42009-09-09 15:08:12 +0000324 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000325 E = D->param_end(); I != E; ++I) {
326 if (PrintComma)
327 Out << ", ";
328 else
329 PrintComma = true;
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000330 Out << **I;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000331 }
332 Out << ")";
333
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000334 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000335 const DeclContext* SemaDC = D->getDeclContext();
336 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000337 if (SemaDC != LexicalDC)
338 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000339 break;
340 }
341 case Decl::CXXDestructor: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000342 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000343 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000344 Out << "[c++ dtor] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000345 else if (D->isImplicit())
346 Out << "(c++ dtor) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000347 else
348 Out << "<c++ dtor> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000349 Out << *D;
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000350 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000351 const DeclContext* SemaDC = D->getDeclContext();
352 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000353 if (SemaDC != LexicalDC)
354 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000355 break;
356 }
357 case Decl::CXXConversion: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000358 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000359 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000360 Out << "[c++ conversion] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000361 else if (D->isImplicit())
362 Out << "(c++ conversion) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000363 else
364 Out << "<c++ conversion> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000365 Out << *D;
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000366 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000367 const DeclContext* SemaDC = D->getDeclContext();
368 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000369 if (SemaDC != LexicalDC)
370 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000371 break;
372 }
373
374 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000375 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000376 }
377
378 Out << "\n";
379
380 // Print decls in the DeclContext.
Aaron Ballman629afae2014-03-07 19:56:05 +0000381 for (auto *I : DC->decls()) {
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000382 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump74a76472009-02-10 20:16:46 +0000383 Out << " ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000384
385 Decl::Kind DK = I->getKind();
386 switch (DK) {
387 case Decl::Namespace:
388 case Decl::Enum:
389 case Decl::Record:
390 case Decl::CXXRecord:
391 case Decl::ObjCMethod:
392 case Decl::ObjCInterface:
Mike Stump11289f42009-09-09 15:08:12 +0000393 case Decl::ObjCCategory:
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000394 case Decl::ObjCProtocol:
395 case Decl::ObjCImplementation:
396 case Decl::ObjCCategoryImpl:
397 case Decl::LinkageSpec:
398 case Decl::Block:
399 case Decl::Function:
400 case Decl::CXXMethod:
401 case Decl::CXXConstructor:
402 case Decl::CXXDestructor:
403 case Decl::CXXConversion:
404 {
Aaron Ballman629afae2014-03-07 19:56:05 +0000405 DeclContext* DC = cast<DeclContext>(I);
Mike Stump74a76472009-02-10 20:16:46 +0000406 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000407 break;
408 }
Francois Pichet48e2d9e2010-12-21 03:08:02 +0000409 case Decl::IndirectField: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000410 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000411 Out << "<IndirectField> " << *IFD << '\n';
Francois Pichet48e2d9e2010-12-21 03:08:02 +0000412 break;
413 }
Chris Lattnerd12b4802011-02-18 00:52:55 +0000414 case Decl::Label: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000415 LabelDecl *LD = cast<LabelDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000416 Out << "<Label> " << *LD << '\n';
Chris Lattnerd12b4802011-02-18 00:52:55 +0000417 break;
418 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000419 case Decl::Field: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000420 FieldDecl *FD = cast<FieldDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000421 Out << "<field> " << *FD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000422 break;
423 }
Richard Smithdda56e42011-04-15 14:24:37 +0000424 case Decl::Typedef:
425 case Decl::TypeAlias: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000426 TypedefNameDecl* TD = cast<TypedefNameDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000427 Out << "<typedef> " << *TD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000428 break;
429 }
430 case Decl::EnumConstant: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000431 EnumConstantDecl* ECD = cast<EnumConstantDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000432 Out << "<enum constant> " << *ECD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000433 break;
434 }
435 case Decl::Var: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000436 VarDecl* VD = cast<VarDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000437 Out << "<var> " << *VD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000438 break;
439 }
440 case Decl::ImplicitParam: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000441 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000442 Out << "<implicit parameter> " << *IPD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000443 break;
444 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000445 case Decl::ParmVar: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000446 ParmVarDecl* PVD = cast<ParmVarDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000447 Out << "<parameter> " << *PVD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000448 break;
449 }
450 case Decl::ObjCProperty: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000451 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000452 Out << "<objc property> " << *OPD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000453 break;
454 }
Eli Friedman897bc032009-12-08 06:22:37 +0000455 case Decl::FunctionTemplate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000456 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000457 Out << "<function template> " << *FTD << '\n';
Eli Friedman897bc032009-12-08 06:22:37 +0000458 break;
459 }
Eli Friedmanc6f59b12010-01-03 02:01:11 +0000460 case Decl::FileScopeAsm: {
461 Out << "<file-scope asm>\n";
462 break;
463 }
464 case Decl::UsingDirective: {
465 Out << "<using directive>\n";
466 break;
467 }
468 case Decl::NamespaceAlias: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000469 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000470 Out << "<namespace alias> " << *NAD << '\n';
Eli Friedmanc6f59b12010-01-03 02:01:11 +0000471 break;
472 }
Zhongxing Xu00c60462010-01-20 03:21:28 +0000473 case Decl::ClassTemplate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000474 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000475 Out << "<class template> " << *CTD << '\n';
Zhongxing Xu00c60462010-01-20 03:21:28 +0000476 break;
477 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000478 case Decl::OMPThreadPrivate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000479 Out << "<omp threadprivate> " << '"' << I << "\"\n";
Alexey Bataeva769e072013-03-22 06:34:35 +0000480 break;
481 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000482 default:
Aaron Ballman629afae2014-03-07 19:56:05 +0000483 Out << "DeclKind: " << DK << '"' << I << "\"\n";
David Blaikie83d382b2011-09-23 05:06:16 +0000484 llvm_unreachable("decl unhandled");
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000485 }
486 }
487}
David Blaikie6beb6aa2014-08-10 19:56:51 +0000488std::unique_ptr<ASTConsumer> clang::CreateDeclContextPrinter() {
489 return llvm::make_unique<DeclContextPrinter>();
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000490}