Skip to content

[clang] Infer lifetime_capture_by for map's subscript operator. #118078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2024
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
58 changes: 36 additions & 22 deletions clang/lib/Sema/SemaAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,34 +270,48 @@ void Sema::inferLifetimeBoundAttribute(FunctionDecl *FD) {
}

void Sema::inferLifetimeCaptureByAttribute(FunctionDecl *FD) {
if (!FD)
auto *MD = dyn_cast_if_present<CXXMethodDecl>(FD);
if (!MD || !MD->getParent()->isInStdNamespace())
return;
auto *MD = dyn_cast<CXXMethodDecl>(FD);
if (!MD || !MD->getIdentifier() || !MD->getParent()->isInStdNamespace())
auto Annotate = [this](const FunctionDecl *MD) {
// Do not infer if any parameter is explicitly annotated.
for (ParmVarDecl *PVD : MD->parameters())
if (PVD->hasAttr<LifetimeCaptureByAttr>())
return;
for (ParmVarDecl *PVD : MD->parameters()) {
// Methods in standard containers that capture values typically accept
// reference-type parameters, e.g., `void push_back(const T& value)`.
// We only apply the lifetime_capture_by attribute to parameters of
// pointer-like reference types (`const T&`, `T&&`).
if (PVD->getType()->isReferenceType() &&
sema::isPointerLikeType(PVD->getType().getNonReferenceType())) {
int CaptureByThis[] = {LifetimeCaptureByAttr::THIS};
PVD->addAttr(
LifetimeCaptureByAttr::CreateImplicit(Context, CaptureByThis, 1));
}
}
};

if (!MD->getIdentifier()) {
static const llvm::StringSet<> MapLikeContainer{
"map",
"multimap",
"unordered_map",
"unordered_multimap",
};
// Infer for the map's operator []:
// std::map<string_view, ...> m;
// m[ReturnString(..)] = ...; // !dangling references in m.
if (MD->getOverloadedOperator() == OO_Subscript &&
MapLikeContainer.contains(MD->getParent()->getName()))
Annotate(MD);
return;
// FIXME: Infer for operator[] for map-like containers. For example:
// std::map<string_view, ...> m;
// m[ReturnString(..)] = ...;
}
static const llvm::StringSet<> CapturingMethods{"insert", "push",
"push_front", "push_back"};
if (!CapturingMethods.contains(MD->getName()))
return;
// Do not infer if any parameter is explicitly annotated.
for (ParmVarDecl *PVD : MD->parameters())
if (PVD->hasAttr<LifetimeCaptureByAttr>())
return;
for (ParmVarDecl *PVD : MD->parameters()) {
// Methods in standard containers that capture values typically accept
// reference-type parameters, e.g., `void push_back(const T& value)`.
// We only apply the lifetime_capture_by attribute to parameters of
// pointer-like reference types (`const T&`, `T&&`).
if (PVD->getType()->isReferenceType() &&
sema::isPointerLikeType(PVD->getType().getNonReferenceType())) {
int CaptureByThis[] = {LifetimeCaptureByAttr::THIS};
PVD->addAttr(
LifetimeCaptureByAttr::CreateImplicit(Context, CaptureByThis, 1));
}
}
Annotate(MD);
}

void Sema::inferNullableClassAttribute(CXXRecordDecl *CRD) {
Expand Down
16 changes: 16 additions & 0 deletions clang/test/AST/attr-lifetime-capture-by.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ struct vector {

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
Expand Down Expand Up @@ -99,3 +105,13 @@ std::vector<int> ints;
// CHECK-NEXT: ParmVarDecl {{.*}} 'iterator'
// CHECK-NEXT: ParmVarDecl {{.*}} 'int &&'
// CHECK-NOT: LifetimeCaptureByAttr

std::map<View, int> map;
// CHECK: ClassTemplateSpecializationDecl {{.*}} struct map definition 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