Skip to content

Commit 2b3e35a

Browse files
committed
[clang] Fix incorrect inferred lifetime_capture_by attr on STL.
We incorrectly annotate the iterator parameter for `insert` method (`void insert(const_iterator, const value_type& value)`), because iterator happens to be a gsl-pointer type. This patch fixes it.
1 parent feb9445 commit 2b3e35a

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

clang/lib/Sema/SemaAttr.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,12 @@ void Sema::inferLifetimeCaptureByAttribute(FunctionDecl *FD) {
287287
if (PVD->hasAttr<LifetimeCaptureByAttr>())
288288
return;
289289
for (ParmVarDecl *PVD : MD->parameters()) {
290-
if (sema::isPointerLikeType(PVD->getType().getNonReferenceType())) {
290+
// Methods in standard containers that capture values typically accept
291+
// reference-type parameters, e.g., `void push_back(const T& value)`.
292+
// We only apply the lifetime_capture_by attribute to parameters of
293+
// pointer-like reference types (`const T&`, `T&&`).
294+
if (PVD->getType()->isReferenceType() &&
295+
sema::isPointerLikeType(PVD->getType().getNonReferenceType())) {
291296
int CaptureByThis[] = {LifetimeCaptureByAttr::THIS};
292297
PVD->addAttr(
293298
LifetimeCaptureByAttr::CreateImplicit(Context, CaptureByThis, 1));

clang/test/AST/attr-lifetime-capture-by.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ std::vector<View> views;
4848

4949
// CHECK: CXXMethodDecl {{.*}} insert 'void (iterator, View &&)'
5050
// CHECK-NEXT: ParmVarDecl {{.*}} 'iterator'
51-
// CHECK-NEXT: LifetimeCaptureByAttr {{.*}} Implicit
51+
// CHECK-NOT: LifetimeCaptureByAttr {{.*}} Implicit
5252
// CHECK-NEXT: ParmVarDecl {{.*}} 'View &&'
5353
// CHECK-NEXT: LifetimeCaptureByAttr {{.*}} Implicit
5454

@@ -67,7 +67,7 @@ std::vector<ViewTemplate<int>> templated_views;
6767

6868
// CHECK: CXXMethodDecl {{.*}} insert 'void (iterator, ViewTemplate<int> &&)'
6969
// CHECK-NEXT: ParmVarDecl {{.*}} 'iterator'
70-
// CHECK-NEXT: LifetimeCaptureByAttr {{.*}} Implicit
70+
// CHECK-NOT: LifetimeCaptureByAttr {{.*}} Implicit
7171
// CHECK-NEXT: ParmVarDecl {{.*}} 'ViewTemplate<int> &&'
7272
// CHECK-NEXT: LifetimeCaptureByAttr {{.*}} Implicit
7373

@@ -84,7 +84,7 @@ std::vector<int*> pointers;
8484

8585
// CHECK: CXXMethodDecl {{.*}} insert 'void (iterator, int *&&)'
8686
// CHECK-NEXT: ParmVarDecl {{.*}} 'iterator'
87-
// CHECK-NEXT: LifetimeCaptureByAttr {{.*}} Implicit
87+
// CHECK-NOT: LifetimeCaptureByAttr {{.*}} Implicit
8888
// CHECK-NEXT: ParmVarDecl {{.*}} 'int *&&'
8989
// CHECK-NEXT: LifetimeCaptureByAttr {{.*}} Implicit
9090

@@ -100,6 +100,6 @@ std::vector<int> ints;
100100

101101
// CHECK: CXXMethodDecl {{.*}} insert 'void (iterator, int &&)'
102102
// CHECK-NEXT: ParmVarDecl {{.*}} 'iterator'
103-
// CHECK-NEXT: LifetimeCaptureByAttr {{.*}} Implicit
103+
// CHECK-NOT: LifetimeCaptureByAttr {{.*}} Implicit
104104
// CHECK-NEXT: ParmVarDecl {{.*}} 'int &&'
105105
// CHECK-NOT: LifetimeCaptureByAttr

0 commit comments

Comments
 (0)