clang 20.0.0git
SmartPointerAccessorCaching.h
Go to the documentation of this file.
1//===-- SmartPointerAccessorCaching.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// This file defines utilities to help cache accessors for smart pointer
10// like objects.
11//
12// These should be combined with CachedConstAccessorsLattice.
13// Beyond basic const accessors, smart pointers may have the following two
14// additional issues:
15//
16// 1) There may be multiple accessors for the same underlying object, e.g.
17// `operator->`, `operator*`, and `get`. Users may use a mixture of these
18// accessors, so the cache should unify them.
19//
20// 2) There may be non-const overloads of accessors. They are still safe to
21// cache, as they don't modify the container object.
22//===----------------------------------------------------------------------===//
23
24#ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_SMARTPOINTERACCESSORCACHING_H
25#define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_SMARTPOINTERACCESSORCACHING_H
26
27#include <cassert>
28
29#include "clang/AST/Decl.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/Stmt.h"
36#include "llvm/ADT/STLFunctionalExtras.h"
37
38namespace clang::dataflow {
39
40/// Matchers:
41/// For now, these match on any class with an `operator*` or `operator->`
42/// where the return types have a similar shape as std::unique_ptr
43/// and std::optional.
44///
45/// - `*` returns a reference to a type `T`
46/// - `->` returns a pointer to `T`
47/// - `get` returns a pointer to `T`
48/// - `value` returns a reference `T`
49///
50/// (1) The `T` should all match across the accessors (ignoring qualifiers).
51///
52/// (2) The specific accessor used in a call isn't required to be const,
53/// but the class must have a const overload of each accessor.
54///
55/// For now, we don't have customization to ignore certain classes.
56/// For example, if writing a ClangTidy check for `std::optional`, these
57/// would also match `std::optional`. In order to have special handling
58/// for `std::optional`, we assume the (Matcher, TransferFunction) case
59/// with custom handling is ordered early so that these generic cases
60/// do not trigger.
65
66// Common transfer functions.
67
68/// Returns the "canonical" callee for smart pointer operators (`*` and `->`)
69/// as a key for caching.
70///
71/// We choose `*` as the canonical one, since it needs a
72/// StorageLocation anyway.
73///
74/// Note: there may be multiple `operator*` (one const, one non-const).
75/// We pick the const one, which the above provided matchers require to exist.
76const FunctionDecl *
78
79/// A transfer function for `operator*` (and `value`) calls that can be
80/// cached. Runs the `InitializeLoc` callback to initialize any new
81/// StorageLocations.
82///
83/// Requirements:
84///
85/// - LatticeT should use the `CachedConstAccessorsLattice` mixin.
86template <typename LatticeT>
88 const CallExpr *DerefExpr, RecordStorageLocation *SmartPointerLoc,
89 TransferState<LatticeT> &State,
90 llvm::function_ref<void(StorageLocation &)> InitializeLoc);
91
92/// A transfer function for `operator->` (and `get`) calls that can be cached.
93/// Runs the `InitializeLoc` callback to initialize any new StorageLocations.
94///
95/// Requirements:
96///
97/// - LatticeT should use the `CachedConstAccessorsLattice` mixin.
98template <typename LatticeT>
100 const CallExpr *GetExpr, RecordStorageLocation *SmartPointerLoc,
101 TransferState<LatticeT> &State,
102 llvm::function_ref<void(StorageLocation &)> InitializeLoc);
103
104template <typename LatticeT>
106 const CallExpr *DerefExpr, RecordStorageLocation *SmartPointerLoc,
108 llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
109 if (State.Env.getStorageLocation(*DerefExpr) != nullptr)
110 return;
111 if (SmartPointerLoc == nullptr)
112 return;
113
114 const FunctionDecl *Callee = DerefExpr->getDirectCallee();
115 if (Callee == nullptr)
116 return;
117 const FunctionDecl *CanonicalCallee =
119 // This shouldn't happen, as we should at least find `Callee` itself.
120 assert(CanonicalCallee != nullptr);
121 if (CanonicalCallee != Callee) {
122 // When using the provided matchers, we should always get a reference to
123 // the same type.
124 assert(CanonicalCallee->getReturnType()->isReferenceType() &&
125 Callee->getReturnType()->isReferenceType());
126 assert(CanonicalCallee->getReturnType()
129 Callee->getReturnType()
130 .getNonReferenceType()
131 ->getCanonicalTypeUnqualified());
132 }
133
134 StorageLocation &LocForValue =
135 State.Lattice.getOrCreateConstMethodReturnStorageLocation(
136 *SmartPointerLoc, CanonicalCallee, State.Env, InitializeLoc);
137 State.Env.setStorageLocation(*DerefExpr, LocForValue);
138}
139
140template <typename LatticeT>
142 const CallExpr *GetExpr, RecordStorageLocation *SmartPointerLoc,
144 llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
145 if (SmartPointerLoc == nullptr)
146 return;
147
148 const FunctionDecl *CanonicalCallee =
150
151 if (CanonicalCallee != nullptr) {
152 auto &LocForValue =
153 State.Lattice.getOrCreateConstMethodReturnStorageLocation(
154 *SmartPointerLoc, CanonicalCallee, State.Env, InitializeLoc);
155 State.Env.setValue(*GetExpr,
156 State.Env.template create<PointerValue>(LocForValue));
157 } else {
158 // Otherwise, just cache the pointer value as if it was a const accessor.
159 Value *Val = State.Lattice.getOrCreateConstMethodReturnValue(
160 *SmartPointerLoc, GetExpr, State.Env);
161 if (Val == nullptr)
162 return;
163 State.Env.setValue(*GetExpr, *Val);
164 }
165}
166
167} // namespace clang::dataflow
168
169#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_SMARTPOINTERACCESSORCACHING_H
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2874
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition: Expr.h:3047
Represents a function declaration or definition.
Definition: Decl.h:1935
QualType getReturnType() const
Definition: Decl.h:2720
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition: Type.h:8139
CanQualType getCanonicalTypeUnqualified() const
bool isReferenceType() const
Definition: Type.h:8209
A storage location for a record (struct, class, or union).
Base class for elements of the local variable store and of the heap.
Base class for all values computed by abstract interpretation.
Definition: Value.h:33
internal::Matcher< Stmt > StatementMatcher
Definition: ASTMatchers.h:144
Dataflow Directional Tag Classes.
Definition: AdornedCFG.h:29
void transferSmartPointerLikeCachedDeref(const CallExpr *DerefExpr, RecordStorageLocation *SmartPointerLoc, TransferState< LatticeT > &State, llvm::function_ref< void(StorageLocation &)> InitializeLoc)
A transfer function for operator* (and value) calls that can be cached.
void transferSmartPointerLikeCachedGet(const CallExpr *GetExpr, RecordStorageLocation *SmartPointerLoc, TransferState< LatticeT > &State, llvm::function_ref< void(StorageLocation &)> InitializeLoc)
A transfer function for operator-> (and get) calls that can be cached.
ast_matchers::StatementMatcher isSmartPointerLikeGetMethodCall()
ast_matchers::StatementMatcher isSmartPointerLikeValueMethodCall()
ast_matchers::StatementMatcher isSmartPointerLikeOperatorArrow()
const FunctionDecl * getCanonicalSmartPointerLikeOperatorCallee(const CallExpr *CE)
Returns the "canonical" callee for smart pointer operators (* and ->) as a key for caching.
ast_matchers::StatementMatcher isSmartPointerLikeOperatorStar()
Matchers: For now, these match on any class with an operator* or operator-> where the return types ha...
A common form of state shared between the cases of a transfer function.
Definition: MatchSwitch.h:41