xref: /llvm-project/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h (revision 6be567bfc22e0d165a4b927beab3933be7ef98e6)
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 
17 namespace clang {
18 class CXXBaseSpecifier;
19 class CXXMethodDecl;
20 class CXXRecordDecl;
21 class Decl;
22 class FunctionDecl;
23 class QualType;
24 class Stmt;
25 class 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.
36 std::optional<const clang::CXXRecordDecl *>
37 hasPublicMethodInBase(const CXXBaseSpecifier *Base,
38                       llvm::StringRef NameToMatch);
39 
40 /// \returns true if \p Class is ref-countable, false if not, std::nullopt if
41 /// inconclusive.
42 std::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.
46 std::optional<bool> isCheckedPtrCapable(const clang::CXXRecordDecl *Class);
47 
48 /// \returns true if \p Class is ref-counted, false if not.
49 bool isRefCounted(const clang::CXXRecordDecl *Class);
50 
51 /// \returns true if \p Class is a CheckedPtr / CheckedRef, false if not.
52 bool isCheckedPtr(const clang::CXXRecordDecl *Class);
53 
54 /// \returns true if \p Class is ref-countable AND not ref-counted, false if
55 /// not, std::nullopt if inconclusive.
56 std::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.
60 std::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.
64 std::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.
68 std::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.
72 std::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.
76 std::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.
80 std::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.
84 bool isSafePtrType(const clang::QualType T);
85 
86 /// \returns true if \p T is a RefPtr, Ref, CheckedPtr, CheckedRef, or
87 /// unique_ptr, false if not.
88 bool isOwnerPtrType(const clang::QualType T);
89 
90 /// \returns true if \p F creates ref-countable object from uncounted parameter,
91 /// false if not.
92 bool isCtorOfRefCounted(const clang::FunctionDecl *F);
93 
94 /// \returns true if \p F creates checked ptr object from uncounted parameter,
95 /// false if not.
96 bool isCtorOfCheckedPtr(const clang::FunctionDecl *F);
97 
98 /// \returns true if \p F creates ref-countable or checked ptr object from
99 /// uncounted parameter, false if not.
100 bool isCtorOfSafePtr(const clang::FunctionDecl *F);
101 
102 /// \returns true if \p Name is RefPtr, Ref, or its variant, false if not.
103 bool isRefType(const std::string &Name);
104 
105 /// \returns true if \p Name is CheckedRef or CheckedPtr, false if not.
106 bool isCheckedPtr(const std::string &Name);
107 
108 /// \returns true if \p M is getter of a ref-counted class, false if not.
109 std::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.
113 bool isPtrConversion(const FunctionDecl *F);
114 
115 /// \returns true if \p F is a static singleton function.
116 bool 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.
120 class TrivialFunctionAnalysis {
121 public:
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 
126 private:
127   friend class TrivialFunctionAnalysisVisitor;
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
140