Skip to content

Commit 1036946

Browse files
committed
address comments
1 parent ab30f17 commit 1036946

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,9 +1122,9 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
11221122
const InitializedEntity *ExtendingEntity, LifetimeKind LK,
11231123
const AssignedEntity *AEntity,
11241124
const CapturingEntity *CapEntity, Expr *Init) {
1125-
assert((AEntity && LK == LK_Assignment) ||
1126-
(CapEntity && LK == LK_LifetimeCapture) ||
1127-
(InitEntity && LK != LK_Assignment));
1125+
assert(!AEntity || LK == LK_Assignment);
1126+
assert(!CapEntity || LK == LK_LifetimeCapture);
1127+
assert(!InitEntity || (LK != LK_Assignment && LK != LK_LifetimeCapture));
11281128
// If this entity doesn't have an interesting lifetime, don't bother looking
11291129
// for temporaries within its initializer.
11301130
if (LK == LK_FullExpression)
@@ -1439,14 +1439,17 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
14391439
};
14401440
bool HasReferenceBinding = Init->isGLValue();
14411441
llvm::SmallVector<IndirectLocalPathEntry, 8> Path;
1442-
if (LK == LK_Assignment &&
1443-
shouldRunGSLAssignmentAnalysis(SemaRef, *AEntity)) {
1444-
Path.push_back(
1445-
{isAssignmentOperatorLifetimeBound(AEntity->AssignmentOperator)
1446-
? IndirectLocalPathEntry::LifetimeBoundCall
1447-
: IndirectLocalPathEntry::GslPointerAssignment,
1448-
Init});
1449-
} else if (LK == LK_LifetimeCapture) {
1442+
switch (LK) {
1443+
case LK_Assignment: {
1444+
if (shouldRunGSLAssignmentAnalysis(SemaRef, *AEntity))
1445+
Path.push_back(
1446+
{isAssignmentOperatorLifetimeBound(AEntity->AssignmentOperator)
1447+
? IndirectLocalPathEntry::LifetimeBoundCall
1448+
: IndirectLocalPathEntry::GslPointerAssignment,
1449+
Init});
1450+
break;
1451+
}
1452+
case LK_LifetimeCapture: {
14501453
Path.push_back({IndirectLocalPathEntry::LifetimeCapture, Init});
14511454
if (isRecordWithAttr<PointerAttr>(Init->getType()))
14521455
HasReferenceBinding = false;
@@ -1455,6 +1458,10 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
14551458
if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init);
14561459
MTE && isPointerLikeType(Init->getType()))
14571460
Init = MTE->getSubExpr();
1461+
break;
1462+
}
1463+
default:
1464+
break;
14581465
}
14591466

14601467
if (HasReferenceBinding)

clang/test/Sema/warn-lifetime-analysis-nocfg.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -816,30 +816,30 @@ struct X {
816816
///////////////////////////
817817
// Detect dangling cases.
818818
///////////////////////////
819-
void captureInt(const int& i [[clang::lifetime_capture_by(x)]], X& x);
820-
void captureRValInt(int&& i [[clang::lifetime_capture_by(x)]], X& x);
821-
void noCaptureInt(int i [[clang::lifetime_capture_by(x)]], X& x);
819+
void captureInt(const int &i [[clang::lifetime_capture_by(x)]], X &x);
820+
void captureRValInt(int &&i [[clang::lifetime_capture_by(x)]], X &x);
821+
void noCaptureInt(int i [[clang::lifetime_capture_by(x)]], X &x);
822822

823823
std::string_view substr(const std::string& s [[clang::lifetimebound]]);
824824
std::string_view strcopy(const std::string& s);
825825

826-
void captureSV(std::string_view s [[clang::lifetime_capture_by(x)]], X& x);
827-
void captureRValSV(std::string_view&& sv [[clang::lifetime_capture_by(x)]], X& x);
828-
void noCaptureSV(std::string_view sv, X& x);
829-
void captureS(const std::string& s [[clang::lifetime_capture_by(x)]], X& x);
830-
void captureRValS(std::string&& s [[clang::lifetime_capture_by(x)]], X& x);
826+
void captureSV(std::string_view s [[clang::lifetime_capture_by(x)]], X &x);
827+
void captureRValSV(std::string_view &&sv [[clang::lifetime_capture_by(x)]], X &x);
828+
void noCaptureSV(std::string_view sv, X &x);
829+
void captureS(const std::string &s [[clang::lifetime_capture_by(x)]], X &x);
830+
void captureRValS(std::string &&s [[clang::lifetime_capture_by(x)]], X &x);
831831

832-
const std::string& getLB(const std::string& s[[clang::lifetimebound]]);
833-
const std::string& getLB(std::string_view sv[[clang::lifetimebound]]);
834-
const std::string* getPointerLB(const std::string& s[[clang::lifetimebound]]);
835-
const std::string* getPointerNoLB(const std::string& s);
832+
const std::string& getLB(const std::string &s [[clang::lifetimebound]]);
833+
const std::string& getLB(std::string_view sv [[clang::lifetimebound]]);
834+
const std::string* getPointerLB(const std::string &s [[clang::lifetimebound]]);
835+
const std::string* getPointerNoLB(const std::string &s);
836836

837-
void capturePointer(const std::string* sp [[clang::lifetime_capture_by(x)]], X& x);
837+
void capturePointer(const std::string* sp [[clang::lifetime_capture_by(x)]], X &x);
838838

839839
struct ThisIsCaptured {
840-
void capture(X& x) [[clang::lifetime_capture_by(x)]];
841-
void bar(X& x) [[clang::lifetime_capture_by(abcd)]]; // expected-error {{'lifetime_capture_by' attribute argument 'abcd' is not a known function parameter}}
842-
void baz(X& x) [[clang::lifetime_capture_by(this)]]; // expected-error {{'lifetime_capture_by' argument references itself}}
840+
void capture(X &x) [[clang::lifetime_capture_by(x)]];
841+
void bar(X &x) [[clang::lifetime_capture_by(abcd)]]; // expected-error {{'lifetime_capture_by' attribute argument 'abcd' is not a known function parameter}}
842+
void baz(X &x) [[clang::lifetime_capture_by(this)]]; // expected-error {{'lifetime_capture_by' argument references itself}}
843843
};
844844

845845
void captureByGlobal(std::string_view s [[clang::lifetime_capture_by(global)]]);

0 commit comments

Comments
 (0)