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), or
53// element accessors (operator[], operator*, front, back, at).
54bool shouldTrackImplicitObjectArg(const CXXMethodDecl *Callee,
55 bool RunningUnderLifetimeSafety);
56
57// Returns true if the first argument of a free function should be tracked for
58// GSL lifetime analysis. This applies to STL free functions that take a pointer
59// to a GSL Owner or Pointer and return a pointer or reference that depends on
60// the lifetime of the argument, such as std::begin, std::data, std::get, or
61// std::any_cast.
62bool shouldTrackFirstArgument(const FunctionDecl *FD);
63
64// Tells whether the type is annotated with [[gsl::Pointer]].
65bool isGslPointerType(QualType QT);
66// Tells whether the type is annotated with [[gsl::Owner]].
67bool isGslOwnerType(QualType QT);
68
69} // namespace clang::lifetimes
70
71#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
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)
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 isInStlNamespace(const Decl *D)
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.