Skip to content

Commit 366f488

Browse files
authored
[clang] AST: fix dependency calculation for TypedefTypes (#143291)
The dependency from the type sugar of the underlying type of a Typedef were not being considered for the dependency of the TypedefType itself. A TypedefType should be instantiation dependent if it involves non-instantiated template parameters, even if they don't contribute to the canonical type. Besides, a TypedefType should be instantiation dependent if it is declared in a dependent context, but fixing that would have performance consequences, as otherwise non-dependent typedef declarations would need to be transformed during instantiation as well. This removes the workaround added in #90032 Fixes #89774
1 parent b1b84a6 commit 366f488

File tree

6 files changed

+14
-37
lines changed

6 files changed

+14
-37
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ Bug Fixes in This Version
687687
- Fixed an assertion failure in serialization of constexpr structs containing unions. (#GH140130)
688688
- Fixed duplicate entries in TableGen that caused the wrong attribute to be selected. (GH#140701)
689689
- Fixed type mismatch error when 'builtin-elementwise-math' arguments have different qualifiers, this should be well-formed. (#GH141397)
690-
- Constant evaluation now correctly runs the destructor of a variable declared in
690+
- Constant evaluation now correctly runs the destructor of a variable declared in
691691
the second clause of a C-style ``for`` loop. (#GH139818)
692692

693693
Bug Fixes to Compiler Builtins
@@ -843,6 +843,7 @@ Bug Fixes to AST Handling
843843
- Fixed uninitialized use check in a lambda within CXXOperatorCallExpr. (#GH129198)
844844
- Fixed a malformed printout of ``CXXParenListInitExpr`` in certain contexts.
845845
- Fixed a malformed printout of certain calling convention function attributes. (#GH143160)
846+
- Fixed dependency calculation for TypedefTypes (#GH89774)
846847

847848
Miscellaneous Bug Fixes
848849
^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/AST/Type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5816,8 +5816,8 @@ class TypedefType final : public Type,
58165816
friend class ASTContext; // ASTContext creates these.
58175817
friend TrailingObjects;
58185818

5819-
TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType underlying,
5820-
QualType can);
5819+
TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType UnderlyingType,
5820+
bool HasTypeDifferentFromDecl);
58215821

58225822
public:
58235823
TypedefNameDecl *getDecl() const { return Decl; }

clang/lib/AST/ASTContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5216,7 +5216,7 @@ QualType ASTContext::getTypedefType(const TypedefNameDecl *Decl,
52165216
if (Underlying.isNull())
52175217
Underlying = Decl->getUnderlyingType();
52185218
auto *NewType = new (*this, alignof(TypedefType)) TypedefType(
5219-
Type::Typedef, Decl, QualType(), getCanonicalType(Underlying));
5219+
Type::Typedef, Decl, Underlying, /*HasTypeDifferentFromDecl=*/false);
52205220
Decl->TypeForDecl = NewType;
52215221
Types.push_back(NewType);
52225222
return QualType(NewType, 0);
@@ -5238,7 +5238,7 @@ QualType ASTContext::getTypedefType(const TypedefNameDecl *Decl,
52385238
void *Mem = Allocate(TypedefType::totalSizeToAlloc<QualType>(true),
52395239
alignof(TypedefType));
52405240
auto *NewType = new (Mem) TypedefType(Type::Typedef, Decl, Underlying,
5241-
getCanonicalType(Underlying));
5241+
/*HasTypeDifferentFromDecl=*/true);
52425242
TypedefTypes.InsertNode(NewType, InsertPos);
52435243
Types.push_back(NewType);
52445244
return QualType(NewType, 0);

clang/lib/AST/Type.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4013,13 +4013,13 @@ StringRef CountAttributedType::getAttributeName(bool WithMacroPrefix) const {
40134013
}
40144014

40154015
TypedefType::TypedefType(TypeClass tc, const TypedefNameDecl *D,
4016-
QualType Underlying, QualType can)
4017-
: Type(tc, can, toSemanticDependence(can->getDependence())),
4016+
QualType UnderlyingType, bool HasTypeDifferentFromDecl)
4017+
: Type(tc, UnderlyingType.getCanonicalType(),
4018+
toSemanticDependence(UnderlyingType->getDependence())),
40184019
Decl(const_cast<TypedefNameDecl *>(D)) {
4019-
assert(!isa<TypedefType>(can) && "Invalid canonical type");
4020-
TypedefBits.hasTypeDifferentFromDecl = !Underlying.isNull();
4020+
TypedefBits.hasTypeDifferentFromDecl = HasTypeDifferentFromDecl;
40214021
if (!typeMatchesDecl())
4022-
*getTrailingObjects() = Underlying;
4022+
*getTrailingObjects() = UnderlyingType;
40234023
}
40244024

40254025
QualType TypedefType::desugar() const {

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,28 +1564,7 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
15641564

15651565
SourceLocation Loc = AliasDecl->getLocation();
15661566

1567-
if (CGM.getCodeGenOpts().DebugTemplateAlias &&
1568-
// FIXME: This is a workaround for the issue
1569-
// https://github.com/llvm/llvm-project/issues/89774
1570-
// The TemplateSpecializationType doesn't contain any instantiation
1571-
// information; dependent template arguments can't be resolved. For now,
1572-
// fall back to DW_TAG_typedefs for template aliases that are
1573-
// instantiation dependent, e.g.:
1574-
// ```
1575-
// template <int>
1576-
// using A = int;
1577-
//
1578-
// template<int I>
1579-
// struct S {
1580-
// using AA = A<I>; // Instantiation dependent.
1581-
// AA aa;
1582-
// };
1583-
//
1584-
// S<0> s;
1585-
// ```
1586-
// S::AA's underlying type A<I> is dependent on I so will be emitted as a
1587-
// DW_TAG_typedef.
1588-
!Ty->isInstantiationDependentType()) {
1567+
if (CGM.getCodeGenOpts().DebugTemplateAlias) {
15891568
auto ArgVector = ::GetTemplateArgs(TD, Ty);
15901569
TemplateArgs Args = {TD->getTemplateParameters(), ArgVector};
15911570

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// RUN: %clang_cc1 -triple x86_64-unk-unk -o - -emit-llvm -debug-info-kind=standalone -gtemplate-alias %s -gsimple-template-names=simple \
22
// RUN: | FileCheck %s
33

4-
//// Check that -gtemplate-alias falls back to DW_TAG_typedef emission
5-
//// for instantiation dependent type aliases.
6-
74
template <int>
85
using A = int;
96

@@ -16,6 +13,6 @@ struct S {
1613
S<0> s;
1714

1815
// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "aa", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[AA:[0-9]+]], size: 32)
19-
// CHECK: [[AA]] = !DIDerivedType(tag: DW_TAG_typedef, name: "AA", file: ![[#]], line: [[#]], baseType: ![[A:[0-9]+]])
20-
// CHECK: [[A]] = !DIDerivedType(tag: DW_TAG_typedef, name: "A<I>", file: ![[#]], line: [[#]], baseType: ![[int:[0-9]+]])
16+
// CHECK: [[AA]] = !DIDerivedType(tag: DW_TAG_typedef, name: "AA", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[A:[0-9]+]])
17+
// CHECK: [[A]] = !DIDerivedType(tag: DW_TAG_template_alias, name: "A", file: ![[#]], line: [[#]], baseType: ![[int:[0-9]+]], extraData: ![[#]])
2118
// CHECK: [[int]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)

0 commit comments

Comments
 (0)