[clang] 4f3ba73 - [LifetimeSafety] Revisit tracking moved objects (#178670)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 5 06:22:24 PST 2026
Author: Utkarsh Saxena
Date: 2026-02-05T14:22:18Z
New Revision: 4f3ba73896e46c8665d59460e5932dabad7977c1
URL: https://github.com/llvm/llvm-project/commit/4f3ba73896e46c8665d59460e5932dabad7977c1
DIFF: https://github.com/llvm/llvm-project/commit/4f3ba73896e46c8665d59460e5932dabad7977c1.diff
LOG: [LifetimeSafety] Revisit tracking moved objects (#178670)
Improve lifetime safety analysis by tracking moved objects and providing
more precise warnings for potentially false positive cases.
- Added support for detecting moves in function calls with rvalue
reference parameters
- Added a new `MovedOriginFact` to track when objects are moved
- Modified the lifetime checker to detect when a loan's storage has been
moved
- Added new diagnostic messages that indicate when a warning might be a
false positive due to moved storage
- Added notes in diagnostics to show where objects were potentially
moved
Added:
clang/include/clang/Analysis/Analyses/LifetimeSafety/MovedLoans.h
clang/lib/Analysis/LifetimeSafety/MovedLoans.cpp
Modified:
clang/include/clang/Analysis/Analyses/LifetimeSafety/Checker.h
clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h
clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
clang/include/clang/Basic/DiagnosticGroups.td
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Analysis/LifetimeSafety/CMakeLists.txt
clang/lib/Analysis/LifetimeSafety/Checker.cpp
clang/lib/Analysis/LifetimeSafety/Dataflow.h
clang/lib/Analysis/LifetimeSafety/Facts.cpp
clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
clang/lib/Analysis/LifetimeSafety/LifetimeSafety.cpp
clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
clang/lib/Sema/AnalysisBasedWarnings.cpp
clang/test/Sema/Inputs/lifetime-analysis.h
clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
clang/test/Sema/warn-lifetime-safety.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Checker.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Checker.h
index 5c631c92c0167..1ad3f9da61f60 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Checker.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Checker.h
@@ -26,8 +26,9 @@ namespace clang::lifetimes::internal {
/// examining loan expiration points and checking if any live origins hold
/// the expired loan.
void runLifetimeChecker(const LoanPropagationAnalysis &LoanPropagation,
+ const MovedLoansAnalysis &MovedLoans,
const LiveOriginsAnalysis &LiveOrigins,
- const FactManager &FactMgr, AnalysisDeclContext &ADC,
+ FactManager &FactMgr, AnalysisDeclContext &ADC,
LifetimeSafetySemaHelper *SemaHelper);
} // namespace clang::lifetimes::internal
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h
index d948965af34d5..0eb5b248fa1f6 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h
@@ -19,6 +19,7 @@
#include "clang/Analysis/Analyses/LifetimeSafety/Utils.h"
#include "clang/Analysis/AnalysisDeclContext.h"
#include "clang/Analysis/CFG.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Debug.h"
#include <cstdint>
@@ -44,6 +45,8 @@ class Fact {
OriginFlow,
/// An origin is used (eg. appears as l-value expression like DeclRefExpr).
Use,
+ /// An origin that is moved (e.g., passed to an rvalue reference parameter).
+ MovedOrigin,
/// A marker for a specific point in the code, for testing.
TestPoint,
/// An origin that escapes the function scope (e.g., via return).
@@ -219,6 +222,27 @@ class UseFact : public Fact {
const OriginManager &OM) const override;
};
+/// Top-level origin of the expression which was found to be moved, e.g, when
+/// being used as an argument to an r-value reference parameter.
+class MovedOriginFact : public Fact {
+ const OriginID MovedOrigin;
+ const Expr *MoveExpr;
+
+public:
+ static bool classof(const Fact *F) {
+ return F->getKind() == Kind::MovedOrigin;
+ }
+
+ MovedOriginFact(const Expr *MoveExpr, OriginID MovedOrigin)
+ : Fact(Kind::MovedOrigin), MovedOrigin(MovedOrigin), MoveExpr(MoveExpr) {}
+
+ OriginID getMovedOrigin() const { return MovedOrigin; }
+ const Expr *getMoveExpr() const { return MoveExpr; }
+
+ void dump(llvm::raw_ostream &OS, const LoanManager &,
+ const OriginManager &OM) const override;
+};
+
/// A dummy-fact used to mark a specific point in the code for testing.
/// It is generated by recognizing a `void("__lifetime_test_point_...")` cast.
class TestPointFact : public Fact {
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
index 8b45337bee218..bffc4d12562bb 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
@@ -67,6 +67,14 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
void handleGSLPointerConstruction(const CXXConstructExpr *CCE);
+ /// Detects arguments passed to rvalue reference parameters and creates
+ /// MovedOriginFact for them. The MovedLoansAnalysis then uses these facts
+ /// to track in a flow-sensitive manner which loans have been moved at each
+ /// program point, allowing warnings to distinguish potentially moved storage
+ /// from other use-after-free errors.
+ void handleMovedArgsInCall(const FunctionDecl *FD,
+ ArrayRef<const Expr *> Args);
+
/// Checks if a call-like expression creates a borrow by passing a value to a
/// reference parameter, creating an IssueFact if it does.
/// \param IsGslConstruction True if this is a GSL construction where all
@@ -110,15 +118,6 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
// corresponding to the left-hand side is updated to be a "write", thereby
// exempting it from the check.
llvm::DenseMap<const DeclRefExpr *, UseFact *> UseFacts;
-
- // This is a flow-insensitive approximation: once a declaration is moved
- // anywhere in the function, it's treated as moved everywhere. This can lead
- // to false negatives on control flow paths where the value is not actually
- // moved, but these are considered lower priority than the false positives
- // this tracking prevents.
- // TODO: The ideal solution would be flow-sensitive ownership tracking that
- // records where values are moved from and to, but this is more complex.
- llvm::DenseSet<const ValueDecl *> MovedDecls;
};
} // namespace clang::lifetimes::internal
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
index 9f22db20e79b1..6e87951c8961d 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
@@ -24,8 +24,10 @@
#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeStats.h"
#include "clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h"
#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
+#include "clang/Analysis/Analyses/LifetimeSafety/MovedLoans.h"
#include "clang/Analysis/Analyses/LifetimeSafety/Origins.h"
#include "clang/Analysis/AnalysisDeclContext.h"
+#include <memory>
namespace clang::lifetimes {
@@ -58,16 +60,18 @@ class LifetimeSafetySemaHelper {
virtual ~LifetimeSafetySemaHelper() = default;
virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr,
- SourceLocation FreeLoc,
+ const Expr *MovedExpr, SourceLocation FreeLoc,
Confidence Confidence) {}
virtual void reportUseAfterReturn(const Expr *IssueExpr,
const Expr *ReturnExpr,
+ const Expr *MovedExpr,
SourceLocation ExpiryLoc,
Confidence Confidence) {}
virtual void reportDanglingField(const Expr *IssueExpr,
const FieldDecl *Field,
+ const Expr *MovedExpr,
SourceLocation ExpiryLoc) {}
// Suggests lifetime bound annotations for function paramters.
@@ -107,6 +111,7 @@ void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM,
struct LifetimeFactory {
OriginLoanMap::Factory OriginMapFactory{/*canonicalize=*/false};
LoanSet::Factory LoanSetFactory{/*canonicalize=*/false};
+ MovedLoansMap::Factory MovedLoansMapFactory{/*canonicalize=*/false};
LivenessMap::Factory LivenessMapFactory{/*canonicalize=*/false};
};
@@ -133,6 +138,7 @@ class LifetimeSafetyAnalysis {
std::unique_ptr<FactManager> FactMgr;
std::unique_ptr<LiveOriginsAnalysis> LiveOrigins;
std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
+ std::unique_ptr<MovedLoansAnalysis> MovedLoans;
};
} // namespace internal
} // namespace clang::lifetimes
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
index a366e3e811cbf..9aaf4627ce5ad 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
@@ -30,8 +30,7 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, LoanID ID) {
/// Represents the storage location being borrowed, e.g., a specific stack
/// variable.
/// TODO: Model access paths of other types, e.g., s.field, heap and globals.
-struct AccessPath {
-private:
+class AccessPath {
// An access path can be:
// - ValueDecl * , to represent the storage location corresponding to the
// variable declared in ValueDecl.
@@ -52,6 +51,8 @@ struct AccessPath {
const clang::MaterializeTemporaryExpr *getAsMaterializeTemporaryExpr() const {
return P.dyn_cast<const clang::MaterializeTemporaryExpr *>();
}
+
+ bool operator==(const AccessPath &RHS) const { return P == RHS.P; }
};
/// An abstract base class for a single "Loan" which represents lending a
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/MovedLoans.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/MovedLoans.h
new file mode 100644
index 0000000000000..a83f40c8855b6
--- /dev/null
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/MovedLoans.h
@@ -0,0 +1,46 @@
+//===- MovedLoans.h - Moved Loans Analysis -----------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the MovedLoansAnalysis, a forward dataflow analysis that
+// tracks which loans have been moved out of their original storage location
+// at each program point.
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_MOVED_LOANS_H
+#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_MOVED_LOANS_H
+
+#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
+#include "clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h"
+#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
+#include "clang/Analysis/AnalysisDeclContext.h"
+#include "clang/Analysis/CFG.h"
+
+namespace clang::lifetimes::internal {
+
+// Map from a loan to an expression responsible for moving the borrowed storage.
+using MovedLoansMap = llvm::ImmutableMap<LoanID, const Expr *>;
+
+class MovedLoansAnalysis {
+public:
+ MovedLoansAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
+ const LoanPropagationAnalysis &LoanPropagation,
+ const LiveOriginsAnalysis &LiveOrigins,
+ const LoanManager &LoanMgr,
+ MovedLoansMap::Factory &MovedLoansMapFactory);
+ ~MovedLoansAnalysis();
+
+ MovedLoansMap getMovedLoans(ProgramPoint P) const;
+
+private:
+ class Impl;
+ std::unique_ptr<Impl> PImpl;
+};
+
+} // namespace clang::lifetimes::internal
+
+#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_MOVED_LOANS_H
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 9cdb41b417576..f7263e4c8218c 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -540,9 +540,17 @@ def LifetimeSafetyDanglingField : DiagGroup<"lifetime-safety-dangling-field"> {
}];
}
+def LifetimeSafetyDanglingFieldStrict : DiagGroup<"lifetime-safety-dangling-field-strict"> {
+ code Documentation = [{
+ Warning to detect dangling field references.
+ This may contain false-positives, e.g. when the borrowed storage is potentially moved and is not destroyed at function exit.
+ }];
+}
+
def LifetimeSafetyPermissive : DiagGroup<"lifetime-safety-permissive",
[LifetimeSafetyDanglingField]>;
-def LifetimeSafetyStrict : DiagGroup<"lifetime-safety-strict">;
+def LifetimeSafetyStrict : DiagGroup<"lifetime-safety-strict",
+ [LifetimeSafetyDanglingFieldStrict]>;
def LifetimeSafety : DiagGroup<"lifetime-safety",
[LifetimeSafetyPermissive, LifetimeSafetyStrict]> {
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 71c478f4ca873..af96b6cf02195 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10816,13 +10816,19 @@ def warn_lifetime_safety_loan_expires_permissive : Warning<
def warn_lifetime_safety_loan_expires_strict : Warning<
"object whose reference is captured may not live long enough">,
InGroup<LifetimeSafetyStrict>, DefaultIgnore;
+def warn_lifetime_safety_loan_expires_moved_strict : Warning<
+ "object whose reference is captured may not live long enough. "
+ "This could be false positive as the storage may have been moved later">,
+ InGroup<LifetimeSafetyStrict>, DefaultIgnore;
def warn_lifetime_safety_return_stack_addr_permissive
: Warning<"address of stack memory is returned later">,
InGroup<LifetimeSafetyPermissive>,
DefaultIgnore;
-def warn_lifetime_safety_return_stack_addr_strict
- : Warning<"address of stack memory may be returned later">,
+def warn_lifetime_safety_return_stack_addr_moved_strict
+ : Warning<"address of stack memory may be returned later. "
+ "This could be false positive as the storage may have been moved. "
+ "Consider moving first and then aliasing later to resolve the issue">,
InGroup<LifetimeSafetyStrict>,
DefaultIgnore;
@@ -10830,10 +10836,17 @@ def warn_lifetime_safety_dangling_field
: Warning<"address of stack memory escapes to a field">,
InGroup<LifetimeSafetyDanglingField>,
DefaultIgnore;
+def warn_lifetime_safety_dangling_field_moved
+ : Warning<"address of stack memory escapes to a field. "
+ "This could be a false positive as the storage may have been moved. "
+ "Consider moving first and then aliasing later to resolve the issue">,
+ InGroup<LifetimeSafetyDanglingFieldStrict>,
+ DefaultIgnore;
def note_lifetime_safety_used_here : Note<"later used here">;
def note_lifetime_safety_destroyed_here : Note<"destroyed here">;
def note_lifetime_safety_returned_here : Note<"returned here">;
+def note_lifetime_safety_moved_here : Note<"potentially moved here">;
def note_lifetime_safety_dangling_field_here: Note<"this field dangles">;
def note_lifetime_safety_escapes_to_field_here: Note<"escapes to this field">;
diff --git a/clang/lib/Analysis/LifetimeSafety/CMakeLists.txt b/clang/lib/Analysis/LifetimeSafety/CMakeLists.txt
index e5876e747610a..247377c7256d9 100644
--- a/clang/lib/Analysis/LifetimeSafety/CMakeLists.txt
+++ b/clang/lib/Analysis/LifetimeSafety/CMakeLists.txt
@@ -8,6 +8,7 @@ add_clang_library(clangAnalysisLifetimeSafety
LifetimeStats.cpp
Loans.cpp
LoanPropagation.cpp
+ MovedLoans.cpp
Origins.cpp
LINK_LIBS
diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
index c954a9b14bcdf..59ae665e652a6 100644
--- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
@@ -47,6 +47,7 @@ namespace {
struct PendingWarning {
SourceLocation ExpiryLoc; // Where the loan expired.
llvm::PointerUnion<const UseFact *, const OriginEscapesFact *> CausingFact;
+ const Expr *MovedExpr;
Confidence ConfidenceLevel;
};
@@ -60,18 +61,21 @@ class LifetimeChecker {
llvm::DenseMap<AnnotationTarget, const Expr *> AnnotationWarningsMap;
llvm::DenseMap<const ParmVarDecl *, EscapingTarget> NoescapeWarningsMap;
const LoanPropagationAnalysis &LoanPropagation;
+ const MovedLoansAnalysis &MovedLoans;
const LiveOriginsAnalysis &LiveOrigins;
- const FactManager &FactMgr;
+ FactManager &FactMgr;
LifetimeSafetySemaHelper *SemaHelper;
ASTContext &AST;
public:
LifetimeChecker(const LoanPropagationAnalysis &LoanPropagation,
- const LiveOriginsAnalysis &LiveOrigins, const FactManager &FM,
+ const MovedLoansAnalysis &MovedLoans,
+ const LiveOriginsAnalysis &LiveOrigins, FactManager &FM,
AnalysisDeclContext &ADC,
LifetimeSafetySemaHelper *SemaHelper)
- : LoanPropagation(LoanPropagation), LiveOrigins(LiveOrigins), FactMgr(FM),
- SemaHelper(SemaHelper), AST(ADC.getASTContext()) {
+ : LoanPropagation(LoanPropagation), MovedLoans(MovedLoans),
+ LiveOrigins(LiveOrigins), FactMgr(FM), SemaHelper(SemaHelper),
+ AST(ADC.getASTContext()) {
for (const CFGBlock *B : *ADC.getAnalysis<PostOrderCFGView>())
for (const Fact *F : FactMgr.getFacts(B))
if (const auto *EF = F->getAs<ExpireFact>())
@@ -140,6 +144,10 @@ class LifetimeChecker {
/// propagation (e.g., a loan may only be held on some execution paths).
void checkExpiry(const ExpireFact *EF) {
LoanID ExpiredLoan = EF->getLoanID();
+ const Expr *MovedExpr = nullptr;
+ if (auto *ME = MovedLoans.getMovedLoans(EF).lookup(ExpiredLoan))
+ MovedExpr = *ME;
+
LivenessMap Origins = LiveOrigins.getLiveOriginsAt(EF);
Confidence CurConfidence = Confidence::None;
// The UseFact or OriginEscapesFact most indicative of a lifetime error,
@@ -166,6 +174,7 @@ class LifetimeChecker {
return;
FinalWarningsMap[ExpiredLoan] = {/*ExpiryLoc=*/EF->getExpiryLoc(),
/*BestCausingFact=*/BestCausingFact,
+ /*MovedExpr=*/MovedExpr,
/*ConfidenceLevel=*/CurConfidence};
}
@@ -179,19 +188,21 @@ class LifetimeChecker {
llvm::PointerUnion<const UseFact *, const OriginEscapesFact *>
CausingFact = Warning.CausingFact;
Confidence Confidence = Warning.ConfidenceLevel;
+ const Expr *MovedExpr = Warning.MovedExpr;
SourceLocation ExpiryLoc = Warning.ExpiryLoc;
if (const auto *UF = CausingFact.dyn_cast<const UseFact *>())
- SemaHelper->reportUseAfterFree(IssueExpr, UF->getUseExpr(), ExpiryLoc,
- Confidence);
+ SemaHelper->reportUseAfterFree(IssueExpr, UF->getUseExpr(), MovedExpr,
+ ExpiryLoc, Confidence);
else if (const auto *OEF =
CausingFact.dyn_cast<const OriginEscapesFact *>()) {
if (const auto *RetEscape = dyn_cast<ReturnEscapeFact>(OEF))
- SemaHelper->reportUseAfterReturn(
- IssueExpr, RetEscape->getReturnExpr(), ExpiryLoc, Confidence);
+ SemaHelper->reportUseAfterReturn(IssueExpr,
+ RetEscape->getReturnExpr(),
+ MovedExpr, ExpiryLoc, Confidence);
else if (const auto *FieldEscape = dyn_cast<FieldEscapeFact>(OEF))
SemaHelper->reportDanglingField(
- IssueExpr, FieldEscape->getFieldDecl(), ExpiryLoc);
+ IssueExpr, FieldEscape->getFieldDecl(), MovedExpr, ExpiryLoc);
else
llvm_unreachable("Unhandled OriginEscapesFact type");
} else
@@ -293,11 +304,12 @@ class LifetimeChecker {
} // namespace
void runLifetimeChecker(const LoanPropagationAnalysis &LP,
- const LiveOriginsAnalysis &LO,
- const FactManager &FactMgr, AnalysisDeclContext &ADC,
+ const MovedLoansAnalysis &MovedLoans,
+ const LiveOriginsAnalysis &LO, FactManager &FactMgr,
+ AnalysisDeclContext &ADC,
LifetimeSafetySemaHelper *SemaHelper) {
llvm::TimeTraceScope TimeProfile("LifetimeChecker");
- LifetimeChecker Checker(LP, LO, FactMgr, ADC, SemaHelper);
+ LifetimeChecker Checker(LP, MovedLoans, LO, FactMgr, ADC, SemaHelper);
}
} // namespace clang::lifetimes::internal
diff --git a/clang/lib/Analysis/LifetimeSafety/Dataflow.h b/clang/lib/Analysis/LifetimeSafety/Dataflow.h
index 05c20d6385368..4de9bbb54af9f 100644
--- a/clang/lib/Analysis/LifetimeSafety/Dataflow.h
+++ b/clang/lib/Analysis/LifetimeSafety/Dataflow.h
@@ -170,6 +170,8 @@ class DataflowAnalysis {
return D->transfer(In, *F->getAs<ExpireFact>());
case Fact::Kind::OriginFlow:
return D->transfer(In, *F->getAs<OriginFlowFact>());
+ case Fact::Kind::MovedOrigin:
+ return D->transfer(In, *F->getAs<MovedOriginFact>());
case Fact::Kind::OriginEscapes:
return D->transfer(In, *F->getAs<OriginEscapesFact>());
case Fact::Kind::Use:
@@ -184,6 +186,7 @@ class DataflowAnalysis {
Lattice transfer(Lattice In, const IssueFact &) { return In; }
Lattice transfer(Lattice In, const ExpireFact &) { return In; }
Lattice transfer(Lattice In, const OriginFlowFact &) { return In; }
+ Lattice transfer(Lattice In, const MovedOriginFact &) { return In; }
Lattice transfer(Lattice In, const OriginEscapesFact &) { return In; }
Lattice transfer(Lattice In, const UseFact &) { return In; }
Lattice transfer(Lattice In, const TestPointFact &) { return In; }
diff --git a/clang/lib/Analysis/LifetimeSafety/Facts.cpp b/clang/lib/Analysis/LifetimeSafety/Facts.cpp
index 1fc72aa0a4259..e782d6de82ea6 100644
--- a/clang/lib/Analysis/LifetimeSafety/Facts.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Facts.cpp
@@ -9,6 +9,8 @@
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
#include "clang/AST/Decl.h"
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
+#include "clang/Analysis/FlowSensitive/DataflowWorklist.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
namespace clang::lifetimes::internal {
@@ -45,6 +47,13 @@ void OriginFlowFact::dump(llvm::raw_ostream &OS, const LoanManager &,
OS << "\n";
}
+void MovedOriginFact::dump(llvm::raw_ostream &OS, const LoanManager &,
+ const OriginManager &OM) const {
+ OS << "MovedOrigins (";
+ OM.dump(getMovedOrigin(), OS);
+ OS << ")\n";
+}
+
void ReturnEscapeFact::dump(llvm::raw_ostream &OS, const LoanManager &,
const OriginManager &OM) const {
OS << "OriginEscapes (";
diff --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
index fb859eeb856af..bdb48b0c81172 100644
--- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
@@ -9,6 +9,8 @@
#include <cassert>
#include <string>
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/ExprCXX.h"
#include "clang/AST/OperationKinds.h"
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
#include "clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h"
@@ -185,6 +187,9 @@ void FactsGenerator::VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
handleGSLPointerConstruction(CCE);
return;
}
+ handleFunctionCall(CCE, CCE->getConstructor(),
+ {CCE->getArgs(), CCE->getNumArgs()},
+ /*IsGslConstruction=*/false);
}
void FactsGenerator::handleCXXCtorInitializer(const CXXCtorInitializer *CII) {
@@ -233,23 +238,9 @@ void FactsGenerator::VisitMemberExpr(const MemberExpr *ME) {
}
}
-static bool isStdMove(const FunctionDecl *FD) {
- return FD && FD->isInStdNamespace() && FD->getIdentifier() &&
- FD->getName() == "move";
-}
-
void FactsGenerator::VisitCallExpr(const CallExpr *CE) {
handleFunctionCall(CE, CE->getDirectCallee(),
{CE->getArgs(), CE->getNumArgs()});
- // Track declarations that are moved via std::move.
- // This is a flow-insensitive approximation: once a declaration is moved
- // anywhere in the function, it's treated as moved everywhere. We do not
- // generate expire facts for moved decls to avoid false alarms.
- if (isStdMove(CE->getDirectCallee()))
- if (CE->getNumArgs() == 1)
- if (auto *DRE =
- dyn_cast<DeclRefExpr>(CE->getArg(0)->IgnoreParenImpCasts()))
- MovedDecls.insert(DRE->getDecl());
}
void FactsGenerator::VisitCXXNullPtrLiteralExpr(
@@ -453,11 +444,6 @@ void FactsGenerator::handleLifetimeEnds(const CFGLifetimeEnds &LifetimeEnds) {
// Iterate through all loans to see if any expire.
for (const auto *Loan : FactMgr.getLoanMgr().getLoans()) {
if (const auto *BL = dyn_cast<PathLoan>(Loan)) {
- // Skip loans for declarations that have been moved. When a value is
- // moved, the original owner no longer has ownership and its destruction
- // should not cause the loan to expire, preventing false positives.
- if (MovedDecls.contains(BL->getAccessPath().getAsValueDecl()))
- continue;
// Check if the loan is for a stack variable and if that variable
// is the one being destructed.
const AccessPath AP = BL->getAccessPath();
@@ -533,10 +519,29 @@ void FactsGenerator::handleGSLPointerConstruction(const CXXConstructExpr *CCE) {
}
}
-/// Checks if a call-like expression creates a borrow by passing a value to a
-/// reference parameter, creating an IssueFact if it does.
-/// \param IsGslConstruction True if this is a GSL construction where all
-/// argument origins should flow to the returned origin.
+void FactsGenerator::handleMovedArgsInCall(const FunctionDecl *FD,
+ ArrayRef<const Expr *> Args) {
+ unsigned IsInstance = 0;
+ if (const auto *Method = dyn_cast<CXXMethodDecl>(FD);
+ Method && Method->isInstance() && !isa<CXXConstructorDecl>(FD))
+ IsInstance = 1;
+
+ // Skip 'this' arg as it cannot be moved.
+ for (unsigned I = IsInstance;
+ I < Args.size() && I < FD->getNumParams() + IsInstance; ++I) {
+ const ParmVarDecl *PVD = FD->getParamDecl(I - IsInstance);
+ if (!PVD->getType()->isRValueReferenceType())
+ continue;
+ const Expr *Arg = Args[I];
+ OriginList *MovedOrigins = getOriginsList(*Arg);
+ assert(MovedOrigins->getLength() >= 1 &&
+ "unexpected length for r-value reference param");
+ // Arg is being moved to this parameter. Mark the origin as moved.
+ CurrentBlockFacts.push_back(FactMgr.createFact<MovedOriginFact>(
+ Arg, MovedOrigins->getOuterOriginID()));
+ }
+}
+
void FactsGenerator::handleFunctionCall(const Expr *Call,
const FunctionDecl *FD,
ArrayRef<const Expr *> Args,
@@ -544,7 +549,10 @@ void FactsGenerator::handleFunctionCall(const Expr *Call,
OriginList *CallList = getOriginsList(*Call);
// Ignore functions returning values with no origin.
FD = getDeclWithMergedLifetimeBoundAttrs(FD);
- if (!FD || !CallList)
+ if (!FD)
+ return;
+ handleMovedArgsInCall(FD, Args);
+ if (!CallList)
return;
auto IsArgLifetimeBound = [FD](unsigned I) -> bool {
const ParmVarDecl *PVD = nullptr;
diff --git a/clang/lib/Analysis/LifetimeSafety/LifetimeSafety.cpp b/clang/lib/Analysis/LifetimeSafety/LifetimeSafety.cpp
index bf9a65254e8fa..a6bea74c50b49 100644
--- a/clang/lib/Analysis/LifetimeSafety/LifetimeSafety.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/LifetimeSafety.cpp
@@ -56,21 +56,12 @@ void LifetimeSafetyAnalysis::run() {
llvm::TimeTraceScope TimeProfile("LifetimeSafetyAnalysis");
const CFG &Cfg = *AC.getCFG();
- DEBUG_WITH_TYPE("PrintCFG", Cfg.dump(AC.getASTContext().getLangOpts(),
- /*ShowColors=*/true));
FactMgr = std::make_unique<FactManager>(AC, Cfg);
FactsGenerator FactGen(*FactMgr, AC);
FactGen.run();
- DEBUG_WITH_TYPE("LifetimeFacts", FactMgr->dump(Cfg, AC));
-
- // Debug print facts for a specific function using
- // -debug-only=EnableFilterByFunctionName,YourFunctionNameFoo
- DEBUG_WITH_TYPE("EnableFilterByFunctionName",
- DebugOnlyFunction(AC, Cfg, *FactMgr));
-
/// TODO(opt): Consider optimizing individual blocks before running the
/// dataflow analysis.
/// 1. Expression Origins: These are assigned once and read at most once,
@@ -87,10 +78,25 @@ void LifetimeSafetyAnalysis::run() {
LiveOrigins = std::make_unique<LiveOriginsAnalysis>(
Cfg, AC, *FactMgr, Factory.LivenessMapFactory);
+
+ MovedLoans = std::make_unique<MovedLoansAnalysis>(
+ Cfg, AC, *FactMgr, *LoanPropagation, *LiveOrigins, FactMgr->getLoanMgr(),
+ Factory.MovedLoansMapFactory);
+
+ runLifetimeChecker(*LoanPropagation, *MovedLoans, *LiveOrigins, *FactMgr, AC,
+ SemaHelper);
+
+ DEBUG_WITH_TYPE("PrintCFG", Cfg.dump(AC.getASTContext().getLangOpts(),
+ /*ShowColors=*/true));
+
+ DEBUG_WITH_TYPE("LifetimeFacts", FactMgr->dump(Cfg, AC));
+
+ // Debug print facts for a specific function using
+ // -debug-only=EnableFilterByFunctionName,YourFunctionNameFoo
+ DEBUG_WITH_TYPE("EnableFilterByFunctionName",
+ DebugOnlyFunction(AC, Cfg, *FactMgr));
DEBUG_WITH_TYPE("LiveOrigins",
LiveOrigins->dump(llvm::dbgs(), FactMgr->getTestPoints()));
-
- runLifetimeChecker(*LoanPropagation, *LiveOrigins, *FactMgr, AC, SemaHelper);
}
void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM,
diff --git a/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp b/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
index 01a74d30408ea..0996d11f0cdeb 100644
--- a/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/LoanPropagation.cpp
@@ -63,6 +63,7 @@ static llvm::BitVector computePersistentOrigins(const FactManager &FactMgr,
Cur = Cur->peelOuterOrigin())
CheckOrigin(Cur->getOuterOriginID());
break;
+ case Fact::Kind::MovedOrigin:
case Fact::Kind::OriginEscapes:
case Fact::Kind::Expire:
case Fact::Kind::TestPoint:
diff --git a/clang/lib/Analysis/LifetimeSafety/MovedLoans.cpp b/clang/lib/Analysis/LifetimeSafety/MovedLoans.cpp
new file mode 100644
index 0000000000000..95de08d4425b0
--- /dev/null
+++ b/clang/lib/Analysis/LifetimeSafety/MovedLoans.cpp
@@ -0,0 +1,133 @@
+//===- MovedLoans.cpp - Moved Loans Analysis --------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the MovedLoansAnalysis, a forward dataflow analysis that
+// tracks which loans have been moved out of their original storage location
+// at each program point.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/Analyses/LifetimeSafety/MovedLoans.h"
+#include "Dataflow.h"
+#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
+#include "clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h"
+#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
+#include "clang/Analysis/Analyses/LifetimeSafety/Loans.h"
+#include "clang/Analysis/Analyses/LifetimeSafety/Utils.h"
+
+namespace clang::lifetimes::internal {
+namespace {
+struct Lattice {
+ MovedLoansMap MovedLoans = MovedLoansMap(nullptr);
+
+ explicit Lattice(MovedLoansMap MovedLoans) : MovedLoans(MovedLoans) {}
+
+ Lattice() = default;
+
+ bool operator==(const Lattice &Other) const {
+ return MovedLoans == Other.MovedLoans;
+ }
+ bool operator!=(const Lattice &Other) const { return !(*this == Other); }
+};
+
+class AnalysisImpl
+ : public DataflowAnalysis<AnalysisImpl, Lattice, Direction::Forward> {
+public:
+ AnalysisImpl(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
+ const LoanPropagationAnalysis &LoanPropagation,
+ const LiveOriginsAnalysis &LiveOrigins,
+ const LoanManager &LoanMgr,
+ MovedLoansMap::Factory &MovedLoansMapFactory)
+ : DataflowAnalysis(C, AC, F), LoanPropagation(LoanPropagation),
+ LiveOrigins(LiveOrigins), LoanMgr(LoanMgr),
+ MovedLoansMapFactory(MovedLoansMapFactory) {}
+
+ using Base::transfer;
+
+ StringRef getAnalysisName() const { return "MovedLoans"; }
+
+ Lattice getInitialState() { return Lattice{}; }
+
+ /// Merges moved loan state from
diff erent control flow paths. When a loan
+ /// is moved on multiple paths, picks the lexically earliest move expression.
+ Lattice join(Lattice A, Lattice B) {
+ MovedLoansMap MovedLoans = utils::join(
+ A.MovedLoans, B.MovedLoans, MovedLoansMapFactory,
+ [](const Expr *const *MoveA, const Expr *const *MoveB) -> const Expr * {
+ assert(MoveA || MoveB);
+ if (!MoveA)
+ return *MoveB;
+ if (!MoveB)
+ return *MoveA;
+ return (*MoveA)->getExprLoc() < (*MoveB)->getExprLoc() ? *MoveA
+ : *MoveB;
+ },
+ utils::JoinKind::Asymmetric);
+ return Lattice(MovedLoans);
+ }
+
+ /// Marks all live loans sharing the same access path as the moved origin as
+ /// potentially moved.
+ Lattice transfer(Lattice In, const MovedOriginFact &F) {
+ MovedLoansMap MovedLoans = In.MovedLoans;
+ OriginID MovedOrigin = F.getMovedOrigin();
+ LoanSet ImmediatelyMovedLoans = LoanPropagation.getLoans(MovedOrigin, &F);
+ auto IsInvalidated = [&](const AccessPath &Path) {
+ for (LoanID LID : ImmediatelyMovedLoans) {
+ const Loan *MovedLoan = LoanMgr.getLoan(LID);
+ auto *PL = dyn_cast<PathLoan>(MovedLoan);
+ if (!PL)
+ continue;
+ if (PL->getAccessPath() == Path)
+ return true;
+ }
+ return false;
+ };
+ for (auto [O, _] : LiveOrigins.getLiveOriginsAt(&F))
+ for (LoanID LiveLoan : LoanPropagation.getLoans(O, &F)) {
+ const Loan *LiveLoanPtr = LoanMgr.getLoan(LiveLoan);
+ auto *PL = dyn_cast<PathLoan>(LiveLoanPtr);
+ if (!PL)
+ continue;
+ if (IsInvalidated(PL->getAccessPath()))
+ MovedLoans =
+ MovedLoansMapFactory.add(MovedLoans, LiveLoan, F.getMoveExpr());
+ }
+ return Lattice(MovedLoans);
+ }
+
+ MovedLoansMap getMovedLoans(ProgramPoint P) { return getState(P).MovedLoans; }
+
+private:
+ const LoanPropagationAnalysis &LoanPropagation;
+ const LiveOriginsAnalysis &LiveOrigins;
+ const LoanManager &LoanMgr;
+ MovedLoansMap::Factory &MovedLoansMapFactory;
+};
+} // namespace
+
+class MovedLoansAnalysis::Impl final : public AnalysisImpl {
+ using AnalysisImpl::AnalysisImpl;
+};
+
+MovedLoansAnalysis::MovedLoansAnalysis(
+ const CFG &C, AnalysisDeclContext &AC, FactManager &F,
+ const LoanPropagationAnalysis &LoanPropagation,
+ const LiveOriginsAnalysis &LiveOrigins, const LoanManager &LoanMgr,
+ MovedLoansMap::Factory &MovedLoansMapFactory)
+ : PImpl(std::make_unique<Impl>(C, AC, F, LoanPropagation, LiveOrigins,
+ LoanMgr, MovedLoansMapFactory)) {
+ PImpl->run();
+}
+
+MovedLoansAnalysis::~MovedLoansAnalysis() = default;
+
+MovedLoansMap MovedLoansAnalysis::getMovedLoans(ProgramPoint P) const {
+ return PImpl->getMovedLoans(P);
+}
+} // namespace clang::lifetimes::internal
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index 0c96b0afef1a7..858d5b7f67b58 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2880,32 +2880,46 @@ class LifetimeSafetySemaHelperImpl : public LifetimeSafetySemaHelper {
LifetimeSafetySemaHelperImpl(Sema &S) : S(S) {}
void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr,
- SourceLocation FreeLoc, Confidence C) override {
+ const Expr *MovedExpr, SourceLocation FreeLoc,
+ Confidence C) override {
S.Diag(IssueExpr->getExprLoc(),
- C == Confidence::Definite
+ MovedExpr ? diag::warn_lifetime_safety_loan_expires_moved_strict
+ : C == Confidence::Definite
? diag::warn_lifetime_safety_loan_expires_permissive
: diag::warn_lifetime_safety_loan_expires_strict)
<< IssueExpr->getSourceRange();
+ if (MovedExpr)
+ S.Diag(MovedExpr->getExprLoc(), diag::note_lifetime_safety_moved_here)
+ << MovedExpr->getSourceRange();
S.Diag(FreeLoc, diag::note_lifetime_safety_destroyed_here);
S.Diag(UseExpr->getExprLoc(), diag::note_lifetime_safety_used_here)
<< UseExpr->getSourceRange();
}
void reportUseAfterReturn(const Expr *IssueExpr, const Expr *ReturnExpr,
- SourceLocation ExpiryLoc, Confidence C) override {
+ const Expr *MovedExpr, SourceLocation ExpiryLoc,
+ Confidence C) override {
S.Diag(IssueExpr->getExprLoc(),
- C == Confidence::Definite
- ? diag::warn_lifetime_safety_return_stack_addr_permissive
- : diag::warn_lifetime_safety_return_stack_addr_strict)
+ MovedExpr ? diag::warn_lifetime_safety_return_stack_addr_moved_strict
+ : diag::warn_lifetime_safety_return_stack_addr_permissive)
<< IssueExpr->getSourceRange();
+ if (MovedExpr)
+ S.Diag(MovedExpr->getExprLoc(), diag::note_lifetime_safety_moved_here)
+ << MovedExpr->getSourceRange();
S.Diag(ReturnExpr->getExprLoc(), diag::note_lifetime_safety_returned_here)
<< ReturnExpr->getSourceRange();
}
void reportDanglingField(const Expr *IssueExpr,
const FieldDecl *DanglingField,
+ const Expr *MovedExpr,
SourceLocation ExpiryLoc) override {
- S.Diag(IssueExpr->getExprLoc(), diag::warn_lifetime_safety_dangling_field)
+ S.Diag(IssueExpr->getExprLoc(),
+ MovedExpr ? diag::warn_lifetime_safety_dangling_field_moved
+ : diag::warn_lifetime_safety_dangling_field)
<< IssueExpr->getSourceRange();
+ if (MovedExpr)
+ S.Diag(MovedExpr->getExprLoc(), diag::note_lifetime_safety_moved_here)
+ << MovedExpr->getSourceRange();
S.Diag(DanglingField->getLocation(),
diag::note_lifetime_safety_dangling_field_here)
<< DanglingField->getEndLoc();
@@ -3117,10 +3131,13 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
D->getBeginLoc()) ||
!Diags.isIgnored(diag::warn_lifetime_safety_loan_expires_strict,
D->getBeginLoc()) ||
- !Diags.isIgnored(diag::warn_lifetime_safety_return_stack_addr_permissive,
+ !Diags.isIgnored(diag::warn_lifetime_safety_loan_expires_moved_strict,
D->getBeginLoc()) ||
- !Diags.isIgnored(diag::warn_lifetime_safety_return_stack_addr_strict,
+ !Diags.isIgnored(diag::warn_lifetime_safety_return_stack_addr_permissive,
D->getBeginLoc()) ||
+ !Diags.isIgnored(
+ diag::warn_lifetime_safety_return_stack_addr_moved_strict,
+ D->getBeginLoc()) ||
!Diags.isIgnored(diag::warn_lifetime_safety_noescape_escapes,
D->getBeginLoc());
bool EnableLifetimeSafetyAnalysis =
diff --git a/clang/test/Sema/Inputs/lifetime-analysis.h b/clang/test/Sema/Inputs/lifetime-analysis.h
index 27882e68c1524..987fb012d59c0 100644
--- a/clang/test/Sema/Inputs/lifetime-analysis.h
+++ b/clang/test/Sema/Inputs/lifetime-analysis.h
@@ -88,6 +88,8 @@ template<class _Mystr> struct iter {
template<typename T>
struct basic_string {
basic_string();
+ basic_string(const basic_string<T> &);
+ basic_string(basic_string<T> &&);
basic_string(const T *);
~basic_string();
const T *c_str() const;
@@ -99,6 +101,8 @@ using string = basic_string<char>;
template<typename T>
struct unique_ptr {
+ unique_ptr();
+ unique_ptr(unique_ptr<T>&&);
~unique_ptr();
T &operator*();
T *get() const;
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index a58b446fe4c07..c40c6a671e3ab 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -349,9 +349,11 @@ const char *trackThroughMultiplePointer() {
struct X {
X(std::unique_ptr<int> up) :
- pointee(*up), pointee2(up.get()), pointer(std::move(up)) {}
- int &pointee;
- int *pointee2;
+ pointee(*up), // cfg-field-warning {{may have been moved.}}
+ pointee2(up.get()), // cfg-field-warning {{may have been moved.}}
+ pointer(std::move(up)) {} // cfg-field-note 2 {{potentially moved here}}
+ int &pointee; // cfg-field-note {{this field dangles}}
+ int *pointee2; // cfg-field-note {{this field dangles}}
std::unique_ptr<int> pointer;
};
@@ -360,11 +362,11 @@ struct [[gsl::Owner]] XOwner {
};
struct X2 {
// A common usage that moves the passing owner to the class.
- // verify no warning on this case.
+ // verify a strict warning on this case.
X2(XOwner owner) :
- pointee(owner.get()),
- owner(std::move(owner)) {}
- int* pointee;
+ pointee(owner.get()), // cfg-field-warning {{may have been moved.}}
+ owner(std::move(owner)) {} // cfg-field-note {{potentially moved here}}
+ int* pointee; // cfg-field-note {{this field dangles}}
XOwner owner;
};
@@ -1131,9 +1133,8 @@ struct Foo2 {
};
struct Test {
- Test(Foo2 foo) : bar(foo.bar.get()), // OK \
- // FIXME: cfg-field-warning {{address of stack memory escapes to a field}}
- storage(std::move(foo.bar)) {};
+ Test(Foo2 foo) : bar(foo.bar.get()), // cfg-field-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
+ storage(std::move(foo.bar)) {}; // cfg-field-note {{potentially moved here}}
Bar* bar; // cfg-field-note {{this field dangles}}
std::unique_ptr<Bar> storage;
diff --git a/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp b/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
index dfc43e49906f2..fa45458371012 100644
--- a/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
@@ -24,15 +24,26 @@ struct CtorInitLifetimeBound {
};
struct CtorInitButMoved {
- std::string_view view;
- CtorInitButMoved(std::string s) : view(s) { takeString(std::move(s)); }
+ std::string_view view; // expected-note {{this field dangles}}
+ CtorInitButMoved(std::string s)
+ : view(s) { // expected-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
+ takeString(std::move(s)); // expected-note {{potentially moved here}}
+ }
};
struct CtorInitButMovedOwned {
+ std::string_view view; // expected-note {{this field dangles}}
+ std::string owned;
+ CtorInitButMovedOwned(std::string s)
+ : view(s), // expected-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
+ owned(std::move(s)) {} // expected-note {{potentially moved here}}
+};
+
+struct CtorInitButMovedOwnedOrderedCorrectly {
std::string owned;
std::string_view view;
- CtorInitButMovedOwned(std::string s) : view(s), owned(std::move(s)) {}
- CtorInitButMovedOwned(Dummy<1>, std::string s) : owned(std::move(s)), view(owned) {}
+ CtorInitButMovedOwnedOrderedCorrectly(std::string s)
+ :owned(std::move(s)), view(owned) {}
};
struct CtorInitMultipleViews {
@@ -65,8 +76,8 @@ struct CtorPointerField {
};
struct MemberSetters {
- std::string_view view; // expected-note 5 {{this field dangles}}
- const char* p; // expected-note 5 {{this field dangles}}
+ std::string_view view; // expected-note 6 {{this field dangles}}
+ const char* p; // expected-note 6 {{this field dangles}}
void setWithParam(std::string s) {
view = s; // expected-warning {{address of stack memory escapes to a field}}
@@ -98,9 +109,9 @@ struct MemberSetters {
void setWithLocalButMoved() {
std::string s;
- view = s;
- p = s.data();
- takeString(std::move(s));
+ view = s; // expected-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
+ p = s.data(); // expected-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
+ takeString(std::move(s)); // expected-note 2 {{potentially moved here}}
}
void setWithGlobal() {
diff --git a/clang/test/Sema/warn-lifetime-safety.cpp b/clang/test/Sema/warn-lifetime-safety.cpp
index 2976c809e389c..5ad856529ffdf 100644
--- a/clang/test/Sema/warn-lifetime-safety.cpp
+++ b/clang/test/Sema/warn-lifetime-safety.cpp
@@ -10,6 +10,8 @@ struct [[gsl::Owner]] MyObj {
MyObj();
MyObj(int);
MyObj(const MyObj&);
+ MyObj(MyObj&&);
+ MyObj& operator=(MyObj&&);
~MyObj() {} // Non-trivial destructor
MyObj operator+(MyObj);
@@ -1404,39 +1406,57 @@ void add(int c, MyObj* node) {
}
} // namespace CppCoverage
-namespace do_not_warn_on_std_move {
-void silenced() {
+namespace strict_warn_on_move {
+void strict_warn_on_move() {
MyObj b;
View v;
{
MyObj a;
- v = a;
- b = std::move(a); // No warning for 'a' being moved.
- }
- (void)v;
+ v = a; // expected-warning-re {{object whose reference {{.*}} may have been moved}}
+ b = std::move(a); // expected-note {{potentially moved here}}
+ } // expected-note {{destroyed here}}
+ (void)v; // expected-note {{later used here}}
}
-void silenced_flow_insensitive(bool c) {
- MyObj a;
- View v = a;
- if (c) {
- MyObj b = std::move(a);
- }
- (void)v;
+void flow_sensitive(bool c) {
+ View v;
+ {
+ MyObj a;
+ if (c) {
+ MyObj b = std::move(a);
+ return;
+ }
+ v = a; // expected-warning {{object whose reference}}
+ } // expected-note {{destroyed here}}
+ (void)v; // expected-note {{later used here}}
}
-// FIXME: Silence when move arg is not a declref.
void take(MyObj&&);
-void not_silenced_via_conditional(bool cond) {
+void detect_conditional(bool cond) {
View v;
{
MyObj a, b;
- v = cond ? a : b; // expected-warning 2 {{object whose reference }}
- take(std::move(cond ? a : b));
+ v = cond ? a : b; // expected-warning-re 2 {{object whose reference {{.*}} may have been moved}}
+ take(std::move(cond ? a : b)); // expected-note 2 {{potentially moved here}}
} // expected-note 2 {{destroyed here}}
(void)v; // expected-note 2 {{later used here}}
}
-} // namespace do_not_warn_on_std_move
+
+void wrong_use_of_move_is_permissive() {
+ View v;
+ {
+ MyObj a;
+ v = std::move(a); // expected-warning {{object whose reference is captured does not live long enough}}
+ } // expected-note {{destroyed here}}
+ (void)v; // expected-note {{later used here}}
+ const int* p;
+ {
+ MyObj a;
+ p = std::move(a).getData(); // expected-warning {{object whose reference is captured does not live long enough}}
+ } // expected-note {{destroyed here}}
+ (void)p; // expected-note {{later used here}}
+}
+} // namespace strict_warn_on_move
// Implicit this annotations with redecls.
namespace GH172013 {
More information about the cfe-commits
mailing list