xref: /llvm-project/clang/lib/Sema/CheckExprLifetime.h (revision 1374aa35a3f62c774548361276a87eb472893262)
1 //===- CheckExprLifetime.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 //  This files implements a statement-local lifetime analysis.
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #ifndef LLVM_CLANG_SEMA_CHECK_EXPR_LIFETIME_H
13 #define LLVM_CLANG_SEMA_CHECK_EXPR_LIFETIME_H
14 
15 #include "clang/AST/Expr.h"
16 #include "clang/Sema/Initialization.h"
17 #include "clang/Sema/Sema.h"
18 
19 namespace clang::sema {
20 
21 // Tells whether the type is annotated with [[gsl::Pointer]].
22 bool isGLSPointerType(QualType QT);
23 
24 /// Describes an entity that is being assigned.
25 struct AssignedEntity {
26   // The left-hand side expression of the assignment.
27   Expr *LHS = nullptr;
28   CXXMethodDecl *AssignmentOperator = nullptr;
29 };
30 
31 struct CapturingEntity {
32   // In an function call involving a lifetime capture, this would be the
33   // argument capturing the lifetime of another argument.
34   //    void addToSet(std::string_view sv [[clang::lifetime_capture_by(setsv)]],
35   //                  set<std::string_view>& setsv);
36   //    set<std::string_view> setsv;
37   //    addToSet(std::string(), setsv); // Here 'setsv' is the 'Entity'.
38   //
39   // This is 'nullptr' when the capturing entity is 'global' or 'unknown'.
40   Expr *Entity = nullptr;
41 };
42 
43 /// Check that the lifetime of the given expr (and its subobjects) is
44 /// sufficient for initializing the entity, and perform lifetime extension
45 /// (when permitted) if not.
46 void checkInitLifetime(Sema &SemaRef, const InitializedEntity &Entity,
47                        Expr *Init);
48 
49 /// Check that the lifetime of the given expr (and its subobjects) is
50 /// sufficient for assigning to the entity.
51 void checkAssignmentLifetime(Sema &SemaRef, const AssignedEntity &Entity,
52                              Expr *Init);
53 
54 void checkCaptureByLifetime(Sema &SemaRef, const CapturingEntity &Entity,
55                             Expr *Init);
56 
57 /// Check that the lifetime of the given expr (and its subobjects) is
58 /// sufficient, assuming that it is passed as an argument to a musttail
59 /// function.
60 void checkExprLifetimeMustTailArg(Sema &SemaRef,
61                                   const InitializedEntity &Entity, Expr *Init);
62 
63 bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD);
64 
65 } // namespace clang::sema
66 
67 #endif // LLVM_CLANG_SEMA_CHECK_EXPR_LIFETIME_H
68