aboutsummaryrefslogtreecommitdiff
path: root/clang/test/AST/attr-lifetime-capture-by.cpp
blob: 76efd45304447b26ae85b6a9a6691feef5879bd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// RUN: %clang_cc1 %s -ast-dump | FileCheck %s

// Verify that we print the [[clang::lifetime_capture_by(X)]] attribute.

struct S {
    void foo(int &a, int &b) [[clang::lifetime_capture_by(a, b, global)]];
};

// CHECK: CXXMethodDecl {{.*}}clang::lifetime_capture_by(a, b, global)

// ****************************************************************************
// Infer annotation for STL container methods.
// ****************************************************************************
namespace __gnu_cxx {
template <typename T>
struct basic_iterator {};
}

namespace std {
template<typename T> class allocator {};
template <typename T, typename Alloc = allocator<T>>
struct vector {
  typedef __gnu_cxx::basic_iterator<T> iterator;
  iterator begin();

  vector();

  void push_back(const T&);
  void push_back(T&&);

  void insert(iterator, T&&);
};

template <typename Key, typename Value>
struct map {
  Value& operator[](Key&& p);
  Value& operator[](const Key& p);
};
} // namespace std

// CHECK-NOT:   LifetimeCaptureByAttr

struct [[gsl::Pointer()]] View {};
std::vector<View> views;
// CHECK:   ClassTemplateSpecializationDecl {{.*}} struct vector definition instantiated_from 0x{{.+}} implicit_instantiation

// CHECK:       CXXMethodDecl {{.*}} push_back 'void (const View &)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'const View &'
// CHECK-NEXT:               LifetimeCaptureByAttr {{.*}} Implicit

// CHECK:       CXXMethodDecl {{.*}} push_back 'void (View &&)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'View &&'
// CHECK-NEXT:               LifetimeCaptureByAttr {{.*}} Implicit

// CHECK:       CXXMethodDecl {{.*}} insert 'void (iterator, View &&)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'iterator'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'View &&'
// CHECK-NEXT:               LifetimeCaptureByAttr {{.*}} Implicit

template <class T> struct [[gsl::Pointer()]] ViewTemplate {};
std::vector<ViewTemplate<int>> templated_views;
// CHECK:       ClassTemplateSpecializationDecl {{.*}} struct vector definition instantiated_from 0x{{.+}} implicit_instantiation

// CHECK:       CXXMethodDecl {{.*}} push_back 'void (const ViewTemplate<int> &)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'const ViewTemplate<int> &'
// CHECK-NEXT:               LifetimeCaptureByAttr {{.*}} Implicit
// CHECK-NOT:   LifetimeCaptureByAttr

// CHECK:       CXXMethodDecl {{.*}} push_back 'void (ViewTemplate<int> &&)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'ViewTemplate<int> &&'
// CHECK-NEXT:               LifetimeCaptureByAttr {{.*}} Implicit

// CHECK:       CXXMethodDecl {{.*}} insert 'void (iterator, ViewTemplate<int> &&)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'iterator'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'ViewTemplate<int> &&'
// CHECK-NEXT:               LifetimeCaptureByAttr {{.*}} Implicit

std::vector<int*> pointers;
// CHECK:   ClassTemplateSpecializationDecl {{.*}} struct vector definition instantiated_from 0x{{.+}} implicit_instantiation

// CHECK:       CXXMethodDecl {{.*}} push_back 'void (int *const &)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'int *const &'
// CHECK-NOT:               LifetimeCaptureByAttr

// CHECK:       CXXMethodDecl {{.*}} push_back 'void (int *&&)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'int *&&'
// CHECK-NOT:               LifetimeCaptureByAttr

// CHECK:       CXXMethodDecl {{.*}} insert 'void (iterator, int *&&)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'iterator'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'int *&&'
// CHECK-NOT:               LifetimeCaptureByAttr

std::vector<int> ints;
// CHECK:   ClassTemplateSpecializationDecl {{.*}} struct vector definition instantiated_from 0x{{.+}} implicit_instantiation
// CHECK:       TemplateArgument type 'int'

// CHECK:       CXXMethodDecl {{.*}} push_back 'void (const int &)'
// CHECK-NOT:   LifetimeCaptureByAttr

// CHECK:       CXXMethodDecl {{.*}} push_back 'void (int &&)'
// CHECK-NOT:   LifetimeCaptureByAttr

// CHECK:       CXXMethodDecl {{.*}} insert 'void (iterator, int &&)'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'iterator'
// CHECK-NEXT:           ParmVarDecl {{.*}} 'int &&'
// CHECK-NOT:   LifetimeCaptureByAttr

std::map<View, int> map;
// CHECK:   ClassTemplateSpecializationDecl {{.*}} struct map definition instantiated_from 0x{{.+}} implicit_instantiation

// CHECK:       CXXMethodDecl {{.*}} operator[] 'int &(View &&)' implicit_instantiation
// CHECK-NEXT:           ParmVarDecl {{.*}} p 'View &&'
// CHECK-NEXT:               LifetimeCaptureByAttr {{.*}} Implicit
// CHECK:       CXXMethodDecl {{.*}} operator[] 'int &(const View &)' implicit_instantiation
// CHECK-NEXT:           ParmVarDecl {{.*}} p 'const View &'
// CHECK-NEXT:               LifetimeCaptureByAttr {{.*}} Implicit