1919#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H
2020#include " clang/Analysis/AnalysisDeclContext.h"
2121#include " clang/Analysis/CFG.h"
22+ #include " clang/Basic/SourceLocation.h"
23+ #include " llvm/ADT/DenseMapInfo.h"
24+ #include " llvm/ADT/ImmutableMap.h"
2225#include " llvm/ADT/ImmutableSet.h"
2326#include " llvm/ADT/StringMap.h"
2427#include < memory>
2528
2629namespace clang ::lifetimes {
2730
31+ // / Enum to track the confidence level of a potential error.
32+ enum class Confidence {
33+ None,
34+ Maybe, // Reported as a potential error (-Wlifetime-safety-strict)
35+ Definite // Reported as a definite error (-Wlifetime-safety-permissive)
36+ };
37+
38+ class LifetimeSafetyReporter {
39+ public:
40+ LifetimeSafetyReporter () = default ;
41+ virtual ~LifetimeSafetyReporter () = default ;
42+
43+ virtual void reportUseAfterFree (const Expr *IssueExpr, const Expr *UseExpr,
44+ SourceLocation FreeLoc,
45+ Confidence Confidence) {}
46+ };
47+
2848// / The main entry point for the analysis.
29- void runLifetimeSafetyAnalysis (AnalysisDeclContext &AC);
49+ void runLifetimeSafetyAnalysis (AnalysisDeclContext &AC,
50+ LifetimeSafetyReporter *Reporter);
3051
3152namespace internal {
3253// Forward declarations of internal types.
@@ -53,6 +74,7 @@ template <typename Tag> struct ID {
5374 IDBuilder.AddInteger (Value);
5475 }
5576};
77+
5678template <typename Tag>
5779inline llvm::raw_ostream &operator <<(llvm::raw_ostream &OS, ID<Tag> ID) {
5880 return OS << ID.Value ;
@@ -78,7 +100,8 @@ using ProgramPoint = const Fact *;
78100// / encapsulates the various dataflow analyses.
79101class LifetimeSafetyAnalysis {
80102public:
81- LifetimeSafetyAnalysis (AnalysisDeclContext &AC);
103+ LifetimeSafetyAnalysis (AnalysisDeclContext &AC,
104+ LifetimeSafetyReporter *Reporter);
82105 ~LifetimeSafetyAnalysis ();
83106
84107 void run ();
@@ -87,7 +110,7 @@ class LifetimeSafetyAnalysis {
87110 LoanSet getLoansAtPoint (OriginID OID, ProgramPoint PP) const ;
88111
89112 // / Returns the set of loans that have expired at a specific program point.
90- LoanSet getExpiredLoansAtPoint (ProgramPoint PP) const ;
113+ std::vector<LoanID> getExpiredLoansAtPoint (ProgramPoint PP) const ;
91114
92115 // / Finds the OriginID for a given declaration.
93116 // / Returns a null optional if not found.
@@ -110,6 +133,7 @@ class LifetimeSafetyAnalysis {
110133
111134private:
112135 AnalysisDeclContext &AC;
136+ LifetimeSafetyReporter *Reporter;
113137 std::unique_ptr<LifetimeFactory> Factory;
114138 std::unique_ptr<FactManager> FactMgr;
115139 std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
@@ -118,4 +142,25 @@ class LifetimeSafetyAnalysis {
118142} // namespace internal
119143} // namespace clang::lifetimes
120144
145+ namespace llvm {
146+ template <typename Tag>
147+ struct DenseMapInfo <clang::lifetimes::internal::ID<Tag>> {
148+ using ID = clang::lifetimes::internal::ID<Tag>;
149+
150+ static inline ID getEmptyKey () {
151+ return {DenseMapInfo<uint32_t >::getEmptyKey ()};
152+ }
153+
154+ static inline ID getTombstoneKey () {
155+ return {DenseMapInfo<uint32_t >::getTombstoneKey ()};
156+ }
157+
158+ static unsigned getHashValue (const ID &Val) {
159+ return DenseMapInfo<uint32_t >::getHashValue (Val.Value );
160+ }
161+
162+ static bool isEqual (const ID &LHS, const ID &RHS) { return LHS == RHS; }
163+ };
164+ } // namespace llvm
165+
121166#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H
0 commit comments