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