Skip to content

Commit 902b2a2

Browse files
authored
[clang] Add lifetimebound attr to std::span/std::string_view constructor (#103716)
With this patch, clang now automatically adds ``[[clang::lifetimebound]]`` to the parameters of `std::span, std::string_view` constructors, this enables Clang to capture more cases where the returned reference outlives the object. Fixes #100567
1 parent 866bec7 commit 902b2a2

File tree

5 files changed

+83
-19
lines changed

5 files changed

+83
-19
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ Attribute Changes in Clang
220220
- ``[[clang::lifetimebound]]`` is now explicitly disallowed on explicit object member functions
221221
where they were previously silently ignored.
222222

223+
- Clang now automatically adds ``[[clang::lifetimebound]]`` to the parameters of
224+
``std::span, std::string_view`` constructors, this enables Clang to capture
225+
more cases where the returned reference outlives the object.
226+
(#GH100567)
227+
223228
Improvements to Clang's diagnostics
224229
-----------------------------------
225230

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,9 @@ class Sema final : public SemaBase {
18061806
/// Add [[gsl::Owner]] and [[gsl::Pointer]] attributes for std:: types.
18071807
void inferGslOwnerPointerAttribute(CXXRecordDecl *Record);
18081808

1809+
/// Add [[clang:::lifetimebound]] attr for std:: functions and methods.
1810+
void inferLifetimeBoundAttribute(FunctionDecl *FD);
1811+
18091812
/// Add [[gsl::Pointer]] attributes for std:: types.
18101813
void inferGslPointerAttribute(TypedefNameDecl *TD);
18111814

clang/lib/Sema/SemaAttr.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,59 @@ void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) {
216216
inferGslPointerAttribute(Record, Record);
217217
}
218218

219+
void Sema::inferLifetimeBoundAttribute(FunctionDecl *FD) {
220+
if (FD->getNumParams() == 0)
221+
return;
222+
223+
if (unsigned BuiltinID = FD->getBuiltinID()) {
224+
// Add lifetime attribute to std::move, std::fowrard et al.
225+
switch (BuiltinID) {
226+
case Builtin::BIaddressof:
227+
case Builtin::BI__addressof:
228+
case Builtin::BI__builtin_addressof:
229+
case Builtin::BIas_const:
230+
case Builtin::BIforward:
231+
case Builtin::BIforward_like:
232+
case Builtin::BImove:
233+
case Builtin::BImove_if_noexcept:
234+
if (ParmVarDecl *P = FD->getParamDecl(0u);
235+
!P->hasAttr<LifetimeBoundAttr>())
236+
P->addAttr(
237+
LifetimeBoundAttr::CreateImplicit(Context, FD->getLocation()));
238+
break;
239+
default:
240+
break;
241+
}
242+
return;
243+
}
244+
if (auto *CMD = dyn_cast<CXXMethodDecl>(FD)) {
245+
const auto *CRD = CMD->getParent();
246+
if (!CRD->isInStdNamespace() || !CRD->getIdentifier())
247+
return;
248+
249+
if (isa<CXXConstructorDecl>(CMD)) {
250+
auto *Param = CMD->getParamDecl(0);
251+
if (Param->hasAttr<LifetimeBoundAttr>())
252+
return;
253+
if (CRD->getName() == "basic_string_view" &&
254+
Param->getType()->isPointerType()) {
255+
// construct from a char array pointed by a pointer.
256+
// basic_string_view(const CharT* s);
257+
// basic_string_view(const CharT* s, size_type count);
258+
Param->addAttr(
259+
LifetimeBoundAttr::CreateImplicit(Context, FD->getLocation()));
260+
} else if (CRD->getName() == "span") {
261+
// construct from a reference of array.
262+
// span(std::type_identity_t<element_type> (&arr)[N]);
263+
const auto *LRT = Param->getType()->getAs<LValueReferenceType>();
264+
if (LRT && LRT->getPointeeType().IgnoreParens()->isArrayType())
265+
Param->addAttr(
266+
LifetimeBoundAttr::CreateImplicit(Context, FD->getLocation()));
267+
}
268+
}
269+
}
270+
}
271+
219272
void Sema::inferNullableClassAttribute(CXXRecordDecl *CRD) {
220273
static const llvm::StringSet<> Nullable{
221274
"auto_ptr", "shared_ptr", "unique_ptr", "exception_ptr",

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16608,27 +16608,9 @@ void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) {
1660816608
default:
1660916609
break;
1661016610
}
16611-
16612-
// Add lifetime attribute to std::move, std::fowrard et al.
16613-
switch (BuiltinID) {
16614-
case Builtin::BIaddressof:
16615-
case Builtin::BI__addressof:
16616-
case Builtin::BI__builtin_addressof:
16617-
case Builtin::BIas_const:
16618-
case Builtin::BIforward:
16619-
case Builtin::BIforward_like:
16620-
case Builtin::BImove:
16621-
case Builtin::BImove_if_noexcept:
16622-
if (ParmVarDecl *P = FD->getParamDecl(0u);
16623-
!P->hasAttr<LifetimeBoundAttr>())
16624-
P->addAttr(
16625-
LifetimeBoundAttr::CreateImplicit(Context, FD->getLocation()));
16626-
break;
16627-
default:
16628-
break;
16629-
}
1663016611
}
1663116612

16613+
inferLifetimeBoundAttribute(FD);
1663216614
AddKnownFunctionAttributesForReplaceableGlobalAllocationFunction(FD);
1663316615

1663416616
// If C++ exceptions are enabled but we are told extern "C" functions cannot

clang/test/SemaCXX/attr-lifetimebound.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ template <class T> T *addressof(T &arg) {
238238
&const_cast<char &>(reinterpret_cast<const volatile char &>(arg)));
239239
}
240240

241+
template<typename T>
242+
struct basic_string_view {
243+
basic_string_view(const T *);
244+
};
245+
246+
template <class T> struct span {
247+
template<size_t _ArrayExtent>
248+
span(const T (&__arr)[_ArrayExtent]) noexcept;
249+
};
250+
241251
} // namespace foo
242252
} // namespace std
243253

@@ -266,3 +276,14 @@ namespace move_forward_et_al_examples {
266276
S *AddressOfOk = std::addressof(X);
267277
} // namespace move_forward_et_al_examples
268278

279+
namespace ctor_cases {
280+
std::basic_string_view<char> test1() {
281+
char abc[10];
282+
return abc; // expected-warning {{address of stack memory associated with local variable}}
283+
}
284+
285+
std::span<int> test2() {
286+
int abc[10];
287+
return abc; // expected-warning {{address of stack memory associated with local variable}}
288+
}
289+
} // namespace ctor_cases

0 commit comments

Comments
 (0)