diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -225,6 +225,11 @@ enabling short-circuiting coroutines use cases. This fixes (`#56532 `_) in antecipation of `CWG2563 _`. +- 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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h --- a/clang/include/clang/AST/DeclTemplate.h +++ b/clang/include/clang/AST/DeclTemplate.h @@ -2926,13 +2926,7 @@ 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()); @@ -3091,13 +3085,7 @@ 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(), diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp --- a/clang/lib/AST/DeclTemplate.cpp +++ b/clang/lib/AST/DeclTemplate.cpp @@ -1402,6 +1402,15 @@ 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 //===----------------------------------------------------------------------===// @@ -1447,6 +1456,14 @@ 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 diff --git a/clang/test/AST/ast-dump-template-decls.cpp b/clang/test/AST/ast-dump-template-decls.cpp --- a/clang/test/AST/ast-dump-template-decls.cpp +++ b/clang/test/AST/ast-dump-template-decls.cpp @@ -1,12 +1,12 @@ // Test without serialization: // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown -ast-dump %s \ -// RUN: | FileCheck -strict-whitespace %s +// RUN: | FileCheck -strict-whitespace %s --check-prefix=DIRECT // // Test with serialization: // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown -emit-pch -o %t %s // RUN: %clang_cc1 -x c++ -std=c++17 -triple x86_64-unknown-unknown -include-pch %t -ast-dump-all /dev/null \ // RUN: | sed -e "s/ //" -e "s/ imported//" \ -// RUN: | FileCheck --strict-whitespace %s +// RUN: | FileCheck --strict-whitespace %s --check-prefix=SERIALIZED template // CHECK: FunctionTemplateDecl 0x{{[^ ]*}} <{{.*}}:1, line:[[@LINE+2]]:10> col:6 a @@ -178,3 +178,76 @@ // CHECK-NEXT: `-RecordType 0x{{[^ ]*}} 'subst_default_argument::A' // CHECK-NEXT: `-ClassTemplateSpecialization 0x{{[^ ]*}} 'A' } // namespace subst_default_argument + +namespace D146733 { +template +T unTempl = 1; +// CHECK:VarTemplateDecl 0x{{[^ ]*}} col:3 unTempl +// CHECK-NEXT: |-TemplateTypeParmDecl 0x{{[^ ]*}} col:16 referenced class depth 0 index 0 T +// CHECK-NEXT: |-VarDecl 0x{{[^ ]*}} col:3 unTempl 'T' cinit +// CHECK-NEXT: | `-IntegerLiteral 0x{{[^ ]*}} 'int' 1 + +template<> +int unTempl; +// FIXME (#61680) - serializing and loading AST should not affect reported source range +// DIRECT: VarTemplateSpecializationDecl 0x{{[^ ]*}} col:5 unTempl 'int' +// SERIALIZED: VarTemplateSpecializationDecl 0x{{[^ ]*}} col:5 unTempl 'int' +// CHECK-NEXT: `-TemplateArgument type 'int' +// CHECK-NEXT: `-BuiltinType 0x{{[^ ]*}} 'int' + +template<> +float unTempl = 1; +// CHECK: VarTemplateSpecializationDecl 0x{{[^ ]*}} col:7 unTempl 'float' cinit +// CHECK-NEXT: |-TemplateArgument type 'float' +// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'float' +// CHECK-NEXT: `-ImplicitCastExpr 0x{{[^ ]*}} 'float' +// CHECK-NEXT: `-IntegerLiteral 0x{{[^ ]*}} 'int' 1 + +template +T binTempl = 1; +// CHECK: VarTemplateDecl 0x{{[^ ]*}} col:3 binTempl +// CHECK-NEXT: |-TemplateTypeParmDecl 0x{{[^ ]*}} col:16 referenced class depth 0 index 0 T +// CHECK-NEXT: |-TemplateTypeParmDecl 0x{{[^ ]*}} col:25 class depth 0 index 1 U +// CHECK-NEXT: |-VarDecl 0x{{[^ ]*}} col:3 binTempl 'T' cinit +// CHECK-NEXT: | `-IntegerLiteral 0x{{[^ ]*}} 'int' 1 + +template +int binTempl; +// CHECK: VarTemplatePartialSpecializationDecl 0x{{[^ ]*}} col:5 binTempl 'int' +// CHECK-NEXT: |-TemplateTypeParmDecl 0x{{[^ ]*}} col:16 referenced class depth 0 index 0 U +// CHECK-NEXT: |-TemplateArgument type 'int' +// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'int' +// CHECK-NEXT: `-TemplateArgument type 'type-parameter-0-0' +// CHECK-NEXT: `-TemplateTypeParmType 0x{{[^ ]*}} 'type-parameter-0-0' dependent depth 0 index 0 + +template +float binTempl = 1; +// CHECK: VarTemplatePartialSpecializationDecl 0x{{[^ ]*}} col:7 binTempl 'float' cinit +// CHECK-NEXT: |-TemplateTypeParmDecl 0x{{[^ ]*}} col:16 referenced class depth 0 index 0 U +// CHECK-NEXT: |-TemplateArgument type 'float' +// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'float' +// CHECK-NEXT: |-TemplateArgument type 'type-parameter-0-0' +// CHECK-NEXT: | `-TemplateTypeParmType 0x{{[^ ]*}} 'type-parameter-0-0' dependent depth 0 index 0 +// CHECK-NEXT: `-ImplicitCastExpr 0x{{[^ ]*}} 'float' +// CHECK-NEXT: `-IntegerLiteral 0x{{[^ ]*}} 'int' 1 + +template<> +int binTempl; +// FIXME (#61680) - serializing and loading AST should not affect reported source range +// DIRECT: VarTemplateSpecializationDecl 0x{{[^ ]*}} col:5 binTempl 'int' +// SERIALIZED: VarTemplateSpecializationDecl 0x{{[^ ]*}} col:5 binTempl 'int' +// CHECK-NEXT: |-TemplateArgument type 'int' +// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'int' +// CHECK-NEXT: `-TemplateArgument type 'int' +// CHECK-NEXT: `-BuiltinType 0x{{[^ ]*}} 'int' + +template<> +float binTempl = 1; +// CHECK: VarTemplateSpecializationDecl 0x{{[^ ]*}} col:7 binTempl 'float' cinit +// CHECK-NEXT: |-TemplateArgument type 'float' +// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'float' +// CHECK-NEXT: |-TemplateArgument type 'float' +// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'float' +// CHECK-NEXT: `-ImplicitCastExpr 0x{{[^ ]*}} 'float' +// CHECK-NEXT: `-IntegerLiteral 0x{{[^ ]*}} 'int' 1 +}