clang 22.0.0git
LiveOrigins.h
Go to the documentation of this file.
1//===- LiveOrigins.h - Live Origins Analysis -------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the LiveOriginAnalysis, a backward dataflow analysis that
10// determines which origins are "live" at each program point. An origin is
11// "live" at a program point if there's a potential future use of a pointer it
12// is associated with. Liveness is "generated" by a use of an origin (e.g., a
13// `UseFact` from a read of a pointer) and is "killed" (i.e., it stops being
14// live) when the origin is replaced by flowing a different origin into it
15// (e.g., an OriginFlow from an assignment that kills the destination).
16//
17// This information is used for detecting use-after-free errors, as it allows us
18// to check if a live origin holds a loan to an object that has already expired.
19//
20//===----------------------------------------------------------------------===//
21#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
22#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
23
27#include "clang/Analysis/CFG.h"
28#include "llvm/ADT/FoldingSet.h"
29#include "llvm/ADT/ImmutableMap.h"
30#include "llvm/Support/Debug.h"
31
33
35 ::llvm::PointerUnion<const UseFact *, const OriginEscapesFact *>;
36
37enum class LivenessKind : uint8_t {
38 Dead, // Not alive
39 Maybe, // Live on some path but not all paths (may-be-live)
40 Must // Live on all paths (must-be-live)
41};
42
43/// Information about why an origin is live at a program point.
45 /// The use that makes the origin live. If liveness is propagated from
46 /// multiple uses along different paths, this will point to the use appearing
47 /// earlier in the translation unit.
48 /// This is 'null' when the origin is not live.
50
51 /// The kind of liveness of the origin.
52 /// `Must`: The origin is live on all control-flow paths from the current
53 /// point to the function's exit (i.e. the current point is dominated by a set
54 /// of uses).
55 /// `Maybe`: indicates it is live on some but not all paths.
56 ///
57 /// This determines the diagnostic's confidence level.
58 /// `Must`-be-alive at expiration implies a definite use-after-free,
59 /// while `Maybe`-be-alive suggests a potential one on some paths.
61
64
65 bool operator==(const LivenessInfo &Other) const {
66 return CausingFact == Other.CausingFact && Kind == Other.Kind;
67 }
68 bool operator!=(const LivenessInfo &Other) const { return !(*this == Other); }
69
70 void Profile(llvm::FoldingSetNodeID &IDBuilder) const {
71 IDBuilder.AddPointer(CausingFact.getOpaqueValue());
72 IDBuilder.Add(Kind);
73 }
74};
75
76using LivenessMap = llvm::ImmutableMap<OriginID, LivenessInfo>;
77
79public:
81 LivenessMap::Factory &SF);
83
84 /// Returns the set of origins that are live at a specific program point,
85 /// along with the the details of the liveness.
87
88 // Dump liveness values on all test points in the program.
89 void dump(llvm::raw_ostream &OS,
90 llvm::StringMap<ProgramPoint> TestPoints) const;
91
92private:
93 class Impl;
94 std::unique_ptr<Impl> PImpl;
95};
96
97} // namespace clang::lifetimes::internal
98
99#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1222
void dump(llvm::raw_ostream &OS, llvm::StringMap< ProgramPoint > TestPoints) const
LiveOriginsAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F, LivenessMap::Factory &SF)
LivenessMap getLiveOriginsAt(ProgramPoint P) const
Returns the set of origins that are live at a specific program point, along with the the details of t...
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
Definition Facts.h:82
::llvm::PointerUnion< const UseFact *, const OriginEscapesFact * > CausingFactType
Definition LiveOrigins.h:34
llvm::ImmutableMap< OriginID, LivenessInfo > LivenessMap
Definition LiveOrigins.h:76
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Other
Other implicit parameter.
Definition Decl.h:1746
LivenessInfo(CausingFactType CF, LivenessKind K)
Definition LiveOrigins.h:63
LivenessKind Kind
The kind of liveness of the origin.
Definition LiveOrigins.h:60
CausingFactType CausingFact
The use that makes the origin live.
Definition LiveOrigins.h:49
bool operator==(const LivenessInfo &Other) const
Definition LiveOrigins.h:65
void Profile(llvm::FoldingSetNodeID &IDBuilder) const
Definition LiveOrigins.h:70
bool operator!=(const LivenessInfo &Other) const
Definition LiveOrigins.h:68