Skip to content

Commit dc17429

Browse files
authored
[clang] improved preservation of template keyword (#133610)
1 parent 3c7a0e6 commit dc17429

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+612
-720
lines changed

clang-tools-extra/clangd/AST.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ getQualification(ASTContext &Context, const DeclContext *DestContext,
119119
// There can't be any more tag parents after hitting a namespace.
120120
assert(!ReachedNS);
121121
(void)ReachedNS;
122-
NNS = NestedNameSpecifier::Create(Context, nullptr, false,
123-
TD->getTypeForDecl());
122+
NNS = NestedNameSpecifier::Create(Context, nullptr, TD->getTypeForDecl());
124123
} else if (auto *NSD = llvm::dyn_cast<NamespaceDecl>(CurContext)) {
125124
ReachedNS = true;
126125
NNS = NestedNameSpecifier::Create(Context, nullptr, NSD);

clang-tools-extra/clangd/CodeComplete.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,6 @@ bool allowIndex(CodeCompletionContext &CC) {
14671467
return true;
14681468
case NestedNameSpecifier::Super:
14691469
case NestedNameSpecifier::TypeSpec:
1470-
case NestedNameSpecifier::TypeSpecWithTemplate:
14711470
// Unresolved inside a template.
14721471
case NestedNameSpecifier::Identifier:
14731472
return false;

clang-tools-extra/clangd/DumpAST.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ class DumpVisitor : public RecursiveASTVisitor<DumpVisitor> {
157157
NNS_KIND(Identifier);
158158
NNS_KIND(Namespace);
159159
NNS_KIND(TypeSpec);
160-
NNS_KIND(TypeSpecWithTemplate);
161160
NNS_KIND(Global);
162161
NNS_KIND(Super);
163162
NNS_KIND(NamespaceAlias);

clang-tools-extra/clangd/FindTarget.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ struct TargetFinder {
500500
}
501501
return;
502502
case NestedNameSpecifier::TypeSpec:
503-
case NestedNameSpecifier::TypeSpecWithTemplate:
504503
add(QualType(NNS->getAsType(), 0), Flags);
505504
return;
506505
case NestedNameSpecifier::Global:

clang-tools-extra/include-cleaner/lib/WalkAST.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
144144
case NestedNameSpecifier::Global:
145145
return true;
146146
case NestedNameSpecifier::TypeSpec:
147-
case NestedNameSpecifier::TypeSpecWithTemplate:
148147
case NestedNameSpecifier::Super:
149148
case NestedNameSpecifier::Identifier:
150149
return false;

clang/docs/ReleaseNotes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ Improvements to Clang's diagnostics
275275
- Diagnostics on chained comparisons (``a < b < c``) are now an error by default. This can be disabled with
276276
``-Wno-error=parentheses``.
277277
- Clang now better preserves the sugared types of pointers to member.
278+
- Clang now better preserves the presence of the template keyword with dependent
279+
prefixes.
280+
- When printing types for diagnostics, clang now doesn't suppress the scopes of
281+
template arguments contained within nested names.
278282
- The ``-Wshift-bool`` warning has been added to warn about shifting a boolean. (#GH28334)
279283
- Fixed diagnostics adding a trailing ``::`` when printing some source code
280284
constructs, like base classes.

clang/include/clang/AST/ASTContext.h

+7-10
Original file line numberDiff line numberDiff line change
@@ -1837,15 +1837,14 @@ class ASTContext : public RefCountedBase<ASTContext> {
18371837
TagDecl *OwnedTagDecl = nullptr) const;
18381838
QualType getDependentNameType(ElaboratedTypeKeyword Keyword,
18391839
NestedNameSpecifier *NNS,
1840-
const IdentifierInfo *Name,
1841-
QualType Canon = QualType()) const;
1840+
const IdentifierInfo *Name) const;
18421841

18431842
QualType getDependentTemplateSpecializationType(
1844-
ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
1845-
const IdentifierInfo *Name, ArrayRef<TemplateArgumentLoc> Args) const;
1843+
ElaboratedTypeKeyword Keyword, const DependentTemplateStorage &Name,
1844+
ArrayRef<TemplateArgumentLoc> Args) const;
18461845
QualType getDependentTemplateSpecializationType(
1847-
ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
1848-
const IdentifierInfo *Name, ArrayRef<TemplateArgument> Args) const;
1846+
ElaboratedTypeKeyword Keyword, const DependentTemplateStorage &Name,
1847+
ArrayRef<TemplateArgument> Args, bool IsCanonical = false) const;
18491848

18501849
TemplateArgument getInjectedTemplateArg(NamedDecl *ParamDecl) const;
18511850

@@ -2393,11 +2392,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
23932392
TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS,
23942393
bool TemplateKeyword,
23952394
TemplateName Template) const;
2395+
TemplateName
2396+
getDependentTemplateName(const DependentTemplateStorage &Name) const;
23962397

2397-
TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
2398-
const IdentifierInfo *Name) const;
2399-
TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
2400-
OverloadedOperatorKind Operator) const;
24012398
TemplateName
24022399
getSubstTemplateTemplateParm(TemplateName replacement, Decl *AssociatedDecl,
24032400
unsigned Index,

clang/include/clang/AST/ASTImporter.h

+8
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,14 @@ class TypeSourceInfo;
446446
/// returns nullptr only if the FromId was nullptr.
447447
IdentifierInfo *Import(const IdentifierInfo *FromId);
448448

449+
/// Import the given identifier or overloaded operator from the "from"
450+
/// context into the "to" context.
451+
///
452+
/// \returns The equivalent identifier or overloaded operator in the "to"
453+
/// context.
454+
IdentifierOrOverloadedOperator
455+
Import(IdentifierOrOverloadedOperator FromIO);
456+
449457
/// Import the given Objective-C selector from the "from"
450458
/// context into the "to" context.
451459
///

clang/include/clang/AST/ASTNodeTraverser.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,7 @@ class ASTNodeTraverser
396396
// FIXME: Provide a NestedNameSpecifier visitor.
397397
NestedNameSpecifier *Qualifier = T->getQualifier();
398398
if (NestedNameSpecifier::SpecifierKind K = Qualifier->getKind();
399-
K == NestedNameSpecifier::TypeSpec ||
400-
K == NestedNameSpecifier::TypeSpecWithTemplate)
399+
K == NestedNameSpecifier::TypeSpec)
401400
Visit(Qualifier->getAsType());
402401
if (T->isSugared())
403402
Visit(T->getMostRecentCXXRecordDecl()->getTypeForDecl());

clang/include/clang/AST/AbstractBasicReader.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,8 @@ class DataStreamBasicReader : public BasicReaderBase<Impl> {
279279
continue;
280280

281281
case NestedNameSpecifier::TypeSpec:
282-
case NestedNameSpecifier::TypeSpecWithTemplate:
283282
cur = NestedNameSpecifier::Create(ctx, cur,
284-
kind == NestedNameSpecifier::TypeSpecWithTemplate,
285-
asImpl().readQualType().getTypePtr());
283+
asImpl().readQualType().getTypePtr());
286284
continue;
287285

288286
case NestedNameSpecifier::Global:

clang/include/clang/AST/AbstractBasicWriter.h

-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ class DataStreamBasicWriter : public BasicWriterBase<Impl> {
260260
continue;
261261

262262
case NestedNameSpecifier::TypeSpec:
263-
case NestedNameSpecifier::TypeSpecWithTemplate:
264263
asImpl().writeQualType(QualType(NNS->getAsType(), 0));
265264
continue;
266265

clang/include/clang/AST/NestedNameSpecifier.h

+5-15
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class NestedNameSpecifier : public llvm::FoldingSetNode {
5252
enum StoredSpecifierKind {
5353
StoredIdentifier = 0,
5454
StoredDecl = 1,
55-
StoredTypeSpec = 2,
56-
StoredTypeSpecWithTemplate = 3
55+
StoredTypeSpec = 2
5756
};
5857

5958
/// The nested name specifier that precedes this nested name
@@ -89,10 +88,6 @@ class NestedNameSpecifier : public llvm::FoldingSetNode {
8988
/// A type, stored as a Type*.
9089
TypeSpec,
9190

92-
/// A type that was preceded by the 'template' keyword,
93-
/// stored as a Type*.
94-
TypeSpecWithTemplate,
95-
9691
/// The global specifier '::'. There is no stored value.
9792
Global,
9893

@@ -137,9 +132,8 @@ class NestedNameSpecifier : public llvm::FoldingSetNode {
137132
const NamespaceAliasDecl *Alias);
138133

139134
/// Builds a nested name specifier that names a type.
140-
static NestedNameSpecifier *Create(const ASTContext &Context,
141-
NestedNameSpecifier *Prefix,
142-
bool Template, const Type *T);
135+
static NestedNameSpecifier *
136+
Create(const ASTContext &Context, NestedNameSpecifier *Prefix, const Type *T);
143137

144138
/// Builds a specifier that consists of just an identifier.
145139
///
@@ -194,8 +188,7 @@ class NestedNameSpecifier : public llvm::FoldingSetNode {
194188

195189
/// Retrieve the type stored in this nested name specifier.
196190
const Type *getAsType() const {
197-
if (Prefix.getInt() == StoredTypeSpec ||
198-
Prefix.getInt() == StoredTypeSpecWithTemplate)
191+
if (Prefix.getInt() == StoredTypeSpec)
199192
return (const Type *)Specifier;
200193

201194
return nullptr;
@@ -401,13 +394,10 @@ class NestedNameSpecifierLocBuilder {
401394
/// \param Context The AST context in which this nested-name-specifier
402395
/// resides.
403396
///
404-
/// \param TemplateKWLoc The location of the 'template' keyword, if present.
405-
///
406397
/// \param TL The TypeLoc that describes the type preceding the '::'.
407398
///
408399
/// \param ColonColonLoc The location of the trailing '::'.
409-
void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
410-
SourceLocation ColonColonLoc);
400+
void Extend(ASTContext &Context, TypeLoc TL, SourceLocation ColonColonLoc);
411401

412402
/// Extend the current nested-name-specifier by another
413403
/// nested-name-specifier component of the form 'identifier::'.

clang/include/clang/AST/ODRHash.h

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class ODRHash {
9494
void AddStmt(const Stmt *S);
9595
void AddIdentifierInfo(const IdentifierInfo *II);
9696
void AddNestedNameSpecifier(const NestedNameSpecifier *NNS);
97+
void AddDependentTemplateName(const DependentTemplateStorage &Name);
9798
void AddTemplateName(TemplateName Name);
9899
void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
99100
void AddTemplateArgument(TemplateArgument TA);

clang/include/clang/AST/PropertiesBase.td

+8-7
Original file line numberDiff line numberDiff line change
@@ -692,25 +692,26 @@ let Class = PropertyTypeCase<TemplateName, "QualifiedTemplate"> in {
692692
let Class = PropertyTypeCase<TemplateName, "DependentTemplate"> in {
693693
def : ReadHelper<[{
694694
auto dtn = node.getAsDependentTemplateName();
695+
auto name = dtn->getName();
695696
}]>;
696697
def : Property<"qualifier", NestedNameSpecifier> {
697698
let Read = [{ dtn->getQualifier() }];
698699
}
699700
def : Property<"identifier", Optional<Identifier>> {
700-
let Read = [{ makeOptionalFromPointer(
701-
dtn->isIdentifier()
702-
? dtn->getIdentifier()
703-
: nullptr) }];
701+
let Read = [{ makeOptionalFromPointer(name.getIdentifier()) }];
704702
}
705703
def : Property<"operatorKind", OverloadedOperatorKind> {
706704
let Conditional = [{ !identifier }];
707-
let Read = [{ dtn->getOperator() }];
705+
let Read = [{ name.getOperator() }];
706+
}
707+
def : Property<"HasTemplateKeyword", Bool> {
708+
let Read = [{ dtn->hasTemplateKeyword() }];
708709
}
709710
def : Creator<[{
710711
if (identifier) {
711-
return ctx.getDependentTemplateName(qualifier, *identifier);
712+
return ctx.getDependentTemplateName({qualifier, *identifier, HasTemplateKeyword});
712713
} else {
713-
return ctx.getDependentTemplateName(qualifier, *operatorKind);
714+
return ctx.getDependentTemplateName({qualifier, *operatorKind, HasTemplateKeyword});
714715
}
715716
}]>;
716717
}

clang/include/clang/AST/RecursiveASTVisitor.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,6 @@ bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
795795
return true;
796796

797797
case NestedNameSpecifier::TypeSpec:
798-
case NestedNameSpecifier::TypeSpecWithTemplate:
799798
TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
800799
}
801800

@@ -820,7 +819,6 @@ bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
820819
return true;
821820

822821
case NestedNameSpecifier::TypeSpec:
823-
case NestedNameSpecifier::TypeSpecWithTemplate:
824822
TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
825823
break;
826824
}
@@ -1172,7 +1170,8 @@ DEF_TRAVERSE_TYPE(DependentNameType,
11721170
{ TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })
11731171

11741172
DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
1175-
TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1173+
const DependentTemplateStorage &S = T->getDependentTemplateName();
1174+
TRY_TO(TraverseNestedNameSpecifier(S.getQualifier()));
11761175
TRY_TO(TraverseTemplateArguments(T->template_arguments()));
11771176
})
11781177

0 commit comments

Comments
 (0)