Skip to content

Commit

Permalink
[clang] source range of variable template specialization should inclu…
Browse files Browse the repository at this point in the history
…de initializer

This patch adjust the getSourceRange() for the
VarTemplateSpecializationDecl and VarTemplatePartialSpecializationDecl,
such that the initializer is included if present:
```
template<typename T>
T temp = 1;

template<> double temp<double> = 1;
```
This patch makes it consistent with the behavior of
non-template variables with initializers and restores
behavior that was present before https://reviews.llvm.org/D139705.

n case, when the initializer is not present we still
include the template arguments in the source range,
which was required for fixing zero-initialization fix-it.

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D146733
  • Loading branch information
Tomasz Kamiński committed Mar 27, 2023
1 parent 9580beb commit a826b90
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
5 changes: 5 additions & 0 deletions clang/docs/ReleaseNotes.rst
Expand Up @@ -230,6 +230,11 @@ Bug Fixes in This Version
antecipation of `CWG2563 <https://cplusplus.github.io/CWG/issues/2563.html>_`.
- Fix highlighting issue with ``_Complex`` and initialization list with more than
2 items. (`#61518 <https://github.com/llvm/llvm-project/issues/61518>`_)
- Fix ``getSourceRange`` on ``VarTemplateSpecializationDecl`` and
``VarTemplatePartialSpecializationDecl``, which represents variable with
the initializer, so it behaves consistently with other ``VarDecls`` and ends
on the last token of initializer, instead of right angle bracket of
the template argument list.

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
16 changes: 2 additions & 14 deletions clang/include/clang/AST/DeclTemplate.h
Expand Up @@ -2926,13 +2926,7 @@ class VarTemplateSpecializationDecl : public VarDecl,
return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
}

SourceRange getSourceRange() const override LLVM_READONLY {
if (isExplicitSpecialization()) {
if (const ASTTemplateArgumentListInfo *Info = getTemplateArgsInfo())
return SourceRange(getOuterLocStart(), Info->getRAngleLoc());
}
return VarDecl::getSourceRange();
}
SourceRange getSourceRange() const override LLVM_READONLY;

void Profile(llvm::FoldingSetNodeID &ID) const {
Profile(ID, TemplateArgs->asArray(), getASTContext());
Expand Down Expand Up @@ -3091,13 +3085,7 @@ class VarTemplatePartialSpecializationDecl
return First->InstantiatedFromMember.setInt(true);
}

SourceRange getSourceRange() const override LLVM_READONLY {
if (isExplicitSpecialization()) {
if (const ASTTemplateArgumentListInfo *Info = getTemplateArgsAsWritten())
return SourceRange(getOuterLocStart(), Info->getRAngleLoc());
}
return VarDecl::getSourceRange();
}
SourceRange getSourceRange() const override LLVM_READONLY;

void Profile(llvm::FoldingSetNodeID &ID) const {
Profile(ID, getTemplateArgs().asArray(), getTemplateParameters(),
Expand Down
17 changes: 17 additions & 0 deletions clang/lib/AST/DeclTemplate.cpp
Expand Up @@ -1402,6 +1402,15 @@ void VarTemplateSpecializationDecl::setTemplateArgsInfo(
ASTTemplateArgumentListInfo::Create(getASTContext(), ArgsInfo);
}

SourceRange VarTemplateSpecializationDecl::getSourceRange() const {
if (isExplicitSpecialization() && !hasInit()) {
if (const ASTTemplateArgumentListInfo *Info = getTemplateArgsInfo())
return SourceRange(getOuterLocStart(), Info->getRAngleLoc());
}
return VarDecl::getSourceRange();
}


//===----------------------------------------------------------------------===//
// VarTemplatePartialSpecializationDecl Implementation
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1447,6 +1456,14 @@ VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
return new (C, ID) VarTemplatePartialSpecializationDecl(C);
}

SourceRange VarTemplatePartialSpecializationDecl::getSourceRange() const {
if (isExplicitSpecialization() && !hasInit()) {
if (const ASTTemplateArgumentListInfo *Info = getTemplateArgsAsWritten())
return SourceRange(getOuterLocStart(), Info->getRAngleLoc());
}
return VarDecl::getSourceRange();
}

static TemplateParameterList *
createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
// typename T
Expand Down
8 changes: 2 additions & 6 deletions clang/test/AST/ast-dump-template-decls.cpp
Expand Up @@ -197,9 +197,7 @@ int unTempl<int>;

template<>
float unTempl<float> = 1;
// FIXME (#61680) - serializing and loading AST should not affect reported source range
// DIRECT: VarTemplateSpecializationDecl 0x{{[^ ]*}} <line:{{[0-9]+}}:1, line:{{[0-9]+}}:20> col:7 unTempl 'float' cinit
// SERIALIZED: VarTemplateSpecializationDecl 0x{{[^ ]*}} <line:{{[0-9]+}}:1, line:{{[0-9]+}}:24> col:7 unTempl 'float' cinit
// CHECK: VarTemplateSpecializationDecl 0x{{[^ ]*}} <line:{{[0-9]+}}:1, line:{{[0-9]+}}:24> col:7 unTempl 'float' cinit
// CHECK-NEXT: |-TemplateArgument type 'float'
// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'float'
// CHECK-NEXT: `-ImplicitCastExpr 0x{{[^ ]*}} <col:24> 'float' <IntegralToFloating>
Expand Down Expand Up @@ -245,9 +243,7 @@ int binTempl<int, int>;

template<>
float binTempl<float, float> = 1;
// FIXME (#61680) - serializing and loading AST should not affect reported source range
// DIRECT: VarTemplateSpecializationDecl 0x{{[^ ]*}} <line:{{[0-9]+}}:1, line:{{[0-9]+}}:28> col:7 binTempl 'float' cinit
// SERIALIZED: VarTemplateSpecializationDecl 0x{{[^ ]*}} <line:{{[0-9]+}}:1, line:{{[0-9]+}}:32> col:7 binTempl 'float' cinit
// CHECK: VarTemplateSpecializationDecl 0x{{[^ ]*}} <line:{{[0-9]+}}:1, line:{{[0-9]+}}:32> col:7 binTempl 'float' cinit
// CHECK-NEXT: |-TemplateArgument type 'float'
// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'float'
// CHECK-NEXT: |-TemplateArgument type 'float'
Expand Down

0 comments on commit a826b90

Please sign in to comment.