From 8d10e497d98b3618b0b09baa081f56be3c5d3c46 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Thu, 24 Oct 2024 12:12:37 +0200 Subject: [PATCH 1/3] Add clang::lifetimebound annotation to ArrayRef constructors. --- llvm/include/llvm/ADT/ArrayRef.h | 5 +++-- llvm/include/llvm/Support/Compiler.h | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h index bf6b55923b84b..c9615e7b61a93 100644 --- a/llvm/include/llvm/ADT/ArrayRef.h +++ b/llvm/include/llvm/ADT/ArrayRef.h @@ -70,7 +70,7 @@ namespace llvm { /*implicit*/ ArrayRef(std::nullopt_t) {} /// Construct an ArrayRef from a single element. - /*implicit*/ ArrayRef(const T &OneElt) + /*implicit*/ ArrayRef(const T &OneElt LLVM_LIFETIME_BOUND) : Data(&OneElt), Length(1) {} /// Construct an ArrayRef from a pointer and length. @@ -103,7 +103,8 @@ namespace llvm { /// Construct an ArrayRef from a C array. template - /*implicit*/ constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {} + /*implicit*/ constexpr ArrayRef(const T (&Arr LLVM_LIFETIME_BOUND)[N]) + : Data(Arr), Length(N) {} /// Construct an ArrayRef from a std::initializer_list. #if LLVM_GNUC_PREREQ(9, 0, 0) diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h index 591e7647795bb..3b03c2851a421 100644 --- a/llvm/include/llvm/Support/Compiler.h +++ b/llvm/include/llvm/Support/Compiler.h @@ -413,6 +413,12 @@ #define LLVM_GSL_POINTER #endif +#if LLVM_HAS_CPP_ATTRIBUTE(clang::lifetimebound) +#define LLVM_LIFETIME_BOUND [[clang::lifetimebound]] +#else +#define LLVM_LLVM_LIFETIME_BOUND +#endif + #if LLVM_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L #define LLVM_CTOR_NODISCARD [[nodiscard]] #else From 0ac2069e5cb3e8d331bc8381cfed3c75f9dc890b Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Thu, 24 Oct 2024 14:22:23 +0200 Subject: [PATCH 2/3] Annotate more constructors. --- llvm/include/llvm/ADT/ArrayRef.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h index c9615e7b61a93..1139fd81cbd07 100644 --- a/llvm/include/llvm/ADT/ArrayRef.h +++ b/llvm/include/llvm/ADT/ArrayRef.h @@ -71,14 +71,15 @@ namespace llvm { /// Construct an ArrayRef from a single element. /*implicit*/ ArrayRef(const T &OneElt LLVM_LIFETIME_BOUND) - : Data(&OneElt), Length(1) {} + : Data(&OneElt), Length(1) {} /// Construct an ArrayRef from a pointer and length. - constexpr /*implicit*/ ArrayRef(const T *data, size_t length) + constexpr /*implicit*/ ArrayRef(const T *data LLVM_LIFETIME_BOUND, + size_t length) : Data(data), Length(length) {} /// Construct an ArrayRef from a range. - constexpr ArrayRef(const T *begin, const T *end) + constexpr ArrayRef(const T *begin LLVM_LIFETIME_BOUND, const T *end) : Data(begin), Length(end - begin) { assert(begin <= end); } @@ -114,7 +115,8 @@ namespace llvm { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Winit-list-lifetime" #endif - constexpr /*implicit*/ ArrayRef(std::initializer_list Vec) + constexpr /*implicit*/ ArrayRef( + std::initializer_list Vec LLVM_LIFETIME_BOUND) : Data(Vec.begin() == Vec.end() ? (T *)nullptr : Vec.begin()), Length(Vec.size()) {} #if LLVM_GNUC_PREREQ(9, 0, 0) From 80d18f9e6b05dc6adf5477a9bafe2e18f9740480 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Fri, 25 Oct 2024 21:25:08 +0200 Subject: [PATCH 3/3] Fix a typo --- llvm/include/llvm/Support/Compiler.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h index 3b03c2851a421..f9c57b89f1f03 100644 --- a/llvm/include/llvm/Support/Compiler.h +++ b/llvm/include/llvm/Support/Compiler.h @@ -416,7 +416,7 @@ #if LLVM_HAS_CPP_ATTRIBUTE(clang::lifetimebound) #define LLVM_LIFETIME_BOUND [[clang::lifetimebound]] #else -#define LLVM_LLVM_LIFETIME_BOUND +#define LLVM_LIFETIME_BOUND #endif #if LLVM_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L