Coverage Report

Created: 2020-10-24 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/ASTDumper.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
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
// This file implements the AST dump methods, which dump out the
10
// AST in a form that exposes type details and other fields.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/ASTDumper.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/DeclLookups.h"
17
#include "clang/AST/JSONNodeDumper.h"
18
#include "clang/Basic/Builtins.h"
19
#include "clang/Basic/Module.h"
20
#include "clang/Basic/SourceManager.h"
21
#include "llvm/Support/raw_ostream.h"
22
using namespace clang;
23
using namespace clang::comments;
24
25
7
void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
26
7
  NodeDumper.AddChild([=] {
27
7
    OS << "StoredDeclsMap ";
28
7
    NodeDumper.dumpBareDeclRef(cast<Decl>(DC));
29
30
7
    const DeclContext *Primary = DC->getPrimaryContext();
31
7
    if (Primary != DC) {
32
0
      OS << " primary";
33
0
      NodeDumper.dumpPointer(cast<Decl>(Primary));
34
0
    }
35
36
7
    bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
37
38
7
    auto Range = getDeserialize()
39
0
                     ? Primary->lookups()
40
7
                     : Primary->noload_lookups(/*PreserveInternalState=*/true);
41
138
    for (auto I = Range.begin(), E = Range.end(); I != E; 
++I131
) {
42
131
      DeclarationName Name = I.getLookupName();
43
131
      DeclContextLookupResult R = *I;
44
45
131
      NodeDumper.AddChild([=] {
46
131
        OS << "DeclarationName ";
47
131
        {
48
131
          ColorScope Color(OS, ShowColors, DeclNameColor);
49
131
          OS << '\'' << Name << '\'';
50
131
        }
51
52
131
        for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
53
299
             RI != RE; 
++RI168
) {
54
168
          NodeDumper.AddChild([=] {
55
168
            NodeDumper.dumpBareDeclRef(*RI);
56
57
168
            if (!(*RI)->isUnconditionallyVisible())
58
7
              OS << " hidden";
59
60
            // If requested, dump the redecl chain for this lookup.
61
168
            if (DumpDecls) {
62
              // Dump earliest decl first.
63
3
              std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
64
3
                if (Decl *Prev = D->getPreviousDecl())
65
1
                  DumpWithPrev(Prev);
66
3
                Visit(D);
67
3
              };
68
2
              DumpWithPrev(*RI);
69
2
            }
70
168
          });
71
168
        }
72
131
      });
73
131
    }
74
75
7
    if (HasUndeserializedLookups) {
76
5
      NodeDumper.AddChild([=] {
77
5
        ColorScope Color(OS, ShowColors, UndeserializedColor);
78
5
        OS << "<undeserialized lookups>";
79
5
      });
80
5
    }
81
7
  });
82
7
}
83
84
template <typename SpecializationDecl>
85
void ASTDumper::dumpTemplateDeclSpecialization(const SpecializationDecl *D,
86
                                               bool DumpExplicitInst,
87
206
                                               bool DumpRefOnly) {
88
206
  bool DumpedAny = false;
89
303
  for (const auto *RedeclWithBadType : D->redecls()) {
90
    // FIXME: The redecls() range sometimes has elements of a less-specific
91
    // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
92
    // us TagDecls, and should give CXXRecordDecls).
93
303
    auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
94
303
    if (!Redecl) {
95
      // Found the injected-class-name for a class template. This will be dumped
96
      // as part of its surrounding class so we don't need to dump it here.
97
89
      assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
98
89
             "expected an injected-class-name");
99
89
      continue;
100
89
    }
101
102
214
    switch (Redecl->getTemplateSpecializationKind()) {
103
15
    case TSK_ExplicitInstantiationDeclaration:
104
15
    case TSK_ExplicitInstantiationDefinition:
105
15
      if (!DumpExplicitInst)
106
10
        break;
107
5
      LLVM_FALLTHROUGH;
108
183
    case TSK_Undeclared:
109
183
    case TSK_ImplicitInstantiation:
110
183
      if (DumpRefOnly)
111
9
        NodeDumper.dumpDeclRef(Redecl);
112
174
      else
113
174
        Visit(Redecl);
114
183
      DumpedAny = true;
115
183
      break;
116
21
    case TSK_ExplicitSpecialization:
117
21
      break;
118
214
    }
119
214
  }
120
121
  // Ensure we dump at least one decl for each specialization.
122
206
  if (!DumpedAny)
123
27
    NodeDumper.dumpDeclRef(D);
124
206
}
void clang::ASTDumper::dumpTemplateDeclSpecialization<clang::FunctionDecl>(clang::FunctionDecl const*, bool, bool)
Line
Count
Source
87
87
                                               bool DumpRefOnly) {
88
87
  bool DumpedAny = false;
89
91
  for (const auto *RedeclWithBadType : D->redecls()) {
90
    // FIXME: The redecls() range sometimes has elements of a less-specific
91
    // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
92
    // us TagDecls, and should give CXXRecordDecls).
93
91
    auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
94
91
    if (!Redecl) {
95
      // Found the injected-class-name for a class template. This will be dumped
96
      // as part of its surrounding class so we don't need to dump it here.
97
0
      assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
98
0
             "expected an injected-class-name");
99
0
      continue;
100
0
    }
101
102
91
    switch (Redecl->getTemplateSpecializationKind()) {
103
5
    case TSK_ExplicitInstantiationDeclaration:
104
5
    case TSK_ExplicitInstantiationDefinition:
105
5
      if (!DumpExplicitInst)
106
0
        break;
107
5
      LLVM_FALLTHROUGH;
108
83
    case TSK_Undeclared:
109
83
    case TSK_ImplicitInstantiation:
110
83
      if (DumpRefOnly)
111
2
        NodeDumper.dumpDeclRef(Redecl);
112
81
      else
113
81
        Visit(Redecl);
114
83
      DumpedAny = true;
115
83
      break;
116
8
    case TSK_ExplicitSpecialization:
117
8
      break;
118
91
    }
119
91
  }
120
121
  // Ensure we dump at least one decl for each specialization.
122
87
  if (!DumpedAny)
123
4
    NodeDumper.dumpDeclRef(D);
124
87
}
void clang::ASTDumper::dumpTemplateDeclSpecialization<clang::ClassTemplateSpecializationDecl>(clang::ClassTemplateSpecializationDecl const*, bool, bool)
Line
Count
Source
87
116
                                               bool DumpRefOnly) {
88
116
  bool DumpedAny = false;
89
207
  for (const auto *RedeclWithBadType : D->redecls()) {
90
    // FIXME: The redecls() range sometimes has elements of a less-specific
91
    // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
92
    // us TagDecls, and should give CXXRecordDecls).
93
207
    auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
94
207
    if (!Redecl) {
95
      // Found the injected-class-name for a class template. This will be dumped
96
      // as part of its surrounding class so we don't need to dump it here.
97
89
      assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
98
89
             "expected an injected-class-name");
99
89
      continue;
100
89
    }
101
102
118
    switch (Redecl->getTemplateSpecializationKind()) {
103
10
    case TSK_ExplicitInstantiationDeclaration:
104
10
    case TSK_ExplicitInstantiationDefinition:
105
10
      if (!DumpExplicitInst)
106
10
        break;
107
0
      LLVM_FALLTHROUGH;
108
95
    case TSK_Undeclared:
109
95
    case TSK_ImplicitInstantiation:
110
95
      if (DumpRefOnly)
111
5
        NodeDumper.dumpDeclRef(Redecl);
112
90
      else
113
90
        Visit(Redecl);
114
95
      DumpedAny = true;
115
95
      break;
116
13
    case TSK_ExplicitSpecialization:
117
13
      break;
118
118
    }
119
118
  }
120
121
  // Ensure we dump at least one decl for each specialization.
122
116
  if (!DumpedAny)
123
23
    NodeDumper.dumpDeclRef(D);
124
116
}
void clang::ASTDumper::dumpTemplateDeclSpecialization<clang::VarTemplateSpecializationDecl>(clang::VarTemplateSpecializationDecl const*, bool, bool)
Line
Count
Source
87
3
                                               bool DumpRefOnly) {
88
3
  bool DumpedAny = false;
89
5
  for (const auto *RedeclWithBadType : D->redecls()) {
90
    // FIXME: The redecls() range sometimes has elements of a less-specific
91
    // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
92
    // us TagDecls, and should give CXXRecordDecls).
93
5
    auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
94
5
    if (!Redecl) {
95
      // Found the injected-class-name for a class template. This will be dumped
96
      // as part of its surrounding class so we don't need to dump it here.
97
0
      assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
98
0
             "expected an injected-class-name");
99
0
      continue;
100
0
    }
101
102
5
    switch (Redecl->getTemplateSpecializationKind()) {
103
0
    case TSK_ExplicitInstantiationDeclaration:
104
0
    case TSK_ExplicitInstantiationDefinition:
105
0
      if (!DumpExplicitInst)
106
0
        break;
107
0
      LLVM_FALLTHROUGH;
108
5
    case TSK_Undeclared:
109
5
    case TSK_ImplicitInstantiation:
110
5
      if (DumpRefOnly)
111
2
        NodeDumper.dumpDeclRef(Redecl);
112
3
      else
113
3
        Visit(Redecl);
114
5
      DumpedAny = true;
115
5
      break;
116
0
    case TSK_ExplicitSpecialization:
117
0
      break;
118
5
    }
119
5
  }
120
121
  // Ensure we dump at least one decl for each specialization.
122
3
  if (!DumpedAny)
123
0
    NodeDumper.dumpDeclRef(D);
124
3
}
125
126
template <typename TemplateDecl>
127
382
void ASTDumper::dumpTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst) {
128
382
  dumpTemplateParameters(D->getTemplateParameters());
129
130
382
  Visit(D->getTemplatedDecl());
131
132
382
  for (const auto *Child : D->specializations())
133
206
    dumpTemplateDeclSpecialization(Child, DumpExplicitInst,
134
206
                                   !D->isCanonicalDecl());
135
382
}
void clang::ASTDumper::dumpTemplateDecl<clang::FunctionTemplateDecl>(clang::FunctionTemplateDecl const*, bool)
Line
Count
Source
127
213
void ASTDumper::dumpTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst) {
128
213
  dumpTemplateParameters(D->getTemplateParameters());
129
130
213
  Visit(D->getTemplatedDecl());
131
132
213
  for (const auto *Child : D->specializations())
133
87
    dumpTemplateDeclSpecialization(Child, DumpExplicitInst,
134
87
                                   !D->isCanonicalDecl());
135
213
}
void clang::ASTDumper::dumpTemplateDecl<clang::ClassTemplateDecl>(clang::ClassTemplateDecl const*, bool)
Line
Count
Source
127
164
void ASTDumper::dumpTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst) {
128
164
  dumpTemplateParameters(D->getTemplateParameters());
129
130
164
  Visit(D->getTemplatedDecl());
131
132
164
  for (const auto *Child : D->specializations())
133
116
    dumpTemplateDeclSpecialization(Child, DumpExplicitInst,
134
116
                                   !D->isCanonicalDecl());
135
164
}
void clang::ASTDumper::dumpTemplateDecl<clang::VarTemplateDecl>(clang::VarTemplateDecl const*, bool)
Line
Count
Source
127
5
void ASTDumper::dumpTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst) {
128
5
  dumpTemplateParameters(D->getTemplateParameters());
129
130
5
  Visit(D->getTemplatedDecl());
131
132
5
  for (const auto *Child : D->specializations())
133
3
    dumpTemplateDeclSpecialization(Child, DumpExplicitInst,
134
3
                                   !D->isCanonicalDecl());
135
5
}
136
137
213
void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
138
  // FIXME: We don't add a declaration of a function template specialization
139
  // to its context when it's explicitly instantiated, so dump explicit
140
  // instantiations when we dump the template itself.
141
213
  dumpTemplateDecl(D, true);
142
213
}
143
144
164
void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
145
164
  dumpTemplateDecl(D, false);
146
164
}
147
148
5
void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
149
5
  dumpTemplateDecl(D, false);
150
5
}
151
152
//===----------------------------------------------------------------------===//
153
// Type method implementations
154
//===----------------------------------------------------------------------===//
155
156
0
void QualType::dump(const char *msg) const {
157
0
  if (msg)
158
0
    llvm::errs() << msg << ": ";
159
0
  dump();
160
0
}
161
162
0
LLVM_DUMP_METHOD void QualType::dump() const {
163
0
  ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false);
164
0
  Dumper.Visit(*this);
165
0
}
166
167
LLVM_DUMP_METHOD void QualType::dump(llvm::raw_ostream &OS,
168
26
                                     const ASTContext &Context) const {
169
26
  ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
170
26
  Dumper.Visit(*this);
171
26
}
172
173
0
LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
174
175
LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS,
176
0
                                 const ASTContext &Context) const {
177
0
  QualType(this, 0).dump(OS, Context);
178
0
}
179
180
//===----------------------------------------------------------------------===//
181
// Decl method implementations
182
//===----------------------------------------------------------------------===//
183
184
23
LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
185
186
LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize,
187
1.51k
                                 ASTDumpOutputFormat Format) const {
188
1.51k
  ASTContext &Ctx = getASTContext();
189
1.51k
  const SourceManager &SM = Ctx.getSourceManager();
190
191
1.51k
  if (ADOF_JSON == Format) {
192
141
    JSONDumper P(OS, SM, Ctx, Ctx.getPrintingPolicy(),
193
141
                 &Ctx.getCommentCommandTraits());
194
141
    (void)Deserialize; // FIXME?
195
141
    P.Visit(this);
196
1.37k
  } else {
197
1.37k
    ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
198
1.37k
    P.setDeserialize(Deserialize);
199
1.37k
    P.Visit(this);
200
1.37k
  }
201
1.51k
}
202
203
0
LLVM_DUMP_METHOD void Decl::dumpColor() const {
204
0
  const ASTContext &Ctx = getASTContext();
205
0
  ASTDumper P(llvm::errs(), Ctx, /*ShowColors=*/true);
206
0
  P.Visit(this);
207
0
}
208
209
0
LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
210
0
  dumpLookups(llvm::errs());
211
0
}
212
213
LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
214
                                               bool DumpDecls,
215
7
                                               bool Deserialize) const {
216
7
  const DeclContext *DC = this;
217
13
  while (!DC->isTranslationUnit())
218
6
    DC = DC->getParent();
219
7
  const ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
220
7
  ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
221
7
  P.setDeserialize(Deserialize);
222
7
  P.dumpLookups(this, DumpDecls);
223
7
}
224
225
//===----------------------------------------------------------------------===//
226
// Stmt method implementations
227
//===----------------------------------------------------------------------===//
228
229
14
LLVM_DUMP_METHOD void Stmt::dump() const {
230
14
  ASTDumper P(llvm::errs(), /*ShowColors=*/false);
231
14
  P.Visit(this);
232
14
}
233
234
LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS,
235
1
                                 const ASTContext &Context) const {
236
1
  ASTDumper P(OS, Context, Context.getDiagnostics().getShowColors());
237
1
  P.Visit(this);
238
1
}
239
240
0
LLVM_DUMP_METHOD void Stmt::dumpColor() const {
241
0
  ASTDumper P(llvm::errs(), /*ShowColors=*/true);
242
0
  P.Visit(this);
243
0
}
244
245
//===----------------------------------------------------------------------===//
246
// Comment method implementations
247
//===----------------------------------------------------------------------===//
248
249
79
LLVM_DUMP_METHOD void Comment::dump() const {
250
79
  const auto *FC = dyn_cast<FullComment>(this);
251
79
  if (!FC)
252
0
    return;
253
79
  ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false);
254
79
  Dumper.Visit(FC, FC);
255
79
}
256
257
LLVM_DUMP_METHOD void Comment::dump(raw_ostream &OS,
258
0
                                    const ASTContext &Context) const {
259
0
  const auto *FC = dyn_cast<FullComment>(this);
260
0
  if (!FC)
261
0
    return;
262
0
  ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
263
0
  Dumper.Visit(FC, FC);
264
0
}
265
266
0
LLVM_DUMP_METHOD void Comment::dumpColor() const {
267
0
  const auto *FC = dyn_cast<FullComment>(this);
268
0
  if (!FC)
269
0
    return;
270
0
  ASTDumper Dumper(llvm::errs(), /*ShowColors=*/true);
271
0
  Dumper.Visit(FC, FC);
272
0
}
273
274
//===----------------------------------------------------------------------===//
275
// APValue method implementations
276
//===----------------------------------------------------------------------===//
277
278
0
LLVM_DUMP_METHOD void APValue::dump() const {
279
0
  ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false);
280
0
  Dumper.Visit(*this, /*Ty=*/QualType());
281
0
}
282
283
LLVM_DUMP_METHOD void APValue::dump(raw_ostream &OS,
284
0
                                    const ASTContext &Context) const {
285
0
  ASTDumper Dumper(llvm::errs(), Context,
286
0
                   Context.getDiagnostics().getShowColors());
287
0
  Dumper.Visit(*this, /*Ty=*/Context.getPointerType(Context.CharTy));
288
0
}