1//=======- RawPtrRefSafetyModel.h -------------------------------*- 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_RAWPTRREFSAFETYMODEL_H
10#define LLVM_CLANG_ANALYZER_WEBKIT_RAWPTRREFSAFETYMODEL_H
11
12#include "PtrTypesSemantics.h"
13#include <memory>
14#include <optional>
15#include <string>
16
17namespace clang {
18class CXXRecordDecl;
19class Decl;
20class Expr;
21class QualType;
22class SourceManager;
23
24/// Models one WebKit pointer-safety policy: ref-counted (RefPtr), checked
25/// (CheckedPtr), or retainable (RetainPtr/OSPtr).
26///
27/// It captures the family-specific "what is a safe/unsafe pointer" questions
28/// that are shared by the various RawPtrRef* checkers, independently of how
29/// each checker traverses the AST (call arguments, local variables, members,
30/// lambda captures, ...). This lets a single policy be defined once and reused
31/// across every traversal.
32class PtrRefSafetyModel {
33public:
34 virtual ~PtrRefSafetyModel() = default;
35
36 /// \returns whether \p QT itself is an unsafe (smart-pointer-capable but not
37 /// managed) type, false if not, std::nullopt if inconclusive.
38 virtual std::optional<bool> isUnsafeType(QualType QT) const = 0;
39
40 /// \returns whether \p QT is a raw pointer or reference to an unsafe type,
41 /// false if not, std::nullopt if inconclusive. \p IgnoreARC requests that
42 /// Objective-C ARC be ignored when deciding retainability.
43 virtual std::optional<bool> isUnsafePtr(QualType QT,
44 bool IgnoreARC = false) const = 0;
45
46 /// \returns whether \p Record is a safe smart pointer for this policy.
47 virtual bool isSafePtr(const CXXRecordDecl *Record) const = 0;
48
49 /// \returns whether \p T is a safe smart pointer type for this policy.
50 virtual bool isSafePtrType(QualType T) const = 0;
51
52 /// \returns whether \p Name is the name of a safe smart pointer class for
53 /// this policy.
54 virtual bool isPtrType(const std::string &Name) const = 0;
55
56 /// \returns whether \p E is known to produce a safe value for this policy.
57 virtual bool isSafeExpr(const Expr *) const { return false; }
58
59 /// \returns whether \p D refers to a declaration that is safe by construction
60 /// for this policy (e.g. immortal system-header globals).
61 virtual bool isSafeDecl(const Decl *, const SourceManager &) const {
62 return false;
63 }
64
65 /// \returns a human readable name for the safe type category, used in
66 /// diagnostics (e.g. "RefPtr-capable type").
67 virtual const char *typeName() const = 0;
68
69 /// \returns the RetainTypeChecker backing this policy, or nullptr if the
70 /// policy does not track retain/OS types.
71 virtual RetainTypeChecker *retainTypeChecker() const { return nullptr; }
72};
73
74/// Applies the memory-management exemptions that hold for a variable, member,
75/// or lambda capture (but not for a call argument) before consulting \p Model:
76/// a __strong / __weak Objective-C storage location is memory managed and thus
77/// safe. \returns whether \p T is an unsafe pointer in such a storage context.
78std::optional<bool> isUnsafePtrForStorage(const PtrRefSafetyModel &Model,
79 QualType T, bool IgnoreARC = false);
80
81/// \returns a policy that treats ref-counted / checked pointers as safe.
82std::unique_ptr<PtrRefSafetyModel> makeRefPtrSafetyModel();
83
84/// \returns a policy that treats checked pointers as safe.
85std::unique_ptr<PtrRefSafetyModel> makeCheckedPtrSafetyModel();
86
87/// \returns a policy that treats RetainPtr / OSPtr as safe.
88std::unique_ptr<PtrRefSafetyModel> makeRetainPtrSafetyModel();
89
90} // namespace clang
91
92#endif
93