[clang] Introduce intra-procedural lifetime analysis in Clang (PR #142313)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 16 02:03:07 PDT 2025
================
@@ -0,0 +1,728 @@
+#include "clang/Analysis/Analyses/LifetimeSafety.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/StmtVisitor.h"
+#include "clang/AST/Type.h"
+#include "clang/Analysis/AnalysisDeclContext.h"
+#include "clang/Analysis/CFG.h"
+#include "clang/Analysis/FlowSensitive/DataflowWorklist.h"
+#include "llvm/ADT/ImmutableMap.h"
+#include "llvm/ADT/ImmutableSet.h"
+#include "llvm/ADT/PointerUnion.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/TimeProfiler.h"
+#include <vector>
+
+namespace clang {
+namespace {
+
+struct Point {
+ const clang::CFGBlock *Block;
+ /// Index into Block->Elements().
+ unsigned ElementIndex;
+
+ Point(const clang::CFGBlock *B = nullptr, unsigned Idx = 0)
+ : Block(B), ElementIndex(Idx) {}
+
+ bool operator==(const Point &Other) const {
+ return Block == Other.Block && ElementIndex == Other.ElementIndex;
+ }
+};
+
+/// Represents the storage location being borrowed, e.g., a specific stack
+/// variable.
+/// TODO: Handle member accesseslike `s.y`.
+struct Path {
+ const clang::ValueDecl *D;
+
+ enum class Kind : uint8_t {
----------------
Xazax-hun wrote:
> Each of the entities like stack variable, temporaries, fields (of stack variables) would need to be handled slightly differently and this is only where this PathKind would be used.
I am not yet convinced that we will actually need this, but this may be because we only have one kind implemented so far:
```
if (LoanPath.PathKind == AccessPath::Kind::StackVariable) {
if (LoanPath.D == DestructedVD) {
```
Here, I'd imagine when we issue the ExpireLoan, we know exactly what kind of entity is expiring. The AST/CFG should tell us if it was a stack variable, a heap location, or a temporary. After that, I'd expect that the `AccessPath` should have all the information we need to check what Loans should expire here (e.g., the fields of an expired loan should also expire).
https://github.com/llvm/llvm-project/pull/142313
More information about the cfe-commits
mailing list