Skip to content

Commit 0e74e6c

Browse files
author
huqizhi
committed
[Clang][Sema] fix crash of attribute transform
1 parent 547685d commit 0e74e6c

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ Bug Fixes in This Version
828828
- Fix an issue with missing symbol definitions when the first coroutine
829829
statement appears in a discarded ``if constexpr`` branch.
830830
Fixes (`#78290 <https://github.com/llvm/llvm-project/issues/78290>`_)
831+
- Fix crash when using lifetimebound attribute in function with trailing return.
832+
Fixes (`#73619 <https://github.com/llvm/llvm-project/issues/73619>`_)
831833

832834
Bug Fixes to Compiler Builtins
833835
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/AST/TypeLoc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,10 @@ class AttributedTypeLoc : public ConcreteTypeLoc<UnqualTypeLoc,
884884
return getInnerTypeLoc();
885885
}
886886

887+
TypeLoc getEquivalentTypeLoc() const {
888+
return TypeLoc(getTypePtr()->getEquivalentType(), getNonLocalData());
889+
}
890+
887891
/// The type attribute.
888892
const Attr *getAttr() const {
889893
return getLocalData()->TypeAttr;

clang/lib/Sema/TreeTransform.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6124,7 +6124,9 @@ QualType TreeTransform<Derived>::TransformFunctionProtoType(
61246124
// "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
61256125
// and the end of the function-definition, member-declarator, or
61266126
// declarator.
6127-
Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals);
6127+
auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.getCurLexicalContext());
6128+
Sema::CXXThisScopeRAII ThisScope(
6129+
SemaRef, !ThisContext && RD ? RD : ThisContext, ThisTypeQuals);
61286130

61296131
ResultType = getDerived().TransformType(TLB, TL.getReturnLoc());
61306132
if (ResultType.isNull())
@@ -7081,10 +7083,10 @@ QualType TreeTransform<Derived>::TransformAttributedType(
70817083
// FIXME: dependent operand expressions?
70827084
if (getDerived().AlwaysRebuild() ||
70837085
modifiedType != oldType->getModifiedType()) {
7084-
// TODO: this is really lame; we should really be rebuilding the
7085-
// equivalent type from first principles.
7086-
QualType equivalentType
7087-
= getDerived().TransformType(oldType->getEquivalentType());
7086+
TypeLocBuilder AuxiliaryTLB;
7087+
AuxiliaryTLB.reserve(TL.getFullDataSize());
7088+
QualType equivalentType =
7089+
getDerived().TransformType(AuxiliaryTLB, TL.getEquivalentTypeLoc());
70887090
if (equivalentType.isNull())
70897091
return QualType();
70907092

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 %s -verify -fsyntax-only
2+
3+
// expected-no-diagnostics
4+
5+
template<typename T>
6+
struct Bar {
7+
int* data;
8+
9+
auto operator[](const int index) const [[clang::lifetimebound]] -> decltype(data[index]) {
10+
return data[index];
11+
}
12+
};
13+
14+
int main() {
15+
Bar<int> b;
16+
(void)b[2];
17+
}

0 commit comments

Comments
 (0)