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#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 is ref-counted, false if not.
57
58/// \returns true if \p Class is a CheckedPtr / CheckedRef, false if not.
60
61/// \returns true if \p Class is a RetainPtr, false if not.
63
64/// \returns true if \p Class is a smart pointer (RefPtr, WeakPtr, etc...),
65/// false if not.
67
68/// \returns true if \p Class is ref-countable AND not ref-counted, false if
69/// not, std::nullopt if inconclusive.
70std::optional<bool> isUncounted(const clang::QualType T);
71
72/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
73/// not, std::nullopt if inconclusive.
74std::optional<bool> isUnchecked(const clang::QualType T);
75
76/// An inter-procedural analysis facility that detects CF types with the
77/// underlying pointer type.
79 llvm::DenseSet<const RecordType *> CFPointees;
80 llvm::DenseSet<const Type *> RecordlessTypes;
81 bool IsARCEnabled{false};
82 bool DefaultSynthProperties{true};
83
84public:
86 void visitTypedef(const TypedefDecl *);
87 bool isUnretained(const QualType, bool ignoreARC = false);
88 bool isARCEnabled() const { return IsARCEnabled; }
89 bool defaultSynthProperties() const { return DefaultSynthProperties; }
90};
91
92/// \returns true if \p Class is ref-countable AND not ref-counted, false if
93/// not, std::nullopt if inconclusive.
94std::optional<bool> isUncounted(const clang::CXXRecordDecl* Class);
95
96/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
97/// not, std::nullopt if inconclusive.
98std::optional<bool> isUnchecked(const clang::CXXRecordDecl *Class);
99
100/// \returns true if \p T is either a raw pointer or reference to an uncounted
101/// class, false if not, std::nullopt if inconclusive.
102std::optional<bool> isUncountedPtr(const clang::QualType T);
103
104/// \returns true if \p T is either a raw pointer or reference to an unchecked
105/// class, false if not, std::nullopt if inconclusive.
106std::optional<bool> isUncheckedPtr(const clang::QualType T);
107
108/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or its
109/// variant, false if not.
111
112/// \returns true if \p T is a RetainPtr, false if not.
114
115/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or
116/// unique_ptr, false if not.
117bool isOwnerPtrType(const clang::QualType T);
118
119/// \returns true if \p F creates ref-countable object from uncounted parameter,
120/// false if not.
122
123/// \returns true if \p F creates checked ptr object from uncounted parameter,
124/// false if not.
126
127/// \returns true if \p F creates ref-countable or checked ptr object from
128/// uncounted parameter, false if not.
130
131/// \returns true if \p F is std::move or WTF::move.
133
134/// \returns true if \p Name is RefPtr, Ref, or its variant, false if not.
135bool isRefType(const std::string &Name);
136
137/// \returns true if \p Name is CheckedRef or CheckedPtr, false if not.
138bool isCheckedPtr(const std::string &Name);
139
140/// \returns true if \p Name is RetainPtr or its variant, false if not.
141bool isRetainPtrOrOSPtr(const std::string &Name);
142
143/// \returns true if \p Name is an owning smar pointer such as Ref, CheckedPtr,
144/// and unique_ptr.
145bool isOwnerPtr(const std::string &Name);
146
147/// \returns true if \p Name is a smart pointer type name, false if not.
148bool isSmartPtrClass(const std::string &Name);
149
150/// \returns true if \p M is getter of a ref-counted class, false if not.
151std::optional<bool> isGetterOfSafePtr(const clang::CXXMethodDecl *Method);
152
153/// \returns true if \p F is a conversion between ref-countable or ref-counted
154/// pointer types.
155bool isPtrConversion(const FunctionDecl *F);
156
157/// \returns true if \p F's return type is annotated with
158/// [[clang::annotate_type("webkit.nodelete")]].
159bool isNoDeleteFunction(const FunctionDecl *F);
160
161/// \returns true if \p F is a builtin function which is considered trivial.
162bool isTrivialBuiltinFunction(const FunctionDecl *F);
163
164/// \returns true if \p F is a static singleton function.
165bool isSingleton(const NamedDecl *F);
166
167/// An inter-procedural analysis facility that detects functions with "trivial"
168/// behavior with respect to reference counting, such as simple field getters.
170public:
171 /// \returns true if \p D is a "trivial" function.
172 bool isTrivial(const Decl *D, const Stmt **OffendingStmt = nullptr) const {
173 return isTrivialImpl(D, TheCache, OffendingStmt);
174 }
175 bool isTrivial(const Stmt *S, const Stmt **OffendingStmt = nullptr) const {
176 return isTrivialImpl(S, TheCache, OffendingStmt);
177 }
178 bool hasTrivialDtor(const VarDecl *VD) const {
179 return hasTrivialDtorImpl(VD, TheCache);
180 }
181
182private:
184
185 using CacheTy =
186 llvm::DenseMap<llvm::PointerUnion<const Decl *, const Stmt *>, bool>;
187 mutable CacheTy TheCache{};
188
189 static bool isTrivialImpl(const Decl *D, CacheTy &Cache, const Stmt **);
190 static bool isTrivialImpl(const Stmt *S, CacheTy &Cache, const Stmt **);
191 static bool hasTrivialDtorImpl(const VarDecl *VD, CacheTy &Cache);
192};
193
194} // namespace clang
195
196#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:2132
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:2018
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:1871
Represents the declaration of a typedef-name via the 'typedef' type specifier.
Definition Decl.h:3685
Represents a variable declaration or definition.
Definition Decl.h:924
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:5972
std::optional< bool > isUncounted(const QualType T)
std::optional< bool > isUncheckedPtr(const QualType T)