blob: b67c019baed84e53bd1ea0e7e754cd38512cc36a [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Basic/SourceManager.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000023#include "llvm/Support/Path.h"
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000024#include "llvm/Support/Timer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnercbe4f772007-08-08 22:51:59 +000026using namespace clang;
Bill Wendlingaec64c32007-06-23 00:39:57 +000027
Ted Kremenek7c81bdd2007-11-27 21:46:50 +000028//===----------------------------------------------------------------------===//
Douglas Gregor278f52e2009-05-30 00:08:05 +000029/// ASTPrinter - Pretty-printer and dumper of ASTs
Chris Lattnercbe4f772007-08-08 22:51:59 +000030
Ted Kremenek7c81bdd2007-11-27 21:46:50 +000031namespace {
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000032 class ASTPrinter : public ASTConsumer,
33 public RecursiveASTVisitor<ASTPrinter> {
34 typedef RecursiveASTVisitor<ASTPrinter> base;
Mike Stump11289f42009-09-09 15:08:12 +000035
Ted Kremenek7c81bdd2007-11-27 21:46:50 +000036 public:
Richard Smith3a36ac12017-03-09 22:00:01 +000037 enum Kind { DumpFull, Dump, Print, None };
38 ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K, StringRef FilterString,
39 bool DumpLookups = false)
40 : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)),
41 OutputKind(K), 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 Smith3a36ac12017-03-09 22:00:01 +000059 Out << (OutputKind != Print ? "Dumping " : "Printing ") << getName(D)
Richard Smith35f986d2014-08-11 22:11:07 +000060 << ":\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())
Richard Smith3a36ac12017-03-09 22:00:01 +000084 DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull);
Richard Smith35f986d2014-08-11 22:11:07 +000085 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";
Joel E. Denny7bcc2102018-05-14 18:41:44 +000090 } else if (OutputKind == Print) {
91 PrintingPolicy Policy(D->getASTContext().getLangOpts());
92 D->print(Out, Policy, /*Indentation=*/0, /*PrintInstantiation=*/true);
93 } else if (OutputKind != None)
Richard Smith3a36ac12017-03-09 22:00:01 +000094 D->dump(Out, OutputKind == DumpFull);
Richard Smith6ea05822013-06-24 01:45:33 +000095 }
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000096
97 raw_ostream &Out;
Peter Collingbourne03f89072016-07-15 00:55:40 +000098 std::unique_ptr<raw_ostream> OwnedOut;
Richard Smith3a36ac12017-03-09 22:00:01 +000099
100 /// How to output individual declarations.
101 Kind OutputKind;
102
103 /// Which declarations or DeclContexts to display.
Alexander Kornienko3db68ee2012-07-26 16:01:23 +0000104 std::string FilterString;
Richard Smith3a36ac12017-03-09 22:00:01 +0000105
106 /// Whether the primary output is lookup results or declarations. Individual
107 /// results will be output with a format determined by OutputKind. This is
108 /// incompatible with OutputKind == Print.
Richard Smith6ea05822013-06-24 01:45:33 +0000109 bool DumpLookups;
Chris Lattner09c39db2007-09-15 23:02:28 +0000110 };
Alexander Kornienko4de03592012-07-31 09:37:40 +0000111
112 class ASTDeclNodeLister : public ASTConsumer,
113 public RecursiveASTVisitor<ASTDeclNodeLister> {
Alexander Kornienko4de03592012-07-31 09:37:40 +0000114 public:
Craig Topper49a27902014-05-22 04:46:25 +0000115 ASTDeclNodeLister(raw_ostream *Out = nullptr)
Alexander Kornienko4de03592012-07-31 09:37:40 +0000116 : Out(Out ? *Out : llvm::outs()) {}
117
Craig Topperafa7cb32014-03-13 06:07:04 +0000118 void HandleTranslationUnit(ASTContext &Context) override {
Alexander Kornienko4de03592012-07-31 09:37:40 +0000119 TraverseDecl(Context.getTranslationUnitDecl());
120 }
121
122 bool shouldWalkTypesOfTypeLocs() const { return false; }
123
Craig Topper45670532014-03-13 07:14:47 +0000124 bool VisitNamedDecl(NamedDecl *D) {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000125 D->printQualifiedName(Out);
126 Out << '\n';
Alexander Kornienko4de03592012-07-31 09:37:40 +0000127 return true;
128 }
129
130 private:
131 raw_ostream &Out;
132 };
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000133} // end anonymous namespace
Chris Lattnercbe4f772007-08-08 22:51:59 +0000134
Peter Collingbourne03f89072016-07-15 00:55:40 +0000135std::unique_ptr<ASTConsumer>
136clang::CreateASTPrinter(std::unique_ptr<raw_ostream> Out,
137 StringRef FilterString) {
Richard Smith3a36ac12017-03-09 22:00:01 +0000138 return llvm::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
Peter Collingbourne03f89072016-07-15 00:55:40 +0000139 FilterString);
Ted Kremenek59337682007-11-28 21:32:21 +0000140}
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000141
Alexander Kornienkod10d7902018-04-06 13:01:12 +0000142std::unique_ptr<ASTConsumer>
143clang::CreateASTDumper(std::unique_ptr<raw_ostream> Out,
144 StringRef FilterString,
145 bool DumpDecls,
146 bool Deserialize,
147 bool DumpLookups) {
Aaron Ballman08d3b392017-06-15 00:00:08 +0000148 assert((DumpDecls || Deserialize || DumpLookups) && "nothing to dump");
Alexander Kornienkod10d7902018-04-06 13:01:12 +0000149 return llvm::make_unique<ASTPrinter>(std::move(Out),
Richard Smith3a36ac12017-03-09 22:00:01 +0000150 Deserialize ? ASTPrinter::DumpFull :
151 DumpDecls ? ASTPrinter::Dump :
152 ASTPrinter::None,
153 FilterString, DumpLookups);
Douglas Gregor32887f02009-04-26 02:02:08 +0000154}
Chris Lattner09c39db2007-09-15 23:02:28 +0000155
David Blaikie6beb6aa2014-08-10 19:56:51 +0000156std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
157 return llvm::make_unique<ASTDeclNodeLister>(nullptr);
Alexander Kornienko4de03592012-07-31 09:37:40 +0000158}
159
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000160//===----------------------------------------------------------------------===//
161/// ASTViewer - AST Visualization
162
Ted Kremenek66d130a2007-09-19 21:29:43 +0000163namespace {
164 class ASTViewer : public ASTConsumer {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000165 ASTContext *Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000166 public:
Craig Topperafa7cb32014-03-13 06:07:04 +0000167 void Initialize(ASTContext &Context) override {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000168 this->Context = &Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000169 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000170
Craig Topperafa7cb32014-03-13 06:07:04 +0000171 bool HandleTopLevelDecl(DeclGroupRef D) override {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000172 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
173 HandleTopLevelSingleDecl(*I);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000174 return true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000175 }
Mike Stump11289f42009-09-09 15:08:12 +0000176
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000177 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek66d130a2007-09-19 21:29:43 +0000178 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000179}
Ted Kremenek66d130a2007-09-19 21:29:43 +0000180
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000181void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Argyrios Kyrtzidis46f556d2010-07-07 11:31:23 +0000182 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
183 D->print(llvm::errs());
184
185 if (Stmt *Body = D->getBody()) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000186 llvm::errs() << '\n';
Douglas Gregore2350a32009-09-12 00:08:48 +0000187 Body->viewAST();
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000188 llvm::errs() << '\n';
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000189 }
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000190 }
191}
192
David Blaikie6beb6aa2014-08-10 19:56:51 +0000193std::unique_ptr<ASTConsumer> clang::CreateASTViewer() {
194 return llvm::make_unique<ASTViewer>();
195}
Ted Kremenek66d130a2007-09-19 21:29:43 +0000196
Ted Kremenek45c9e962007-09-07 23:47:56 +0000197//===----------------------------------------------------------------------===//
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000198/// DeclContextPrinter - Decl and DeclContext Visualization
199
200namespace {
201
202class DeclContextPrinter : public ASTConsumer {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000203 raw_ostream& Out;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000204public:
205 DeclContextPrinter() : Out(llvm::errs()) {}
206
Craig Topperafa7cb32014-03-13 06:07:04 +0000207 void HandleTranslationUnit(ASTContext &C) override {
Chris Lattnercf169832009-03-28 04:11:33 +0000208 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000209 }
210
211 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
212};
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000213} // end anonymous namespace
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000214
Mike Stump11289f42009-09-09 15:08:12 +0000215void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000216 unsigned Indentation) {
217 // Print DeclContext name.
Argyrios Kyrtzidisbe40b7e2009-01-13 13:11:58 +0000218 switch (DC->getDeclKind()) {
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000219 case Decl::TranslationUnit:
220 Out << "[translation unit] " << DC;
221 break;
222 case Decl::Namespace: {
223 Out << "[namespace] ";
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000224 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000225 Out << *ND;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000226 break;
227 }
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000228 case Decl::Enum: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000229 const EnumDecl* ED = cast<EnumDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000230 if (ED->isCompleteDefinition())
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000231 Out << "[enum] ";
232 else
233 Out << "<enum> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000234 Out << *ED;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000235 break;
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000236 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000237 case Decl::Record: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000238 const RecordDecl* RD = cast<RecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000239 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000240 Out << "[struct] ";
241 else
242 Out << "<struct> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000243 Out << *RD;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000244 break;
245 }
246 case Decl::CXXRecord: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000247 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000248 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000249 Out << "[class] ";
250 else
251 Out << "<class> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000252 Out << *RD << ' ' << DC;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000253 break;
254 }
255 case Decl::ObjCMethod:
256 Out << "[objc method]";
257 break;
258 case Decl::ObjCInterface:
259 Out << "[objc interface]";
260 break;
261 case Decl::ObjCCategory:
262 Out << "[objc category]";
263 break;
264 case Decl::ObjCProtocol:
265 Out << "[objc protocol]";
266 break;
267 case Decl::ObjCImplementation:
268 Out << "[objc implementation]";
269 break;
270 case Decl::ObjCCategoryImpl:
271 Out << "[objc categoryimpl]";
272 break;
273 case Decl::LinkageSpec:
274 Out << "[linkage spec]";
275 break;
276 case Decl::Block:
277 Out << "[block]";
278 break;
279 case Decl::Function: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000280 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000281 if (FD->doesThisDeclarationHaveABody())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000282 Out << "[function] ";
283 else
284 Out << "<function> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000285 Out << *FD;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000286 // Print the parameters.
287 Out << "(";
288 bool PrintComma = false;
David Majnemer59f77922016-06-24 04:05:48 +0000289 for (auto I : FD->parameters()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000290 if (PrintComma)
291 Out << ", ";
292 else
293 PrintComma = true;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000294 Out << *I;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000295 }
296 Out << ")";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000297 break;
298 }
299 case Decl::CXXMethod: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000300 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000301 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000302 Out << "[c++ method] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000303 else if (D->isImplicit())
304 Out << "(c++ method) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000305 else
306 Out << "<c++ method> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000307 Out << *D;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000308 // Print the parameters.
309 Out << "(";
310 bool PrintComma = false;
David Majnemera3debed2016-06-24 05:33:44 +0000311 for (ParmVarDecl *Parameter : D->parameters()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000312 if (PrintComma)
313 Out << ", ";
314 else
315 PrintComma = true;
David Majnemera3debed2016-06-24 05:33:44 +0000316 Out << *Parameter;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000317 }
318 Out << ")";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000319
320 // Check the semantic DeclContext.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000321 const DeclContext* SemaDC = D->getDeclContext();
322 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000323 if (SemaDC != LexicalDC)
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000324 Out << " [[" << SemaDC << "]]";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000325
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000326 break;
327 }
328 case Decl::CXXConstructor: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000329 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000330 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000331 Out << "[c++ ctor] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000332 else if (D->isImplicit())
333 Out << "(c++ ctor) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000334 else
335 Out << "<c++ ctor> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000336 Out << *D;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000337 // Print the parameters.
338 Out << "(";
339 bool PrintComma = false;
David Majnemera3debed2016-06-24 05:33:44 +0000340 for (ParmVarDecl *Parameter : D->parameters()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000341 if (PrintComma)
342 Out << ", ";
343 else
344 PrintComma = true;
David Majnemera3debed2016-06-24 05:33:44 +0000345 Out << *Parameter;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000346 }
347 Out << ")";
348
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::CXXDestructor: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000357 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000358 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000359 Out << "[c++ dtor] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000360 else if (D->isImplicit())
361 Out << "(c++ dtor) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000362 else
363 Out << "<c++ dtor> ";
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 case Decl::CXXConversion: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000373 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000374 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000375 Out << "[c++ conversion] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000376 else if (D->isImplicit())
377 Out << "(c++ conversion) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000378 else
379 Out << "<c++ conversion> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000380 Out << *D;
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000381 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000382 const DeclContext* SemaDC = D->getDeclContext();
383 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000384 if (SemaDC != LexicalDC)
385 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000386 break;
387 }
388
Alex Lorenzeebc4942017-01-03 12:11:17 +0000389 case Decl::ClassTemplateSpecialization: {
390 const auto *CTSD = cast<ClassTemplateSpecializationDecl>(DC);
391 if (CTSD->isCompleteDefinition())
392 Out << "[class template specialization] ";
393 else
394 Out << "<class template specialization> ";
395 Out << *CTSD;
396 break;
397 }
398
399 case Decl::ClassTemplatePartialSpecialization: {
400 const auto *CTPSD = cast<ClassTemplatePartialSpecializationDecl>(DC);
401 if (CTPSD->isCompleteDefinition())
402 Out << "[class template partial specialization] ";
403 else
404 Out << "<class template partial specialization> ";
405 Out << *CTPSD;
406 break;
407 }
408
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000409 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000410 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000411 }
412
413 Out << "\n";
414
415 // Print decls in the DeclContext.
Aaron Ballman629afae2014-03-07 19:56:05 +0000416 for (auto *I : DC->decls()) {
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000417 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump74a76472009-02-10 20:16:46 +0000418 Out << " ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000419
420 Decl::Kind DK = I->getKind();
421 switch (DK) {
422 case Decl::Namespace:
423 case Decl::Enum:
424 case Decl::Record:
425 case Decl::CXXRecord:
426 case Decl::ObjCMethod:
427 case Decl::ObjCInterface:
Mike Stump11289f42009-09-09 15:08:12 +0000428 case Decl::ObjCCategory:
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000429 case Decl::ObjCProtocol:
430 case Decl::ObjCImplementation:
431 case Decl::ObjCCategoryImpl:
432 case Decl::LinkageSpec:
433 case Decl::Block:
434 case Decl::Function:
435 case Decl::CXXMethod:
436 case Decl::CXXConstructor:
437 case Decl::CXXDestructor:
438 case Decl::CXXConversion:
Alex Lorenzeebc4942017-01-03 12:11:17 +0000439 case Decl::ClassTemplateSpecialization:
440 case Decl::ClassTemplatePartialSpecialization: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000441 DeclContext* DC = cast<DeclContext>(I);
Mike Stump74a76472009-02-10 20:16:46 +0000442 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000443 break;
444 }
Francois Pichet48e2d9e2010-12-21 03:08:02 +0000445 case Decl::IndirectField: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000446 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000447 Out << "<IndirectField> " << *IFD << '\n';
Francois Pichet48e2d9e2010-12-21 03:08:02 +0000448 break;
449 }
Chris Lattnerd12b4802011-02-18 00:52:55 +0000450 case Decl::Label: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000451 LabelDecl *LD = cast<LabelDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000452 Out << "<Label> " << *LD << '\n';
Chris Lattnerd12b4802011-02-18 00:52:55 +0000453 break;
454 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000455 case Decl::Field: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000456 FieldDecl *FD = cast<FieldDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000457 Out << "<field> " << *FD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000458 break;
459 }
Richard Smithdda56e42011-04-15 14:24:37 +0000460 case Decl::Typedef:
461 case Decl::TypeAlias: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000462 TypedefNameDecl* TD = cast<TypedefNameDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000463 Out << "<typedef> " << *TD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000464 break;
465 }
466 case Decl::EnumConstant: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000467 EnumConstantDecl* ECD = cast<EnumConstantDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000468 Out << "<enum constant> " << *ECD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000469 break;
470 }
471 case Decl::Var: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000472 VarDecl* VD = cast<VarDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000473 Out << "<var> " << *VD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000474 break;
475 }
476 case Decl::ImplicitParam: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000477 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000478 Out << "<implicit parameter> " << *IPD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000479 break;
480 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000481 case Decl::ParmVar: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000482 ParmVarDecl* PVD = cast<ParmVarDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000483 Out << "<parameter> " << *PVD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000484 break;
485 }
486 case Decl::ObjCProperty: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000487 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000488 Out << "<objc property> " << *OPD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000489 break;
490 }
Eli Friedman897bc032009-12-08 06:22:37 +0000491 case Decl::FunctionTemplate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000492 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000493 Out << "<function template> " << *FTD << '\n';
Eli Friedman897bc032009-12-08 06:22:37 +0000494 break;
495 }
Eli Friedmanc6f59b12010-01-03 02:01:11 +0000496 case Decl::FileScopeAsm: {
497 Out << "<file-scope asm>\n";
498 break;
499 }
500 case Decl::UsingDirective: {
501 Out << "<using directive>\n";
502 break;
503 }
504 case Decl::NamespaceAlias: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000505 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000506 Out << "<namespace alias> " << *NAD << '\n';
Eli Friedmanc6f59b12010-01-03 02:01:11 +0000507 break;
508 }
Zhongxing Xu00c60462010-01-20 03:21:28 +0000509 case Decl::ClassTemplate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000510 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000511 Out << "<class template> " << *CTD << '\n';
Zhongxing Xu00c60462010-01-20 03:21:28 +0000512 break;
513 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000514 case Decl::OMPThreadPrivate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000515 Out << "<omp threadprivate> " << '"' << I << "\"\n";
Alexey Bataeva769e072013-03-22 06:34:35 +0000516 break;
517 }
Alex Lorenz14abc7f2017-01-03 12:07:20 +0000518 case Decl::Friend: {
519 Out << "<friend>";
520 if (const NamedDecl *ND = cast<FriendDecl>(I)->getFriendDecl())
521 Out << ' ' << *ND;
522 Out << "\n";
523 break;
524 }
Alex Lorenz1d86ab42017-01-03 12:08:40 +0000525 case Decl::Using: {
526 Out << "<using> " << *cast<UsingDecl>(I) << "\n";
527 break;
528 }
529 case Decl::UsingShadow: {
530 Out << "<using shadow> " << *cast<UsingShadowDecl>(I) << "\n";
531 break;
532 }
Alex Lorenz009978b2017-01-03 12:09:39 +0000533 case Decl::Empty: {
534 Out << "<empty>\n";
535 break;
536 }
Alex Lorenz21c32932017-01-03 12:12:36 +0000537 case Decl::AccessSpec: {
538 Out << "<access specifier>\n";
539 break;
540 }
Alex Lorenza1864172017-01-03 12:14:59 +0000541 case Decl::VarTemplate: {
542 Out << "<var template> " << *cast<VarTemplateDecl>(I) << "\n";
543 break;
544 }
Alex Lorenzb44b4cb2017-01-03 12:16:02 +0000545 case Decl::StaticAssert: {
546 Out << "<static assert>\n";
547 break;
548 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000549 default:
Aaron Ballman629afae2014-03-07 19:56:05 +0000550 Out << "DeclKind: " << DK << '"' << I << "\"\n";
David Blaikie83d382b2011-09-23 05:06:16 +0000551 llvm_unreachable("decl unhandled");
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000552 }
553 }
554}
David Blaikie6beb6aa2014-08-10 19:56:51 +0000555std::unique_ptr<ASTConsumer> clang::CreateDeclContextPrinter() {
556 return llvm::make_unique<DeclContextPrinter>();
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000557}