| 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 |
|
| 18 |
namespace clang { |
| 19 |
class CXXBaseSpecifier; |
| 20 |
class CXXMethodDecl; |
| 21 |
class CXXRecordDecl; |
| 22 |
class Decl; |
| 23 |
class FunctionDecl; |
| 24 |
class NamedDecl; |
| 25 |
class QualType; |
| 26 |
class RecordType; |
| 27 |
class Stmt; |
| 28 |
class TranslationUnitDecl; |
| 29 |
class Type; |
| 30 |
class 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. |
| 41 |
std::optional<const clang::CXXRecordDecl *> |
| 42 |
hasPublicMethodInBase(const CXXBaseSpecifier *Base, |
| 43 |
llvm::StringRef NameToMatch); |
| 44 |
|
| 45 |
/// \returns true if \p Class is ref-countable, false if not, std::nullopt if |
| 46 |
/// inconclusive. |
| 47 |
std::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. |
| 51 |
std::optional<bool> isCheckedPtrCapable(const clang::CXXRecordDecl *Class); |
| 52 |
|
| 53 |
/// \returns true if \p Class is ref-counted, false if not. |
| 54 |
bool isRefCounted(const clang::CXXRecordDecl *Class); |
| 55 |
|
| 56 |
/// \returns true if \p Class is a CheckedPtr / CheckedRef, false if not. |
| 57 |
bool isCheckedPtr(const clang::CXXRecordDecl *Class); |
| 58 |
|
| 59 |
/// \returns true if \p Class is a RetainPtr, false if not. |
| 60 |
bool isRetainPtrOrOSPtr(const clang::CXXRecordDecl *Class); |
| 61 |
|
| 62 |
/// \returns true if \p Class is a smart pointer (RefPtr, WeakPtr, etc...), |
| 63 |
/// false if not. |
| 64 |
bool isSmartPtr(const clang::CXXRecordDecl *Class); |
| 65 |
|
| 66 |
/// \returns true if \p Class is ref-countable AND not ref-counted, false if |
| 67 |
/// not, std::nullopt if inconclusive. |
| 68 |
std::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. |
| 72 |
std::optional<bool> isUnchecked(const clang::QualType T); |
| 73 |
|
| 74 |
/// An inter-procedural analysis facility that detects CF types with the |
| 75 |
/// underlying pointer type. |
| 76 |
class RetainTypeChecker { |
| 77 |
llvm::DenseSet<const RecordType *> CFPointees; |
| 78 |
llvm::DenseSet<const Type *> RecordlessTypes; |
| 79 |
bool IsARCEnabled{false}; |
| 80 |
bool DefaultSynthProperties{true}; |
| 81 |
|
| 82 |
public: |
| 83 |
void visitTranslationUnitDecl(const TranslationUnitDecl *); |
| 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 ref-countable AND not ref-counted, false if |
| 91 |
/// not, std::nullopt if inconclusive. |
| 92 |
std::optional<bool> isUncounted(const clang::CXXRecordDecl* Class); |
| 93 |
|
| 94 |
/// \returns true if \p Class is CheckedPtr capable AND not checked, false if |
| 95 |
/// not, std::nullopt if inconclusive. |
| 96 |
std::optional<bool> isUnchecked(const clang::CXXRecordDecl *Class); |
| 97 |
|
| 98 |
/// \returns true if \p T is either a raw pointer or reference to an uncounted |
| 99 |
/// class, false if not, std::nullopt if inconclusive. |
| 100 |
std::optional<bool> isUncountedPtr(const clang::QualType T); |
| 101 |
|
| 102 |
/// \returns true if \p T is either a raw pointer or reference to an unchecked |
| 103 |
/// class, false if not, std::nullopt if inconclusive. |
| 104 |
std::optional<bool> isUncheckedPtr(const clang::QualType T); |
| 105 |
|
| 106 |
/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or its |
| 107 |
/// variant, false if not. |
| 108 |
bool isRefOrCheckedPtrType(const clang::QualType T); |
| 109 |
|
| 110 |
/// \returns true if \p T is a RetainPtr, false if not. |
| 111 |
bool isRetainPtrOrOSPtrType(const clang::QualType T); |
| 112 |
|
| 113 |
/// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or |
| 114 |
/// unique_ptr, false if not. |
| 115 |
bool isOwnerPtrType(const clang::QualType T); |
| 116 |
|
| 117 |
/// \returns true if \p F creates ref-countable object from uncounted parameter, |
| 118 |
/// false if not. |
| 119 |
bool isCtorOfRefCounted(const clang::FunctionDecl *F); |
| 120 |
|
| 121 |
/// \returns true if \p F creates checked ptr object from uncounted parameter, |
| 122 |
/// false if not. |
| 123 |
bool isCtorOfCheckedPtr(const clang::FunctionDecl *F); |
| 124 |
|
| 125 |
/// \returns true if \p F creates ref-countable or checked ptr object from |
| 126 |
/// uncounted parameter, false if not. |
| 127 |
bool isCtorOfSafePtr(const clang::FunctionDecl *F); |
| 128 |
|
| 129 |
/// \returns true if \p F is std::move or WTF::move. |
| 130 |
bool isStdOrWTFMove(const clang::FunctionDecl *F); |
| 131 |
|
| 132 |
/// \returns true if \p Name is RefPtr, Ref, or its variant, false if not. |
| 133 |
bool isRefType(const std::string &Name); |
| 134 |
|
| 135 |
/// \returns true if \p Name is CheckedRef or CheckedPtr, false if not. |
| 136 |
bool isCheckedPtr(const std::string &Name); |
| 137 |
|
| 138 |
/// \returns true if \p Name is RetainPtr or its variant, false if not. |
| 139 |
bool isRetainPtrOrOSPtr(const std::string &Name); |
| 140 |
|
| 141 |
/// \returns true if \p Name is an owning smar pointer such as Ref, CheckedPtr, |
| 142 |
/// and unique_ptr. |
| 143 |
bool isOwnerPtr(const std::string &Name); |
| 144 |
|
| 145 |
/// \returns true if \p Name is a smart pointer type name, false if not. |
| 146 |
bool isSmartPtrClass(const std::string &Name); |
| 147 |
|
| 148 |
/// \returns true if \p M is getter of a ref-counted class, false if not. |
| 149 |
std::optional<bool> isGetterOfSafePtr(const clang::CXXMethodDecl *Method); |
| 150 |
|
| 151 |
/// \returns true if \p F is a conversion between ref-countable or ref-counted |
| 152 |
/// pointer types. |
| 153 |
bool isPtrConversion(const FunctionDecl *F); |
| 154 |
|
| 155 |
/// \returns true if \p F is a builtin function which is considered trivial. |
| 156 |
bool isTrivialBuiltinFunction(const FunctionDecl *F); |
| 157 |
|
| 158 |
/// \returns true if \p F is a static singleton function. |
| 159 |
bool isSingleton(const NamedDecl *F); |
| 160 |
|
| 161 |
/// An inter-procedural analysis facility that detects functions with "trivial" |
| 162 |
/// behavior with respect to reference counting, such as simple field getters. |
| 163 |
class TrivialFunctionAnalysis { |
| 164 |
public: |
| 165 |
/// \returns true if \p D is a "trivial" function. |
| 166 |
bool isTrivial(const Decl *D) const { return isTrivialImpl(D, TheCache); } |
| 167 |
bool isTrivial(const Stmt *S) const { return isTrivialImpl(S, TheCache); } |
| 168 |
|
| 169 |
private: |
| 170 |
friend class TrivialFunctionAnalysisVisitor; |
| 171 |
|
| 172 |
using CacheTy = |
| 173 |
llvm::DenseMap<llvm::PointerUnion<const Decl *, const Stmt *>, bool>; |
| 174 |
mutable CacheTy TheCache{}; |
| 175 |
|
| 176 |
static bool isTrivialImpl(const Decl *D, CacheTy &Cache); |
| 177 |
static bool isTrivialImpl(const Stmt *S, CacheTy &Cache); |
| 178 |
}; |
| 179 |
|
| 180 |
} // namespace clang |
| 181 |
|
| 182 |
#endif |