| Chris Lattner | d1cdee7 | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 1 | //===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===// |
| Bill Wendling | aec64c3 | 2007-06-23 00:39:57 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Bill Wendling | aec64c3 | 2007-06-23 00:39:57 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| Chris Lattner | d1cdee7 | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 9 | // AST Consumer Implementations. |
| Bill Wendling | aec64c3 | 2007-06-23 00:39:57 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| Eli Friedman | 9f30fc3 | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/ASTConsumers.h" |
| Bill Wendling | aec64c3 | 2007-06-23 00:39:57 +0000 | [diff] [blame] | 14 | #include "clang/AST/AST.h" |
| Chris Lattner | 09c39db | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
| Chris Lattner | a5adead | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 17 | #include "clang/AST/PrettyPrinter.h" |
| Alexander Kornienko | 3db68ee | 2012-07-26 16:01:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecordLayout.h" |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
| Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Path.h" |
| Alexander Kornienko | 3db68ee | 2012-07-26 16:01:23 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Timer.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| Chris Lattner | cbe4f77 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 25 | using namespace clang; |
| Bill Wendling | aec64c3 | 2007-06-23 00:39:57 +0000 | [diff] [blame] | 26 | |
| Ted Kremenek | 7c81bdd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 28 | /// ASTPrinter - Pretty-printer and dumper of ASTs |
| Chris Lattner | cbe4f77 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 29 | |
| Ted Kremenek | 7c81bdd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 30 | namespace { |
| Alexander Kornienko | 3db68ee | 2012-07-26 16:01:23 +0000 | [diff] [blame] | 31 | class ASTPrinter : public ASTConsumer, |
| 32 | public RecursiveASTVisitor<ASTPrinter> { |
| 33 | typedef RecursiveASTVisitor<ASTPrinter> base; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
| Ted Kremenek | 7c81bdd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 35 | public: |
| Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 36 | enum Kind { DumpFull, Dump, Print, None }; |
| Aaron Ballman | 2ce598a | 2019-05-13 21:39:55 +0000 | [diff] [blame] | 37 | ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K, |
| 38 | ASTDumpOutputFormat Format, StringRef FilterString, |
| Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 39 | bool DumpLookups = false) |
| 40 | : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)), |
| Aaron Ballman | 2ce598a | 2019-05-13 21:39:55 +0000 | [diff] [blame] | 41 | OutputKind(K), OutputFormat(Format), FilterString(FilterString), |
| 42 | DumpLookups(DumpLookups) {} |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
| Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 44 | void HandleTranslationUnit(ASTContext &Context) override { |
| Alexander Kornienko | 3db68ee | 2012-07-26 16:01:23 +0000 | [diff] [blame] | 45 | TranslationUnitDecl *D = Context.getTranslationUnitDecl(); |
| 46 | |
| Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 47 | if (FilterString.empty()) |
| 48 | return print(D); |
| Alexander Kornienko | 3db68ee | 2012-07-26 16:01:23 +0000 | [diff] [blame] | 49 | |
| 50 | TraverseDecl(D); |
| Bill Wendling | aec64c3 | 2007-06-23 00:39:57 +0000 | [diff] [blame] | 51 | } |
| Alexander Kornienko | 3db68ee | 2012-07-26 16:01:23 +0000 | [diff] [blame] | 52 | |
| 53 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 54 | |
| 55 | bool TraverseDecl(Decl *D) { |
| Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 56 | if (D && filterMatches(D)) { |
| Dmitri Gribenko | 3233391 | 2012-11-21 10:54:55 +0000 | [diff] [blame] | 57 | bool ShowColors = Out.has_colors(); |
| 58 | if (ShowColors) |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 59 | Out.changeColor(raw_ostream::BLUE); |
| Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 60 | Out << (OutputKind != Print ? "Dumping " : "Printing ") << getName(D) |
| Richard Smith | 35f986d | 2014-08-11 22:11:07 +0000 | [diff] [blame] | 61 | << ":\n"; |
| Dmitri Gribenko | 3233391 | 2012-11-21 10:54:55 +0000 | [diff] [blame] | 62 | if (ShowColors) |
| 63 | Out.resetColor(); |
| Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 64 | print(D); |
| Alexander Kornienko | 2018618 | 2012-08-17 17:38:39 +0000 | [diff] [blame] | 65 | Out << "\n"; |
| Alexander Kornienko | 3db68ee | 2012-07-26 16:01:23 +0000 | [diff] [blame] | 66 | // Don't traverse child nodes to avoid output duplication. |
| 67 | return true; |
| 68 | } |
| 69 | return base::TraverseDecl(D); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | std::string getName(Decl *D) { |
| 74 | if (isa<NamedDecl>(D)) |
| 75 | return cast<NamedDecl>(D)->getQualifiedNameAsString(); |
| 76 | return ""; |
| 77 | } |
| 78 | bool filterMatches(Decl *D) { |
| 79 | return getName(D).find(FilterString) != std::string::npos; |
| 80 | } |
| Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 81 | void print(Decl *D) { |
| 82 | if (DumpLookups) { |
| Richard Smith | 35f986d | 2014-08-11 22:11:07 +0000 | [diff] [blame] | 83 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) { |
| 84 | if (DC == DC->getPrimaryContext()) |
| Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 85 | DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull); |
| Richard Smith | 35f986d | 2014-08-11 22:11:07 +0000 | [diff] [blame] | 86 | else |
| 87 | Out << "Lookup map is in primary DeclContext " |
| 88 | << DC->getPrimaryContext() << "\n"; |
| 89 | } else |
| Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 90 | Out << "Not a DeclContext\n"; |
| Joel E. Denny | 7bcc210 | 2018-05-14 18:41:44 +0000 | [diff] [blame] | 91 | } else if (OutputKind == Print) { |
| 92 | PrintingPolicy Policy(D->getASTContext().getLangOpts()); |
| 93 | D->print(Out, Policy, /*Indentation=*/0, /*PrintInstantiation=*/true); |
| 94 | } else if (OutputKind != None) |
| Aaron Ballman | 2ce598a | 2019-05-13 21:39:55 +0000 | [diff] [blame] | 95 | D->dump(Out, OutputKind == DumpFull, OutputFormat); |
| Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 96 | } |
| Alexander Kornienko | 3db68ee | 2012-07-26 16:01:23 +0000 | [diff] [blame] | 97 | |
| 98 | raw_ostream &Out; |
| Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 99 | std::unique_ptr<raw_ostream> OwnedOut; |
| Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 100 | |
| 101 | /// How to output individual declarations. |
| 102 | Kind OutputKind; |
| 103 | |
| Aaron Ballman | 2ce598a | 2019-05-13 21:39:55 +0000 | [diff] [blame] | 104 | /// What format should the output take? |
| 105 | ASTDumpOutputFormat OutputFormat; |
| 106 | |
| Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 107 | /// Which declarations or DeclContexts to display. |
| Alexander Kornienko | 3db68ee | 2012-07-26 16:01:23 +0000 | [diff] [blame] | 108 | std::string FilterString; |
| Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 109 | |
| 110 | /// Whether the primary output is lookup results or declarations. Individual |
| 111 | /// results will be output with a format determined by OutputKind. This is |
| 112 | /// incompatible with OutputKind == Print. |
| Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 113 | bool DumpLookups; |
| Chris Lattner | 09c39db | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 114 | }; |
| Alexander Kornienko | 4de0359 | 2012-07-31 09:37:40 +0000 | [diff] [blame] | 115 | |
| 116 | class ASTDeclNodeLister : public ASTConsumer, |
| 117 | public RecursiveASTVisitor<ASTDeclNodeLister> { |
| Alexander Kornienko | 4de0359 | 2012-07-31 09:37:40 +0000 | [diff] [blame] | 118 | public: |
| Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 119 | ASTDeclNodeLister(raw_ostream *Out = nullptr) |
| Alexander Kornienko | 4de0359 | 2012-07-31 09:37:40 +0000 | [diff] [blame] | 120 | : Out(Out ? *Out : llvm::outs()) {} |
| 121 | |
| Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 122 | void HandleTranslationUnit(ASTContext &Context) override { |
| Alexander Kornienko | 4de0359 | 2012-07-31 09:37:40 +0000 | [diff] [blame] | 123 | TraverseDecl(Context.getTranslationUnitDecl()); |
| 124 | } |
| 125 | |
| 126 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 127 | |
| Craig Topper | 4567053 | 2014-03-13 07:14:47 +0000 | [diff] [blame] | 128 | bool VisitNamedDecl(NamedDecl *D) { |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 129 | D->printQualifiedName(Out); |
| 130 | Out << '\n'; |
| Alexander Kornienko | 4de0359 | 2012-07-31 09:37:40 +0000 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | raw_ostream &Out; |
| 136 | }; |
| Chris Lattner | c0c3dff | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 137 | } // end anonymous namespace |
| Chris Lattner | cbe4f77 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 138 | |
| Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 139 | std::unique_ptr<ASTConsumer> |
| 140 | clang::CreateASTPrinter(std::unique_ptr<raw_ostream> Out, |
| 141 | StringRef FilterString) { |
| Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 142 | return std::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print, |
| Aaron Ballman | 2ce598a | 2019-05-13 21:39:55 +0000 | [diff] [blame] | 143 | ADOF_Default, FilterString); |
| Ted Kremenek | 5933768 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 144 | } |
| Ted Kremenek | 7c81bdd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 145 | |
| Alexander Kornienko | d10d790 | 2018-04-06 13:01:12 +0000 | [diff] [blame] | 146 | std::unique_ptr<ASTConsumer> |
| Aaron Ballman | 2ce598a | 2019-05-13 21:39:55 +0000 | [diff] [blame] | 147 | clang::CreateASTDumper(std::unique_ptr<raw_ostream> Out, StringRef FilterString, |
| 148 | bool DumpDecls, bool Deserialize, bool DumpLookups, |
| 149 | ASTDumpOutputFormat Format) { |
| Aaron Ballman | 08d3b39 | 2017-06-15 00:00:08 +0000 | [diff] [blame] | 150 | assert((DumpDecls || Deserialize || DumpLookups) && "nothing to dump"); |
| Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 151 | return std::make_unique<ASTPrinter>(std::move(Out), |
| Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 152 | Deserialize ? ASTPrinter::DumpFull : |
| 153 | DumpDecls ? ASTPrinter::Dump : |
| Aaron Ballman | 2ce598a | 2019-05-13 21:39:55 +0000 | [diff] [blame] | 154 | ASTPrinter::None, Format, |
| Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 155 | FilterString, DumpLookups); |
| Douglas Gregor | 32887f0 | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 156 | } |
| Chris Lattner | 09c39db | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 157 | |
| David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 158 | std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() { |
| Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 159 | return std::make_unique<ASTDeclNodeLister>(nullptr); |
| Alexander Kornienko | 4de0359 | 2012-07-31 09:37:40 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| Ted Kremenek | 7c81bdd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 162 | //===----------------------------------------------------------------------===// |
| 163 | /// ASTViewer - AST Visualization |
| 164 | |
| Ted Kremenek | 66d130a | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 165 | namespace { |
| 166 | class ASTViewer : public ASTConsumer { |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 167 | ASTContext *Context; |
| Ted Kremenek | 66d130a | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 168 | public: |
| Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 169 | void Initialize(ASTContext &Context) override { |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 170 | this->Context = &Context; |
| Ted Kremenek | 66d130a | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 171 | } |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 172 | |
| Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 173 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 174 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 175 | HandleTopLevelSingleDecl(*I); |
| Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 176 | return true; |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 177 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 179 | void HandleTopLevelSingleDecl(Decl *D); |
| Ted Kremenek | 66d130a | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 180 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 181 | } |
| Ted Kremenek | 66d130a | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 182 | |
| Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 183 | void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { |
| Argyrios Kyrtzidis | 46f556d | 2010-07-07 11:31:23 +0000 | [diff] [blame] | 184 | if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 185 | D->print(llvm::errs()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 186 | |
| Argyrios Kyrtzidis | 46f556d | 2010-07-07 11:31:23 +0000 | [diff] [blame] | 187 | if (Stmt *Body = D->getBody()) { |
| Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 188 | llvm::errs() << '\n'; |
| Douglas Gregor | e2350a3 | 2009-09-12 00:08:48 +0000 | [diff] [blame] | 189 | Body->viewAST(); |
| Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 190 | llvm::errs() << '\n'; |
| Chris Lattner | c0c3dff | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 191 | } |
| Chris Lattner | c0c3dff | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 195 | std::unique_ptr<ASTConsumer> clang::CreateASTViewer() { |
| Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 196 | return std::make_unique<ASTViewer>(); |
| David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 197 | } |