clang 24.0.0git
LifetimeAnnotations.h
Go to the documentation of this file.
1//===- LifetimeAnnotations.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// Helper functions to inspect and infer lifetime annotations.
9//===----------------------------------------------------------------------===//
10#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H
11#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H
12
13#include "clang/AST/Attr.h"
14#include "clang/AST/DeclCXX.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/PointerUnion.h"
17#include "llvm/ADT/SmallVector.h"
18#include <optional>
19
20namespace clang ::lifetimes {
21
22// This function is needed because Decl::isInStdNamespace will return false for
23// iterators in some STL implementations due to them being defined in a
24// namespace outside of the std namespace.
25bool isInStlNamespace(const Decl *D);
26
27bool isPointerLikeType(QualType QT);
28
29/// Returns the most recent declaration of the method to ensure all
30/// lifetime-bound attributes from redeclarations are considered.
31const FunctionDecl *getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD);
32
33/// Returns the most recent declaration of the method to ensure all
34/// lifetime-bound attributes from redeclarations are considered.
35const CXXMethodDecl *
36getDeclWithMergedLifetimeBoundAttrs(const CXXMethodDecl *CMD);
37
38// Return true if this is an "normal" assignment operator.
39// We assume that a normal assignment operator always returns *this, that is,
40// an lvalue reference that is the same type as the implicit object parameter
41// (or the LHS for a non-member operator==).
42bool isNormalAssignmentOperator(const FunctionDecl *FD);
43
44/// Returns true if this is an assignment operator where the parameter
45/// has the lifetimebound attribute.
46bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD);
47
48/// Returns the lifetimebound attribute for the implicit this parameter, if it
49/// exists on the current type.
50const LifetimeBoundAttr *
52
53/// Returns the lifetimebound attribute for the implicit this parameter, if it
54/// exists on any redeclaration.
55const LifetimeBoundAttr *
57
58/// Returns true if the implicit object parameter (this) should be considered
59/// lifetimebound, either due to an explicit lifetimebound attribute on the
60/// method or because it's a normal assignment operator.
61bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD);
62
64 llvm::PointerUnion<const ParmVarDecl *, const CXXMethodDecl *>;
65
67 const FunctionDecl *FD = nullptr;
69};
70
71/// Returns the callee and arguments corresponding to Call. For instance member
72/// calls, Args includes the implicit object argument as argument 0.
74
75/// Returns the parameter corresponding to argument I when the argument should
76/// be tracked for lifetime safety.
77std::optional<LifetimeBoundParamInfo>
79 unsigned I);
80
81/// Returns lifetime safety tracking info for the call argument containing
82/// Source.
83std::optional<LifetimeBoundParamInfo>
84getTrackingInfoForCallArg(const Expr *Call, const Expr *Source);
85
86// Returns true if the implicit object argument (this) of a method call should
87// be tracked for GSL lifetime analysis. This applies to STL methods that return
88// pointers or references that depend on the lifetime of the object, such as
89// container iterators (begin, end), data accessors (c_str, data, get),
90// element accessors (operator[], operator*, front, back, at), or propagating
91// operations (operator+, operator-, operator++, operator--).
92bool shouldTrackImplicitObjectArg(const Expr &ImplicitObjectArgument,
93 const CXXMethodDecl *Callee,
94 bool RunningUnderLifetimeSafety);
95
96// Returns true if the first argument of a free function should be tracked for
97// GSL lifetime analysis. This applies to STL free functions that take a pointer
98// to a GSL Owner or Pointer and return a pointer or reference that depends on
99// the lifetime of the argument, such as std::begin, std::data, std::get, or
100// std::any_cast.
101bool shouldTrackFirstArgument(const FunctionDecl *FD);
102
103// Returns true if the second argument of a free function should be tracked for
104// lifetime analysis. This applies to free operator functions that take a
105// GSL Pointer as their second argument.
106bool shouldTrackSecondArgument(const FunctionDecl *FD);
107
108// Tells whether the type is annotated with [[gsl::Pointer]].
109bool isGslPointerType(QualType QT);
110// Tells whether the type is annotated with [[gsl::Owner]].
111bool isGslOwnerType(QualType QT);
112bool isGslOwnerType(const CXXRecordDecl *RD);
113
114// Returns true if the given method is std::unique_ptr::release().
115// This is treated as a move in lifetime analysis to avoid false-positives
116// when ownership is manually transferred.
117bool isUniquePtrRelease(const CXXMethodDecl &MD);
118
119// Returns true if the given method invalidates references tracked by lifetime
120// analysis (e.g. vector::push_back). Methods that only invalidate iterators but
121// not references (e.g. unordered_map::emplace) are not considered invalidating
122// here.
123//
124// Container invalidation rules are based on:
125// https://en.cppreference.com/w/cpp/container#Iterator_invalidation
126bool isInvalidationMethod(const CXXMethodDecl &MD);
127
128// Returns true if the function destroys its first argument
129// (e.g., destructors via implicit 'this', std::destroy_at).
130bool destructsFirstArg(const FunctionDecl &FD);
131
132/// Returns true for standard library callable wrappers (e.g., std::function)
133/// that can propagate the stored lambda's origins.
134bool isStdCallableWrapperType(const CXXRecordDecl *RD);
135
136/// Returns true for std reference-cast builtins (e.g., std::move). Their result
137/// refers to the same object as the argument, so all origins propagate from
138/// argument to result.
139bool isStdReferenceCast(const FunctionDecl *FD);
140
141} // namespace clang::lifetimes
142
143#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
bool isInvalidationMethod(const CXXMethodDecl &MD)
bool isStdReferenceCast(const FunctionDecl *FD)
Returns true for std reference-cast builtins (e.g., std::move).
bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD)
Returns true if this is an assignment operator where the parameter has the lifetimebound attribute.
bool isPointerLikeType(QualType QT)
bool shouldTrackImplicitObjectArg(const Expr &ImplicitObjectArgument, const CXXMethodDecl *Callee, bool RunningUnderLifetimeSafety)
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD)
Returns true if the implicit object parameter (this) should be considered lifetimebound,...
std::optional< LifetimeBoundParamInfo > getTrackedArgInfo(const FunctionDecl *FD, llvm::ArrayRef< const Expr * > Args, unsigned I)
Returns the parameter corresponding to argument I when the argument should be tracked for lifetime sa...
bool shouldTrackFirstArgument(const FunctionDecl *FD)
bool shouldTrackSecondArgument(const FunctionDecl *FD)
bool destructsFirstArg(const FunctionDecl &FD)
const FunctionDecl * getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD)
Returns the most recent declaration of the method to ensure all lifetime-bound attributes from redecl...
const LifetimeBoundAttr * getImplicitObjectParamLifetimeBoundAttr(const FunctionDecl *FD)
Returns the lifetimebound attribute for the implicit this parameter, if it exists on any redeclaratio...
std::optional< LifetimeBoundParamInfo > getTrackingInfoForCallArg(const Expr *Call, const Expr *Source)
Returns lifetime safety tracking info for the call argument containing Source.
FunctionCallInfo getFunctionCallInfo(const Expr *Call)
Returns the callee and arguments corresponding to Call.
bool isNormalAssignmentOperator(const FunctionDecl *FD)
bool isUniquePtrRelease(const CXXMethodDecl &MD)
bool isInStlNamespace(const Decl *D)
llvm::PointerUnion< const ParmVarDecl *, const CXXMethodDecl * > LifetimeBoundParamInfo
bool isStdCallableWrapperType(const CXXRecordDecl *RD)
Returns true for standard library callable wrappers (e.g., std::function) that can propagate the stor...
const LifetimeBoundAttr * getDirectImplicitObjectLifetimeBoundAttr(const FunctionDecl *FD)
Returns the lifetimebound attribute for the implicit this parameter, if it exists on the current type...
bool isGslPointerType(QualType QT)
bool isGslOwnerType(QualType QT)
Top level wrappers for InstallAPI frontend operations.
llvm::SmallVector< const Expr *, 4 > Args