Skip to content

Commit 7422a13

Browse files
Make [[clang::lifetimebound]] work for expressions coming from default arguments
1 parent a643836 commit 7422a13

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10107,6 +10107,8 @@ def note_lambda_capture_initializer : Note<
1010710107
" via initialization of lambda capture %0}1">;
1010810108
def note_init_with_default_member_initializer : Note<
1010910109
"initializing field %0 with default member initializer">;
10110+
def note_init_with_default_argument : Note<
10111+
"initializing parameter %0 with default argument">;
1011010112

1011110113
// Check for initializing a member variable with the address or a reference to
1011210114
// a constructor parameter.

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ struct IndirectLocalPathEntry {
194194
GslReferenceInit,
195195
GslPointerInit,
196196
GslPointerAssignment,
197+
DefaultArg,
197198
} Kind;
198199
Expr *E;
199200
union {
@@ -465,17 +466,27 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
465466
for (unsigned I = 0,
466467
N = std::min<unsigned>(Callee->getNumParams(), Args.size());
467468
I != N; ++I) {
469+
Expr *Arg = Args[I];
470+
auto *DAE = dyn_cast<CXXDefaultArgExpr>(Arg);
471+
if (DAE) {
472+
Path.push_back(
473+
{IndirectLocalPathEntry::DefaultArg, DAE, DAE->getParam()});
474+
Arg = DAE->getExpr();
475+
}
468476
if (CheckCoroCall || Callee->getParamDecl(I)->hasAttr<LifetimeBoundAttr>())
469-
VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]);
477+
VisitLifetimeBoundArg(Callee->getParamDecl(I), Arg);
470478
else if (EnableGSLAnalysis && I == 0) {
471479
if (shouldTrackFirstArgument(Callee)) {
472-
VisitGSLPointerArg(Callee, Args[0]);
480+
VisitGSLPointerArg(Callee, Arg);
473481
} else if (auto *CCE = dyn_cast<CXXConstructExpr>(Call);
474482
CCE &&
475483
CCE->getConstructor()->getParent()->hasAttr<PointerAttr>()) {
476-
VisitGSLPointerArg(CCE->getConstructor(), Args[0]);
484+
VisitGSLPointerArg(CCE->getConstructor(), Arg);
477485
}
478486
}
487+
if (DAE) {
488+
Path.pop_back();
489+
}
479490
}
480491
}
481492

@@ -916,6 +927,9 @@ static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I,
916927
if (!Path[I].Capture->capturesVariable())
917928
continue;
918929
return Path[I].E->getSourceRange();
930+
931+
case IndirectLocalPathEntry::DefaultArg:
932+
return cast<CXXDefaultArgExpr>(Path[I].E)->getUsedLocation();
919933
}
920934
}
921935
return E->getSourceRange();
@@ -1221,7 +1235,7 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
12211235
break;
12221236
}
12231237

1224-
case IndirectLocalPathEntry::LambdaCaptureInit:
1238+
case IndirectLocalPathEntry::LambdaCaptureInit: {
12251239
if (!Elem.Capture->capturesVariable())
12261240
break;
12271241
// FIXME: We can't easily tell apart an init-capture from a nested
@@ -1234,6 +1248,15 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
12341248
<< nextPathEntryRange(Path, I + 1, L);
12351249
break;
12361250
}
1251+
1252+
case IndirectLocalPathEntry::DefaultArg: {
1253+
const auto *DAE = cast<CXXDefaultArgExpr>(Elem.E);
1254+
SemaRef.Diag(DAE->getParam()->getDefaultArgRange().getBegin(),
1255+
diag::note_init_with_default_argument)
1256+
<< DAE->getParam() << nextPathEntryRange(Path, I + 1, L);
1257+
break;
1258+
}
1259+
}
12371260
}
12381261

12391262
// We didn't lifetime-extend, so don't go any further; we don't need more

clang/test/SemaCXX/attr-lifetimebound.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ namespace usage_invalid {
1818

1919
namespace usage_ok {
2020
struct IntRef { int *target; };
21+
using IntArray = int[];
2122

23+
const int *defaultparam(const int &def1 [[clang::lifetimebound]] = 0); // #def1
2224
int &refparam(int &param [[clang::lifetimebound]]);
2325
int &classparam(IntRef param [[clang::lifetimebound]]);
26+
const int *c = defaultparam(); // expected-warning {{temporary whose address is used as value of local variable 'c' will be destroyed at the end of the full-expression}} expected-note@#def1 {{initializing parameter 'def1' with default argument}}
27+
const int &defaultparam_array([[clang::lifetimebound]] const int *p = IntArray{1, 2, 3}); // #def2
2428

2529
// Do not diagnose non-void return types; they can still be lifetime-bound.
2630
long long ptrintcast(int &param [[clang::lifetimebound]]) {
@@ -34,13 +38,20 @@ namespace usage_ok {
3438
struct A {
3539
A();
3640
A(int);
41+
A(const char*, const int& def3 [[clang::lifetimebound]] = 0); // #def3
3742
int *class_member() [[clang::lifetimebound]];
3843
operator int*() [[clang::lifetimebound]];
44+
static const int &defaulted_param(const int &def4 [[clang::lifetimebound]] = 0); // #def4
45+
static const int &defaulted_param2(const int &def5 [[clang::lifetimebound]] = defaulted_param()); // #def5
3946
};
4047

4148
int *p = A().class_member(); // expected-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
4249
int *q = A(); // expected-warning {{temporary whose address is used as value of local variable 'q' will be destroyed at the end of the full-expression}}
4350
int *r = A(1); // expected-warning {{temporary whose address is used as value of local variable 'r' will be destroyed at the end of the full-expression}}
51+
A a = A(""); // expected-warning {{temporary whose address is used as value of local variable 'a' will be destroyed at the end of the full-expression}} expected-note@#def3 {{initializing parameter 'def3' with default argument}}
52+
const int &s = A::defaulted_param(); // expected-warning {{temporary bound to local reference 's' will be destroyed at the end of the full-expression}} expected-note@#def4 {{initializing parameter 'def4' with default argument}}
53+
const int &t = A::defaulted_param2(); // expected-warning {{temporary bound to local reference 't' will be destroyed at the end of the full-expression}} expected-note@#def4 {{initializing parameter 'def4' with default argument}} expected-note@#def5 {{initializing parameter 'def5' with default argument}}
54+
const int &u = defaultparam_array(); // expected-warning {{temporary bound to local reference 'u' will be destroyed at the end of the full-expression}} expected-note@#def2 {{initializing parameter 'p' with default argument}}
4455

4556
void test_assignment() {
4657
p = A().class_member(); // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}

0 commit comments

Comments
 (0)