Skip to content

Commit b40d507

Browse files
mrcvtlMarco Vitale
authored andcommitted
Move check to support clang::lifetimebound and boost performance
1 parent d9a88d5 commit b40d507

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,13 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
13041304
if (LK == LK_FullExpression)
13051305
return;
13061306

1307+
if (LK == LK_Extended && SemaRef.getLangOpts().CPlusPlus23) {
1308+
if (const auto *VD = dyn_cast_if_present<VarDecl>(InitEntity->getDecl())) {
1309+
if (VD->isCXXForRangeImplicitVar())
1310+
return;
1311+
}
1312+
}
1313+
13071314
// FIXME: consider moving the TemporaryVisitor and visitLocalsRetained*
13081315
// functions to a dedicated class.
13091316
auto TemporaryVisitor = [&](const IndirectLocalPath &Path, Local L,
@@ -1341,14 +1348,6 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
13411348
}
13421349

13431350
if (IsGslPtrValueFromGslTempOwner && DiagLoc.isValid()) {
1344-
1345-
if (SemaRef.getLangOpts().CPlusPlus23) {
1346-
if (const VarDecl *VD =
1347-
dyn_cast_if_present<VarDecl>(InitEntity->getDecl());
1348-
VD && VD->isCXXForRangeImplicitVar())
1349-
return false;
1350-
}
1351-
13521351
SemaRef.Diag(DiagLoc, diag::warn_dangling_lifetime_pointer)
13531352
<< DiagRange;
13541353
return false;

clang/test/SemaCXX/attr-lifetimebound.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ namespace p0936r0_examples {
192192

193193
std::vector make_vector();
194194
void use_reversed_range() {
195-
// FIXME: Don't expose the name of the internal range variable.
196-
for (auto x : reversed(make_vector())) {} // expected-warning {{temporary implicitly bound to local reference will be destroyed at the end of the full-expression}}
195+
// No warning here because C++23 extends the lifetime of the temporary
196+
// in a range-based for loop.
197+
for (auto x : reversed(make_vector())) {}
197198
}
198199

199200
template <typename K, typename V>

0 commit comments

Comments
 (0)