clang 22.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;
31
32// Ref-countability of a type is implicitly defined by Ref<T> and RefPtr<T>
33// implementation. It can be modeled as: type T having public methods ref() and
34// deref()
35
36// In WebKit there are two ref-counted templated smart pointers: RefPtr<T> and
37// Ref<T>.
38
39/// \returns CXXRecordDecl of the base if the type has ref as a public method,
40/// nullptr if not, std::nullopt if inconclusive.
41std::optional<const clang::CXXRecordDecl *>
43 llvm::StringRef NameToMatch);
44
45/// \returns true if \p Class is ref-countable, false if not, std::nullopt if
46/// inconclusive.
47std::optional<bool> isRefCountable(const clang::CXXRecordDecl *Class);
48
49/// \returns true if \p Class is checked-pointer compatible, false if not,
50/// std::nullopt if inconclusive.
51std::optional<bool> isCheckedPtrCapable(const clang::CXXRecordDecl *Class);
52
53/// \returns true if \p Class is ref-counted, false if not.
55
56/// \returns true if \p Class is a CheckedPtr / CheckedRef, false if not.
58
59/// \returns true if \p Class is a RetainPtr, false if not.
61
62/// \returns true if \p Class is a smart pointer (RefPtr, WeakPtr, etc...),
63/// false if not.
65
66/// \returns true if \p Class is ref-countable AND not ref-counted, false if
67/// not, std::nullopt if inconclusive.
68std::optional<bool> isUncounted(const clang::QualType T);
69
70/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
71/// not, std::nullopt if inconclusive.
72std::optional<bool> isUnchecked(const clang::QualType T);
73
74/// An inter-procedural analysis facility that detects CF types with the
75/// underlying pointer type.
77 llvm::DenseSet<const RecordType *> CFPointees;
78 llvm::DenseSet<const Type *> RecordlessTypes;
79 bool IsARCEnabled{false};
80 bool DefaultSynthProperties{true};
81
82public:
84 void visitTypedef(const TypedefDecl *);
85 bool isUnretained(const QualType, bool ignoreARC = false);
86 bool isARCEnabled() const { return IsARCEnabled; }
87 bool defaultSynthProperties() const { return DefaultSynthProperties; }
88};
89
90/// \returns true if \p Class is NS or CF objects AND not retained, false if
91/// not, std::nullopt if inconclusive.
92std::optional<bool> isUnretained(const clang::QualType T, bool IsARCEnabled);
93
94/// \returns true if \p Class is ref-countable AND not ref-counted, false if
95/// not, std::nullopt if inconclusive.
96std::optional<bool> isUncounted(const clang::CXXRecordDecl* Class);
97
98/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
99/// not, std::nullopt if inconclusive.
100std::optional<bool> isUnchecked(const clang::CXXRecordDecl *Class);
101
102/// \returns true if \p T is either a raw pointer or reference to an uncounted
103/// class, false if not, std::nullopt if inconclusive.
104std::optional<bool> isUncountedPtr(const clang::QualType T);
105
106/// \returns true if \p T is either a raw pointer or reference to an unchecked
107/// class, false if not, std::nullopt if inconclusive.
108std::optional<bool> isUncheckedPtr(const clang::QualType T);
109
110/// \returns true if \p T is either a raw pointer or reference to an uncounted
111/// or unchecked class, false if not, std::nullopt if inconclusive.
112std::optional<bool> isUnsafePtr(const QualType T, bool IsArcEnabled);
113
114/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or its
115/// variant, false if not.
117
118/// \returns true if \p T is a RetainPtr, false if not.
119bool isRetainPtrType(const clang::QualType T);
120
121/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or
122/// unique_ptr, false if not.
123bool isOwnerPtrType(const clang::QualType T);
124
125/// \returns true if \p F creates ref-countable object from uncounted parameter,
126/// false if not.
128
129/// \returns true if \p F creates checked ptr object from uncounted parameter,
130/// false if not.
132
133/// \returns true if \p F creates ref-countable or checked ptr object from
134/// uncounted parameter, false if not.
136
137/// \returns true if \p Name is RefPtr, Ref, or its variant, false if not.
138bool isRefType(const std::string &Name);
139
140/// \returns true if \p Name is CheckedRef or CheckedPtr, false if not.
141bool isCheckedPtr(const std::string &Name);
142
143/// \returns true if \p Name is RetainPtr or its variant, false if not.
144bool isRetainPtr(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 is a builtin function which is considered trivial.
157bool isTrivialBuiltinFunction(const FunctionDecl *F);
158
159/// \returns true if \p F is a static singleton function.
160bool isSingleton(const NamedDecl *F);
161
162/// An inter-procedural analysis facility that detects functions with "trivial"
163/// behavior with respect to reference counting, such as simple field getters.
165public:
166 /// \returns true if \p D is a "trivial" function.
167 bool isTrivial(const Decl *D) const { return isTrivialImpl(D, TheCache); }
168 bool isTrivial(const Stmt *S) const { return isTrivialImpl(S, TheCache); }
169
170private:
172
173 using CacheTy =
174 llvm::DenseMap<llvm::PointerUnion<const Decl *, const Stmt *>, bool>;
175 mutable CacheTy TheCache{};
176
177 static bool isTrivialImpl(const Decl *D, CacheTy &Cache);
178 static bool isTrivialImpl(const Stmt *S, CacheTy &Cache);
179};
180
181} // namespace clang
182
183#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:2129
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:1999
This represents a decl that may have a name.
Definition Decl.h:273
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:85
The top declaration context.
Definition Decl.h:104
An inter-procedural analysis facility that detects functions with "trivial" behavior with respect to ...
bool isTrivial(const Stmt *S) const
bool isTrivial(const Decl *D) const
The base class of the type hierarchy.
Definition TypeBase.h:1833
Represents the declaration of a typedef-name via the 'typedef' type specifier.
Definition Decl.h:3664
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)
std::optional< bool > isUnsafePtr(const QualType T, bool IsArcEnabled)
bool isRetainPtrType(const clang::QualType T)
const FunctionProtoType * T
std::optional< bool > isUnretained(const QualType T, bool IsARCEnabled)
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 isOwnerPtrType(const clang::QualType T)
bool isSmartPtr(const CXXRecordDecl *R)
std::optional< bool > isGetterOfSafePtr(const CXXMethodDecl *M)
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)
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5874
std::optional< bool > isUncounted(const QualType T)
bool isRetainPtr(const std::string &Name)
std::optional< bool > isUncheckedPtr(const QualType T)