blob: 7dc475e26f76969429a80789f66c34af47cee27a [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";
Richard Smith3a36ac12017-03-09 22:00:01 +000090 } else if (OutputKind == Print)
Richard Smith6ea05822013-06-24 01:45:33 +000091 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
Richard Smith3a36ac12017-03-09 22:00:01 +000092 else if (OutputKind != None)
93 D->dump(Out, OutputKind == DumpFull);
Richard Smith6ea05822013-06-24 01:45:33 +000094 }
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000095
96 raw_ostream &Out;
Peter Collingbourne03f89072016-07-15 00:55:40 +000097 std::unique_ptr<raw_ostream> OwnedOut;
Richard Smith3a36ac12017-03-09 22:00:01 +000098
99 /// How to output individual declarations.
100 Kind OutputKind;
101
102 /// Which declarations or DeclContexts to display.
Alexander Kornienko3db68ee2012-07-26 16:01:23 +0000103 std::string FilterString;
Richard Smith3a36ac12017-03-09 22:00:01 +0000104
105 /// Whether the primary output is lookup results or declarations. Individual
106 /// results will be output with a format determined by OutputKind. This is
107 /// incompatible with OutputKind == Print.
Richard Smith6ea05822013-06-24 01:45:33 +0000108 bool DumpLookups;
Chris Lattner09c39db2007-09-15 23:02:28 +0000109 };
Alexander Kornienko4de03592012-07-31 09:37:40 +0000110
111 class ASTDeclNodeLister : public ASTConsumer,
112 public RecursiveASTVisitor<ASTDeclNodeLister> {
Alexander Kornienko4de03592012-07-31 09:37:40 +0000113 public:
Craig Topper49a27902014-05-22 04:46:25 +0000114 ASTDeclNodeLister(raw_ostream *Out = nullptr)
Alexander Kornienko4de03592012-07-31 09:37:40 +0000115 : Out(Out ? *Out : llvm::outs()) {}
116
Craig Topperafa7cb32014-03-13 06:07:04 +0000117 void HandleTranslationUnit(ASTContext &Context) override {
Alexander Kornienko4de03592012-07-31 09:37:40 +0000118 TraverseDecl(Context.getTranslationUnitDecl());
119 }
120
121 bool shouldWalkTypesOfTypeLocs() const { return false; }
122
Craig Topper45670532014-03-13 07:14:47 +0000123 bool VisitNamedDecl(NamedDecl *D) {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000124 D->printQualifiedName(Out);
125 Out << '\n';
Alexander Kornienko4de03592012-07-31 09:37:40 +0000126 return true;
127 }
128
129 private:
130 raw_ostream &Out;
131 };
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000132} // end anonymous namespace
Chris Lattnercbe4f772007-08-08 22:51:59 +0000133
Peter Collingbourne03f89072016-07-15 00:55:40 +0000134std::unique_ptr<ASTConsumer>
135clang::CreateASTPrinter(std::unique_ptr<raw_ostream> Out,
136 StringRef FilterString) {
Richard Smith3a36ac12017-03-09 22:00:01 +0000137 return llvm::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print,
Peter Collingbourne03f89072016-07-15 00:55:40 +0000138 FilterString);
Ted Kremenek59337682007-11-28 21:32:21 +0000139}
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000140
David Blaikie6beb6aa2014-08-10 19:56:51 +0000141std::unique_ptr<ASTConsumer> clang::CreateASTDumper(StringRef FilterString,
Richard Smith35f986d2014-08-11 22:11:07 +0000142 bool DumpDecls,
Richard Smith3a36ac12017-03-09 22:00:01 +0000143 bool Deserialize,
David Blaikie6beb6aa2014-08-10 19:56:51 +0000144 bool DumpLookups) {
Aaron Ballman08d3b392017-06-15 00:00:08 +0000145 assert((DumpDecls || Deserialize || DumpLookups) && "nothing to dump");
Richard Smith3a36ac12017-03-09 22:00:01 +0000146 return llvm::make_unique<ASTPrinter>(nullptr,
147 Deserialize ? ASTPrinter::DumpFull :
148 DumpDecls ? ASTPrinter::Dump :
149 ASTPrinter::None,
150 FilterString, DumpLookups);
Douglas Gregor32887f02009-04-26 02:02:08 +0000151}
Chris Lattner09c39db2007-09-15 23:02:28 +0000152
David Blaikie6beb6aa2014-08-10 19:56:51 +0000153std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
154 return llvm::make_unique<ASTDeclNodeLister>(nullptr);
Alexander Kornienko4de03592012-07-31 09:37:40 +0000155}
156
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000157//===----------------------------------------------------------------------===//
158/// ASTViewer - AST Visualization
159
Ted Kremenek66d130a2007-09-19 21:29:43 +0000160namespace {
161 class ASTViewer : public ASTConsumer {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000162 ASTContext *Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000163 public:
Craig Topperafa7cb32014-03-13 06:07:04 +0000164 void Initialize(ASTContext &Context) override {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000165 this->Context = &Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000166 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000167
Craig Topperafa7cb32014-03-13 06:07:04 +0000168 bool HandleTopLevelDecl(DeclGroupRef D) override {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000169 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
170 HandleTopLevelSingleDecl(*I);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000171 return true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000172 }
Mike Stump11289f42009-09-09 15:08:12 +0000173
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000174 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek66d130a2007-09-19 21:29:43 +0000175 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000176}
Ted Kremenek66d130a2007-09-19 21:29:43 +0000177
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000178void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Argyrios Kyrtzidis46f556d2010-07-07 11:31:23 +0000179 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
180 D->print(llvm::errs());
181
182 if (Stmt *Body = D->getBody()) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000183 llvm::errs() << '\n';
Douglas Gregore2350a32009-09-12 00:08:48 +0000184 Body->viewAST();
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000185 llvm::errs() << '\n';
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000186 }
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000187 }
188}
189
David Blaikie6beb6aa2014-08-10 19:56:51 +0000190std::unique_ptr<ASTConsumer> clang::CreateASTViewer() {
191 return llvm::make_unique<ASTViewer>();
192}
Ted Kremenek66d130a2007-09-19 21:29:43 +0000193
Ted Kremenek45c9e962007-09-07 23:47:56 +0000194//===----------------------------------------------------------------------===//
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000195/// DeclContextPrinter - Decl and DeclContext Visualization
196
197namespace {
198
199class DeclContextPrinter : public ASTConsumer {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000200 raw_ostream& Out;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000201public:
202 DeclContextPrinter() : Out(llvm::errs()) {}
203
Craig Topperafa7cb32014-03-13 06:07:04 +0000204 void HandleTranslationUnit(ASTContext &C) override {
Chris Lattnercf169832009-03-28 04:11:33 +0000205 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000206 }
207
208 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
209};
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000210} // end anonymous namespace
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000211
Mike Stump11289f42009-09-09 15:08:12 +0000212void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000213 unsigned Indentation) {
214 // Print DeclContext name.
Argyrios Kyrtzidisbe40b7e2009-01-13 13:11:58 +0000215 switch (DC->getDeclKind()) {
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000216 case Decl::TranslationUnit:
217 Out << "[translation unit] " << DC;
218 break;
219 case Decl::Namespace: {
220 Out << "[namespace] ";
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000221 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000222 Out << *ND;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000223 break;
224 }
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000225 case Decl::Enum: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000226 const EnumDecl* ED = cast<EnumDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000227 if (ED->isCompleteDefinition())
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000228 Out << "[enum] ";
229 else
230 Out << "<enum> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000231 Out << *ED;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000232 break;
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000233 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000234 case Decl::Record: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000235 const RecordDecl* RD = cast<RecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000236 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000237 Out << "[struct] ";
238 else
239 Out << "<struct> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000240 Out << *RD;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000241 break;
242 }
243 case Decl::CXXRecord: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000244 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000245 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000246 Out << "[class] ";
247 else
248 Out << "<class> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000249 Out << *RD << ' ' << DC;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000250 break;
251 }
252 case Decl::ObjCMethod:
253 Out << "[objc method]";
254 break;
255 case Decl::ObjCInterface:
256 Out << "[objc interface]";
257 break;
258 case Decl::ObjCCategory:
259 Out << "[objc category]";
260 break;
261 case Decl::ObjCProtocol:
262 Out << "[objc protocol]";
263 break;
264 case Decl::ObjCImplementation:
265 Out << "[objc implementation]";
266 break;
267 case Decl::ObjCCategoryImpl:
268 Out << "[objc categoryimpl]";
269 break;
270 case Decl::LinkageSpec:
271 Out << "[linkage spec]";
272 break;
273 case Decl::Block:
274 Out << "[block]";
275 break;
276 case Decl::Function: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000277 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000278 if (FD->doesThisDeclarationHaveABody())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000279 Out << "[function] ";
280 else
281 Out << "<function> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000282 Out << *FD;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000283 // Print the parameters.
284 Out << "(";
285 bool PrintComma = false;
David Majnemer59f77922016-06-24 04:05:48 +0000286 for (auto I : FD->parameters()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000287 if (PrintComma)
288 Out << ", ";
289 else
290 PrintComma = true;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000291 Out << *I;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000292 }
293 Out << ")";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000294 break;
295 }
296 case Decl::CXXMethod: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000297 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000298 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000299 Out << "[c++ method] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000300 else if (D->isImplicit())
301 Out << "(c++ method) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000302 else
303 Out << "<c++ method> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000304 Out << *D;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000305 // Print the parameters.
306 Out << "(";
307 bool PrintComma = false;
David Majnemera3debed2016-06-24 05:33:44 +0000308 for (ParmVarDecl *Parameter : D->parameters()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000309 if (PrintComma)
310 Out << ", ";
311 else
312 PrintComma = true;
David Majnemera3debed2016-06-24 05:33:44 +0000313 Out << *Parameter;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000314 }
315 Out << ")";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000316
317 // Check the semantic DeclContext.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000318 const DeclContext* SemaDC = D->getDeclContext();
319 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000320 if (SemaDC != LexicalDC)
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000321 Out << " [[" << SemaDC << "]]";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000322
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000323 break;
324 }
325 case Decl::CXXConstructor: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000326 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000327 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000328 Out << "[c++ ctor] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000329 else if (D->isImplicit())
330 Out << "(c++ ctor) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000331 else
332 Out << "<c++ ctor> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000333 Out << *D;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000334 // Print the parameters.
335 Out << "(";
336 bool PrintComma = false;
David Majnemera3debed2016-06-24 05:33:44 +0000337 for (ParmVarDecl *Parameter : D->parameters()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000338 if (PrintComma)
339 Out << ", ";
340 else
341 PrintComma = true;
David Majnemera3debed2016-06-24 05:33:44 +0000342 Out << *Parameter;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000343 }
344 Out << ")";
345
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000346 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000347 const DeclContext* SemaDC = D->getDeclContext();
348 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000349 if (SemaDC != LexicalDC)
350 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000351 break;
352 }
353 case Decl::CXXDestructor: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000354 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000355 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000356 Out << "[c++ dtor] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000357 else if (D->isImplicit())
358 Out << "(c++ dtor) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000359 else
360 Out << "<c++ dtor> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000361 Out << *D;
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000362 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000363 const DeclContext* SemaDC = D->getDeclContext();
364 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000365 if (SemaDC != LexicalDC)
366 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000367 break;
368 }
369 case Decl::CXXConversion: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000370 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000371 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000372 Out << "[c++ conversion] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000373 else if (D->isImplicit())
374 Out << "(c++ conversion) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000375 else
376 Out << "<c++ conversion> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000377 Out << *D;
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000378 // Check the semantic DC.
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000379 const DeclContext* SemaDC = D->getDeclContext();
380 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000381 if (SemaDC != LexicalDC)
382 Out << " [[" << SemaDC << "]]";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000383 break;
384 }
385
Alex Lorenzeebc4942017-01-03 12:11:17 +0000386 case Decl::ClassTemplateSpecialization: {
387 const auto *CTSD = cast<ClassTemplateSpecializationDecl>(DC);
388 if (CTSD->isCompleteDefinition())
389 Out << "[class template specialization] ";
390 else
391 Out << "<class template specialization> ";
392 Out << *CTSD;
393 break;
394 }
395
396 case Decl::ClassTemplatePartialSpecialization: {
397 const auto *CTPSD = cast<ClassTemplatePartialSpecializationDecl>(DC);
398 if (CTPSD->isCompleteDefinition())
399 Out << "[class template partial specialization] ";
400 else
401 Out << "<class template partial specialization> ";
402 Out << *CTPSD;
403 break;
404 }
405
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000406 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000407 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000408 }
409
410 Out << "\n";
411
412 // Print decls in the DeclContext.
Aaron Ballman629afae2014-03-07 19:56:05 +0000413 for (auto *I : DC->decls()) {
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000414 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump74a76472009-02-10 20:16:46 +0000415 Out << " ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000416
417 Decl::Kind DK = I->getKind();
418 switch (DK) {
419 case Decl::Namespace:
420 case Decl::Enum:
421 case Decl::Record:
422 case Decl::CXXRecord:
423 case Decl::ObjCMethod:
424 case Decl::ObjCInterface:
Mike Stump11289f42009-09-09 15:08:12 +0000425 case Decl::ObjCCategory:
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000426 case Decl::ObjCProtocol:
427 case Decl::ObjCImplementation:
428 case Decl::ObjCCategoryImpl:
429 case Decl::LinkageSpec:
430 case Decl::Block:
431 case Decl::Function:
432 case Decl::CXXMethod:
433 case Decl::CXXConstructor:
434 case Decl::CXXDestructor:
435 case Decl::CXXConversion:
Alex Lorenzeebc4942017-01-03 12:11:17 +0000436 case Decl::ClassTemplateSpecialization:
437 case Decl::ClassTemplatePartialSpecialization: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000438 DeclContext* DC = cast<DeclContext>(I);
Mike Stump74a76472009-02-10 20:16:46 +0000439 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000440 break;
441 }
Francois Pichet48e2d9e2010-12-21 03:08:02 +0000442 case Decl::IndirectField: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000443 IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000444 Out << "<IndirectField> " << *IFD << '\n';
Francois Pichet48e2d9e2010-12-21 03:08:02 +0000445 break;
446 }
Chris Lattnerd12b4802011-02-18 00:52:55 +0000447 case Decl::Label: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000448 LabelDecl *LD = cast<LabelDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000449 Out << "<Label> " << *LD << '\n';
Chris Lattnerd12b4802011-02-18 00:52:55 +0000450 break;
451 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000452 case Decl::Field: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000453 FieldDecl *FD = cast<FieldDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000454 Out << "<field> " << *FD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000455 break;
456 }
Richard Smithdda56e42011-04-15 14:24:37 +0000457 case Decl::Typedef:
458 case Decl::TypeAlias: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000459 TypedefNameDecl* TD = cast<TypedefNameDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000460 Out << "<typedef> " << *TD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000461 break;
462 }
463 case Decl::EnumConstant: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000464 EnumConstantDecl* ECD = cast<EnumConstantDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000465 Out << "<enum constant> " << *ECD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000466 break;
467 }
468 case Decl::Var: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000469 VarDecl* VD = cast<VarDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000470 Out << "<var> " << *VD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000471 break;
472 }
473 case Decl::ImplicitParam: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000474 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000475 Out << "<implicit parameter> " << *IPD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000476 break;
477 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000478 case Decl::ParmVar: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000479 ParmVarDecl* PVD = cast<ParmVarDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000480 Out << "<parameter> " << *PVD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000481 break;
482 }
483 case Decl::ObjCProperty: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000484 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000485 Out << "<objc property> " << *OPD << '\n';
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000486 break;
487 }
Eli Friedman897bc032009-12-08 06:22:37 +0000488 case Decl::FunctionTemplate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000489 FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000490 Out << "<function template> " << *FTD << '\n';
Eli Friedman897bc032009-12-08 06:22:37 +0000491 break;
492 }
Eli Friedmanc6f59b12010-01-03 02:01:11 +0000493 case Decl::FileScopeAsm: {
494 Out << "<file-scope asm>\n";
495 break;
496 }
497 case Decl::UsingDirective: {
498 Out << "<using directive>\n";
499 break;
500 }
501 case Decl::NamespaceAlias: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000502 NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000503 Out << "<namespace alias> " << *NAD << '\n';
Eli Friedmanc6f59b12010-01-03 02:01:11 +0000504 break;
505 }
Zhongxing Xu00c60462010-01-20 03:21:28 +0000506 case Decl::ClassTemplate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000507 ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(I);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000508 Out << "<class template> " << *CTD << '\n';
Zhongxing Xu00c60462010-01-20 03:21:28 +0000509 break;
510 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000511 case Decl::OMPThreadPrivate: {
Aaron Ballman629afae2014-03-07 19:56:05 +0000512 Out << "<omp threadprivate> " << '"' << I << "\"\n";
Alexey Bataeva769e072013-03-22 06:34:35 +0000513 break;
514 }
Alex Lorenz14abc7f2017-01-03 12:07:20 +0000515 case Decl::Friend: {
516 Out << "<friend>";
517 if (const NamedDecl *ND = cast<FriendDecl>(I)->getFriendDecl())
518 Out << ' ' << *ND;
519 Out << "\n";
520 break;
521 }
Alex Lorenz1d86ab42017-01-03 12:08:40 +0000522 case Decl::Using: {
523 Out << "<using> " << *cast<UsingDecl>(I) << "\n";
524 break;
525 }
526 case Decl::UsingShadow: {
527 Out << "<using shadow> " << *cast<UsingShadowDecl>(I) << "\n";
528 break;
529 }
Alex Lorenz009978b2017-01-03 12:09:39 +0000530 case Decl::Empty: {
531 Out << "<empty>\n";
532 break;
533 }
Alex Lorenz21c32932017-01-03 12:12:36 +0000534 case Decl::AccessSpec: {
535 Out << "<access specifier>\n";
536 break;
537 }
Alex Lorenza1864172017-01-03 12:14:59 +0000538 case Decl::VarTemplate: {
539 Out << "<var template> " << *cast<VarTemplateDecl>(I) << "\n";
540 break;
541 }
Alex Lorenzb44b4cb2017-01-03 12:16:02 +0000542 case Decl::StaticAssert: {
543 Out << "<static assert>\n";
544 break;
545 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000546 default:
Aaron Ballman629afae2014-03-07 19:56:05 +0000547 Out << "DeclKind: " << DK << '"' << I << "\"\n";
David Blaikie83d382b2011-09-23 05:06:16 +0000548 llvm_unreachable("decl unhandled");
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000549 }
550 }
551}
David Blaikie6beb6aa2014-08-10 19:56:51 +0000552std::unique_ptr<ASTConsumer> clang::CreateDeclContextPrinter() {
553 return llvm::make_unique<DeclContextPrinter>();
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000554}