Coverage Report

Created: 2020-02-25 14:32

/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/DeclBase.h"
15
#include "clang/AST/DeclTemplate.h"
16
#include "clang/AST/NestedNameSpecifier.h"
17
#include "clang/AST/PrettyPrinter.h"
18
#include "clang/AST/TemplateBase.h"
19
#include "clang/Basic/Diagnostic.h"
20
#include "clang/Basic/LLVM.h"
21
#include "clang/Basic/LangOptions.h"
22
#include "clang/Basic/OperatorKinds.h"
23
#include "llvm/ADT/ArrayRef.h"
24
#include "llvm/ADT/FoldingSet.h"
25
#include "llvm/Support/Casting.h"
26
#include "llvm/Support/Compiler.h"
27
#include "llvm/Support/raw_ostream.h"
28
#include <cassert>
29
#include <string>
30
31
using namespace clang;
32
33
TemplateArgument
34
9
SubstTemplateTemplateParmPackStorage::getArgumentPack() const {
35
9
  return TemplateArgument(llvm::makeArrayRef(Arguments, size()));
36
9
}
37
38
29.6k
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
39
29.6k
  Profile(ID, Parameter, Replacement);
40
29.6k
}
41
42
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID,
43
                                           TemplateTemplateParmDecl *parameter,
44
61.0k
                                               TemplateName replacement) {
45
61.0k
  ID.AddPointer(parameter);
46
61.0k
  ID.AddPointer(replacement.getAsVoidPointer());
47
61.0k
}
48
49
void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
50
0
                                                   ASTContext &Context) {
51
0
  Profile(ID, Context, Parameter, getArgumentPack());
52
0
}
53
54
void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
55
                                                   ASTContext &Context,
56
                                           TemplateTemplateParmDecl *Parameter,
57
8
                                             const TemplateArgument &ArgPack) {
58
8
  ID.AddPointer(Parameter);
59
8
  ArgPack.Profile(ID, Context);
60
8
}
61
62
3.19M
TemplateName::TemplateName(void *Ptr) {
63
3.19M
  Storage = StorageType::getFromOpaqueValue(Ptr);
64
3.19M
}
65
66
9.67M
TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
67
TemplateName::TemplateName(OverloadedTemplateStorage *Storage)
68
122k
    : Storage(Storage) {}
69
TemplateName::TemplateName(AssumedTemplateStorage *Storage)
70
728
    : Storage(Storage) {}
71
TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
72
31.4k
    : Storage(Storage) {}
73
TemplateName::TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
74
8
    : Storage(Storage) {}
75
261k
TemplateName::TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) {}
76
234k
TemplateName::TemplateName(DependentTemplateName *Dep) : Storage(Dep) {}
77
78
3.88M
bool TemplateName::isNull() const { return Storage.isNull(); }
79
80
15.2M
TemplateName::NameKind TemplateName::getKind() const {
81
15.2M
  if (Storage.is<TemplateDecl *>())
82
14.9M
    return Template;
83
263k
  if (Storage.is<DependentTemplateName *>())
84
12.7k
    return DependentTemplate;
85
250k
  if (Storage.is<QualifiedTemplateName *>())
86
65.8k
    return QualifiedTemplate;
87
185k
88
185k
  UncommonTemplateNameStorage *uncommon
89
185k
    = Storage.get<UncommonTemplateNameStorage*>();
90
185k
  if (uncommon->getAsOverloadedStorage())
91
125k
    return OverloadedTemplate;
92
59.5k
  if (uncommon->getAsAssumedTemplateName())
93
50
    return AssumedTemplate;
94
59.5k
  if (uncommon->getAsSubstTemplateTemplateParm())
95
59.5k
    return SubstTemplateTemplateParm;
96
14
  return SubstTemplateTemplateParmPack;
97
14
}
98
99
53.1M
TemplateDecl *TemplateName::getAsTemplateDecl() const {
100
53.1M
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
101
52.2M
    return Template;
102
895k
103
895k
  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
104
739k
    return QTN->getTemplateDecl();
105
156k
106
156k
  if (SubstTemplateTemplateParmStorage *sub = getAsSubstTemplateTemplateParm())
107
131k
    return sub->getReplacement().getAsTemplateDecl();
108
24.7k
109
24.7k
  return nullptr;
110
24.7k
}
111
112
1.31M
OverloadedTemplateStorage *TemplateName::getAsOverloadedTemplate() const {
113
1.31M
  if (UncommonTemplateNameStorage *Uncommon =
114
126k
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
115
126k
    return Uncommon->getAsOverloadedStorage();
116
1.19M
117
1.19M
  return nullptr;
118
1.19M
}
119
120
2.23M
AssumedTemplateStorage *TemplateName::getAsAssumedTemplateName() const {
121
2.23M
  if (UncommonTemplateNameStorage *Uncommon =
122
322
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
123
322
    return Uncommon->getAsAssumedTemplateName();
124
2.23M
125
2.23M
  return nullptr;
126
2.23M
}
127
128
SubstTemplateTemplateParmStorage *
129
6.14M
TemplateName::getAsSubstTemplateTemplateParm() const {
130
6.14M
  if (UncommonTemplateNameStorage *uncommon =
131
166k
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
132
166k
    return uncommon->getAsSubstTemplateTemplateParm();
133
5.97M
134
5.97M
  return nullptr;
135
5.97M
}
136
137
SubstTemplateTemplateParmPackStorage *
138
3.37M
TemplateName::getAsSubstTemplateTemplateParmPack() const {
139
3.37M
  if (UncommonTemplateNameStorage *Uncommon =
140
1.01k
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
141
1.01k
    return Uncommon->getAsSubstTemplateTemplateParmPack();
142
3.37M
143
3.37M
  return nullptr;
144
3.37M
}
145
146
24.7M
QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
147
24.7M
  return Storage.dyn_cast<QualifiedTemplateName *>();
148
24.7M
}
149
150
38.3M
DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
151
38.3M
  return Storage.dyn_cast<DependentTemplateName *>();
152
38.3M
}
153
154
30.0k
TemplateName TemplateName::getNameToSubstitute() const {
155
30.0k
  TemplateDecl *Decl = getAsTemplateDecl();
156
30.0k
157
30.0k
  // Substituting a dependent template name: preserve it as written.
158
30.0k
  if (!Decl)
159
762
    return *this;
160
29.2k
161
29.2k
  // If we have a template declaration, use the most recent non-friend
162
29.2k
  // declaration of that template.
163
29.2k
  Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
164
29.2k
  while (Decl->getFriendObjectKind()) {
165
0
    Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
166
0
    assert(Decl && "all declarations of template are friends");
167
0
  }
168
29.2k
  return TemplateName(Decl);
169
29.2k
}
170
171
5.60M
bool TemplateName::isDependent() const {
172
5.60M
  if (TemplateDecl *Template = getAsTemplateDecl()) {
173
5.60M
    if (isa<TemplateTemplateParmDecl>(Template))
174
12.5k
      return true;
175
5.58M
    // FIXME: Hack, getDeclContext() can be null if Template is still
176
5.58M
    // initializing due to PCH reading, so we check it before using it.
177
5.58M
    // Should probably modify TemplateSpecializationType to allow constructing
178
5.58M
    // it without the isDependent() checking.
179
5.58M
    return Template->getDeclContext() &&
180
5.58M
           Template->getDeclContext()->isDependentContext();
181
5.58M
  }
182
3.16k
183
3.16k
  assert(!getAsOverloadedTemplate() &&
184
3.16k
         "overloaded templates shouldn't survive to here");
185
3.16k
186
3.16k
  return true;
187
3.16k
}
188
189
62.0k
bool TemplateName::isInstantiationDependent() const {
190
62.0k
  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
191
34.9k
    if (QTN->getQualifier()->isInstantiationDependent())
192
2
      return true;
193
62.0k
  }
194
62.0k
195
62.0k
  return isDependent();
196
62.0k
}
197
198
8.58M
bool TemplateName::containsUnexpandedParameterPack() const {
199
8.58M
  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
200
34.8k
    if (QTN->getQualifier()->containsUnexpandedParameterPack())
201
5
      return true;
202
8.58M
  }
203
8.58M
204
8.58M
  if (TemplateDecl *Template = getAsTemplateDecl()) {
205
8.58M
    if (TemplateTemplateParmDecl *TTP
206
17.6k
                                  = dyn_cast<TemplateTemplateParmDecl>(Template))
207
17.6k
      return TTP->isParameterPack();
208
8.56M
209
8.56M
    return false;
210
8.56M
  }
211
1.65k
212
1.65k
  if (DependentTemplateName *DTN = getAsDependentTemplateName())
213
896
    return DTN->getQualifier() &&
214
896
      DTN->getQualifier()->containsUnexpandedParameterPack();
215
760
216
760
  return getAsSubstTemplateTemplateParmPack() != nullptr;
217
760
}
218
219
void
220
TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
221
28.8k
                    bool SuppressNNS) const {
222
28.8k
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
223
28.4k
    OS << *Template;
224
370
  else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
225
49
    if (!SuppressNNS)
226
49
      QTN->getQualifier()->print(OS, Policy);
227
49
    if (QTN->hasTemplateKeyword())
228
19
      OS << "template ";
229
49
    OS << *QTN->getDecl();
230
321
  } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
231
8
    if (!SuppressNNS && DTN->getQualifier())
232
8
      DTN->getQualifier()->print(OS, Policy);
233
8
    OS << "template ";
234
8
235
8
    if (DTN->isIdentifier())
236
0
      OS << DTN->getIdentifier()->getName();
237
8
    else
238
8
      OS << "operator " << getOperatorSpelling(DTN->getOperator());
239
313
  } else if (SubstTemplateTemplateParmStorage *subst
240
310
               = getAsSubstTemplateTemplateParm()) {
241
310
    subst->getReplacement().print(OS, Policy, SuppressNNS);
242
310
  } else 
if (SubstTemplateTemplateParmPackStorage *3
SubstPack3
243
0
                                        = getAsSubstTemplateTemplateParmPack())
244
0
    OS << *SubstPack->getParameterPack();
245
3
  else if (AssumedTemplateStorage *Assumed = getAsAssumedTemplateName()) {
246
0
    Assumed->getDeclName().print(OS, Policy);
247
3
  } else {
248
3
    OverloadedTemplateStorage *OTS = getAsOverloadedTemplate();
249
3
    (*OTS->begin())->printName(OS);
250
3
  }
251
28.8k
}
252
253
const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
254
260
                                           TemplateName N) {
255
260
  std::string NameStr;
256
260
  llvm::raw_string_ostream OS(NameStr);
257
260
  LangOptions LO;
258
260
  LO.CPlusPlus = true;
259
260
  LO.Bool = true;
260
260
  OS << '\'';
261
260
  N.print(OS, PrintingPolicy(LO));
262
260
  OS << '\'';
263
260
  OS.flush();
264
260
  return DB << NameStr;
265
260
}
266
267
const PartialDiagnostic&clang::operator<<(const PartialDiagnostic &PD,
268
24
                                           TemplateName N) {
269
24
  std::string NameStr;
270
24
  llvm::raw_string_ostream OS(NameStr);
271
24
  LangOptions LO;
272
24
  LO.CPlusPlus = true;
273
24
  LO.Bool = true;
274
24
  OS << '\'';
275
24
  N.print(OS, PrintingPolicy(LO));
276
24
  OS << '\'';
277
24
  OS.flush();
278
24
  return PD << NameStr;
279
24
}
280
281
62
void TemplateName::dump(raw_ostream &OS) const {
282
62
  LangOptions LO;  // FIXME!
283
62
  LO.CPlusPlus = true;
284
62
  LO.Bool = true;
285
62
  print(OS, PrintingPolicy(LO));
286
62
}
287
288
0
LLVM_DUMP_METHOD void TemplateName::dump() const {
289
0
  dump(llvm::errs());
290
0
}