blob: 329f15fbef1f2de10b4cd8bbacf9d13eb5526b61 [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:
Peter Collingbourne03f89072016-07-15 00:55:40 +000037 ASTPrinter(std::unique_ptr<raw_ostream> Out = nullptr, bool Dump = false,
Richard Smith6ea05822013-06-24 01:45:33 +000038 StringRef FilterString = "", bool DumpLookups = false)
Peter Collingbourne03f89072016-07-15 00:55:40 +000039 : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)), Dump(Dump),
Richard Smith6ea05822013-06-24 01:45:33 +000040 FilterString(FilterString), DumpLookups(DumpLookups) {}
Mike Stump11289f42009-09-09 15:08:12 +000041
Craig Topperafa7cb32014-03-13 06:07:04 +000042 void HandleTranslationUnit(ASTContext &Context) override {
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000043 TranslationUnitDecl *D = Context.getTranslationUnitDecl();
44
Richard Smith6ea05822013-06-24 01:45:33 +000045 if (FilterString.empty())
46 return print(D);
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000047
48 TraverseDecl(D);
Bill Wendlingaec64c32007-06-23 00:39:57 +000049 }
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000050
51 bool shouldWalkTypesOfTypeLocs() const { return false; }
52
53 bool TraverseDecl(Decl *D) {
Craig Topper49a27902014-05-22 04:46:25 +000054 if (D && filterMatches(D)) {
Dmitri Gribenko32333912012-11-21 10:54:55 +000055 bool ShowColors = Out.has_colors();
56 if (ShowColors)
Dmitri Gribenkof8579502013-01-12 19:30:44 +000057 Out.changeColor(raw_ostream::BLUE);
Richard Smith35f986d2014-08-11 22:11:07 +000058 Out << ((Dump || DumpLookups) ? "Dumping " : "Printing ") << getName(D)
59 << ":\n";
Dmitri Gribenko32333912012-11-21 10:54:55 +000060 if (ShowColors)
61 Out.resetColor();
Richard Smith6ea05822013-06-24 01:45:33 +000062 print(D);
Alexander Kornienko20186182012-08-17 17:38:39 +000063 Out << "\n";
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000064 // Don't traverse child nodes to avoid output duplication.
65 return true;
66 }
67 return base::TraverseDecl(D);
68 }
69
70 private:
71 std::string getName(Decl *D) {
72 if (isa<NamedDecl>(D))
73 return cast<NamedDecl>(D)->getQualifiedNameAsString();
74 return "";
75 }
76 bool filterMatches(Decl *D) {
77 return getName(D).find(FilterString) != std::string::npos;
78 }
Richard Smith6ea05822013-06-24 01:45:33 +000079 void print(Decl *D) {
80 if (DumpLookups) {
Richard Smith35f986d2014-08-11 22:11:07 +000081 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
82 if (DC == DC->getPrimaryContext())
83 DC->dumpLookups(Out, Dump);
84 else
85 Out << "Lookup map is in primary DeclContext "
86 << DC->getPrimaryContext() << "\n";
87 } else
Richard Smith6ea05822013-06-24 01:45:33 +000088 Out << "Not a DeclContext\n";
89 } else if (Dump)
90 D->dump(Out);
91 else
92 D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
93 }
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000094
95 raw_ostream &Out;
Peter Collingbourne03f89072016-07-15 00:55:40 +000096 std::unique_ptr<raw_ostream> OwnedOut;
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000097 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
Peter Collingbourne03f89072016-07-15 00:55:40 +0000125std::unique_ptr<ASTConsumer>
126clang::CreateASTPrinter(std::unique_ptr<raw_ostream> Out,
127 StringRef FilterString) {
128 return llvm::make_unique<ASTPrinter>(std::move(Out), /*Dump=*/false,
129 FilterString);
Ted Kremenek59337682007-11-28 21:32:21 +0000130}
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000131
David Blaikie6beb6aa2014-08-10 19:56:51 +0000132std::unique_ptr<ASTConsumer> clang::CreateASTDumper(StringRef FilterString,
Richard Smith35f986d2014-08-11 22:11:07 +0000133 bool DumpDecls,
David Blaikie6beb6aa2014-08-10 19:56:51 +0000134 bool DumpLookups) {
Richard Smith35f986d2014-08-11 22:11:07 +0000135 assert((DumpDecls || DumpLookups) && "nothing to dump");
136 return llvm::make_unique<ASTPrinter>(nullptr, DumpDecls, FilterString,
David Blaikie6beb6aa2014-08-10 19:56:51 +0000137 DumpLookups);
Douglas Gregor32887f02009-04-26 02:02:08 +0000138}
Chris Lattner09c39db2007-09-15 23:02:28 +0000139
David Blaikie6beb6aa2014-08-10 19:56:51 +0000140std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
141 return llvm::make_unique<ASTDeclNodeLister>(nullptr);
Alexander Kornienko4de03592012-07-31 09:37:40 +0000142}
143
Ted Kremenek7c81bdd2007-11-27 21:46:50 +0000144//===----------------------------------------------------------------------===//
145/// ASTViewer - AST Visualization
146
Ted Kremenek66d130a2007-09-19 21:29:43 +0000147namespace {
148 class ASTViewer : public ASTConsumer {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000149 ASTContext *Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000150 public:
Craig Topperafa7cb32014-03-13 06:07:04 +0000151 void Initialize(ASTContext &Context) override {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000152 this->Context = &Context;
Ted Kremenek66d130a2007-09-19 21:29:43 +0000153 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000154
Craig Topperafa7cb32014-03-13 06:07:04 +0000155 bool HandleTopLevelDecl(DeclGroupRef D) override {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000156 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
157 HandleTopLevelSingleDecl(*I);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000158 return true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000159 }
Mike Stump11289f42009-09-09 15:08:12 +0000160
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000161 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek66d130a2007-09-19 21:29:43 +0000162 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000163}
Ted Kremenek66d130a2007-09-19 21:29:43 +0000164
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000165void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Argyrios Kyrtzidis46f556d2010-07-07 11:31:23 +0000166 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
167 D->print(llvm::errs());
168
169 if (Stmt *Body = D->getBody()) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000170 llvm::errs() << '\n';
Douglas Gregore2350a32009-09-12 00:08:48 +0000171 Body->viewAST();
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000172 llvm::errs() << '\n';
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000173 }
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000174 }
175}
176
David Blaikie6beb6aa2014-08-10 19:56:51 +0000177std::unique_ptr<ASTConsumer> clang::CreateASTViewer() {
178 return llvm::make_unique<ASTViewer>();
179}
Ted Kremenek66d130a2007-09-19 21:29:43 +0000180
Ted Kremenek45c9e962007-09-07 23:47:56 +0000181//===----------------------------------------------------------------------===//
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000182/// DeclContextPrinter - Decl and DeclContext Visualization
183
184namespace {
185
186class DeclContextPrinter : public ASTConsumer {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000187 raw_ostream& Out;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000188public:
189 DeclContextPrinter() : Out(llvm::errs()) {}
190
Craig Topperafa7cb32014-03-13 06:07:04 +0000191 void HandleTranslationUnit(ASTContext &C) override {
Chris Lattnercf169832009-03-28 04:11:33 +0000192 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000193 }
194
195 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
196};
Chris Lattnerc0c3dff2009-03-28 05:44:17 +0000197} // end anonymous namespace
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000198
Mike Stump11289f42009-09-09 15:08:12 +0000199void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000200 unsigned Indentation) {
201 // Print DeclContext name.
Argyrios Kyrtzidisbe40b7e2009-01-13 13:11:58 +0000202 switch (DC->getDeclKind()) {
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000203 case Decl::TranslationUnit:
204 Out << "[translation unit] " << DC;
205 break;
206 case Decl::Namespace: {
207 Out << "[namespace] ";
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000208 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000209 Out << *ND;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000210 break;
211 }
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000212 case Decl::Enum: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000213 const EnumDecl* ED = cast<EnumDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000214 if (ED->isCompleteDefinition())
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000215 Out << "[enum] ";
216 else
217 Out << "<enum> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000218 Out << *ED;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000219 break;
Zhongxing Xu100f30b2009-01-13 02:41:08 +0000220 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000221 case Decl::Record: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000222 const RecordDecl* RD = cast<RecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000223 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000224 Out << "[struct] ";
225 else
226 Out << "<struct> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000227 Out << *RD;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000228 break;
229 }
230 case Decl::CXXRecord: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000231 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
John McCallf937c022011-10-07 06:10:15 +0000232 if (RD->isCompleteDefinition())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000233 Out << "[class] ";
234 else
235 Out << "<class> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000236 Out << *RD << ' ' << DC;
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000237 break;
238 }
239 case Decl::ObjCMethod:
240 Out << "[objc method]";
241 break;
242 case Decl::ObjCInterface:
243 Out << "[objc interface]";
244 break;
245 case Decl::ObjCCategory:
246 Out << "[objc category]";
247 break;
248 case Decl::ObjCProtocol:
249 Out << "[objc protocol]";
250 break;
251 case Decl::ObjCImplementation:
252 Out << "[objc implementation]";
253 break;
254 case Decl::ObjCCategoryImpl:
255 Out << "[objc categoryimpl]";
256 break;
257 case Decl::LinkageSpec:
258 Out << "[linkage spec]";
259 break;
260 case Decl::Block:
261 Out << "[block]";
262 break;
263 case Decl::Function: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000264 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000265 if (FD->doesThisDeclarationHaveABody())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000266 Out << "[function] ";
267 else
268 Out << "<function> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000269 Out << *FD;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000270 // Print the parameters.
271 Out << "(";
272 bool PrintComma = false;
David Majnemer59f77922016-06-24 04:05:48 +0000273 for (auto I : FD->parameters()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000274 if (PrintComma)
275 Out << ", ";
276 else
277 PrintComma = true;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000278 Out << *I;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000279 }
280 Out << ")";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000281 break;
282 }
283 case Decl::CXXMethod: {
Argyrios Kyrtzidis9b7bd9b2009-02-16 14:29:59 +0000284 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Argyrios Kyrtzidisc4b766b2009-06-17 22:49:50 +0000285 if (D->isOutOfLine())
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000286 Out << "[c++ method] ";
Zhongxing Xud90c6d92009-01-13 03:26:38 +0000287 else if (D->isImplicit())
288 Out << "(c++ method) ";
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000289 else
290 Out << "<c++ method> ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000291 Out << *D;
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000292 // Print the parameters.
293 Out << "(";
294 bool PrintComma = false;
David Majnemera3debed2016-06-24 05:33:44 +0000295 for (ParmVarDecl *Parameter : D->parameters()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000296 if (PrintComma)
297 Out << ", ";
298 else
299 PrintComma = true;
David Majnemera3debed2016-06-24 05:33:44 +0000300 Out << *Parameter;
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;
David Majnemera3debed2016-06-24 05:33:44 +0000324 for (ParmVarDecl *Parameter : D->parameters()) {
Zhongxing Xu13e82c02009-01-13 06:25:33 +0000325 if (PrintComma)
326 Out << ", ";
327 else
328 PrintComma = true;
David Majnemera3debed2016-06-24 05:33:44 +0000329 Out << *Parameter;
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 }
Alex Lorenz14abc7f2017-01-03 12:07:20 +0000481 case Decl::Friend: {
482 Out << "<friend>";
483 if (const NamedDecl *ND = cast<FriendDecl>(I)->getFriendDecl())
484 Out << ' ' << *ND;
485 Out << "\n";
486 break;
487 }
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000488 default:
Aaron Ballman629afae2014-03-07 19:56:05 +0000489 Out << "DeclKind: " << DK << '"' << I << "\"\n";
David Blaikie83d382b2011-09-23 05:06:16 +0000490 llvm_unreachable("decl unhandled");
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000491 }
492 }
493}
David Blaikie6beb6aa2014-08-10 19:56:51 +0000494std::unique_ptr<ASTConsumer> clang::CreateDeclContextPrinter() {
495 return llvm::make_unique<DeclContextPrinter>();
Zhongxing Xu9aabf022009-01-13 01:29:24 +0000496}