/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/ASTConsumers.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // AST Consumer Implementations. |
10 | | // |
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/SourceManager.h" |
22 | | #include "llvm/Support/Path.h" |
23 | | #include "llvm/Support/Timer.h" |
24 | | #include "llvm/Support/raw_ostream.h" |
25 | | using namespace clang; |
26 | | |
27 | | //===----------------------------------------------------------------------===// |
28 | | /// ASTPrinter - Pretty-printer and dumper of ASTs |
29 | | |
30 | | namespace { |
31 | | class ASTPrinter : public ASTConsumer, |
32 | | public RecursiveASTVisitor<ASTPrinter> { |
33 | | typedef RecursiveASTVisitor<ASTPrinter> base; |
34 | | |
35 | | public: |
36 | | enum Kind { DumpFull, Dump, Print, None }; |
37 | | ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K, |
38 | | ASTDumpOutputFormat Format, StringRef FilterString, |
39 | | bool DumpLookups = false, bool DumpDeclTypes = false) |
40 | | : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)), |
41 | | OutputKind(K), OutputFormat(Format), FilterString(FilterString), |
42 | 984 | DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {} |
43 | | |
44 | 984 | void HandleTranslationUnit(ASTContext &Context) override { |
45 | 984 | TranslationUnitDecl *D = Context.getTranslationUnitDecl(); |
46 | | |
47 | 984 | if (FilterString.empty()) |
48 | 887 | return print(D); |
49 | | |
50 | 97 | TraverseDecl(D); |
51 | 97 | } |
52 | | |
53 | 2.14k | bool shouldWalkTypesOfTypeLocs() const { return false; } |
54 | | |
55 | 3.78k | bool TraverseDecl(Decl *D) { |
56 | 3.78k | if (D && filterMatches(D)3.77k ) { |
57 | 703 | bool ShowColors = Out.has_colors(); |
58 | 703 | if (ShowColors) |
59 | 0 | Out.changeColor(raw_ostream::BLUE); |
60 | 703 | Out << (OutputKind != Print ? "Dumping " : "Printing "0 ) << getName(D) |
61 | 703 | << ":\n"; |
62 | 703 | if (ShowColors) |
63 | 0 | Out.resetColor(); |
64 | 703 | print(D); |
65 | 703 | Out << "\n"; |
66 | | // Don't traverse child nodes to avoid output duplication. |
67 | 703 | return true; |
68 | 703 | } |
69 | 3.08k | return base::TraverseDecl(D); |
70 | 3.08k | } |
71 | | |
72 | | private: |
73 | 4.47k | std::string getName(Decl *D) { |
74 | 4.47k | if (isa<NamedDecl>(D)) |
75 | 4.29k | return cast<NamedDecl>(D)->getQualifiedNameAsString(); |
76 | 186 | return ""; |
77 | 186 | } |
78 | 3.77k | bool filterMatches(Decl *D) { |
79 | 3.77k | return getName(D).find(FilterString) != std::string::npos; |
80 | 3.77k | } |
81 | 1.59k | void print(Decl *D) { |
82 | 1.59k | if (DumpLookups) { |
83 | 17 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) { |
84 | 13 | if (DC == DC->getPrimaryContext()) |
85 | 7 | DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull); |
86 | 6 | else |
87 | 6 | Out << "Lookup map is in primary DeclContext " |
88 | 6 | << DC->getPrimaryContext() << "\n"; |
89 | 13 | } else |
90 | 4 | Out << "Not a DeclContext\n"; |
91 | 1.57k | } else if (OutputKind == Print) { |
92 | 533 | PrintingPolicy Policy(D->getASTContext().getLangOpts()); |
93 | 533 | D->print(Out, Policy, /*Indentation=*/0, /*PrintInstantiation=*/true); |
94 | 1.04k | } else if (OutputKind != None) { |
95 | 1.04k | D->dump(Out, OutputKind == DumpFull, OutputFormat); |
96 | 1.04k | } |
97 | | |
98 | 1.59k | if (DumpDeclTypes) { |
99 | 10 | Decl *InnerD = D; |
100 | 10 | if (auto *TD = dyn_cast<TemplateDecl>(D)) |
101 | 10 | InnerD = TD->getTemplatedDecl(); |
102 | | |
103 | | // FIXME: Support OutputFormat in type dumping. |
104 | | // FIXME: Support combining -ast-dump-decl-types with -ast-dump-lookups. |
105 | 10 | if (auto *VD = dyn_cast<ValueDecl>(InnerD)) |
106 | 10 | VD->getType().dump(Out, VD->getASTContext()); |
107 | 10 | if (auto *TD = dyn_cast<TypeDecl>(InnerD)) |
108 | 0 | TD->getTypeForDecl()->dump(Out, TD->getASTContext()); |
109 | 10 | } |
110 | 1.59k | } |
111 | | |
112 | | raw_ostream &Out; |
113 | | std::unique_ptr<raw_ostream> OwnedOut; |
114 | | |
115 | | /// How to output individual declarations. |
116 | | Kind OutputKind; |
117 | | |
118 | | /// What format should the output take? |
119 | | ASTDumpOutputFormat OutputFormat; |
120 | | |
121 | | /// Which declarations or DeclContexts to display. |
122 | | std::string FilterString; |
123 | | |
124 | | /// Whether the primary output is lookup results or declarations. Individual |
125 | | /// results will be output with a format determined by OutputKind. This is |
126 | | /// incompatible with OutputKind == Print. |
127 | | bool DumpLookups; |
128 | | |
129 | | /// Whether to dump the type for each declaration dumped. |
130 | | bool DumpDeclTypes; |
131 | | }; |
132 | | |
133 | | class ASTDeclNodeLister : public ASTConsumer, |
134 | | public RecursiveASTVisitor<ASTDeclNodeLister> { |
135 | | public: |
136 | | ASTDeclNodeLister(raw_ostream *Out = nullptr) |
137 | 1 | : Out(Out ? *Out : llvm::outs()) {} |
138 | | |
139 | 1 | void HandleTranslationUnit(ASTContext &Context) override { |
140 | 1 | TraverseDecl(Context.getTranslationUnitDecl()); |
141 | 1 | } |
142 | | |
143 | 4 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
144 | | |
145 | 10 | bool VisitNamedDecl(NamedDecl *D) { |
146 | 10 | D->printQualifiedName(Out); |
147 | 10 | Out << '\n'; |
148 | 10 | return true; |
149 | 10 | } |
150 | | |
151 | | private: |
152 | | raw_ostream &Out; |
153 | | }; |
154 | | } // end anonymous namespace |
155 | | |
156 | | std::unique_ptr<ASTConsumer> |
157 | | clang::CreateASTPrinter(std::unique_ptr<raw_ostream> Out, |
158 | 533 | StringRef FilterString) { |
159 | 533 | return std::make_unique<ASTPrinter>(std::move(Out), ASTPrinter::Print, |
160 | 533 | ADOF_Default, FilterString); |
161 | 533 | } |
162 | | |
163 | | std::unique_ptr<ASTConsumer> |
164 | | clang::CreateASTDumper(std::unique_ptr<raw_ostream> Out, StringRef FilterString, |
165 | | bool DumpDecls, bool Deserialize, bool DumpLookups, |
166 | 451 | bool DumpDeclTypes, ASTDumpOutputFormat Format) { |
167 | 451 | assert((DumpDecls || Deserialize || DumpLookups) && "nothing to dump"); |
168 | 451 | return std::make_unique<ASTPrinter>( |
169 | 451 | std::move(Out), |
170 | 68 | Deserialize ? ASTPrinter::DumpFull |
171 | 383 | : DumpDecls ? ASTPrinter::Dump378 : ASTPrinter::None5 , |
172 | 451 | Format, FilterString, DumpLookups, DumpDeclTypes); |
173 | 451 | } |
174 | | |
175 | 1 | std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() { |
176 | 1 | return std::make_unique<ASTDeclNodeLister>(nullptr); |
177 | 1 | } |
178 | | |
179 | | //===----------------------------------------------------------------------===// |
180 | | /// ASTViewer - AST Visualization |
181 | | |
182 | | namespace { |
183 | | class ASTViewer : public ASTConsumer { |
184 | | ASTContext *Context; |
185 | | public: |
186 | 0 | void Initialize(ASTContext &Context) override { |
187 | 0 | this->Context = &Context; |
188 | 0 | } |
189 | | |
190 | 0 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
191 | 0 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
192 | 0 | HandleTopLevelSingleDecl(*I); |
193 | 0 | return true; |
194 | 0 | } |
195 | | |
196 | | void HandleTopLevelSingleDecl(Decl *D); |
197 | | }; |
198 | | } |
199 | | |
200 | 0 | void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { |
201 | 0 | if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { |
202 | 0 | D->print(llvm::errs()); |
203 | |
|
204 | 0 | if (Stmt *Body = D->getBody()) { |
205 | 0 | llvm::errs() << '\n'; |
206 | 0 | Body->viewAST(); |
207 | 0 | llvm::errs() << '\n'; |
208 | 0 | } |
209 | 0 | } |
210 | 0 | } |
211 | | |
212 | 0 | std::unique_ptr<ASTConsumer> clang::CreateASTViewer() { |
213 | 0 | return std::make_unique<ASTViewer>(); |
214 | 0 | } |