Skip to content

Commit 4f11ea0

Browse files
committed
lifetime-lattice-tracking-per-point
1 parent d9190f8 commit 4f11ea0

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

clang/lib/Analysis/LifetimeSafety.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
502502

503503
enum class Direction { Forward, Backward };
504504

505+
/// A program point is a pair of a CFGBlock and a Fact within that block.
506+
///
507+
/// This is used to represent the state of the program *after* the Fact is
508+
/// executed.
509+
using ProgramPoint = std::pair<const CFGBlock *, const Fact *>;
510+
505511
/// A generic, policy-based driver for dataflow analyses. It combines
506512
/// the dataflow runner and the transferer logic into a single class hierarchy.
507513
///
@@ -532,6 +538,7 @@ class DataflowAnalysis {
532538

533539
llvm::DenseMap<const CFGBlock *, Lattice> InStates;
534540
llvm::DenseMap<const CFGBlock *, Lattice> OutStates;
541+
llvm::DenseMap<ProgramPoint, Lattice> PerPointStates;
535542

536543
static constexpr bool isForward() { return Dir == Direction::Forward; }
537544

@@ -577,6 +584,8 @@ class DataflowAnalysis {
577584
}
578585
}
579586

587+
Lattice getState(ProgramPoint P) const { return PerPointStates.lookup(P); }
588+
580589
Lattice getInState(const CFGBlock *B) const { return InStates.lookup(B); }
581590

582591
Lattice getOutState(const CFGBlock *B) const { return OutStates.lookup(B); }
@@ -590,18 +599,22 @@ class DataflowAnalysis {
590599
getOutState(&B).dump(llvm::dbgs());
591600
}
592601

602+
private:
593603
/// Computes the state at one end of a block by applying all its facts
594604
/// sequentially to a given state from the other end.
595-
/// TODO: We might need to store intermediate states per-fact in the block for
596-
/// later analysis.
597605
Lattice transferBlock(const CFGBlock *Block, Lattice State) {
598606
auto Facts = AllFacts.getFacts(Block);
599-
if constexpr (isForward())
600-
for (const Fact *F : Facts)
607+
if constexpr (isForward()) {
608+
for (const Fact *F : Facts) {
601609
State = transferFact(State, F);
602-
else
603-
for (const Fact *F : llvm::reverse(Facts))
610+
PerPointStates[{Block, F}] = State;
611+
}
612+
} else {
613+
for (const Fact *F : llvm::reverse(Facts)) {
604614
State = transferFact(State, F);
615+
PerPointStates[{Block, F}] = State;
616+
}
617+
}
605618
return State;
606619
}
607620

@@ -769,6 +782,10 @@ class LoanPropagationAnalysis
769782
Factory.OriginMapFactory.add(In.Origins, DestOID, SrcLoans));
770783
}
771784

785+
LoanSet getLoans(OriginID OID, ProgramPoint P) {
786+
return getLoans(getState(P), OID);
787+
}
788+
772789
private:
773790
LoanSet getLoans(Lattice L, OriginID OID) {
774791
if (auto *Loans = L.Origins.lookup(OID))
@@ -779,7 +796,6 @@ class LoanPropagationAnalysis
779796

780797
// ========================================================================= //
781798
// TODO:
782-
// - Modifying loan propagation to answer `LoanSet getLoans(Origin O, Point P)`
783799
// - Modify loan expiry analysis to answer `bool isExpired(Loan L, Point P)`
784800
// - Modify origin liveness analysis to answer `bool isLive(Origin O, Point P)`
785801
// - Using the above three to perform the final error reporting.

0 commit comments

Comments
 (0)