Skip to content

Commit 3276f4e

Browse files
committed
Fix a false postive in member initializer.
1 parent 87fea69 commit 3276f4e

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ enum AnalysisResult {
11881188
// Analyze cases where a GSLPointer is initialized or assigned from a
11891189
// temporary owner object.
11901190
static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
1191-
Local L) {
1191+
Local L, LifetimeKind LK) {
11921192
if (!pathOnlyHandlesGslPointer(Path))
11931193
return NotGSLPointer;
11941194

@@ -1208,7 +1208,8 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
12081208
// auto p2 = Temp().owner; // Here p2 is dangling.
12091209
if (const auto *FD = llvm::dyn_cast_or_null<FieldDecl>(E.D);
12101210
FD && !FD->getType()->isReferenceType() &&
1211-
isRecordWithAttr<OwnerAttr>(FD->getType())) {
1211+
isRecordWithAttr<OwnerAttr>(FD->getType()) &&
1212+
LK != LK_MemInitializer) {
12121213
return Report;
12131214
}
12141215
return Abandon;
@@ -1312,7 +1313,7 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
13121313
auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L);
13131314

13141315
bool IsGslPtrValueFromGslTempOwner = true;
1315-
switch (analyzePathForGSLPointer(Path, L)) {
1316+
switch (analyzePathForGSLPointer(Path, L, LK)) {
13161317
case Abandon:
13171318
return false;
13181319
case Skip:
@@ -1444,6 +1445,7 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
14441445
auto *DRE = dyn_cast<DeclRefExpr>(L);
14451446
// Suppress false positives for code like the one below:
14461447
// Ctor(unique_ptr<T> up) : pointer(up.get()), owner(move(up)) {}
1448+
// FIXME: move this logic to analyzePathForGSLPointer.
14471449
if (DRE && isRecordWithAttr<OwnerAttr>(DRE->getType()))
14481450
return false;
14491451

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,4 +839,16 @@ void test2() {
839839
const Bar& bar = foo.v.back(); // OK
840840
}
841841

842+
struct Foo2 {
843+
std::unique_ptr<Bar> bar;
844+
};
845+
846+
struct Test {
847+
Test(Foo2 foo) : bar(foo.bar.get()), // OK
848+
storage(std::move(foo.bar)) {};
849+
850+
Bar* bar;
851+
std::unique_ptr<Bar> storage;
852+
};
853+
842854
} // namespace GH120543

0 commit comments

Comments
 (0)