clang 20.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/PointerUnion.h"
15#include <optional>
16
17namespace clang {
18class CXXBaseSpecifier;
19class CXXMethodDecl;
20class CXXRecordDecl;
21class Decl;
22class FunctionDecl;
23class QualType;
24class Stmt;
25class Type;
26
27// Ref-countability of a type is implicitly defined by Ref<T> and RefPtr<T>
28// implementation. It can be modeled as: type T having public methods ref() and
29// deref()
30
31// In WebKit there are two ref-counted templated smart pointers: RefPtr<T> and
32// Ref<T>.
33
34/// \returns CXXRecordDecl of the base if the type has ref as a public method,
35/// nullptr if not, std::nullopt if inconclusive.
36std::optional<const clang::CXXRecordDecl *>
38 llvm::StringRef NameToMatch);
39
40/// \returns true if \p Class is ref-countable, false if not, std::nullopt if
41/// inconclusive.
42std::optional<bool> isRefCountable(const clang::CXXRecordDecl *Class);
43
44/// \returns true if \p Class is checked-pointer compatible, false if not,
45/// std::nullopt if inconclusive.
46std::optional<bool> isCheckedPtrCapable(const clang::CXXRecordDecl *Class);
47
48/// \returns true if \p Class is ref-counted, false if not.
50
51/// \returns true if \p Class is a CheckedPtr / CheckedRef, false if not.
53
54/// \returns true if \p Class is ref-countable AND not ref-counted, false if
55/// not, std::nullopt if inconclusive.
56std::optional<bool> isUncounted(const clang::QualType T);
57
58/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
59/// not, std::nullopt if inconclusive.
60std::optional<bool> isUnchecked(const clang::QualType T);
61
62/// \returns true if \p Class is ref-countable AND not ref-counted, false if
63/// not, std::nullopt if inconclusive.
64std::optional<bool> isUncounted(const clang::CXXRecordDecl* Class);
65
66/// \returns true if \p Class is CheckedPtr capable AND not checked, false if
67/// not, std::nullopt if inconclusive.
68std::optional<bool> isUnchecked(const clang::CXXRecordDecl *Class);
69
70/// \returns true if \p T is either a raw pointer or reference to an uncounted
71/// class, false if not, std::nullopt if inconclusive.
72std::optional<bool> isUncountedPtr(const clang::QualType T);
73
74/// \returns true if \p T is either a raw pointer or reference to an unchecked
75/// class, false if not, std::nullopt if inconclusive.
76std::optional<bool> isUncheckedPtr(const clang::QualType T);
77
78/// \returns true if \p T is either a raw pointer or reference to an uncounted
79/// or unchecked class, false if not, std::nullopt if inconclusive.
80std::optional<bool> isUnsafePtr(const QualType T);
81
82/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or its
83/// variant, false if not.
85
86/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or
87/// unique_ptr, false if not.
89
90/// \returns true if \p F creates ref-countable object from uncounted parameter,
91/// false if not.
93
94/// \returns true if \p F creates checked ptr object from uncounted parameter,
95/// false if not.
97
98/// \returns true if \p F creates ref-countable or checked ptr object from
99/// uncounted parameter, false if not.
101
102/// \returns true if \p Name is RefPtr, Ref, or its variant, false if not.
103bool isRefType(const std::string &Name);
104
105/// \returns true if \p Name is CheckedRef or CheckedPtr, false if not.
106bool isCheckedPtr(const std::string &Name);
107
108/// \returns true if \p M is getter of a ref-counted class, false if not.
109std::optional<bool> isGetterOfSafePtr(const clang::CXXMethodDecl *Method);
110
111/// \returns true if \p F is a conversion between ref-countable or ref-counted
112/// pointer types.
113bool isPtrConversion(const FunctionDecl *F);
114
115/// \returns true if \p F is a static singleton function.
116bool isSingleton(const FunctionDecl *F);
117
118/// An inter-procedural analysis facility that detects functions with "trivial"
119/// behavior with respect to reference counting, such as simple field getters.
121public:
122 /// \returns true if \p D is a "trivial" function.
123 bool isTrivial(const Decl *D) const { return isTrivialImpl(D, TheCache); }
124 bool isTrivial(const Stmt *S) const { return isTrivialImpl(S, TheCache); }
125
126private:
128
129 using CacheTy =
130 llvm::DenseMap<llvm::PointerUnion<const Decl *, const Stmt *>, bool>;
131 mutable CacheTy TheCache{};
132
133 static bool isTrivialImpl(const Decl *D, CacheTy &Cache);
134 static bool isTrivialImpl(const Stmt *S, CacheTy &Cache);
135};
136
137} // namespace clang
138
139#endif
MatchType Type
const Decl * D
TypePropertyCache< Private > Cache
Definition: Type.cpp:4547
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:2078
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:1935
A (possibly-)qualified type.
Definition: Type.h:929
Stmt - This represents one statement.
Definition: Stmt.h:84
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
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
The JSON file list parser is used to communicate input to InstallAPI.
bool isCtorOfSafePtr(const clang::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)
std::optional< bool > isRefCountable(const clang::CXXRecordDecl *R)
std::optional< const clang::CXXRecordDecl * > hasPublicMethodInBase(const CXXBaseSpecifier *Base, StringRef NameToMatch)
bool isSingleton(const FunctionDecl *F)
bool isRefCounted(const CXXRecordDecl *R)
bool isOwnerPtrType(const clang::QualType T)
bool isSafePtrType(const clang::QualType T)
std::optional< bool > isGetterOfSafePtr(const CXXMethodDecl *M)
bool isRefType(const std::string &Name)
const FunctionProtoType * T
std::optional< bool > isUncountedPtr(const QualType T)
bool isCtorOfCheckedPtr(const clang::FunctionDecl *F)
bool isCheckedPtr(const std::string &Name)
std::optional< bool > isUnsafePtr(const QualType T)
@ Class
The "class" keyword introduces the elaborated-type-specifier.
std::optional< bool > isUncounted(const QualType T)
std::optional< bool > isUncheckedPtr(const QualType T)