Coverage Report

Created: 2020-09-15 12:33

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/TemplateName.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- TemplateName.cpp - C++ Template Name Representation ----------------===//
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 defines the TemplateName interface and subclasses.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/TemplateName.h"
14
#include "clang/AST/Decl.h"
15
#include "clang/AST/DeclBase.h"
16
#include "clang/AST/DeclTemplate.h"
17
#include "clang/AST/DependenceFlags.h"
18
#include "clang/AST/NestedNameSpecifier.h"
19
#include "clang/AST/PrettyPrinter.h"
20
#include "clang/AST/TemplateBase.h"
21
#include "clang/Basic/Diagnostic.h"
22
#include "clang/Basic/LLVM.h"
23
#include "clang/Basic/LangOptions.h"
24
#include "clang/Basic/OperatorKinds.h"
25
#include "llvm/ADT/ArrayRef.h"
26
#include "llvm/ADT/FoldingSet.h"
27
#include "llvm/Support/Casting.h"
28
#include "llvm/Support/Compiler.h"
29
#include "llvm/Support/raw_ostream.h"
30
#include <cassert>
31
#include <string>
32
33
using namespace clang;
34
35
TemplateArgument
36
9
SubstTemplateTemplateParmPackStorage::getArgumentPack() const {
37
9
  return TemplateArgument(llvm::makeArrayRef(Arguments, size()));
38
9
}
39
40
28.3k
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
41
28.3k
  Profile(ID, Parameter, Replacement);
42
28.3k
}
43
44
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID,
45
                                           TemplateTemplateParmDecl *parameter,
46
59.6k
                                               TemplateName replacement) {
47
59.6k
  ID.AddPointer(parameter);
48
59.6k
  ID.AddPointer(replacement.getAsVoidPointer());
49
59.6k
}
50
51
void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
52
0
                                                   ASTContext &Context) {
53
0
  Profile(ID, Context, Parameter, getArgumentPack());
54
0
}
55
56
void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
57
                                                   ASTContext &Context,
58
                                           TemplateTemplateParmDecl *Parameter,
59
8
                                             const TemplateArgument &ArgPack) {
60
8
  ID.AddPointer(Parameter);
61
8
  ArgPack.Profile(ID, Context);
62
8
}
63
64
2.96M
TemplateName::TemplateName(void *Ptr) {
65
2.96M
  Storage = StorageType::getFromOpaqueValue(Ptr);
66
2.96M
}
67
68
9.12M
TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
69
TemplateName::TemplateName(OverloadedTemplateStorage *Storage)
70
116k
    : Storage(Storage) {}
71
TemplateName::TemplateName(AssumedTemplateStorage *Storage)
72
661
    : Storage(Storage) {}
73
TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
74
31.2k
    : Storage(Storage) {}
75
TemplateName::TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
76
8
    : Storage(Storage) {}
77
136k
TemplateName::TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) {}
78
225k
TemplateName::TemplateName(DependentTemplateName *Dep) : Storage(Dep) {}
79
80
3.89M
bool TemplateName::isNull() const { return Storage.isNull(); }
81
82
28.1M
TemplateName::NameKind TemplateName::getKind() const {
83
28.1M
  if (Storage.is<TemplateDecl *>())
84
27.7M
    return Template;
85
427k
  if (Storage.is<DependentTemplateName *>())
86
15.3k
    return DependentTemplate;
87
412k
  if (Storage.is<QualifiedTemplateName *>())
88
191k
    return QualifiedTemplate;
89
220k
90
220k
  UncommonTemplateNameStorage *uncommon
91
220k
    = Storage.get<UncommonTemplateNameStorage*>();
92
220k
  if (uncommon->getAsOverloadedStorage())
93
119k
    return OverloadedTemplate;
94
100k
  if (uncommon->getAsAssumedTemplateName())
95
52
    return AssumedTemplate;
96
100k
  if (uncommon->getAsSubstTemplateTemplateParm())
97
100k
    return SubstTemplateTemplateParm;
98
22
  return SubstTemplateTemplateParmPack;
99
22
}
100
101
51.4M
TemplateDecl *TemplateName::getAsTemplateDecl() const {
102
51.4M
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
103
50.9M
    return Template;
104
526k
105
526k
  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
106
378k
    return QTN->getTemplateDecl();
107
148k
108
148k
  if (SubstTemplateTemplateParmStorage *sub = getAsSubstTemplateTemplateParm())
109
125k
    return sub->getReplacement().getAsTemplateDecl();
110
23.5k
111
23.5k
  return nullptr;
112
23.5k
}
113
114
1.17M
OverloadedTemplateStorage *TemplateName::getAsOverloadedTemplate() const {
115
1.17M
  if (UncommonTemplateNameStorage *Uncommon =
116
119k
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
117
119k
    return Uncommon->getAsOverloadedStorage();
118
1.05M
119
1.05M
  return nullptr;
120
1.05M
}
121
122
7.91M
AssumedTemplateStorage *TemplateName::getAsAssumedTemplateName() const {
123
7.91M
  if (UncommonTemplateNameStorage *Uncommon =
124
25.4k
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
125
25.4k
    return Uncommon->getAsAssumedTemplateName();
126
7.88M
127
7.88M
  return nullptr;
128
7.88M
}
129
130
SubstTemplateTemplateParmStorage *
131
6.00M
TemplateName::getAsSubstTemplateTemplateParm() const {
132
6.00M
  if (UncommonTemplateNameStorage *uncommon =
133
159k
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
134
159k
    return uncommon->getAsSubstTemplateTemplateParm();
135
5.84M
136
5.84M
  return nullptr;
137
5.84M
}
138
139
SubstTemplateTemplateParmPackStorage *
140
3.38M
TemplateName::getAsSubstTemplateTemplateParmPack() const {
141
3.38M
  if (UncommonTemplateNameStorage *Uncommon =
142
250
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
143
250
    return Uncommon->getAsSubstTemplateTemplateParmPack();
144
3.38M
145
3.38M
  return nullptr;
146
3.38M
}
147
148
15.3M
QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
149
15.3M
  return Storage.dyn_cast<QualifiedTemplateName *>();
150
15.3M
}
151
152
37.2M
DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
153
37.2M
  return Storage.dyn_cast<DependentTemplateName *>();
154
37.2M
}
155
156
29.8k
TemplateName TemplateName::getNameToSubstitute() const {
157
29.8k
  TemplateDecl *Decl = getAsTemplateDecl();
158
29.8k
159
  // Substituting a dependent template name: preserve it as written.
160
29.8k
  if (!Decl)
161
804
    return *this;
162
29.0k
163
  // If we have a template declaration, use the most recent non-friend
164
  // declaration of that template.
165
29.0k
  Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
166
29.0k
  while (Decl->getFriendObjectKind()) {
167
0
    Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
168
0
    assert(Decl && "all declarations of template are friends");
169
0
  }
170
29.0k
  return TemplateName(Decl);
171
29.0k
}
172
173
13.7M
TemplateNameDependence TemplateName::getDependence() const {
174
13.7M
  auto D = TemplateNameDependence::None;
175
13.7M
  switch (getKind()) {
176
129k
  case TemplateName::NameKind::QualifiedTemplate:
177
129k
    D |= toTemplateNameDependence(
178
129k
        getAsQualifiedTemplateName()->getQualifier()->getDependence());
179
129k
    break;
180
1.78k
  case TemplateName::NameKind::DependentTemplate:
181
1.78k
    D |= toTemplateNameDependence(
182
1.78k
        getAsDependentTemplateName()->getQualifier()->getDependence());
183
1.78k
    break;
184
8
  case TemplateName::NameKind::SubstTemplateTemplateParmPack:
185
8
    D |= TemplateNameDependence::UnexpandedPack;
186
8
    break;
187
0
  case TemplateName::NameKind::OverloadedTemplate:
188
0
    llvm_unreachable("overloaded templates shouldn't survive to here.");
189
13.6M
  default:
190
13.6M
    break;
191
13.7M
  }
192
13.7M
  if (TemplateDecl *Template = getAsTemplateDecl()) {
193
13.7M
    if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template)) {
194
25.8k
      D |= TemplateNameDependence::DependentInstantiation;
195
25.8k
      if (TTP->isParameterPack())
196
173
        D |= TemplateNameDependence::UnexpandedPack;
197
25.8k
    }
198
    // FIXME: Hack, getDeclContext() can be null if Template is still
199
    // initializing due to PCH reading, so we check it before using it.
200
    // Should probably modify TemplateSpecializationType to allow constructing
201
    // it without the isDependent() checking.
202
13.7M
    if (Template->getDeclContext() &&
203
13.7M
        Template->getDeclContext()->isDependentContext())
204
156k
      D |= TemplateNameDependence::DependentInstantiation;
205
3.38k
  } else {
206
3.38k
    D |= TemplateNameDependence::DependentInstantiation;
207
3.38k
  }
208
13.7M
  return D;
209
13.7M
}
210
211
5.32M
bool TemplateName::isDependent() const {
212
5.32M
  return getDependence() & TemplateNameDependence::Dependent;
213
5.32M
}
214
215
0
bool TemplateName::isInstantiationDependent() const {
216
0
  return getDependence() & TemplateNameDependence::Instantiation;
217
0
}
218
219
220
bool TemplateName::containsUnexpandedParameterPack() const {
220
220
  return getDependence() & TemplateNameDependence::UnexpandedPack;
221
220
}
222
223
void
224
TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
225
32.0k
                    bool SuppressNNS) const {
226
32.0k
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
227
31.7k
    OS << *Template;
228
279
  else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
229
50
    if (!SuppressNNS)
230
50
      QTN->getQualifier()->print(OS, Policy);
231
50
    if (QTN->hasTemplateKeyword())
232
19
      OS << "template ";
233
50
    OS << *QTN->getDecl();
234
229
  } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
235
8
    if (!SuppressNNS && DTN->getQualifier())
236
8
      DTN->getQualifier()->print(OS, Policy);
237
8
    OS << "template ";
238
8
239
8
    if (DTN->isIdentifier())
240
0
      OS << DTN->getIdentifier()->getName();
241
8
    else
242
8
      OS << "operator " << getOperatorSpelling(DTN->getOperator());
243
221
  } else if (SubstTemplateTemplateParmStorage *subst
244
218
               = getAsSubstTemplateTemplateParm()) {
245
218
    subst->getReplacement().print(OS, Policy, SuppressNNS);
246
3
  } else if (SubstTemplateTemplateParmPackStorage *SubstPack
247
0
                                        = getAsSubstTemplateTemplateParmPack())
248
0
    OS << *SubstPack->getParameterPack();
249
3
  else if (AssumedTemplateStorage *Assumed = getAsAssumedTemplateName()) {
250
0
    Assumed->getDeclName().print(OS, Policy);
251
3
  } else {
252
3
    OverloadedTemplateStorage *OTS = getAsOverloadedTemplate();
253
3
    (*OTS->begin())->printName(OS);
254
3
  }
255
32.0k
}
256
257
const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
258
276
                                           TemplateName N) {
259
276
  std::string NameStr;
260
276
  llvm::raw_string_ostream OS(NameStr);
261
276
  LangOptions LO;
262
276
  LO.CPlusPlus = true;
263
276
  LO.Bool = true;
264
276
  OS << '\'';
265
276
  N.print(OS, PrintingPolicy(LO));
266
276
  OS << '\'';
267
276
  OS.flush();
268
276
  return DB << NameStr;
269
276
}
270
271
const PartialDiagnostic&clang::operator<<(const PartialDiagnostic &PD,
272
27
                                           TemplateName N) {
273
27
  std::string NameStr;
274
27
  llvm::raw_string_ostream OS(NameStr);
275
27
  LangOptions LO;
276
27
  LO.CPlusPlus = true;
277
27
  LO.Bool = true;
278
27
  OS << '\'';
279
27
  N.print(OS, PrintingPolicy(LO));
280
27
  OS << '\'';
281
27
  OS.flush();
282
27
  return PD << NameStr;
283
27
}
284
285
67
void TemplateName::dump(raw_ostream &OS) const {
286
67
  LangOptions LO;  // FIXME!
287
67
  LO.CPlusPlus = true;
288
67
  LO.Bool = true;
289
67
  print(OS, PrintingPolicy(LO));
290
67
}
291
292
0
LLVM_DUMP_METHOD void TemplateName::dump() const {
293
0
  dump(llvm::errs());
294
0
}