Skip to content

[clang] improved preservation of template keyword #133610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions clang-tools-extra/clangd/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ getQualification(ASTContext &Context, const DeclContext *DestContext,
// There can't be any more tag parents after hitting a namespace.
assert(!ReachedNS);
(void)ReachedNS;
NNS = NestedNameSpecifier::Create(Context, nullptr, false,
TD->getTypeForDecl());
NNS = NestedNameSpecifier::Create(Context, nullptr, TD->getTypeForDecl());
} else if (auto *NSD = llvm::dyn_cast<NamespaceDecl>(CurContext)) {
ReachedNS = true;
NNS = NestedNameSpecifier::Create(Context, nullptr, NSD);
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clangd/CodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,6 @@ bool allowIndex(CodeCompletionContext &CC) {
return true;
case NestedNameSpecifier::Super:
case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate:
// Unresolved inside a template.
case NestedNameSpecifier::Identifier:
return false;
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clangd/DumpAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ class DumpVisitor : public RecursiveASTVisitor<DumpVisitor> {
NNS_KIND(Identifier);
NNS_KIND(Namespace);
NNS_KIND(TypeSpec);
NNS_KIND(TypeSpecWithTemplate);
NNS_KIND(Global);
NNS_KIND(Super);
NNS_KIND(NamespaceAlias);
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clangd/FindTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,6 @@ struct TargetFinder {
}
return;
case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate:
add(QualType(NNS->getAsType(), 0), Flags);
return;
case NestedNameSpecifier::Global:
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/include-cleaner/lib/WalkAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
case NestedNameSpecifier::Global:
return true;
case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate:
case NestedNameSpecifier::Super:
case NestedNameSpecifier::Identifier:
return false;
Expand Down
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ Improvements to Clang's diagnostics
- Diagnostics on chained comparisons (``a < b < c``) are now an error by default. This can be disabled with
``-Wno-error=parentheses``.
- Clang now better preserves the sugared types of pointers to member.
- Clang now better preserves the presence of the template keyword with dependent
prefixes.
- When printing types for diagnostics, clang now doesn't suppress the scopes of
template arguments contained within nested names.
- The ``-Wshift-bool`` warning has been added to warn about shifting a boolean. (#GH28334)
- Fixed diagnostics adding a trailing ``::`` when printing some source code
constructs, like base classes.
Expand Down
17 changes: 7 additions & 10 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1837,15 +1837,14 @@ class ASTContext : public RefCountedBase<ASTContext> {
TagDecl *OwnedTagDecl = nullptr) const;
QualType getDependentNameType(ElaboratedTypeKeyword Keyword,
NestedNameSpecifier *NNS,
const IdentifierInfo *Name,
QualType Canon = QualType()) const;
const IdentifierInfo *Name) const;

QualType getDependentTemplateSpecializationType(
ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
const IdentifierInfo *Name, ArrayRef<TemplateArgumentLoc> Args) const;
ElaboratedTypeKeyword Keyword, const DependentTemplateStorage &Name,
ArrayRef<TemplateArgumentLoc> Args) const;
QualType getDependentTemplateSpecializationType(
ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
const IdentifierInfo *Name, ArrayRef<TemplateArgument> Args) const;
ElaboratedTypeKeyword Keyword, const DependentTemplateStorage &Name,
ArrayRef<TemplateArgument> Args, bool IsCanonical = false) const;

TemplateArgument getInjectedTemplateArg(NamedDecl *ParamDecl) const;

Expand Down Expand Up @@ -2393,11 +2392,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS,
bool TemplateKeyword,
TemplateName Template) const;
TemplateName
getDependentTemplateName(const DependentTemplateStorage &Name) const;

TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
const IdentifierInfo *Name) const;
TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
OverloadedOperatorKind Operator) const;
TemplateName
getSubstTemplateTemplateParm(TemplateName replacement, Decl *AssociatedDecl,
unsigned Index,
Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/AST/ASTImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ class TypeSourceInfo;
/// returns nullptr only if the FromId was nullptr.
IdentifierInfo *Import(const IdentifierInfo *FromId);

/// Import the given identifier or overloaded operator from the "from"
/// context into the "to" context.
///
/// \returns The equivalent identifier or overloaded operator in the "to"
/// context.
IdentifierOrOverloadedOperator
Import(IdentifierOrOverloadedOperator FromIO);

/// Import the given Objective-C selector from the "from"
/// context into the "to" context.
///
Expand Down
3 changes: 1 addition & 2 deletions clang/include/clang/AST/ASTNodeTraverser.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ class ASTNodeTraverser
// FIXME: Provide a NestedNameSpecifier visitor.
NestedNameSpecifier *Qualifier = T->getQualifier();
if (NestedNameSpecifier::SpecifierKind K = Qualifier->getKind();
K == NestedNameSpecifier::TypeSpec ||
K == NestedNameSpecifier::TypeSpecWithTemplate)
K == NestedNameSpecifier::TypeSpec)
Visit(Qualifier->getAsType());
if (T->isSugared())
Visit(T->getMostRecentCXXRecordDecl()->getTypeForDecl());
Expand Down
4 changes: 1 addition & 3 deletions clang/include/clang/AST/AbstractBasicReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,8 @@ class DataStreamBasicReader : public BasicReaderBase<Impl> {
continue;

case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate:
cur = NestedNameSpecifier::Create(ctx, cur,
kind == NestedNameSpecifier::TypeSpecWithTemplate,
asImpl().readQualType().getTypePtr());
asImpl().readQualType().getTypePtr());
continue;

case NestedNameSpecifier::Global:
Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/AST/AbstractBasicWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ class DataStreamBasicWriter : public BasicWriterBase<Impl> {
continue;

case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate:
asImpl().writeQualType(QualType(NNS->getAsType(), 0));
continue;

Expand Down
20 changes: 5 additions & 15 deletions clang/include/clang/AST/NestedNameSpecifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class NestedNameSpecifier : public llvm::FoldingSetNode {
enum StoredSpecifierKind {
StoredIdentifier = 0,
StoredDecl = 1,
StoredTypeSpec = 2,
StoredTypeSpecWithTemplate = 3
StoredTypeSpec = 2
};

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

/// A type that was preceded by the 'template' keyword,
/// stored as a Type*.
TypeSpecWithTemplate,

/// The global specifier '::'. There is no stored value.
Global,

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

/// Builds a nested name specifier that names a type.
static NestedNameSpecifier *Create(const ASTContext &Context,
NestedNameSpecifier *Prefix,
bool Template, const Type *T);
static NestedNameSpecifier *
Create(const ASTContext &Context, NestedNameSpecifier *Prefix, const Type *T);

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

/// Retrieve the type stored in this nested name specifier.
const Type *getAsType() const {
if (Prefix.getInt() == StoredTypeSpec ||
Prefix.getInt() == StoredTypeSpecWithTemplate)
if (Prefix.getInt() == StoredTypeSpec)
return (const Type *)Specifier;

return nullptr;
Expand Down Expand Up @@ -401,13 +394,10 @@ class NestedNameSpecifierLocBuilder {
/// \param Context The AST context in which this nested-name-specifier
/// resides.
///
/// \param TemplateKWLoc The location of the 'template' keyword, if present.
///
/// \param TL The TypeLoc that describes the type preceding the '::'.
///
/// \param ColonColonLoc The location of the trailing '::'.
void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
SourceLocation ColonColonLoc);
void Extend(ASTContext &Context, TypeLoc TL, SourceLocation ColonColonLoc);

/// Extend the current nested-name-specifier by another
/// nested-name-specifier component of the form 'identifier::'.
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/AST/ODRHash.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class ODRHash {
void AddStmt(const Stmt *S);
void AddIdentifierInfo(const IdentifierInfo *II);
void AddNestedNameSpecifier(const NestedNameSpecifier *NNS);
void AddDependentTemplateName(const DependentTemplateStorage &Name);
void AddTemplateName(TemplateName Name);
void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
void AddTemplateArgument(TemplateArgument TA);
Expand Down
15 changes: 8 additions & 7 deletions clang/include/clang/AST/PropertiesBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -692,25 +692,26 @@ let Class = PropertyTypeCase<TemplateName, "QualifiedTemplate"> in {
let Class = PropertyTypeCase<TemplateName, "DependentTemplate"> in {
def : ReadHelper<[{
auto dtn = node.getAsDependentTemplateName();
auto name = dtn->getName();
}]>;
def : Property<"qualifier", NestedNameSpecifier> {
let Read = [{ dtn->getQualifier() }];
}
def : Property<"identifier", Optional<Identifier>> {
let Read = [{ makeOptionalFromPointer(
dtn->isIdentifier()
? dtn->getIdentifier()
: nullptr) }];
let Read = [{ makeOptionalFromPointer(name.getIdentifier()) }];
}
def : Property<"operatorKind", OverloadedOperatorKind> {
let Conditional = [{ !identifier }];
let Read = [{ dtn->getOperator() }];
let Read = [{ name.getOperator() }];
}
def : Property<"HasTemplateKeyword", Bool> {
let Read = [{ dtn->hasTemplateKeyword() }];
}
def : Creator<[{
if (identifier) {
return ctx.getDependentTemplateName(qualifier, *identifier);
return ctx.getDependentTemplateName({qualifier, *identifier, HasTemplateKeyword});
} else {
return ctx.getDependentTemplateName(qualifier, *operatorKind);
return ctx.getDependentTemplateName({qualifier, *operatorKind, HasTemplateKeyword});
}
}]>;
}
Expand Down
5 changes: 2 additions & 3 deletions clang/include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,6 @@ bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
return true;

case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate:
TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
}

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

case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate:
TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
break;
}
Expand Down Expand Up @@ -1172,7 +1170,8 @@ DEF_TRAVERSE_TYPE(DependentNameType,
{ TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); })

DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
const DependentTemplateStorage &S = T->getDependentTemplateName();
TRY_TO(TraverseNestedNameSpecifier(S.getQualifier()));
TRY_TO(TraverseTemplateArguments(T->template_arguments()));
})

Expand Down
Loading