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 true if the implicit object parameter (this) should be considered
45/// lifetimebound, either due to an explicit lifetimebound attribute on the
46/// method or because it's a normal assignment operator.
47bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD);
48
49// Returns true if the implicit object argument (this) of a method call should
50// be tracked for GSL lifetime analysis. This applies to STL methods that return
51// pointers or references that depend on the lifetime of the object, such as
52// container iterators (begin, end), data accessors (c_str, data, get),
53// element accessors (operator[], operator*, front, back, at), or propagating
54// operations (operator+, operator-, operator++, operator--).
55bool shouldTrackImplicitObjectArg(const CXXMethodDecl *Callee,
56 bool RunningUnderLifetimeSafety);
57
58// Returns true if the first argument of a free function should be tracked for
59// GSL lifetime analysis. This applies to STL free functions that take a pointer
60// to a GSL Owner or Pointer and return a pointer or reference that depends on
61// the lifetime of the argument, such as std::begin, std::data, std::get, or
62// std::any_cast.
63bool shouldTrackFirstArgument(const FunctionDecl *FD);
64
65// Returns true if the second argument of a free function should be tracked for
66// lifetime analysis. This applies to free operator functions that take a
67// GSL Pointer as their second argument.
68bool shouldTrackSecondArgument(const FunctionDecl *FD);
69
70// Tells whether the type is annotated with [[gsl::Pointer]].
71bool isGslPointerType(QualType QT);
72// Tells whether the type is annotated with [[gsl::Owner]].
73bool isGslOwnerType(QualType QT);
74
75// Returns true if the given method is std::unique_ptr::release().
76// This is treated as a move in lifetime analysis to avoid false-positives
77// when ownership is manually transferred.
78bool isUniquePtrRelease(const CXXMethodDecl &MD);
79
80// Returns true if the given method invalidates references tracked by lifetime
81// analysis (e.g. vector::push_back). Methods that only invalidate iterators but
82// not references (e.g. unordered_map::emplace) are not considered invalidating
83// here.
84//
85// Container invalidation rules are based on:
86// https://en.cppreference.com/w/cpp/container#Iterator_invalidation
87bool isInvalidationMethod(const CXXMethodDecl &MD);
88
89// Returns true if the function destroys its first argument
90// (e.g., destructors via implicit 'this', std::destroy_at).
91bool destructsFirstArg(const FunctionDecl &FD);
92
93/// Returns true for standard library callable wrappers (e.g., std::function)
94/// that can propagate the stored lambda's origins.
95bool isStdCallableWrapperType(const CXXRecordDecl *RD);
96
97} // namespace clang::lifetimes
98
99#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 isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD)
Returns true if this is an assignment operator where the parameter has the lifetimebound attribute.
bool isPointerLikeType(QualType QT)
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...
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...
bool shouldTrackImplicitObjectArg(const CXXMethodDecl *Callee, bool RunningUnderLifetimeSafety)
bool isGslPointerType(QualType QT)
bool isGslOwnerType(QualType QT)
The JSON file list parser is used to communicate input to InstallAPI.