|
| 1 | +// RUN: %clang_cc1 -fsyntax-only -std=c++17 -flifetime-safety-inference \ |
| 2 | +// RUN: -fexperimental-lifetime-safety-tu-analysis \ |
| 3 | +// RUN: -Wlifetime-safety-suggestions -Wno-dangling \ |
| 4 | +// RUN: -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s |
| 5 | + |
| 6 | +struct View; |
| 7 | + |
| 8 | +struct [[gsl::Owner]] MyObj { |
| 9 | + int id; |
| 10 | + MyObj(int i) : id(i) {} |
| 11 | + MyObj() {} |
| 12 | + ~MyObj() {} |
| 13 | + MyObj operator+(MyObj); |
| 14 | + View getView() const [[clang::lifetimebound]]; |
| 15 | +}; |
| 16 | + |
| 17 | +struct [[gsl::Pointer()]] View { |
| 18 | + View(const MyObj &); |
| 19 | + View(); |
| 20 | + void use() const; |
| 21 | +}; |
| 22 | + |
| 23 | +View return_view(View a) { |
| 24 | + // CHECK: :[[@LINE-1]]:18: warning: parameter in intra-TU function should be marked {{\[\[}}clang::lifetimebound]] [-Wlifetime-safety-intra-tu-suggestions] |
| 25 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:24-[[@LINE-2]]:24}:" {{\[\[}}clang::lifetimebound]]" |
| 26 | + return a; |
| 27 | +} |
| 28 | + |
| 29 | +MyObj &return_multi(MyObj &a, bool c, MyObj &b) { |
| 30 | + // CHECK-DAG: :[[@LINE-1]]:21: warning: parameter in intra-TU function should be marked |
| 31 | + // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:29-[[@LINE-2]]:29}:" {{\[\[}}clang::lifetimebound]]" |
| 32 | + // CHECK-DAG: :[[@LINE-3]]:39: warning: parameter in intra-TU function should be marked |
| 33 | + // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-4]]:47-[[@LINE-4]]:47}:" {{\[\[}}clang::lifetimebound]]" |
| 34 | + if (c) |
| 35 | + return a; |
| 36 | + return b; |
| 37 | +} |
| 38 | + |
| 39 | +View return_partial(View a [[clang::lifetimebound]], bool c, View b) { |
| 40 | + // CHECK: :[[@LINE-1]]:62: warning: parameter in intra-TU function should be marked |
| 41 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:68-[[@LINE-2]]:68}:" {{\[\[}}clang::lifetimebound]]" |
| 42 | + if (c) |
| 43 | + return a; |
| 44 | + return b; |
| 45 | +} |
| 46 | + |
| 47 | +View param_with_attr(View a [[maybe_unused]]) { |
| 48 | + // CHECK: :[[@LINE-1]]:22: warning: parameter in intra-TU function should be marked |
| 49 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:28-[[@LINE-2]]:28}:" {{\[\[}}clang::lifetimebound]]" |
| 50 | + return a; |
| 51 | +} |
| 52 | + |
| 53 | +View param_default(View a = View()) { |
| 54 | + // CHECK: :[[@LINE-1]]:20: warning: parameter in intra-TU function should be marked |
| 55 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:35-[[@LINE-2]]:35}:" {{\[\[}}clang::lifetimebound]]" |
| 56 | + return a; |
| 57 | +} |
| 58 | + |
| 59 | +// FIXME: Iterate over redecls and add [[clang::lifetimebound]] |
| 60 | +View multi_decl(View a); |
| 61 | +View multi_decl(View a); |
| 62 | +View multi_decl(View a) { |
| 63 | + // CHECK: :[[@LINE-1]]:17: warning: parameter in intra-TU function should be marked |
| 64 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:23-[[@LINE-2]]:23}:" {{\[\[}}clang::lifetimebound]]" |
| 65 | + return a; |
| 66 | +} |
| 67 | + |
| 68 | +template <typename T> |
| 69 | +T *template_identity(T *a) { |
| 70 | + // CHECK: :[[@LINE-1]]:22: warning: parameter in intra-TU function should be marked |
| 71 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:26-[[@LINE-2]]:26}:" {{\[\[}}clang::lifetimebound]]" |
| 72 | + return a; |
| 73 | +} |
| 74 | + |
| 75 | +MyObj *instantiate_template() { |
| 76 | + MyObj local; |
| 77 | + return template_identity(&local); |
| 78 | +} |
| 79 | + |
| 80 | +struct ViewMember { |
| 81 | + ViewMember(int d) : data(d) {} |
| 82 | + ~ViewMember() {} |
| 83 | + MyObj data; |
| 84 | + |
| 85 | + View get_view() { |
| 86 | + // CHECK: :[[@LINE-1]]:18: warning: implicit this in intra-TU function should be marked |
| 87 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:18-[[@LINE-2]]:18}:" {{\[\[}}clang::lifetimebound]]" |
| 88 | + return data; |
| 89 | + } |
| 90 | + |
| 91 | + View get_view_const() const { |
| 92 | + // CHECK: :[[@LINE-1]]:30: warning: implicit this in intra-TU function should be marked |
| 93 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:30-[[@LINE-2]]:30}:" {{\[\[}}clang::lifetimebound]]" |
| 94 | + return data; |
| 95 | + } |
| 96 | + |
| 97 | + const View get_view_const_noexcept() const noexcept { |
| 98 | + // CHECK: :[[@LINE-1]]:54: warning: implicit this in intra-TU function should be marked |
| 99 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:54-[[@LINE-2]]:54}:" {{\[\[}}clang::lifetimebound]]" |
| 100 | + return data; |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +struct Base { |
| 105 | + Base() {} |
| 106 | + virtual ~Base() {} |
| 107 | + MyObj data; |
| 108 | + virtual const MyObj &get_virtual() const { |
| 109 | + // CHECK: :[[@LINE-1]]:43: warning: implicit this in intra-TU function should be marked |
| 110 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:43-[[@LINE-2]]:43}:" {{\[\[}}clang::lifetimebound]]" |
| 111 | + return data; |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +struct Derived : Base { |
| 116 | + const MyObj &get_virtual() const override { |
| 117 | + // CHECK: :[[@LINE-1]]:35: warning: implicit this in intra-TU function should be marked |
| 118 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:35-[[@LINE-2]]:35}:" {{\[\[}}clang::lifetimebound]]" |
| 119 | + return data; |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +struct DerivedFinal : Base { |
| 124 | + const MyObj &get_virtual() const final { |
| 125 | + // CHECK: :[[@LINE-1]]:35: warning: implicit this in intra-TU function should be marked |
| 126 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:35-[[@LINE-2]]:35}:" {{\[\[}}clang::lifetimebound]]" |
| 127 | + return data; |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +struct OutOfLine { |
| 132 | + OutOfLine() {} |
| 133 | + ~OutOfLine() {} |
| 134 | + const OutOfLine &get() const; |
| 135 | +}; |
| 136 | +const OutOfLine &OutOfLine::get() const { |
| 137 | + // CHECK: :[[@LINE-1]]:40: warning: implicit this in intra-TU function should be marked |
| 138 | + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:40-[[@LINE-2]]:40}:" {{\[\[}}clang::lifetimebound]]" |
| 139 | + return *this; |
| 140 | +} |
0 commit comments