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