Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
25.4k
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
39
25.4k
  Profile(ID, Parameter, Replacement);
40
25.4k
}
41
42
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID,
43
                                           TemplateTemplateParmDecl *parameter,
44
51.3k
                                               TemplateName replacement) {
45
51.3k
  ID.AddPointer(parameter);
46
51.3k
  ID.AddPointer(replacement.getAsVoidPointer());
47
51.3k
}
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.27M
TemplateName::TemplateName(void *Ptr) {
63
3.27M
  Storage = StorageType::getFromOpaqueValue(Ptr);
64
3.27M
}
65
66
10.9M
TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
67
TemplateName::TemplateName(OverloadedTemplateStorage *Storage)
68
139k
    : Storage(Storage) {}
69
TemplateName::TemplateName(AssumedTemplateStorage *Storage)
70
935
    : Storage(Storage) {}
71
TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
72
25.9k
    : Storage(Storage) {}
73
TemplateName::TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
74
8
    : Storage(Storage) {}
75
304k
TemplateName::TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) {}
76
118k
TemplateName::TemplateName(DependentTemplateName *Dep) : Storage(Dep) {}
77
78
4.39M
bool TemplateName::isNull() const { return Storage.isNull(); }
79
80
6.91M
TemplateName::NameKind TemplateName::getKind() const {
81
6.91M
  if (Storage.is<TemplateDecl *>())
82
6.70M
    return Template;
83
205k
  if (Storage.is<DependentTemplateName *>())
84
9.96k
    return DependentTemplate;
85
195k
  if (Storage.is<QualifiedTemplateName *>())
86
50.9k
    return QualifiedTemplate;
87
144k
88
144k
  UncommonTemplateNameStorage *uncommon
89
144k
    = Storage.get<UncommonTemplateNameStorage*>();
90
144k
  if (uncommon->getAsOverloadedStorage())
91
142k
    return OverloadedTemplate;
92
1.49k
  if (uncommon->getAsAssumedTemplateName())
93
53
    return AssumedTemplate;
94
1.44k
  if (uncommon->getAsSubstTemplateTemplateParm())
95
1.44k
    return SubstTemplateTemplateParm;
96
2
  return SubstTemplateTemplateParmPack;
97
2
}
98
99
58.5M
TemplateDecl *TemplateName::getAsTemplateDecl() const {
100
58.5M
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
101
57.6M
    return Template;
102
953k
103
953k
  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
104
823k
    return QTN->getTemplateDecl();
105
130k
106
130k
  if (SubstTemplateTemplateParmStorage *sub = getAsSubstTemplateTemplateParm())
107
113k
    return sub->getReplacement().getAsTemplateDecl();
108
17.0k
109
17.0k
  return nullptr;
110
17.0k
}
111
112
1.43M
OverloadedTemplateStorage *TemplateName::getAsOverloadedTemplate() const {
113
1.43M
  if (UncommonTemplateNameStorage *Uncommon =
114
142k
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
115
142k
    return Uncommon->getAsOverloadedStorage();
116
1.29M
117
1.29M
  return nullptr;
118
1.29M
}
119
120
2.38M
AssumedTemplateStorage *TemplateName::getAsAssumedTemplateName() const {
121
2.38M
  if (UncommonTemplateNameStorage *Uncommon =
122
469
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
123
469
    return Uncommon->getAsAssumedTemplateName();
124
2.38M
125
2.38M
  return nullptr;
126
2.38M
}
127
128
SubstTemplateTemplateParmStorage *
129
6.86M
TemplateName::getAsSubstTemplateTemplateParm() const {
130
6.86M
  if (UncommonTemplateNameStorage *uncommon =
131
139k
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
132
139k
    return uncommon->getAsSubstTemplateTemplateParm();
133
6.73M
134
6.73M
  return nullptr;
135
6.73M
}
136
137
SubstTemplateTemplateParmPackStorage *
138
3.89M
TemplateName::getAsSubstTemplateTemplateParmPack() const {
139
3.89M
  if (UncommonTemplateNameStorage *Uncommon =
140
269
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
141
269
    return Uncommon->getAsSubstTemplateTemplateParmPack();
142
3.89M
143
3.89M
  return nullptr;
144
3.89M
}
145
146
25.4M
QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
147
25.4M
  return Storage.dyn_cast<QualifiedTemplateName *>();
148
25.4M
}
149
150
13.5M
DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
151
13.5M
  return Storage.dyn_cast<DependentTemplateName *>();
152
13.5M
}
153
154
25.9k
TemplateName TemplateName::getNameToSubstitute() const {
155
25.9k
  TemplateDecl *Decl = getAsTemplateDecl();
156
25.9k
157
25.9k
  // Substituting a dependent template name: preserve it as written.
158
25.9k
  if (!Decl)
159
34
    return *this;
160
25.8k
161
25.8k
  // If we have a template declaration, use the most recent non-friend
162
25.8k
  // declaration of that template.
163
25.8k
  Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
164
25.8k
  while (Decl->getFriendObjectKind()) {
165
0
    Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
166
0
    assert(Decl && "all declarations of template are friends");
167
0
  }
168
25.8k
  return TemplateName(Decl);
169
25.8k
}
170
171
6.45M
bool TemplateName::isDependent() const {
172
6.45M
  if (TemplateDecl *Template = getAsTemplateDecl()) {
173
6.45M
    if (isa<TemplateTemplateParmDecl>(Template))
174
12.7k
      return true;
175
6.44M
    // FIXME: Hack, getDeclContext() can be null if Template is still
176
6.44M
    // initializing due to PCH reading, so we check it before using it.
177
6.44M
    // Should probably modify TemplateSpecializationType to allow constructing
178
6.44M
    // it without the isDependent() checking.
179
6.44M
    return Template->getDeclContext() &&
180
6.44M
           Template->getDeclContext()->isDependentContext();
181
6.44M
  }
182
83
183
83
  assert(!getAsOverloadedTemplate() &&
184
83
         "overloaded templates shouldn't survive to here");
185
83
186
83
  return true;
187
83
}
188
189
51.0k
bool TemplateName::isInstantiationDependent() const {
190
51.0k
  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
191
33.0k
    if (QTN->getQualifier()->isInstantiationDependent())
192
2
      return true;
193
51.0k
  }
194
51.0k
195
51.0k
  return isDependent();
196
51.0k
}
197
198
8.69M
bool TemplateName::containsUnexpandedParameterPack() const {
199
8.69M
  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
200
32.9k
    if (QTN->getQualifier()->containsUnexpandedParameterPack())
201
3
      return true;
202
8.69M
  }
203
8.69M
204
8.69M
  if (TemplateDecl *Template = getAsTemplateDecl()) {
205
8.69M
    if (TemplateTemplateParmDecl *TTP
206
16.3k
                                  = dyn_cast<TemplateTemplateParmDecl>(Template))
207
16.3k
      return TTP->isParameterPack();
208
8.68M
209
8.68M
    return false;
210
8.68M
  }
211
84
212
84
  if (DependentTemplateName *DTN = getAsDependentTemplateName())
213
56
    return DTN->getQualifier() &&
214
56
      DTN->getQualifier()->containsUnexpandedParameterPack();
215
28
216
28
  return getAsSubstTemplateTemplateParmPack() != nullptr;
217
28
}
218
219
void
220
TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
221
7.39k
                    bool SuppressNNS) const {
222
7.39k
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
223
7.26k
    OS << *Template;
224
130
  else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
225
47
    if (!SuppressNNS)
226
47
      QTN->getQualifier()->print(OS, Policy);
227
47
    if (QTN->hasTemplateKeyword())
228
18
      OS << "template ";
229
47
    OS << *QTN->getDecl();
230
83
  } 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
75
  } else if (SubstTemplateTemplateParmStorage *subst
240
72
               = getAsSubstTemplateTemplateParm()) {
241
72
    subst->getReplacement().print(OS, Policy, SuppressNNS);
242
72
  } 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
7.39k
}
252
253
const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
254
236
                                           TemplateName N) {
255
236
  std::string NameStr;
256
236
  llvm::raw_string_ostream OS(NameStr);
257
236
  LangOptions LO;
258
236
  LO.CPlusPlus = true;
259
236
  LO.Bool = true;
260
236
  OS << '\'';
261
236
  N.print(OS, PrintingPolicy(LO));
262
236
  OS << '\'';
263
236
  OS.flush();
264
236
  return DB << NameStr;
265
236
}
266
267
const PartialDiagnostic&clang::operator<<(const PartialDiagnostic &PD,
268
18
                                           TemplateName N) {
269
18
  std::string NameStr;
270
18
  llvm::raw_string_ostream OS(NameStr);
271
18
  LangOptions LO;
272
18
  LO.CPlusPlus = true;
273
18
  LO.Bool = true;
274
18
  OS << '\'';
275
18
  N.print(OS, PrintingPolicy(LO));
276
18
  OS << '\'';
277
18
  OS.flush();
278
18
  return PD << NameStr;
279
18
}
280
281
23
void TemplateName::dump(raw_ostream &OS) const {
282
23
  LangOptions LO;  // FIXME!
283
23
  LO.CPlusPlus = true;
284
23
  LO.Bool = true;
285
23
  print(OS, PrintingPolicy(LO));
286
23
}
287
288
0
LLVM_DUMP_METHOD void TemplateName::dump() const {
289
0
  dump(llvm::errs());
290
0
}