clang 24.0.0git
PtrTypesSemantics.h
Go to the documentation of this file.
1//=======- PtrTypesSemantics.cpp ---------------------------------*- 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#ifndef LLVM_CLANG_ANALYZER_WEBKIT_PTRTYPESEMANTICS_H
10#define LLVM_CLANG_ANALYZER_WEBKIT_PTRTYPESEMANTICS_H
11
12#include "llvm/ADT/APInt.h"
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/DenseSet.h"
15#include "llvm/ADT/PointerUnion.h"
16#include <optional>
17#include <string>
18
19namespace clang {
21class CXXMethodDecl;
22class CXXRecordDecl;
23class Decl;
24class FunctionDecl;
25class NamedDecl;
26class QualType;
27class RecordType;
28class Stmt;
30class Type;
31class TypedefDecl;
32class VarDecl;
33
34// Ref-countability of a type is implicitly defined by Ref<T> and RefPtr<T>
35// implementation. It can be modeled as: type T having public methods ref() and
36// deref()
37
38// In WebKit there are two ref-counted templated smart pointers: RefPtr<T> and
39// Ref<T>.
40
41/// \returns CXXRecordDecl of the base if the type has ref as a public method,
42/// nullptr if not, std::nullopt if inconclusive.
43std::optional<const clang::CXXRecordDecl *>
45 llvm::StringRef NameToMatch);
46
47/// \returns true if \p Class is ref-countable, false if not, std::nullopt if
48/// inconclusive.
49std::optional<bool> isRefCountable(const clang::CXXRecordDecl *Class);
50
51/// \returns true if \p Class is checked-pointer compatible, false if not,
52/// std::nullopt if inconclusive.
53std::optional<bool> isCheckedPtrCapable(const clang::CXXRecordDecl *Class);
54
55/// \returns true if \p Class implements the CanBorrow protocol, meaning a
56/// Borrow<T> can be taken on it, false if not, std::nullopt if inconclusive.
57std::optional<bool> isBorrowable(const clang::CXXRecordDecl *Class);
58
59/// \returns true if \p Class is a Borrow<T>, false if not.
61
62/// \returns true if \p T is a Borrow<T>.
64
65/// \returns the innermost type reached by stripping every pointer/reference
66/// layer from \p T; \p T itself if it has none; a null type if \p T is null.
68
69/// \returns the type a Borrow<T> specialization \p T borrows, or a null type
70/// if \p T is not a template specialization whose first argument is a type.
72
73/// \returns true if a value of type \p T is a pointer/reference/view.
74bool isView(const clang::QualType T);
75
76/// \returns true if \p Class declares reference semantics structurally: it is
77/// annotated [[gsl::Pointer]] (explicitly, or by Sema's inference for
78/// standard types), derives from std::ranges::view_interface, is a standard
79/// iterator adaptor, or is nested inside such a class, as the iterators of
80/// standard views are.
82
83/// \returns true if \p Class is ref-counted, false if not.
85
86/// \returns true if \p Class is a CheckedPtr / CheckedRef, false if not.
88
89/// \returns true if \p Class is a RetainPtr, false if not.
91
92/// \returns true if \p Class is a weak smart pointer (WeakPtr, InlineWeakPtr,
93/// etc...), false if not.
95
96/// \returns true if \p Class is a smart pointer (RefPtr, WeakPtr, etc...),
97/// false if not.
99
100/// \returns true if \p Class is ref-countable AND not ref-counted, false if
101/// not, std::nullopt if inconclusive.
102std::optional<bool> isUncounted(const clang::QualType T);
103
104/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
105/// not, std::nullopt if inconclusive.
106std::optional<bool> isUnchecked(const clang::QualType T);
107
108/// An inter-procedural analysis facility that detects CF types with the
109/// underlying pointer type.
111 llvm::DenseMap<const RecordType *, const TypedefDecl *> CFPointees;
112 llvm::DenseSet<const Type *> RecordlessTypes;
113 bool IsARCEnabled{false};
114 bool DefaultSynthProperties{true};
115
116public:
118 void visitTypedef(const TypedefDecl *);
119 bool isUnretained(const QualType, bool ignoreARC = false);
120 bool isARCEnabled() const { return IsARCEnabled; }
121 bool defaultSynthProperties() const { return DefaultSynthProperties; }
123};
124
125/// \returns true if \p Class is ref-countable AND not ref-counted, false if
126/// not, std::nullopt if inconclusive.
127std::optional<bool> isUncounted(const clang::CXXRecordDecl* Class);
128
129/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
130/// not, std::nullopt if inconclusive.
131std::optional<bool> isUnchecked(const clang::CXXRecordDecl *Class);
132
133/// \returns true if \p T is either a raw pointer or reference to an uncounted
134/// class, false if not, std::nullopt if inconclusive.
135std::optional<bool> isUncountedPtr(const clang::QualType T);
136
137/// \returns true if \p T is either a raw pointer or reference to an unchecked
138/// class, false if not, std::nullopt if inconclusive.
139std::optional<bool> isUncheckedPtr(const clang::QualType T);
140
141/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or its
142/// variant, false if not.
144
145/// \returns true if \p T is a RetainPtr, false if not.
147
148/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or
149/// unique_ptr, false if not.
150bool isOwnerPtrType(const clang::QualType T);
151
152/// \returns true if \p F creates ref-countable object from uncounted parameter,
153/// false if not.
155
156/// \returns true if \p F creates checked ptr object from uncounted parameter,
157/// false if not.
159
160/// \returns true if \p F creates ref-countable or checked ptr object from
161/// uncounted parameter, false if not.
163
164/// \returns true if \p F is std::move or WTF::move.
166
167/// \returns true if \p Name is RefPtr, Ref, or its variant, false if not.
168bool isRefType(const std::string &Name);
169
170/// \returns true if \p Name is CheckedRef or CheckedPtr, false if not.
171bool isCheckedPtr(const std::string &Name);
172
173/// \returns true if \p Name is Borrow, false if not.
174bool isBorrow(const std::string &Name);
175
176/// \returns true if \p Name is RetainPtr or its variant, false if not.
177bool isRetainPtrOrOSPtr(const std::string &Name);
178
179/// \returns true if \p Name is an owning smart pointer such as Ref, CheckedPtr,
180/// and unique_ptr.
181bool isOwnerPtr(const std::string &Name);
182
183/// \returns true if \p Name is unique_ptr, UniqueRef, or LazyUniqueRef.
184bool isUniquePtr(const std::string &Name);
185
186/// \returns true if \p Name is a smart pointer type name, false if not.
187bool isSmartPtrClass(const std::string &Name);
188
189/// \returns true if \p M is getter of a ref-counted class, false if not.
190std::optional<bool> isGetterOfSafePtr(const clang::CXXMethodDecl *Method);
191
192/// \returns true if \p M is a getter of unique_ptr, UniqueRef, or
193/// LazyUniqueRef, false if not.
194bool isGetterOfUniquePtr(const clang::CXXMethodDecl *Method);
195
196/// \returns true if \p F is a conversion between ref-countable or ref-counted
197/// pointer types.
198bool isPtrConversion(const FunctionDecl *F);
199
200/// \returns true if \p F's return type is annotated with
201/// [[clang::annotate_type("webkit.nodelete")]].
202bool isNoDeleteFunction(const FunctionDecl *F);
203
204/// \returns true if \p F is a builtin function which is considered trivial.
205bool isTrivialBuiltinFunction(const FunctionDecl *F);
206
207/// \returns true if \p F is a static singleton function.
208bool isSingleton(const NamedDecl *F);
209
210/// An inter-procedural analysis facility that detects functions with "trivial"
211/// behavior with respect to reference counting, such as simple field getters.
213public:
214 /// \returns true if \p D is a "trivial" function.
215 bool isTrivial(const Decl *D, const Stmt **OffendingStmt = nullptr) const {
216 return isTrivialImpl(D, TheCache, OffendingStmt);
217 }
218 bool isTrivial(const Stmt *S, const Stmt **OffendingStmt = nullptr) const {
219 return isTrivialImpl(S, TheCache, OffendingStmt);
220 }
221 bool hasTrivialDtor(const VarDecl *VD) const {
222 return hasTrivialDtorImpl(VD, TheCache);
223 }
224
225private:
227
228 using CacheTy =
229 llvm::DenseMap<llvm::PointerUnion<const Decl *, const Stmt *>, bool>;
230 mutable CacheTy TheCache{};
231
232 static bool isTrivialImpl(const Decl *D, CacheTy &Cache, const Stmt **);
233 static bool isTrivialImpl(const Stmt *S, CacheTy &Cache, const Stmt **);
234 static bool hasTrivialDtorImpl(const VarDecl *VD, CacheTy &Cache);
235};
236
237} // namespace clang
238
239#endif
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2150
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Represents a function declaration or definition.
Definition Decl.h:2059
This represents a decl that may have a name.
Definition Decl.h:275
A (possibly-)qualified type.
Definition TypeBase.h:938
An inter-procedural analysis facility that detects CF types with the underlying pointer type.
const TypedefDecl * getCanonicalDecl(QualType)
bool isUnretained(const QualType, bool ignoreARC=false)
void visitTranslationUnitDecl(const TranslationUnitDecl *)
void visitTypedef(const TypedefDecl *)
Stmt - This represents one statement.
Definition Stmt.h:85
The top declaration context.
Definition Decl.h:106
An inter-procedural analysis facility that detects functions with "trivial" behavior with respect to ...
bool isTrivial(const Stmt *S, const Stmt **OffendingStmt=nullptr) const
bool hasTrivialDtor(const VarDecl *VD) const
bool isTrivial(const Decl *D, const Stmt **OffendingStmt=nullptr) const
The base class of the type hierarchy.
Definition TypeBase.h:1879
Represents the declaration of a typedef-name via the 'typedef' type specifier.
Definition Decl.h:3802
Represents a variable declaration or definition.
Definition Decl.h:933
Top level wrappers for InstallAPI frontend operations.
bool isCtorOfSafePtr(const clang::FunctionDecl *F)
bool isTrivialBuiltinFunction(const FunctionDecl *F)
bool isPtrConversion(const FunctionDecl *F)
QualType pointeeType(QualType T)
std::optional< bool > isCheckedPtrCapable(const clang::CXXRecordDecl *R)
std::optional< bool > isUnchecked(const QualType T)
QualType borrowedType(QualType T)
bool isCtorOfRefCounted(const clang::FunctionDecl *F)
bool isRefOrCheckedPtrType(const clang::QualType T)
bool isView(const clang::QualType T)
bool isRetainPtrOrOSPtrType(const clang::QualType T)
bool isGetterOfUniquePtr(const CXXMethodDecl *M)
bool isBorrow(const clang::CXXRecordDecl *R)
std::optional< bool > isBorrowable(const clang::CXXRecordDecl *R)
bool isOwnerPtr(const std::string &Name)
bool isStdView(const clang::CXXRecordDecl *R)
const FunctionProtoType * T
std::optional< bool > isRefCountable(const clang::CXXRecordDecl *R)
std::optional< const clang::CXXRecordDecl * > hasPublicMethodInBase(const CXXBaseSpecifier *Base, StringRef NameToMatch)
bool isSmartPtrClass(const std::string &Name)
bool isRefCounted(const CXXRecordDecl *R)
bool isNoDeleteFunction(const FunctionDecl *F)
bool isOwnerPtrType(const clang::QualType T)
bool isSmartPtr(const CXXRecordDecl *R)
std::optional< bool > isGetterOfSafePtr(const CXXMethodDecl *M)
bool isRetainPtrOrOSPtr(const std::string &Name)
bool isRefType(const std::string &Name)
bool isUniquePtr(const std::string &Name)
std::optional< bool > isUncountedPtr(const QualType T)
bool isCtorOfCheckedPtr(const clang::FunctionDecl *F)
bool isSingleton(const NamedDecl *F)
bool isCheckedPtr(const std::string &Name)
bool isStdOrWTFMove(const clang::FunctionDecl *F)
bool isBorrowType(const clang::QualType T)
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:6024
std::optional< bool > isUncounted(const QualType T)
std::optional< bool > isUncheckedPtr(const QualType T)
bool isWeakPtr(const CXXRecordDecl *R)