[clang] 6c68e76 - [LifetimeSafety] Use buildOriginFlowChain for remaining diagnostics in issuePendingWarnings (#212083)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 31 03:37:11 PDT 2026
Author: Yuan Suo
Date: 2026-10-31T18:37:06+08:00
New Revision: 6c68e76fcd68290ba8e72d244830a79b15b11b4d
URL: https://github.com/llvm/llvm-project/commit/6c68e76fcd68290ba8e72d244830a79b15b11b4d
DIFF: https://github.com/llvm/llvm-project/commit/6c68e76fcd68290ba8e72d244830a79b15b11b4d.diff
LOG: [LifetimeSafety] Use buildOriginFlowChain for remaining diagnostics in issuePendingWarnings (#212083)
Added:
Modified:
clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
clang/include/clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h
clang/lib/Analysis/LifetimeSafety/Checker.cpp
clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
clang/lib/Sema/SemaLifetimeSafety.h
clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
clang/test/Sema/LifetimeSafety/capture-by.cpp
clang/test/Sema/LifetimeSafety/dangling-field.cpp
clang/test/Sema/LifetimeSafety/dangling-global.cpp
clang/test/Sema/LifetimeSafety/invalidations.cpp
clang/test/Sema/LifetimeSafety/nocfg.cpp
clang/test/Sema/LifetimeSafety/safety.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
index eb227eb156686..aec56ef8605f5 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
@@ -93,12 +93,14 @@ class LifetimeSafetySemaHelper {
const FieldDecl *Field,
const Expr *MovedExpr,
bool IsCapturedByLambda,
- SourceLocation ExpiryLoc) {}
+ SourceLocation ExpiryLoc,
+ llvm::ArrayRef<const Expr *> ExprChain) {}
virtual void reportDanglingGlobal(const Expr *IssueExpr,
const VarDecl *DanglingGlobal,
const Expr *MovedExpr,
SourceLocation ExpiryLoc,
+ llvm::ArrayRef<const Expr *> ExprChain,
bool IsMain = false) {}
// Reports when a reference/iterator is used after the container operation
@@ -121,16 +123,22 @@ class LifetimeSafetySemaHelper {
llvm::ArrayRef<const Expr *> ExprChain) {}
virtual void reportInvalidatedField(const Expr *IssueExpr,
const FieldDecl *Field,
- const Expr *InvalidationExpr) {}
+ const Expr *InvalidationExpr,
+ llvm::ArrayRef<const Expr *> ExprChain) {}
virtual void reportInvalidatedField(const ParmVarDecl *PVD,
const FieldDecl *Field,
- const Expr *InvalidationExpr) {}
+ const Expr *InvalidationExpr,
+ llvm::ArrayRef<const Expr *> ExprChain) {}
virtual void reportInvalidatedGlobal(const Expr *IssueExpr,
const VarDecl *Global,
- const Expr *InvalidationExpr) {}
+ const Expr *InvalidationExpr,
+ llvm::ArrayRef<const Expr *> ExprChain) {
+ }
virtual void reportInvalidatedGlobal(const ParmVarDecl *PVD,
const VarDecl *Global,
- const Expr *InvalidationExpr) {}
+ const Expr *InvalidationExpr,
+ llvm::ArrayRef<const Expr *> ExprChain) {
+ }
using EscapingTarget =
llvm::PointerUnion<const Expr *, const FieldDecl *, const VarDecl *>;
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h
index de7eca39228ca..d410adb1caef0 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h
@@ -53,13 +53,7 @@ class LoanPropagationAnalysis {
/// Like the above, starting from the used origin holding \p TargetLoan. The
/// casts that load the used variable itself are left out.
- llvm::SmallVector<OriginID> buildOriginFlowChain(const UseFact *UF,
- const LoanID TargetLoan,
- const CFG *Cfg) const;
-
- /// Like the above, starting from the origin \p OEF lets escape. Empty if it
- /// does not hold \p TargetLoan.
- llvm::SmallVector<OriginID> buildOriginFlowChain(const OriginEscapesFact *OEF,
+ llvm::SmallVector<OriginID> buildOriginFlowChain(const Fact *F,
const LoanID TargetLoan,
const CFG *Cfg) const;
diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
index ae0297af25d8d..c5e7b9cd2d62b 100644
--- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
@@ -322,33 +322,35 @@ class LifetimeChecker {
} else if (const auto *OEF =
dyn_cast<const OriginEscapesFact *>(CausingFact)) {
+ llvm::SmallVector<const Expr *> ExprChain =
+ getExprChain(LoanPropagation.buildOriginFlowChain(OEF, LID, Cfg));
if (Warning.InvalidatedByExpr) {
if (const auto *FieldEscape = dyn_cast<FieldEscapeFact>(OEF)) {
// Invalidated object escapes to a field.
if (IssueExpr)
// Invalidated object on stack escapes to a field.
- SemaHelper->reportInvalidatedField(IssueExpr,
- FieldEscape->getFieldDecl(),
- Warning.InvalidatedByExpr);
+ SemaHelper->reportInvalidatedField(
+ IssueExpr, FieldEscape->getFieldDecl(),
+ Warning.InvalidatedByExpr, ExprChain);
else if (InvalidatedPVD)
// Invalidated parameter escapes to a field.
- SemaHelper->reportInvalidatedField(InvalidatedPVD,
- FieldEscape->getFieldDecl(),
- Warning.InvalidatedByExpr);
+ SemaHelper->reportInvalidatedField(
+ InvalidatedPVD, FieldEscape->getFieldDecl(),
+ Warning.InvalidatedByExpr, ExprChain);
} else if (const auto *GlobalEscape =
dyn_cast<GlobalEscapeFact>(OEF)) {
// Invalidated object escapes to global or static storage.
if (IssueExpr)
// Invalidated object on stack escapes to global or static
// storage.
- SemaHelper->reportInvalidatedGlobal(IssueExpr,
- GlobalEscape->getGlobal(),
- Warning.InvalidatedByExpr);
+ SemaHelper->reportInvalidatedGlobal(
+ IssueExpr, GlobalEscape->getGlobal(),
+ Warning.InvalidatedByExpr, ExprChain);
else if (InvalidatedPVD)
// Invalidated parameter escapes to global or static storage.
- SemaHelper->reportInvalidatedGlobal(InvalidatedPVD,
- GlobalEscape->getGlobal(),
- Warning.InvalidatedByExpr);
+ SemaHelper->reportInvalidatedGlobal(
+ InvalidatedPVD, GlobalEscape->getGlobal(),
+ Warning.InvalidatedByExpr, ExprChain);
} else if (isa<ReturnEscapeFact>(OEF)) {
// FIXME: Diagnose invalidated return escapes separately.
} else if (const auto *ThisEscape = dyn_cast<ThisEscapeFact>(OEF)) {
@@ -369,23 +371,22 @@ class LifetimeChecker {
} else if (const auto *RetEscape = dyn_cast<ReturnEscapeFact>(OEF)) {
// Return stack address.
SemaHelper->reportUseAfterReturn(
- IssueExpr, RetEscape->getReturnExpr(), MovedExpr,
- getExprChain(
- LoanPropagation.buildOriginFlowChain(OEF, LID, Cfg)));
+ IssueExpr, RetEscape->getReturnExpr(), MovedExpr, ExprChain);
} else if (const auto *FieldEscape = dyn_cast<FieldEscapeFact>(OEF)) {
// Dangling field.
bool IsCapturedByLambda =
FactMgr.isFieldCapturedByLambda(FieldEscape->getFieldDecl());
SemaHelper->reportDanglingField(
IssueExpr, FieldEscape->getFieldDecl(), MovedExpr,
- IsCapturedByLambda, ExpiryLoc);
+ IsCapturedByLambda, ExpiryLoc, ExprChain);
} else if (const auto *GlobalEscape = dyn_cast<GlobalEscapeFact>(OEF)) {
// Global escape.
bool IsMain = false;
if (const auto *Func = dyn_cast_if_present<FunctionDecl>(FD))
IsMain = Func->isMain();
SemaHelper->reportDanglingGlobal(IssueExpr, GlobalEscape->getGlobal(),
- MovedExpr, ExpiryLoc, IsMain);
+ MovedExpr, ExpiryLoc, ExprChain,
+ IsMain);
} else if (const auto *ThisEscape = dyn_cast<ThisEscapeFact>(OEF))
// A borrow is still held by the object (e.g. captured via
// lifetime_capture_by_this) when the captured local goes out of
diff --git a/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp b/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
index df66f029a5400..4f3ee8c7dab8a 100644
--- a/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
@@ -352,13 +352,11 @@ llvm::SmallVector<OriginID> LoanPropagationAnalysis::buildOriginFlowChain(
}
llvm::SmallVector<OriginID> LoanPropagationAnalysis::buildOriginFlowChain(
- const UseFact *UF, const LoanID TargetLoan, const CFG *Cfg) const {
- return PImpl->buildOriginFlowChain(UF, TargetLoan, Cfg);
-}
-llvm::SmallVector<OriginID>
-LoanPropagationAnalysis::buildOriginFlowChain(const OriginEscapesFact *OEF,
- const LoanID TargetLoan,
- const CFG *Cfg) const {
- return PImpl->buildOriginFlowChain(OEF, TargetLoan, Cfg);
+ const Fact *F, const LoanID TargetLoan, const CFG *Cfg) const {
+ if (const auto *UF = llvm::dyn_cast<UseFact>(F))
+ return PImpl->buildOriginFlowChain(UF, TargetLoan, Cfg);
+ if (const auto *OEF = llvm::dyn_cast<OriginEscapesFact>(F))
+ return PImpl->buildOriginFlowChain(OEF, TargetLoan, Cfg);
+ llvm_unreachable("Unhandled Fact type");
}
} // namespace clang::lifetimes::internal
diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h
index bd7cf68277152..0df2ae84f9274 100644
--- a/clang/lib/Sema/SemaLifetimeSafety.h
+++ b/clang/lib/Sema/SemaLifetimeSafety.h
@@ -159,9 +159,7 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
<< MovedExpr->getSourceRange();
S.Diag(FreeLoc, diag::note_lifetime_safety_destroyed_here)
<< DestroyedSubject;
-
reportAliasingChain(ExprChain);
-
S.Diag(UseExpr->getExprLoc(), diag::note_lifetime_safety_used_here)
<< UseExpr->getSourceRange();
}
@@ -200,9 +198,7 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
if (MovedExpr)
S.Diag(MovedExpr->getExprLoc(), diag::note_lifetime_safety_moved_here)
<< MovedExpr->getSourceRange();
-
reportAliasingChain(ExprChain);
-
S.Diag(ReturnExpr->getExprLoc(), diag::note_lifetime_safety_returned_here)
<< ReturnExpr->getSourceRange();
}
@@ -210,7 +206,8 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
void reportDanglingField(const Expr *IssueExpr,
const FieldDecl *DanglingField,
const Expr *MovedExpr, bool IsCapturedByLambda,
- SourceLocation ExpiryLoc) override {
+ SourceLocation ExpiryLoc,
+ llvm::ArrayRef<const Expr *> ExprChain) override {
unsigned DiagID =
IsCapturedByLambda
? diag::warn_lifetime_safety_dangling_field_lambda_capture
@@ -224,6 +221,7 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
if (MovedExpr)
S.Diag(MovedExpr->getExprLoc(), diag::note_lifetime_safety_moved_here)
<< MovedExpr->getSourceRange();
+ reportAliasingChain(ExprChain);
S.Diag(DanglingField->getLocation(),
diag::note_lifetime_safety_dangling_field_here)
<< DanglingField->getEndLoc();
@@ -232,6 +230,7 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
void reportDanglingGlobal(const Expr *IssueExpr,
const VarDecl *DanglingGlobal,
const Expr *MovedExpr, SourceLocation ExpiryLoc,
+ llvm::ArrayRef<const Expr *> ExprChain,
bool IsMain = false) override {
unsigned DiagID;
if (IsMain) {
@@ -249,6 +248,7 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
if (MovedExpr)
S.Diag(MovedExpr->getExprLoc(), diag::note_lifetime_safety_moved_here)
<< MovedExpr->getSourceRange();
+ reportAliasingChain(ExprChain);
if (DanglingGlobal->isStaticLocal() || DanglingGlobal->isStaticDataMember())
S.Diag(DanglingGlobal->getLocation(),
diag::note_lifetime_safety_dangling_static_here)
@@ -298,13 +298,15 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
void reportInvalidatedField(const Expr *IssueExpr,
const FieldDecl *DanglingField,
- const Expr *InvalidationExpr) override {
+ const Expr *InvalidationExpr,
+ llvm::ArrayRef<const Expr *> ExprChain) override {
std::string InvalidatedSubject = getDiagSubjectDescription(IssueExpr);
S.Diag(IssueExpr->getExprLoc(),
diag::warn_lifetime_safety_invalidated_field)
<< InvalidatedSubject << getDiagSubjectDescription(DanglingField)
<< IssueExpr->getSourceRange();
reportInvalidationSite(InvalidationExpr, InvalidatedSubject);
+ reportAliasingChain(ExprChain);
S.Diag(DanglingField->getLocation(),
diag::note_lifetime_safety_dangling_field_here)
<< DanglingField->getEndLoc();
@@ -312,27 +314,31 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
void reportInvalidatedField(const ParmVarDecl *PVD,
const FieldDecl *DanglingField,
- const Expr *InvalidationExpr) override {
+ const Expr *InvalidationExpr,
+ llvm::ArrayRef<const Expr *> ExprChain) override {
std::string InvalidatedSubject = getDiagSubjectDescription(PVD);
S.Diag(PVD->getSourceRange().getBegin(),
diag::warn_lifetime_safety_invalidated_field)
<< InvalidatedSubject << getDiagSubjectDescription(DanglingField)
<< PVD->getSourceRange();
reportInvalidationSite(InvalidationExpr, InvalidatedSubject);
+ reportAliasingChain(ExprChain);
S.Diag(DanglingField->getLocation(),
diag::note_lifetime_safety_dangling_field_here)
<< DanglingField->getEndLoc();
}
- void reportInvalidatedGlobal(const Expr *IssueExpr,
- const VarDecl *DanglingGlobal,
- const Expr *InvalidationExpr) override {
+ void
+ reportInvalidatedGlobal(const Expr *IssueExpr, const VarDecl *DanglingGlobal,
+ const Expr *InvalidationExpr,
+ llvm::ArrayRef<const Expr *> ExprChain) override {
std::string InvalidatedSubject = getDiagSubjectDescription(IssueExpr);
S.Diag(IssueExpr->getExprLoc(),
diag::warn_lifetime_safety_invalidated_global)
<< InvalidatedSubject << getDiagSubjectDescription(DanglingGlobal)
<< IssueExpr->getSourceRange();
reportInvalidationSite(InvalidationExpr, InvalidatedSubject);
+ reportAliasingChain(ExprChain);
if (DanglingGlobal->isStaticLocal() || DanglingGlobal->isStaticDataMember())
S.Diag(DanglingGlobal->getLocation(),
diag::note_lifetime_safety_dangling_static_here)
@@ -343,15 +349,17 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
<< DanglingGlobal->getEndLoc();
}
- void reportInvalidatedGlobal(const ParmVarDecl *PVD,
- const VarDecl *DanglingGlobal,
- const Expr *InvalidationExpr) override {
+ void
+ reportInvalidatedGlobal(const ParmVarDecl *PVD, const VarDecl *DanglingGlobal,
+ const Expr *InvalidationExpr,
+ llvm::ArrayRef<const Expr *> ExprChain) override {
std::string InvalidatedSubject = getDiagSubjectDescription(PVD);
S.Diag(PVD->getSourceRange().getBegin(),
diag::warn_lifetime_safety_invalidated_global)
<< InvalidatedSubject << getDiagSubjectDescription(DanglingGlobal)
<< PVD->getSourceRange();
reportInvalidationSite(InvalidationExpr, InvalidatedSubject);
+ reportAliasingChain(ExprChain);
if (DanglingGlobal->isStaticLocal() || DanglingGlobal->isStaticDataMember())
S.Diag(DanglingGlobal->getLocation(),
diag::note_lifetime_safety_dangling_static_here)
diff --git a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
index 5d451469f45f3..0c0d7b5403a28 100644
--- a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
+++ b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
@@ -558,7 +558,7 @@ struct CaptureRefToView {
CaptureRefToView test_ref_to_view() {
MyObj obj;
CaptureRefToView x(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
- return x; // expected-note {{returned here}}
+ return x; // expected-note {{returned here}}
}
struct CaptureRefToPtr {
@@ -569,7 +569,7 @@ struct CaptureRefToPtr {
CaptureRefToPtr test_ref_to_ptr() {
MyObj obj;
CaptureRefToPtr x(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
- return x; // expected-note {{returned here}}
+ return x; // expected-note {{returned here}}
}
struct CaptureViewToView {
@@ -579,7 +579,7 @@ struct CaptureViewToView {
CaptureViewToView test_view_to_view() {
MyObj obj;
- View v(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
+ View v(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
CaptureViewToView x(v); // expected-note {{local variable 'v' aliases the storage of local variable 'obj'}}
return x; // expected-note {{returned here}}
}
@@ -592,7 +592,7 @@ struct CapturePtrToPtr {
CapturePtrToPtr test_ptr_to_ptr() {
MyObj obj;
CapturePtrToPtr x(&obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
- return x; // expected-note {{returned here}}
+ return x; // expected-note {{returned here}}
}
struct CaptureRefToRef {
@@ -603,7 +603,7 @@ struct CaptureRefToRef {
CaptureRefToRef test_ref_to_ref() {
MyObj obj;
CaptureRefToRef x(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
- return x; // expected-note {{returned here}}
+ return x; // expected-note {{returned here}}
}
struct BaseWithView {
@@ -618,7 +618,7 @@ struct CaptureRefToBaseView : BaseWithView {
CaptureRefToBaseView test_ref_to_base_view() {
MyObj obj;
CaptureRefToBaseView x(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
- return x; // expected-note {{returned here}}
+ return x; // expected-note {{returned here}}
}
} // namespace capturing_constructor
diff --git a/clang/test/Sema/LifetimeSafety/capture-by.cpp b/clang/test/Sema/LifetimeSafety/capture-by.cpp
index 0b69a4b51201e..ff8ec8544930a 100644
--- a/clang/test/Sema/LifetimeSafety/capture-by.cpp
+++ b/clang/test/Sema/LifetimeSafety/capture-by.cpp
@@ -196,7 +196,7 @@ void temporary_pointer_lifetimebound_capture() {
}
void temporary_nested_lifetimebound_capture() {
- capturePointer(getLifetimeBoundPointer(*getLifetimeBoundPointer(
+ capturePointer(getLifetimeBoundPointer(*getLifetimeBoundPointer( // cfg-note 2 {{result of call to 'getLifetimeBoundPointer' aliases the storage of temporary object because parameter 's' is marked as lifetimebound}}
std::string())), x); // expected-warning {{object whose reference is captured by 'x' will be destroyed at the end of the full-expression}} \
// cfg-warning {{stack memory associated with temporary object escapes to the global variable 'x' which will dangle}}
}
diff --git a/clang/test/Sema/LifetimeSafety/dangling-field.cpp b/clang/test/Sema/LifetimeSafety/dangling-field.cpp
index e7681b272a2b1..206bda0aa0494 100644
--- a/clang/test/Sema/LifetimeSafety/dangling-field.cpp
+++ b/clang/test/Sema/LifetimeSafety/dangling-field.cpp
@@ -40,7 +40,8 @@ struct CtorSetInLoop {
struct CtorInitLifetimeBound {
std::string_view view; // expected-note {{this field dangles}}
- CtorInitLifetimeBound(std::string s) : view(construct_view(s)) {} // expected-warning {{stack memory associated with parameter 's' escapes to the field 'view' which will dangle}}
+ CtorInitLifetimeBound(std::string s) : view(construct_view(s)) {} // expected-warning {{stack memory associated with parameter 's' escapes to the field 'view' which will dangle}} \
+ // expected-note {{result of call to 'construct_view' aliases the storage of parameter 's'}}
};
struct CtorInitButMoved {
@@ -90,7 +91,8 @@ struct CtorRefField {
struct CtorPointerField {
const char* ptr; // expected-note {{this field dangles}}
- CtorPointerField(std::string s) : ptr(s.data()) {} // expected-warning {{stack memory associated with parameter 's' escapes to the field 'ptr' which will dangle}}
+ CtorPointerField(std::string s) : ptr(s.data()) {} // expected-warning {{stack memory associated with parameter 's' escapes to the field 'ptr' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of parameter 's'}}
CtorPointerField(Dummy<1> ok, const std::string& s) : ptr(s.data()) {}
CtorPointerField(Dummy<2> ok, std::string_view view) : ptr(view.data()) {}
};
@@ -101,12 +103,14 @@ struct MemberSetters {
void setWithParam(std::string s) {
view = s; // expected-warning {{stack memory associated with parameter 's' escapes to the field 'view' which will dangle}}
- p = s.data(); // expected-warning {{stack memory associated with parameter 's' escapes to the field 'p' which will dangle}}
+ p = s.data(); // expected-warning {{stack memory associated with parameter 's' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of parameter 's'}}
}
void setWithParamAndReturn(std::string s) {
view = s; // expected-warning {{stack memory associated with parameter 's' escapes to the field 'view' which will dangle}}
- p = s.data(); // expected-warning {{stack memory associated with parameter 's' escapes to the field 'p' which will dangle}}
+ p = s.data(); // expected-warning {{stack memory associated with parameter 's' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of parameter 's'}}
return;
}
@@ -124,13 +128,15 @@ struct MemberSetters {
void setWithLocal() {
std::string s;
view = s; // expected-warning {{stack memory associated with local variable 's' escapes to the field 'view' which will dangle}}
- p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}}
+ p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 's'}}
}
void setWithLocalButMoved() {
std::string s;
view = s; // expected-warning-re {{stack memory associated with local variable 's' may escape to the field 'view' which will dangle. {{.*}} may have been moved}}
- p = s.data(); // expected-warning-re {{stack memory associated with local variable 's' may escape to the field 'p' which will dangle. {{.*}} may have been moved}}
+ p = s.data(); // expected-warning-re {{stack memory associated with local variable 's' may escape to the field 'p' which will dangle. {{.*}} may have been moved}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 's'}}
takeString(std::move(s)); // expected-note 2 {{potentially moved here}}
}
@@ -154,14 +160,16 @@ struct MemberSetters {
std::string local;
view = local; // expected-warning {{stack memory associated with local variable 'local' escapes to the field 'view' which will dangle}}
- p = local.data(); // expected-warning {{stack memory associated with local variable 'local' escapes to the field 'p' which will dangle}}
+ p = local.data(); // expected-warning {{stack memory associated with local variable 'local' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 'local'}}
}
void escape_to_field() {
{
std::string local;
view = local; // expected-warning {{stack memory associated with local variable 'local' escapes to the field 'view' which will dangle}}
- p = local.data(); // expected-warning {{stack memory associated with local variable 'local' escapes to the field 'p' which will dangle}}
+ p = local.data(); // expected-warning {{stack memory associated with local variable 'local' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 'local'}}
}
(void)view; // Discarding the value is not a use.
(void)p;
@@ -235,7 +243,8 @@ struct IndirectEscape2 {
void foo() {
std::string s;
- p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}}
+ p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 's' because the implicit object parameter is inferred as lifetimebound}}
}
};
@@ -262,7 +271,8 @@ struct NestedAnonStructEscape {
void foo() {
std::string s;
- p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}}
+ p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 's' because the implicit object parameter is inferred as lifetimebound}}
}
};
@@ -274,7 +284,8 @@ struct MultiFieldAnonStructEscapesOnlyWrittenField {
void foo() {
std::string s;
- p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}}
+ p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 's' because the implicit object parameter is inferred as lifetimebound}}
}
};
@@ -286,7 +297,8 @@ struct AnonUnionEscapesOnlyWrittenField {
void foo() {
std::string s;
- p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}}
+ p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 's' because the implicit object parameter is inferred as lifetimebound}}
}
};
@@ -300,8 +312,10 @@ struct AnonUnionAliasingWarnsPerMember {
void foo() {
std::string s;
- p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}}
- q = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'q' which will dangle}}
+ p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 's' because the implicit object parameter is inferred as lifetimebound}}
+ q = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'q' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 's' because the implicit object parameter is inferred as lifetimebound}}
}
};
@@ -316,7 +330,8 @@ struct AnonUnionSafeOverwriteStillWarns {
void foo() {
std::string s;
- p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}}
+ p = s.data(); // expected-warning {{stack memory associated with local variable 's' escapes to the field 'p' which will dangle}} \
+ // expected-note {{result of call to 'data' aliases the storage of local variable 's' because the implicit object parameter is inferred as lifetimebound}}
q = kGlobal.data(); // safe overwrite of the shared storage; `p` is still (wrongly) reported
}
};
@@ -357,7 +372,8 @@ struct HasUniquePtrField {
std::unique_ptr<LifetimeBoundCtor> field; // tu-note {{this field dangles}}
void setWithParam(MyObj obj) {
- field = std::make_unique<LifetimeBoundCtor>(obj); // tu-warning {{stack memory associated with parameter 'obj' escapes to the field 'field' which will dangle}}
+ field = std::make_unique<LifetimeBoundCtor>(obj); // tu-warning {{stack memory associated with parameter 'obj' escapes to the field 'field' which will dangle}} \
+ // tu-note {{result of call to 'make_unique<MakeUnique::LifetimeBoundCtor, MakeUnique::MyObj &>' aliases the storage of parameter 'obj'}}
}
};
} // namespace MakeUnique
diff --git a/clang/test/Sema/LifetimeSafety/dangling-global.cpp b/clang/test/Sema/LifetimeSafety/dangling-global.cpp
index ef00582fb9c12..2013f9479a784 100644
--- a/clang/test/Sema/LifetimeSafety/dangling-global.cpp
+++ b/clang/test/Sema/LifetimeSafety/dangling-global.cpp
@@ -26,8 +26,8 @@ void invoke_function_with_side_effects() {
// We can however catch the inlined one of course!
void inlined() {
int local;
- global = &local; // expected-warning {{stack memory associated with local variable 'local' escapes to the global variable 'global_backup' which will dangle}}
- global_backup = global;
+ global = &local; // expected-warning {{stack memory associated with local variable 'local' escapes to the global variable 'global_backup' which will dangle}}
+ global_backup = global;
global = nullptr;
}
diff --git a/clang/test/Sema/LifetimeSafety/invalidations.cpp b/clang/test/Sema/LifetimeSafety/invalidations.cpp
index d9955ec42a208..c45dc64f55b8c 100644
--- a/clang/test/Sema/LifetimeSafety/invalidations.cpp
+++ b/clang/test/Sema/LifetimeSafety/invalidations.cpp
@@ -596,13 +596,17 @@ struct S {
void InvalidatedFieldLocalVector() {
std::vector<std::string> strings;
- FieldFromLocalVector = *strings.begin(); // expected-warning {{local variable 'strings' escapes to the field 'FieldFromLocalVector' and is later invalidated}}
- strings.push_back("1"); // expected-note {{local variable 'strings' is invalidated here}}
+ FieldFromLocalVector = *strings.begin(); // expected-warning {{local variable 'strings' escapes to the field 'FieldFromLocalVector' and is later invalidated}} \
+ // expected-note {{result of call to 'begin' aliases the storage of local variable 'strings'}} \
+ // expected-note {{result of call to 'operator*' aliases the storage of local variable 'strings' because the implicit object parameter is inferred as lifetimebound}}
+ strings.push_back("1"); // expected-note {{local variable 'strings' is invalidated here}}
}
void InvalidatedFieldByValueParamVector(std::vector<std::string> strings) {
- FieldFromByValueParamVector = *strings.begin(); // expected-warning {{parameter 'strings' escapes to the field 'FieldFromByValueParamVector' and is later invalidated}}
- strings.push_back("1"); // expected-note {{parameter 'strings' is invalidated here}}
+ FieldFromByValueParamVector = *strings.begin(); // expected-warning {{parameter 'strings' escapes to the field 'FieldFromByValueParamVector' and is later invalidated}} \
+ // expected-note {{result of call to 'begin' aliases the storage of parameter 'strings'}} \
+ // expected-note {{result of call to 'operator*' aliases the storage of parameter 'strings' because the implicit object parameter is inferred as lifetimebound}}
+ strings.push_back("1"); // expected-note {{parameter 'strings' is invalidated here}}
}
void InvalidatedFieldLocalString() {
@@ -624,7 +628,7 @@ struct S {
void InvalidatedFieldDelete() {
int *p = new int; // expected-warning {{allocated object escapes to the field 'FieldFromNew' and is later invalidated}}
FieldFromNew = p;
- delete p; // expected-note {{allocated object is freed here}}
+ delete p; // expected-note {{allocated object is freed here}}
}
void InvalidatedFieldDeleteParam(int *p) { // expected-warning {{parameter 'p' escapes to the field 'FieldFromPointerParam' and is later invalidated}}
@@ -656,8 +660,10 @@ struct S {
void InvalidatedGlobalLocalVector() {
std::vector<std::string> strings;
- GlobalFromLocalVector = *strings.begin(); // expected-warning {{local variable 'strings' escapes to the global variable 'GlobalFromLocalVector' and is later invalidated}}
- strings.push_back("1"); // expected-note {{local variable 'strings' is invalidated here}}
+ GlobalFromLocalVector = *strings.begin(); // expected-warning {{local variable 'strings' escapes to the global variable 'GlobalFromLocalVector' and is later invalidated}} \
+ // expected-note {{result of call to 'begin' aliases the storage of local variable 'strings'}} \
+ // expected-note {{result of call to 'operator*' aliases the storage of local variable 'strings' because the implicit object parameter is inferred as lifetimebound}}
+ strings.push_back("1"); // expected-note {{local variable 'strings' is invalidated here}}
}
void InvalidatedGlobalByValueParamString(std::string s) {
@@ -671,9 +677,9 @@ void InvalidatedGlobalRefParamString(std::string &s) { // expected-warning {{par
}
void InvalidatedGlobalDelete() {
- int *p = new int; // expected-warning {{allocated object escapes to the global variable 'GlobalFromNew' and is later invalidated}}
+ int *p = new int; // expected-warning {{allocated object escapes to the global variable 'GlobalFromNew' and is later invalidated}}
GlobalFromNew = p;
- delete p; // expected-note {{allocated object is freed here}}
+ delete p; // expected-note {{allocated object is freed here}}
}
void InvalidatedGlobalDeleteParam(int *p) { // expected-warning {{parameter 'p' escapes to the global variable 'GlobalFromPointerParam' and is later invalidated}}
diff --git a/clang/test/Sema/LifetimeSafety/nocfg.cpp b/clang/test/Sema/LifetimeSafety/nocfg.cpp
index addbe9213a0ac..82aec5d3b279d 100644
--- a/clang/test/Sema/LifetimeSafety/nocfg.cpp
+++ b/clang/test/Sema/LifetimeSafety/nocfg.cpp
@@ -374,8 +374,10 @@ const char *trackThroughMultiplePointer() {
struct X {
X(std::unique_ptr<int> up) :
- pointee(*up), // cfg-warning {{may have been moved.}}
- pointee2(up.get()), // cfg-warning {{may have been moved.}}
+ pointee(*up), // cfg-warning {{may have been moved.}} \
+ // cfg-note {{result of call to 'operator*' aliases the storage of parameter 'up' because the implicit object parameter is inferred as lifetimebound}}
+ pointee2(up.get()), // cfg-warning {{may have been moved.}} \
+ // cfg-note {{result of call to 'get' aliases the storage of parameter 'up'}}
pointer(std::move(up)) {} // cfg-note 2 {{potentially moved here}}
int &pointee; // cfg-note {{this field dangles}}
int *pointee2; // cfg-note {{this field dangles}}
@@ -389,7 +391,8 @@ struct X2 {
// A common usage that moves the passing owner to the class.
// verify a strict warning on this case.
X2(XOwner owner) :
- pointee(owner.get()), // cfg-warning {{may have been moved.}}
+ pointee(owner.get()), // cfg-warning {{may have been moved.}} \
+ // cfg-note {{result of call to 'get' aliases the storage of parameter 'owner'}}
owner(std::move(owner)) {} // cfg-note {{potentially moved here}}
int* pointee; // cfg-note {{this field dangles}}
XOwner owner;
@@ -1197,7 +1200,8 @@ struct Foo2 {
};
struct Test {
- Test(Foo2 foo) : bar(foo.bar.get()), // cfg-warning-re {{stack memory associated with parameter 'foo' may escape to the field 'bar' which will dangle. {{.*}} may have been moved}}
+ Test(Foo2 foo) : bar(foo.bar.get()), // cfg-warning-re {{stack memory associated with parameter 'foo' may escape to the field 'bar' which will dangle. {{.*}} may have been moved}} \
+ // cfg-note {{result of call to 'get' aliases the storage of parameter 'foo'}}
storage(std::move(foo.bar)) {}; // cfg-note {{potentially moved here}}
Bar* bar; // cfg-note {{this field dangles}}
diff --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp
index ada2ebf3e3517..acfecdc7dadb2 100644
--- a/clang/test/Sema/LifetimeSafety/safety.cpp
+++ b/clang/test/Sema/LifetimeSafety/safety.cpp
@@ -645,7 +645,7 @@ const int* return_pointer_to_parameter_via_reference(int a, int b, bool cond) {
const int& return_pointer_to_parameter_via_reference_1(int a) {
const int* d = &a; // expected-warning {{stack memory associated with parameter 'a' is returned}}
- return *d; // expected-note {{returned here}}
+ return *d; // expected-note {{returned here}}
}
const int& get_ref_to_local() {
@@ -1634,7 +1634,7 @@ View range_based_for_use_after_return() {
MyObjStorage s;
for (const MyObj &o : s) { // expected-warning {{stack memory associated with local variable 's' is returned}} \
// expected-note {{local variable '__range1' aliases the storage of local variable 's'}}
- return o; // expected-note {{returned here}}
+ return o; // expected-note {{returned here}}
}
return *s.begin(); // expected-warning {{stack memory associated with local variable 's' is returned}} \
// expected-note {{result of call to 'begin' aliases the storage of local variable 's' because the implicit object parameter is marked as lifetimebound}} \
@@ -1985,7 +1985,7 @@ const MyObj* testData(MyObj obj) {
return v.data(); // expected-note {{returned here}}
}
const int* testLifetimeboundAccessorOfMyObj(MyObj obj) {
- View v = obj; // expected-warning {{stack memory associated with parameter 'obj' is returned}}
+ View v = obj; // expected-warning {{stack memory associated with parameter 'obj' is returned}}
const MyObj* ptr = v.data(); // expected-note {{local variable 'v' aliases the storage of parameter 'obj'}} \
// expected-note {{result of call to 'data' aliases the storage of parameter 'obj' because the implicit object parameter is inferred as lifetimebound}}
return ptr->getData(); // expected-note {{returned here}}
@@ -2350,7 +2350,7 @@ namespace lambda_captures {
auto return_ref_capture() {
int local = 1;
auto lambda = [&local]() { return local; }; // expected-warning {{stack memory associated with local variable 'local' is returned}}
- return lambda; // expected-note {{returned here}}
+ return lambda; // expected-note {{returned here}}
}
void safe_ref_capture() {
@@ -2367,7 +2367,7 @@ auto capture_int_by_value() {
auto capture_view_by_value() {
MyObj obj;
- View v(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
+ View v(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
auto lambda = [v]() { return v; }; // expected-note {{local variable 'v' aliases the storage of local variable 'obj'}}
return lambda; // expected-note {{returned here}}
}
@@ -2383,7 +2383,7 @@ auto capture_pointer_by_ref() {
MyObj obj;
MyObj* p = &obj;
auto lambda = [&p]() { return p; }; // expected-warning {{stack memory associated with local variable 'p' is returned}}
- return lambda; // expected-note {{returned here}}
+ return lambda; // expected-note {{returned here}}
}
auto capture_multiple() {
@@ -2397,28 +2397,28 @@ auto capture_multiple() {
auto capture_raw_pointer_by_value() {
int x;
- int* p = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
+ int* p = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
auto lambda = [p]() { return p; }; // expected-note {{local variable 'p' aliases the storage of local variable 'x'}}
return lambda; // expected-note {{returned here}}
}
auto capture_raw_pointer_init_capture() {
int x;
- int* p = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
+ int* p = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
auto lambda = [q = p]() { return q; }; // expected-note {{local variable 'p' aliases the storage of local variable 'x'}}
return lambda; // expected-note {{returned here}}
}
auto capture_view_init_capture() {
MyObj obj;
- View v(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
+ View v(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
auto lambda = [w = v]() { return w; }; // expected-note {{local variable 'v' aliases the storage of local variable 'obj'}}
return lambda; // expected-note {{returned here}}
}
auto capture_lambda() {
int x;
- auto inner = [&x]() { return x; }; // expected-warning {{stack memory associated with local variable 'x' is returned}}
+ auto inner = [&x]() { return x; }; // expected-warning {{stack memory associated with local variable 'x' is returned}}
auto outer = [inner]() { return inner(); }; // expected-note {{local variable 'inner' aliases the storage of local variable 'x'}}
return outer; // expected-note {{returned here}}
}
@@ -2433,7 +2433,7 @@ auto return_copied_lambda() {
auto implicit_ref_capture() {
int local = 1;
auto lambda = [&]() { return local; }; // expected-warning {{stack memory associated with local variable 'local' is returned}}
- return lambda; // expected-note {{returned here}}
+ return lambda; // expected-note {{returned here}}
}
// TODO: Include the name of the variable in the diagnostic to improve
@@ -2442,12 +2442,12 @@ auto implicit_ref_capture() {
auto implicit_ref_capture_multiple() {
int local = 1, local2 = 2;
auto lambda = [&]() { return local + local2; }; // expected-warning {{stack memory associated with local variable 'local2' is returned}} expected-warning {{stack memory associated with local variable 'local' is returned}}
- return lambda; // expected-note 2 {{returned here}}
+ return lambda; // expected-note 2 {{returned here}}
}
auto implicit_value_capture() {
MyObj obj;
- View v(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
+ View v(obj); // expected-warning {{stack memory associated with local variable 'obj' is returned}}
auto lambda = [=]() { return v; }; // expected-note {{local variable 'v' aliases the storage of local variable 'obj'}}
return lambda; // expected-note {{returned here}}
}
@@ -2480,14 +2480,14 @@ auto capture_static_address_by_ref() {
static int local = 1;
int* p = &local;
auto lambda = [&p]() { return p; }; // expected-warning {{stack memory associated with local variable 'p' is returned}}
- return lambda; // expected-note {{returned here}}
+ return lambda; // expected-note {{returned here}}
}
auto capture_multilevel_pointer() {
int x;
- int *p = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
- int **q = &p; // expected-warning {{stack memory associated with local variable 'p' is returned}}
- int ***r = &q; // expected-warning {{stack memory associated with local variable 'q' is returned}}
+ int *p = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
+ int **q = &p; // expected-warning {{stack memory associated with local variable 'p' is returned}}
+ int ***r = &q; // expected-warning {{stack memory associated with local variable 'q' is returned}}
auto lambda = [=]() { return *p + **q + ***r; }; // expected-note {{local variable 'p' aliases the storage of local variable 'x'}} \
// expected-note {{local variable 'r' aliases the storage of local variable 'q'}} \
// expected-note {{local variable 'q' aliases the storage of local variable 'p'}}
@@ -2885,7 +2885,8 @@ void from_template_instantiation() {
struct FieldInitFromLifetimebound {
S value; // expected-note {{this field dangles}}
- FieldInitFromLifetimebound() : value(getS(std::string("temp"))) {} // expected-warning {{stack memory associated with temporary object escapes to the field 'value' which will dangle}}
+ FieldInitFromLifetimebound() : value(getS(std::string("temp"))) {} // expected-warning {{stack memory associated with temporary object escapes to the field 'value' which will dangle}} \
+ // expected-note {{result of call to 'getS' aliases the storage of temporary object}}
};
S S::return_self_after_registration() const {
@@ -3116,7 +3117,7 @@ void binary_conditional_throw_void(int *in) {
int *binary_conditional_throw_dangling() {
int x;
- int *p = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
+ int *p = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
return p ?: throw 1; // expected-note {{returned here}}
}
@@ -3800,7 +3801,7 @@ std::function<void()> reassign_safe_then_unsafe() {
int local = 2;
std::function<void()> f = []() { (void)safe; };
f = [&local]() { (void)local; }; // expected-warning {{stack memory associated with local variable 'local' is returned}}
- return f; // expected-note {{returned here}}
+ return f; // expected-note {{returned here}}
}
std::function<void()> reassign_unsafe_then_safe() {
@@ -3920,10 +3921,10 @@ void deref_use_after_scope() {
namespace GH191954 {
int* return_moved_pointer() {
int x;
- int* f = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
+ int* f = &x; // expected-warning {{stack memory associated with local variable 'x' is returned}}
int* a;
a = std::move(f); // expected-note {{result of call to 'move<int *&>' aliases the storage of local variable 'x'}}
- return a; // expected-note {{returned here}}
+ return a; // expected-note {{returned here}}
}
int* return_moved_pointer2() {
@@ -3935,7 +3936,7 @@ namespace GH191954 {
View return_moved_view() {
MyObj o;
- View v(o); // expected-warning {{stack memory associated with local variable 'o' is returned}}
+ View v(o); // expected-warning {{stack memory associated with local variable 'o' is returned}}
View v2 = std::move(v); // expected-note {{result of call to 'move<View &>' aliases the storage of local variable 'o'}}
return v2; // expected-note {{returned here}}
}
@@ -3973,7 +3974,7 @@ View use_after_return_capture_by() {
MyObj a;
View res;
setCaptureBy(res, a); // expected-warning {{stack memory associated with local variable 'a' is returned}}
- return res; // expected-note {{returned here}}
+ return res; // expected-note {{returned here}}
}
More information about the cfe-commits
mailing list