ASTConsumers.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. //===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // AST Consumer Implementations.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Frontend/ASTConsumers.h"
  14. #include "clang/AST/AST.h"
  15. #include "clang/AST/ASTConsumer.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/AST/PrettyPrinter.h"
  18. #include "clang/AST/RecordLayout.h"
  19. #include "clang/AST/RecursiveASTVisitor.h"
  20. #include "clang/Basic/Diagnostic.h"
  21. #include "clang/Basic/FileManager.h"
  22. #include "clang/Basic/SourceManager.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/Support/Path.h"
  25. #include "llvm/Support/Timer.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. using namespace clang;
  28. //===----------------------------------------------------------------------===//
  29. /// ASTPrinter - Pretty-printer and dumper of ASTs
  30. namespace {
  31. class ASTPrinter : public ASTConsumer,
  32. public RecursiveASTVisitor<ASTPrinter> {
  33. typedef RecursiveASTVisitor<ASTPrinter> base;
  34. public:
  35. ASTPrinter(raw_ostream *Out = nullptr, bool Dump = false,
  36. StringRef FilterString = "", bool DumpLookups = false)
  37. : Out(Out ? *Out : llvm::outs()), Dump(Dump),
  38. FilterString(FilterString), DumpLookups(DumpLookups) {}
  39. void HandleTranslationUnit(ASTContext &Context) override {
  40. TranslationUnitDecl *D = Context.getTranslationUnitDecl();
  41. if (FilterString.empty())
  42. return print(D);
  43. TraverseDecl(D);
  44. }
  45. bool shouldWalkTypesOfTypeLocs() const { return false; }
  46. bool TraverseDecl(Decl *D) {
  47. if (D && filterMatches(D)) {
  48. bool ShowColors = Out.has_colors();
  49. if (ShowColors)
  50. Out.changeColor(raw_ostream::BLUE);
  51. Out << ((Dump || DumpLookups) ? "Dumping " : "Printing ") << getName(D)
  52. << ":\n";
  53. if (ShowColors)
  54. Out.resetColor();
  55. print(D);
  56. Out << "\n";
  57. // Don't traverse child nodes to avoid output duplication.
  58. return true;
  59. }
  60. return base::TraverseDecl(D);
  61. }
  62. private:
  63. std::string getName(Decl *D) {
  64. if (isa<NamedDecl>(D))
  65. return cast<NamedDecl>(D)->getQualifiedNameAsString();
  66. return "";
  67. }
  68. bool filterMatches(Decl *D) {
  69. return getName(D).find(FilterString) != std::string::npos;
  70. }
  71. void print(Decl *D) {
  72. if (DumpLookups) {
  73. if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
  74. if (DC == DC->getPrimaryContext())
  75. DC->dumpLookups(Out, Dump);
  76. else
  77. Out << "Lookup map is in primary DeclContext "
  78. << DC->getPrimaryContext() << "\n";
  79. } else
  80. Out << "Not a DeclContext\n";
  81. } else if (Dump)
  82. D->dump(Out);
  83. else
  84. D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
  85. }
  86. raw_ostream &Out;
  87. bool Dump;
  88. std::string FilterString;
  89. bool DumpLookups;
  90. };
  91. class ASTDeclNodeLister : public ASTConsumer,
  92. public RecursiveASTVisitor<ASTDeclNodeLister> {
  93. public:
  94. ASTDeclNodeLister(raw_ostream *Out = nullptr)
  95. : Out(Out ? *Out : llvm::outs()) {}
  96. void HandleTranslationUnit(ASTContext &Context) override {
  97. TraverseDecl(Context.getTranslationUnitDecl());
  98. }
  99. bool shouldWalkTypesOfTypeLocs() const { return false; }
  100. bool VisitNamedDecl(NamedDecl *D) {
  101. D->printQualifiedName(Out);
  102. Out << '\n';
  103. return true;
  104. }
  105. private:
  106. raw_ostream &Out;
  107. };
  108. } // end anonymous namespace
  109. std::unique_ptr<ASTConsumer> clang::CreateASTPrinter(raw_ostream *Out,
  110. StringRef FilterString) {
  111. return llvm::make_unique<ASTPrinter>(Out, /*Dump=*/false, FilterString);
  112. }
  113. std::unique_ptr<ASTConsumer> clang::CreateASTDumper(StringRef FilterString,
  114. bool DumpDecls,
  115. bool DumpLookups) {
  116. assert((DumpDecls || DumpLookups) && "nothing to dump");
  117. return llvm::make_unique<ASTPrinter>(nullptr, DumpDecls, FilterString,
  118. DumpLookups);
  119. }
  120. std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
  121. return llvm::make_unique<ASTDeclNodeLister>(nullptr);
  122. }
  123. //===----------------------------------------------------------------------===//
  124. /// ASTViewer - AST Visualization
  125. namespace {
  126. class ASTViewer : public ASTConsumer {
  127. ASTContext *Context;
  128. public:
  129. void Initialize(ASTContext &Context) override {
  130. this->Context = &Context;
  131. }
  132. bool HandleTopLevelDecl(DeclGroupRef D) override {
  133. for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
  134. HandleTopLevelSingleDecl(*I);
  135. return true;
  136. }
  137. void HandleTopLevelSingleDecl(Decl *D);
  138. };
  139. }
  140. void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
  141. if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
  142. D->print(llvm::errs());
  143. if (Stmt *Body = D->getBody()) {
  144. llvm::errs() << '\n';
  145. Body->viewAST();
  146. llvm::errs() << '\n';
  147. }
  148. }
  149. }
  150. std::unique_ptr<ASTConsumer> clang::CreateASTViewer() {
  151. return llvm::make_unique<ASTViewer>();
  152. }
  153. //===----------------------------------------------------------------------===//
  154. /// DeclContextPrinter - Decl and DeclContext Visualization
  155. namespace {
  156. class DeclContextPrinter : public ASTConsumer {
  157. raw_ostream& Out;
  158. public:
  159. DeclContextPrinter() : Out(llvm::errs()) {}
  160. void HandleTranslationUnit(ASTContext &C) override {
  161. PrintDeclContext(C.getTranslationUnitDecl(), 4);
  162. }
  163. void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
  164. };
  165. } // end anonymous namespace
  166. void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
  167. unsigned Indentation) {
  168. // Print DeclContext name.
  169. switch (DC->getDeclKind()) {
  170. case Decl::TranslationUnit:
  171. Out << "[translation unit] " << DC;
  172. break;
  173. case Decl::Namespace: {
  174. Out << "[namespace] ";
  175. const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
  176. Out << *ND;
  177. break;
  178. }
  179. case Decl::Enum: {
  180. const EnumDecl* ED = cast<EnumDecl>(DC);
  181. if (ED->isCompleteDefinition())
  182. Out << "[enum] ";
  183. else
  184. Out << "<enum> ";
  185. Out << *ED;
  186. break;
  187. }
  188. case Decl::Record: {
  189. const RecordDecl* RD = cast<RecordDecl>(DC);
  190. if (RD->isCompleteDefinition())
  191. Out << "[struct] ";
  192. else
  193. Out << "<struct> ";
  194. Out << *RD;
  195. break;
  196. }
  197. case Decl::CXXRecord: {
  198. const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
  199. if (RD->isCompleteDefinition())
  200. Out << "[class] ";
  201. else
  202. Out << "<class> ";
  203. Out << *RD << ' ' << DC;
  204. break;
  205. }
  206. case Decl::ObjCMethod:
  207. Out << "[objc method]";
  208. break;
  209. case Decl::ObjCInterface:
  210. Out << "[objc interface]";
  211. break;
  212. case Decl::ObjCCategory:
  213. Out << "[objc category]";
  214. break;
  215. case Decl::ObjCProtocol:
  216. Out << "[objc protocol]";
  217. break;
  218. case Decl::ObjCImplementation:
  219. Out << "[objc implementation]";
  220. break;
  221. case Decl::ObjCCategoryImpl:
  222. Out << "[objc categoryimpl]";
  223. break;
  224. case Decl::LinkageSpec:
  225. Out << "[linkage spec]";
  226. break;
  227. case Decl::Block:
  228. Out << "[block]";
  229. break;
  230. case Decl::Function: {
  231. const FunctionDecl* FD = cast<FunctionDecl>(DC);
  232. if (FD->doesThisDeclarationHaveABody())
  233. Out << "[function] ";
  234. else
  235. Out << "<function> ";
  236. Out << *FD;
  237. // Print the parameters.
  238. Out << "(";
  239. bool PrintComma = false;
  240. for (auto I : FD->params()) {
  241. if (PrintComma)
  242. Out << ", ";
  243. else
  244. PrintComma = true;
  245. Out << *I;
  246. }
  247. Out << ")";
  248. break;
  249. }
  250. case Decl::CXXMethod: {
  251. const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
  252. if (D->isOutOfLine())
  253. Out << "[c++ method] ";
  254. else if (D->isImplicit())
  255. Out << "(c++ method) ";
  256. else
  257. Out << "<c++ method> ";
  258. Out << *D;
  259. // Print the parameters.
  260. Out << "(";
  261. bool PrintComma = false;
  262. for (FunctionDecl::param_const_iterator I = D->param_begin(),
  263. E = D->param_end(); I != E; ++I) {
  264. if (PrintComma)
  265. Out << ", ";
  266. else
  267. PrintComma = true;
  268. Out << **I;
  269. }
  270. Out << ")";
  271. // Check the semantic DeclContext.
  272. const DeclContext* SemaDC = D->getDeclContext();
  273. const DeclContext* LexicalDC = D->getLexicalDeclContext();
  274. if (SemaDC != LexicalDC)
  275. Out << " [[" << SemaDC << "]]";
  276. break;
  277. }
  278. case Decl::CXXConstructor: {
  279. const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
  280. if (D->isOutOfLine())
  281. Out << "[c++ ctor] ";
  282. else if (D->isImplicit())
  283. Out << "(c++ ctor) ";
  284. else
  285. Out << "<c++ ctor> ";
  286. Out << *D;
  287. // Print the parameters.
  288. Out << "(";
  289. bool PrintComma = false;
  290. for (FunctionDecl::param_const_iterator I = D->param_begin(),
  291. E = D->param_end(); I != E; ++I) {
  292. if (PrintComma)
  293. Out << ", ";
  294. else
  295. PrintComma = true;
  296. Out << **I;
  297. }
  298. Out << ")";
  299. // Check the semantic DC.
  300. const DeclContext* SemaDC = D->getDeclContext();
  301. const DeclContext* LexicalDC = D->getLexicalDeclContext();
  302. if (SemaDC != LexicalDC)
  303. Out << " [[" << SemaDC << "]]";
  304. break;
  305. }
  306. case Decl::CXXDestructor: {
  307. const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
  308. if (D->isOutOfLine())
  309. Out << "[c++ dtor] ";
  310. else if (D->isImplicit())
  311. Out << "(c++ dtor) ";
  312. else
  313. Out << "<c++ dtor> ";
  314. Out << *D;
  315. // Check the semantic DC.
  316. const DeclContext* SemaDC = D->getDeclContext();
  317. const DeclContext* LexicalDC = D->getLexicalDeclContext();
  318. if (SemaDC != LexicalDC)
  319. Out << " [[" << SemaDC << "]]";
  320. break;
  321. }
  322. case Decl::CXXConversion: {
  323. const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
  324. if (D->isOutOfLine())
  325. Out << "[c++ conversion] ";
  326. else if (D->isImplicit())
  327. Out << "(c++ conversion) ";
  328. else
  329. Out << "<c++ conversion> ";
  330. Out << *D;
  331. // Check the semantic DC.
  332. const DeclContext* SemaDC = D->getDeclContext();
  333. const DeclContext* LexicalDC = D->getLexicalDeclContext();
  334. if (SemaDC != LexicalDC)
  335. Out << " [[" << SemaDC << "]]";
  336. break;
  337. }
  338. default:
  339. llvm_unreachable("a decl that inherits DeclContext isn't handled");
  340. }
  341. Out << "\n";
  342. // Print decls in the DeclContext.
  343. for (auto *I : DC->decls()) {
  344. for (unsigned i = 0; i < Indentation; ++i)
  345. Out << " ";
  346. Decl::Kind DK = I->getKind();
  347. switch (DK) {
  348. case Decl::Namespace:
  349. case Decl::Enum:
  350. case Decl::Record:
  351. case Decl::CXXRecord:
  352. case Decl::ObjCMethod:
  353. case Decl::ObjCInterface:
  354. case Decl::ObjCCategory:
  355. case Decl::ObjCProtocol:
  356. case Decl::ObjCImplementation:
  357. case Decl::ObjCCategoryImpl:
  358. case Decl::LinkageSpec:
  359. case Decl::Block:
  360. case Decl::Function:
  361. case Decl::CXXMethod:
  362. case Decl::CXXConstructor:
  363. case Decl::CXXDestructor:
  364. case Decl::CXXConversion:
  365. {
  366. DeclContext* DC = cast<DeclContext>(I);
  367. PrintDeclContext(DC, Indentation+2);
  368. break;
  369. }
  370. case Decl::IndirectField: {
  371. IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(I);
  372. Out << "<IndirectField> " << *IFD << '\n';
  373. break;
  374. }
  375. case Decl::Label: {
  376. LabelDecl *LD = cast<LabelDecl>(I);
  377. Out << "<Label> " << *LD << '\n';
  378. break;
  379. }
  380. case Decl::Field: {
  381. FieldDecl *FD = cast<FieldDecl>(I);
  382. Out << "<field> " << *FD << '\n';
  383. break;
  384. }
  385. case Decl::Typedef:
  386. case Decl::TypeAlias: {
  387. TypedefNameDecl* TD = cast<TypedefNameDecl>(I);
  388. Out << "<typedef> " << *TD << '\n';
  389. break;
  390. }
  391. case Decl::EnumConstant: {
  392. EnumConstantDecl* ECD = cast<EnumConstantDecl>(I);
  393. Out << "<enum constant> " << *ECD << '\n';
  394. break;
  395. }
  396. case Decl::Var: {
  397. VarDecl* VD = cast<VarDecl>(I);
  398. Out << "<var> " << *VD << '\n';
  399. break;
  400. }
  401. case Decl::ImplicitParam: {
  402. ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(I);
  403. Out << "<implicit parameter> " << *IPD << '\n';
  404. break;
  405. }
  406. case Decl::ParmVar: {
  407. ParmVarDecl* PVD = cast<ParmVarDecl>(I);
  408. Out << "<parameter> " << *PVD << '\n';
  409. break;
  410. }
  411. case Decl::ObjCProperty: {
  412. ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(I);
  413. Out << "<objc property> " << *OPD << '\n';
  414. break;
  415. }
  416. case Decl::FunctionTemplate: {
  417. FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(I);
  418. Out << "<function template> " << *FTD << '\n';
  419. break;
  420. }
  421. case Decl::FileScopeAsm: {
  422. Out << "<file-scope asm>\n";
  423. break;
  424. }
  425. case Decl::UsingDirective: {
  426. Out << "<using directive>\n";
  427. break;
  428. }
  429. case Decl::NamespaceAlias: {
  430. NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(I);
  431. Out << "<namespace alias> " << *NAD << '\n';
  432. break;
  433. }
  434. case Decl::ClassTemplate: {
  435. ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(I);
  436. Out << "<class template> " << *CTD << '\n';
  437. break;
  438. }
  439. case Decl::OMPThreadPrivate: {
  440. Out << "<omp threadprivate> " << '"' << I << "\"\n";
  441. break;
  442. }
  443. default:
  444. Out << "DeclKind: " << DK << '"' << I << "\"\n";
  445. llvm_unreachable("decl unhandled");
  446. }
  447. }
  448. }
  449. std::unique_ptr<ASTConsumer> clang::CreateDeclContextPrinter() {
  450. return llvm::make_unique<DeclContextPrinter>();
  451. }