blob: 52776b6a848377dfb4da042ddae025e874699d23 [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"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000024#include "llvm/Support/Path.h"
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000025#include "llvm/Support/Timer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnercbe4f772007-08-08 22:51:59 +000027using namespace clang;
Bill Wendlingaec64c32007-06-23 00:39:57 +000028
Ted Kremenek7c81bdd2007-11-27 21:46:50 +000029//===----------------------------------------------------------------------===//
Douglas Gregor278f52e2009-05-30 00:08:05 +000030/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattnercbe4f772007-08-08 22:51:59 +000031
Ted Kremenek7c81bdd2007-11-27 21:46:50 +000032namespace {
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000033 class ASTPrinter : public ASTConsumer,
34 public RecursiveASTVisitor<ASTPrinter> {
35 typedef RecursiveASTVisitor<ASTPrinter> base;
Mike Stump11289f42009-09-09 15:08:12 +000036
Ted Kremenek7c81bdd2007-11-27 21:46:50 +000037 public:
Craig Topper49a27902014-05-22 04:46:25 +000038 ASTPrinter(raw_ostream *Out = nullptr, bool Dump = false,
Richard Smith6ea05822013-06-24 01:45:33 +000039 StringRef FilterString = "", bool DumpLookups = false)
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000040 : Out(Out ? *Out : llvm::outs()), Dump(Dump),
Richard Smith6ea05822013-06-24 01:45:33 +000041 FilterString(FilterString), DumpLookups(DumpLookups) {}
Mike Stump11289f42009-09-09 15:08:12 +000042
Craig Topperafa7cb32014-03-13 06:07:04 +000043 void HandleTranslationUnit(ASTContext &Context) override {
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000044 TranslationUnitDecl *D = Context.getTranslationUnitDecl();
45
Richard Smith6ea05822013-06-24 01:45:33 +000046 if (FilterString.empty())
47 return print(D);
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000048
49 TraverseDecl(D);
Bill Wendlingaec64c32007-06-23 00:39:57 +000050 }
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000051
52 bool shouldWalkTypesOfTypeLocs() const { return false; }
53
54 bool TraverseDecl(Decl *D) {
Craig Topper49a27902014-05-22 04:46:25 +000055 if (D && filterMatches(D)) {
Dmitri Gribenko32333912012-11-21 10:54:55 +000056 bool ShowColors = Out.has_colors();
57 if (ShowColors)
Dmitri Gribenkof8579502013-01-12 19:30:44 +000058 Out.changeColor(raw_ostream::BLUE);
Richard Smith35f986d2014-08-11 22:11:07 +000059 Out << ((Dump || DumpLookups) ? "Dumping " : "Printing ") << getName(D)
60 << ":\n";
Dmitri Gribenko32333912012-11-21 10:54:55 +000061 if (ShowColors)
62 Out.resetColor();
Richard Smith6ea05822013-06-24 01:45:33 +000063 print(D);
Alexander Kornienko20186182012-08-17 17:38:39 +000064 Out << "\n";
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000065 // 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 Smith6ea05822013-06-24 01:45:33 +000080 void print(Decl *D) {
81 if (DumpLookups) {
Richard Smith35f986d2014-08-11 22:11:07 +000082 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
83 if (DC == DC->getPrimaryContext())
84 DC->dumpLookups(Out, Dump);
85 else
86 Out << "Lookup map is in primary DeclContext "
87 << DC->getPrimaryContext() << "\n";
88 } else
Richard Smith6ea05822013-06-24 01:45:33 +000089 Out << "Not a DeclContext\n";
90 } else if (Dump)
91 D->dump(Out);
92 else
93 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
94 }
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000095
96 raw_ostream &Out;
97 bool Dump;
98 std::string FilterString;
Richard Smith6ea05822013-06-24 01:45:33 +000099 bool DumpLookups;
Chris Lattner09c39db2007-09-15 23:02:28 +0000100 };
Alexander Kornienko4de03592012-07-31 09:37:40 +0000101
102 class ASTDeclNodeLister : public ASTConsumer,
103 public RecursiveASTVisitor<ASTDeclNodeLister> {
Alexander Kornienko4de03592012-07-31 09:37:40 +0000104 public:
Craig Topper49a27902014-05-22 04:46:25 +0000105 ASTDeclNodeLister(raw_ostream *Out = nullptr)
Alexander Kornienko4de03592012-07-31 09:37:40 +0000106 : Out(Out ? *Out : llvm::outs()) {}
107
Craig Topperafa7cb32014-03-13 06:07:04 +0000108 void HandleTranslationUnit(ASTContext &Context) override {
Alexander Kornienko4de03592012-07-31 09:37:40 +0000109 TraverseDecl(Context.getTranslationUnitDecl());
110 }
111
112 bool shouldWalkTypesOfTypeLocs() const { return false; }
113
Craig Topper45670532014-03-13 07:14:47 +0000114 bool VisitNamedDecl(NamedDecl *D) {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000115 D->printQualifiedName(Out);
116 Out << '\n';
Alexander Kornienko4de03592012-07-31 09:37:40 +0000117 return true;
118 }
119
120 private:
121 raw_ostream &Out;
122 };
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000123} // end anonymous namespace
Chris Lattnercbe4f772007-08-08 22:51:59 +0000124
David Blaikie6beb6aa2014-08-10 19:56:51 +0000125std::unique_ptr<ASTConsumer> clang::CreateASTPrinter(raw_ostream *Out,
126 StringRef FilterString) {
127 return llvm::make_unique<ASTPrinter>(Out, /*Dump=*/false, FilterString);
Ted Kremenek59337682007-11-28 21:32:21 +0000128}
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000129
David Blaikie6beb6aa2014-08-10 19:56:51 +0000130std::unique_ptr<ASTConsumer> clang::CreateASTDumper(StringRef FilterString,
Richard Smith35f986d2014-08-11 22:11:07 +0000131 bool DumpDecls,
David Blaikie6beb6aa2014-08-10 19:56:51 +0000132 bool DumpLookups) {
Richard Smith35f986d2014-08-11 22:11:07 +0000133 assert((DumpDecls || DumpLookups) && "nothing to dump");
134 return llvm::make_unique<ASTPrinter>(nullptr, DumpDecls, FilterString,
David Blaikie6beb6aa2014-08-10 19:56:51 +0000135 DumpLookups);
Douglas Gregor32887f02009-04-26 02:02:08 +0000136}
Chris Lattner09c39db2007-09-15 23:02:28 +0000137
David Blaikie6beb6aa2014-08-10 19:56:51 +0000138std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
139 return llvm::make_unique<ASTDeclNodeLister>(nullptr);
Alexander Kornienko4de03592012-07-31 09:37:40 +0000140}
141
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000142//===----------------------------------------------------------------------===//
143/// ASTViewer - AST Visualization
144
Ted Kremenek66d130a2007-09-19 21:29:43 +0000145namespace {
146 class ASTViewer : public ASTConsumer {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000147 ASTContext *Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000148 public:
Craig Topperafa7cb32014-03-13 06:07:04 +0000149 void Initialize(ASTContext &Context) override {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000150 this->Context = &Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000151 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000152
Craig Topperafa7cb32014-03-13 06:07:04 +0000153 bool HandleTopLevelDecl(DeclGroupRef D) override {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000154 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
155 HandleTopLevelSingleDecl(*I);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000156 return true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000159 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek66d130a2007-09-19 21:29:43 +0000160 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000161}
Ted Kremenek66d130a2007-09-19 21:29:43 +0000162
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000163void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Argyrios Kyrtzidis46f556d2010-07-07 11:31:23 +0000164 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
165 D->print(llvm::errs());
166
167 if (Stmt *Body = D->getBody()) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000168 llvm::errs() << '\n';
Douglas Gregore2350a32009-09-12 00:08:48 +0000169 Body->viewAST();
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000170 llvm::errs() << '\n';
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000171 }
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000172 }
173}
174
David Blaikie6beb6aa2014-08-10 19:56:51 +0000175std::unique_ptr<ASTConsumer> clang::CreateASTViewer() {
176 return llvm::make_unique<ASTViewer>();
177}
Ted Kremenek66d130a2007-09-19 21:29:43 +0000178
Ted Kremenek45c9e962007-09-07 23:47:56 +0000179//===----------------------------------------------------------------------===//
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000180/// DeclContextPrinter - Decl and DeclContext Visualization
181
182namespace {
183
184class DeclContextPrinter : public ASTConsumer {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000185 raw_ostream& Out;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000186public:
187 DeclContextPrinter() : Out(llvm::errs()) {}
188
Craig Topperafa7cb32014-03-13 06:07:04 +0000189 void HandleTranslationUnit(ASTContext &C) override {
Chris Lattnercf169832009-03-28 04:11:33 +0000190 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000191 }
192
193 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
194};
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000195} // end anonymous namespace
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000196
Mike Stump11289f42009-09-09 15:08:12 +0000197void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000198 unsigned Indentation) {
199 // Print DeclContext name.
Argyrios Kyrtzidisbe40b7e2009-01-13 13:11:58 +0000200 switch (DC->getDeclKind()) {
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000201 case Decl::TranslationUnit:
202 Out << "[translation unit] " << DC;
203 break;
204 case Decl::Namespace: {
205 Out << "[namespace] ";
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000206 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000207 Out << *ND;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000208 break;
209 }
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000210 case Decl::Enum: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000211 const EnumDecl* ED = cast<EnumDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000212 if (ED->isCompleteDefinition())
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000213 Out << "[enum] ";
214 else
215 Out << "<enum> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000216 Out << *ED;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000217 break;
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000218 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000219 case Decl::Record: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000220 const RecordDecl* RD = cast<RecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000221 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000222 Out << "[struct] ";
223 else
224 Out << "<struct> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000225 Out << *RD;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000226 break;
227 }
228 case Decl::CXXRecord: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000229 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000230 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000231 Out << "[class] ";
232 else
233 Out << "<class> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000234 Out << *RD << ' ' << DC;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000235 break;
236 }
237 case Decl::ObjCMethod:
238 Out << "[objc method]";
239 break;
240 case Decl::ObjCInterface:
241 Out << "[objc interface]";
242 break;
243 case Decl::ObjCCategory:
244 Out << "[objc category]";
245 break;
246 case Decl::ObjCProtocol:
247 Out << "[objc protocol]";
248 break;
249 case Decl::ObjCImplementation:
250 Out << "[objc implementation]";
251 break;
252 case Decl::ObjCCategoryImpl:
253 Out << "[objc categoryimpl]";
254 break;
255 case Decl::LinkageSpec:
256 Out << "[linkage spec]";
257 break;
258 case Decl::Block:
259 Out << "[block]";
260 break;
261 case Decl::Function: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000262 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000263 if (FD->doesThisDeclarationHaveABody())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000264 Out << "[function] ";
265 else
266 Out << "<function> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000267 Out << *FD;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000268 // Print the parameters.
269 Out << "(";
270 bool PrintComma = false;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000271 for (auto I : FD->params()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000272 if (PrintComma)
273 Out << ", ";
274 else
275 PrintComma = true;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000276 Out << *I;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000277 }
278 Out << ")";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000279 break;
280 }
281 case Decl::CXXMethod: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000282 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000283 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000284 Out << "[c++ method] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000285 else if (D->isImplicit())
286 Out << "(c++ method) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000287 else
288 Out << "<c++ method> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000289 Out << *D;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000290 // Print the parameters.
291 Out << "(";
292 bool PrintComma = false;
Mike Stump11289f42009-09-09 15:08:12 +0000293 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000294 E = D->param_end(); I != E; ++I) {
295 if (PrintComma)
296 Out << ", ";
297 else
298 PrintComma = true;
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000299 Out << **I;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000300 }
301 Out << ")";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000302
303 // Check the semantic DeclContext.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000304 const DeclContext* SemaDC = D->getDeclContext();
305 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000306 if (SemaDC != LexicalDC)
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000307 Out << " [[" << SemaDC << "]]";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000308
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000309 break;
310 }
311 case Decl::CXXConstructor: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000312 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000313 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000314 Out << "[c++ ctor] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000315 else if (D->isImplicit())
316 Out << "(c++ ctor) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000317 else
318 Out << "<c++ ctor> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000319 Out << *D;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000320 // Print the parameters.
321 Out << "(";
322 bool PrintComma = false;
Mike Stump11289f42009-09-09 15:08:12 +0000323 for (FunctionDecl::param_const_iterator I = D->param_begin(),
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000324 E = D->param_end(); I != E; ++I) {
325 if (PrintComma)
326 Out << ", ";
327 else
328 PrintComma = true;
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000329 Out << **I;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000330 }
331 Out << ")";
332
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000333 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000334 const DeclContext* SemaDC = D->getDeclContext();
335 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000336 if (SemaDC != LexicalDC)
337 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000338 break;
339 }
340 case Decl::CXXDestructor: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000341 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000342 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000343 Out << "[c++ dtor] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000344 else if (D->isImplicit())
345 Out << "(c++ dtor) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000346 else
347 Out << "<c++ dtor> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000348 Out << *D;
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000349 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000350 const DeclContext* SemaDC = D->getDeclContext();
351 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000352 if (SemaDC != LexicalDC)
353 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000354 break;
355 }
356 case Decl::CXXConversion: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000357 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000358 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000359 Out << "[c++ conversion] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000360 else if (D->isImplicit())
361 Out << "(c++ conversion) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000362 else
363 Out << "<c++ conversion> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000364 Out << *D;
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000365 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000366 const DeclContext* SemaDC = D->getDeclContext();
367 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000368 if (SemaDC != LexicalDC)
369 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000370 break;
371 }
372
373 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000374 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000375 }
376
377 Out << "\n";
378
379 // Print decls in the DeclContext.
Aaron Ballman629afae2014-03-07 19:56:05 +0000380 for (auto *I : DC->decls()) {
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000381 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump74a76472009-02-10 20:16:46 +0000382 Out << " ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000383
384 Decl::Kind DK = I->getKind();
385 switch (DK) {
386 case Decl::Namespace:
387 case Decl::Enum:
388 case Decl::Record:
389 case Decl::CXXRecord:
390 case Decl::ObjCMethod:
391 case Decl::ObjCInterface:
Mike Stump11289f42009-09-09 15:08:12 +0000392 case Decl::ObjCCategory:
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000393 case Decl::ObjCProtocol:
394 case Decl::ObjCImplementation:
395 case Decl::ObjCCategoryImpl:
396 case Decl::LinkageSpec:
397 case Decl::Block:
398 case Decl::Function:
399 case Decl::CXXMethod:
400 case Decl::CXXConstructor:
401 case Decl::CXXDestructor:
402 case Decl::CXXConversion:
403 {
Aaron Ballman629afae2014-03-07 19:56:05 +0000404 DeclContext* DC = cast<DeclContext>(I);
Mike Stump74a76472009-02-10 20:16:46 +0000405 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000406 break;
407 }
Francois Pichet48e2d9e2010-12-21 03:08:02 +0000408 case Decl::IndirectField: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000409 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000410 Out << "<IndirectField> " << *IFD << '\n';
Francois Pichet48e2d9e2010-12-21 03:08:02 +0000411 break;
412 }
Chris Lattnerd12b4802011-02-18 00:52:55 +0000413 case Decl::Label: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000414 LabelDecl *LD = cast<LabelDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000415 Out << "<Label> " << *LD << '\n';
Chris Lattnerd12b4802011-02-18 00:52:55 +0000416 break;
417 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000418 case Decl::Field: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000419 FieldDecl *FD = cast<FieldDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000420 Out << "<field> " << *FD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000421 break;
422 }
Richard Smithdda56e42011-04-15 14:24:37 +0000423 case Decl::Typedef:
424 case Decl::TypeAlias: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000425 TypedefNameDecl* TD = cast<TypedefNameDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000426 Out << "<typedef> " << *TD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000427 break;
428 }
429 case Decl::EnumConstant: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000430 EnumConstantDecl* ECD = cast<EnumConstantDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000431 Out << "<enum constant> " << *ECD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000432 break;
433 }
434 case Decl::Var: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000435 VarDecl* VD = cast<VarDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000436 Out << "<var> " << *VD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000437 break;
438 }
439 case Decl::ImplicitParam: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000440 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000441 Out << "<implicit parameter> " << *IPD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000442 break;
443 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000444 case Decl::ParmVar: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000445 ParmVarDecl* PVD = cast<ParmVarDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000446 Out << "<parameter> " << *PVD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000447 break;
448 }
449 case Decl::ObjCProperty: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000450 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000451 Out << "<objc property> " << *OPD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000452 break;
453 }
Eli Friedman897bc032009-12-08 06:22:37 +0000454 case Decl::FunctionTemplate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000455 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000456 Out << "<function template> " << *FTD << '\n';
Eli Friedman897bc032009-12-08 06:22:37 +0000457 break;
458 }
Eli Friedmanc6f59b12010-01-03 02:01:11 +0000459 case Decl::FileScopeAsm: {
460 Out << "<file-scope asm>\n";
461 break;
462 }
463 case Decl::UsingDirective: {
464 Out << "<using directive>\n";
465 break;
466 }
467 case Decl::NamespaceAlias: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000468 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000469 Out << "<namespace alias> " << *NAD << '\n';
Eli Friedmanc6f59b12010-01-03 02:01:11 +0000470 break;
471 }
Zhongxing Xu00c60462010-01-20 03:21:28 +0000472 case Decl::ClassTemplate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000473 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000474 Out << "<class template> " << *CTD << '\n';
Zhongxing Xu00c60462010-01-20 03:21:28 +0000475 break;
476 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000477 case Decl::OMPThreadPrivate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000478 Out << "<omp threadprivate> " << '"' << I << "\"\n";
Alexey Bataeva769e072013-03-22 06:34:35 +0000479 break;
480 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000481 default:
Aaron Ballman629afae2014-03-07 19:56:05 +0000482 Out << "DeclKind: " << DK << '"' << I << "\"\n";
David Blaikie83d382b2011-09-23 05:06:16 +0000483 llvm_unreachable("decl unhandled");
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000484 }
485 }
486}
David Blaikie6beb6aa2014-08-10 19:56:51 +0000487std::unique_ptr<ASTConsumer> clang::CreateDeclContextPrinter() {
488 return llvm::make_unique<DeclContextPrinter>();
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000489}