Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -3976,6 +3976,8 @@ The capturing entity ``X`` can be one of the following:
std::set<std::string_view> s;
};

Note: When applied to a constructor parameter, `[[clang::lifetime_capture_by(this)]]` is just an alias of `[[clang::lifetimebound]]`.

- `global`, `unknown`.

.. code-block:: c++
Expand Down
20 changes: 20 additions & 0 deletions clang/lib/Sema/CheckExprLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,26 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
}
if (CheckCoroCall || Callee->getParamDecl(I)->hasAttr<LifetimeBoundAttr>())
VisitLifetimeBoundArg(Callee->getParamDecl(I), Arg);
else if (const auto *CaptureAttr =
Callee->getParamDecl(I)->getAttr<LifetimeCaptureByAttr>();
CaptureAttr && isa<CXXConstructorDecl>(Callee) &&
llvm::any_of(CaptureAttr->params(), [](int ArgIdx) {
return ArgIdx == LifetimeCaptureByAttr::THIS;
}))
// `lifetime_capture_by(this)` in a class constructor has the same
// semantics as `lifetimebound`:
//
// struct Foo {
// const int& a;
// // Equivalent to Foo(const int& t [[clang::lifetimebound]])
// Foo(const int& t [[clang::lifetime_capture_by(this)]]) : a(t) {}
// };
//
// In the implementation, `lifetime_capture_by` is treated as an alias for
// `lifetimebound` and shares the same code path. This implies the emitted
// diagnostics will be emitted under `-Wdangling`, not
// `-Wdangling-capture`.
VisitLifetimeBoundArg(Callee->getParamDecl(I), Arg);
Comment on lines +632 to +645
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Can you also add a one line description to the capture by docs as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

else if (EnableGSLAnalysis && I == 0) {
// Perform GSL analysis for the first argument
if (shouldTrackFirstArgument(Callee)) {
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3240,8 +3240,14 @@ void Sema::checkLifetimeCaptureBy(FunctionDecl *FD, bool IsMemberFunction,
unsigned ArgIdx) {
if (!Attr)
return;

Expr *Captured = const_cast<Expr *>(GetArgAt(ArgIdx));
for (int CapturingParamIdx : Attr->params()) {
// lifetime_capture_by(this) case is handled in the lifetimebound expr
// initialization codepath.
if (CapturingParamIdx == LifetimeCaptureByAttr::THIS &&
isa<CXXConstructorDecl>(FD))
continue;
Expr *Capturing = const_cast<Expr *>(GetArgAt(CapturingParamIdx));
CapturingEntity CE{Capturing};
// Ensure that 'Captured' outlives the 'Capturing' entity.
Expand Down
26 changes: 26 additions & 0 deletions clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,29 @@ void use() {
}
} // namespace with_span
} // namespace inferred_capture_by

namespace on_constructor {
struct T {
T(const int& t [[clang::lifetime_capture_by(this)]]);
};
struct T2 {
T2(const int& t [[clang::lifetime_capture_by(x)]], int& x);
};
struct T3 {
T3(const T& t [[clang::lifetime_capture_by(this)]]);
};

int foo(const T& t);
int bar(const T& t[[clang::lifetimebound]]);

void test() {
auto x = foo(T(1)); // OK. no diagnosic
T(1); // OK. no diagnostic
T t(1); // expected-warning {{temporary whose address is used}}
auto y = bar(T(1)); // expected-warning {{temporary whose address is used}}
T3 t3(T(1)); // expected-warning {{temporary whose address is used}}

int a;
T2(1, a); // expected-warning {{object whose reference is captured by}}
}
} // namespace on_constructor