clang 23.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
18namespace clang {
20class CXXMethodDecl;
21class CXXRecordDecl;
22class Decl;
23class FunctionDecl;
24class NamedDecl;
25class QualType;
26class RecordType;
27class Stmt;
29class Type;
30class TypedefDecl;
31class VarDecl;
32
33// Ref-countability of a type is implicitly defined by Ref<T> and RefPtr<T>
34// implementation. It can be modeled as: type T having public methods ref() and
35// deref()
36
37// In WebKit there are two ref-counted templated smart pointers: RefPtr<T> and
38// Ref<T>.
39
40/// \returns CXXRecordDecl of the base if the type has ref as a public method,
41/// nullptr if not, std::nullopt if inconclusive.
42std::optional<const clang::CXXRecordDecl *>
44 llvm::StringRef NameToMatch);
45
46/// \returns true if \p Class is ref-countable, false if not, std::nullopt if
47/// inconclusive.
48std::optional<bool> isRefCountable(const clang::CXXRecordDecl *Class);
49
50/// \returns true if \p Class is checked-pointer compatible, false if not,
51/// std::nullopt if inconclusive.
52std::optional<bool> isCheckedPtrCapable(const clang::CXXRecordDecl *Class);
53
54/// \returns true if \p Class is ref-counted, false if not.
56
57/// \returns true if \p Class is a CheckedPtr / CheckedRef, false if not.
59
60/// \returns true if \p Class is a RetainPtr, false if not.
62
63/// \returns true if \p Class is a smart pointer (RefPtr, WeakPtr, etc...),
64/// false if not.
66
67/// \returns true if \p Class is ref-countable AND not ref-counted, false if
68/// not, std::nullopt if inconclusive.
69std::optional<bool> isUncounted(const clang::QualType T);
70
71/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
72/// not, std::nullopt if inconclusive.
73std::optional<bool> isUnchecked(const clang::QualType T);
74
75/// An inter-procedural analysis facility that detects CF types with the
76/// underlying pointer type.
78 llvm::DenseSet<const RecordType *> CFPointees;
79 llvm::DenseSet<const Type *> RecordlessTypes;
80 bool IsARCEnabled{false};
81 bool DefaultSynthProperties{true};
82
83public:
85 void visitTypedef(const TypedefDecl *);
86 bool isUnretained(const QualType, bool ignoreARC = false);
87 bool isARCEnabled() const { return IsARCEnabled; }
88 bool defaultSynthProperties() const { return DefaultSynthProperties; }
89};
90
91/// \returns true if \p Class is ref-countable AND not ref-counted, false if
92/// not, std::nullopt if inconclusive.
93std::optional<bool> isUncounted(const clang::CXXRecordDecl* Class);
94
95/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
96/// not, std::nullopt if inconclusive.
97std::optional<bool> isUnchecked(const clang::CXXRecordDecl *Class);
98
99/// \returns true if \p T is either a raw pointer or reference to an uncounted
100/// class, false if not, std::nullopt if inconclusive.
101std::optional<bool> isUncountedPtr(const clang::QualType T);
102
103/// \returns true if \p T is either a raw pointer or reference to an unchecked
104/// class, false if not, std::nullopt if inconclusive.
105std::optional<bool> isUncheckedPtr(const clang::QualType T);
106
107/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or its
108/// variant, false if not.
110
111/// \returns true if \p T is a RetainPtr, false if not.
113
114/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or
115/// unique_ptr, false if not.
116bool isOwnerPtrType(const clang::QualType T);
117
118/// \returns true if \p F creates ref-countable object from uncounted parameter,
119/// false if not.
121
122/// \returns true if \p F creates checked ptr object from uncounted parameter,
123/// false if not.
125
126/// \returns true if \p F creates ref-countable or checked ptr object from
127/// uncounted parameter, false if not.
129
130/// \returns true if \p F is std::move or WTF::move.
132
133/// \returns true if \p Name is RefPtr, Ref, or its variant, false if not.
134bool isRefType(const std::string &Name);
135
136/// \returns true if \p Name is CheckedRef or CheckedPtr, false if not.
137bool isCheckedPtr(const std::string &Name);
138
139/// \returns true if \p Name is RetainPtr or its variant, false if not.
140bool isRetainPtrOrOSPtr(const std::string &Name);
141
142/// \returns true if \p Name is an owning smar pointer such as Ref, CheckedPtr,
143/// and unique_ptr.
144bool isOwnerPtr(const std::string &Name);
145
146/// \returns true if \p Name is a smart pointer type name, false if not.
147bool isSmartPtrClass(const std::string &Name);
148
149/// \returns true if \p M is getter of a ref-counted class, false if not.
150std::optional<bool> isGetterOfSafePtr(const clang::CXXMethodDecl *Method);
151
152/// \returns true if \p F is a conversion between ref-countable or ref-counted
153/// pointer types.
154bool isPtrConversion(const FunctionDecl *F);
155
156/// \returns true if \p F's return type is annotated with
157/// [[clang::annotate_type("webkit.nodelete")]].
158bool isNoDeleteFunction(const FunctionDecl *F);
159
160/// \returns true if \p F is a builtin function which is considered trivial.
161bool isTrivialBuiltinFunction(const FunctionDecl *F);
162
163/// \returns true if \p F is a static singleton function.
164bool isSingleton(const NamedDecl *F);
165
166/// An inter-procedural analysis facility that detects functions with "trivial"
167/// behavior with respect to reference counting, such as simple field getters.
169public:
170 /// \returns true if \p D is a "trivial" function.
171 bool isTrivial(const Decl *D, const Stmt **OffendingStmt = nullptr) const {
172 return isTrivialImpl(D, TheCache, OffendingStmt);
173 }
174 bool isTrivial(const Stmt *S, const Stmt **OffendingStmt = nullptr) const {
175 return isTrivialImpl(S, TheCache, OffendingStmt);
176 }
177 bool hasTrivialDtor(const VarDecl *VD) const {
178 return hasTrivialDtorImpl(VD, TheCache);
179 }
180
181private:
183
184 using CacheTy =
185 llvm::DenseMap<llvm::PointerUnion<const Decl *, const Stmt *>, bool>;
186 mutable CacheTy TheCache{};
187
188 static bool isTrivialImpl(const Decl *D, CacheTy &Cache, const Stmt **);
189 static bool isTrivialImpl(const Stmt *S, CacheTy &Cache, const Stmt **);
190 static bool hasTrivialDtorImpl(const VarDecl *VD, CacheTy &Cache);
191};
192
193} // namespace clang
194
195#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:2136
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:2000
This represents a decl that may have a name.
Definition Decl.h:274
A (possibly-)qualified type.
Definition TypeBase.h:937
An inter-procedural analysis facility that detects CF types with the underlying pointer type.
bool isUnretained(const QualType, bool ignoreARC=false)
void visitTranslationUnitDecl(const TranslationUnitDecl *)
void visitTypedef(const TypedefDecl *)
Stmt - This represents one statement.
Definition Stmt.h:86
The top declaration context.
Definition Decl.h:105
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:1839
Represents the declaration of a typedef-name via the 'typedef' type specifier.
Definition Decl.h:3667
Represents a variable declaration or definition.
Definition Decl.h:926
The JSON file list parser is used to communicate input to InstallAPI.
bool isCtorOfSafePtr(const clang::FunctionDecl *F)
bool isTrivialBuiltinFunction(const FunctionDecl *F)
bool isPtrConversion(const FunctionDecl *F)
std::optional< bool > isCheckedPtrCapable(const clang::CXXRecordDecl *R)
std::optional< bool > isUnchecked(const QualType T)
bool isCtorOfRefCounted(const clang::FunctionDecl *F)
bool isRefOrCheckedPtrType(const clang::QualType T)
bool isRetainPtrOrOSPtrType(const clang::QualType T)
bool isOwnerPtr(const std::string &Name)
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)
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)
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5925
std::optional< bool > isUncounted(const QualType T)
std::optional< bool > isUncheckedPtr(const QualType T)