Skip to content

Commit 0205a04

Browse files
[libc] add basic lifetime annotations for support data structures
1 parent e91786a commit 0205a04

File tree

12 files changed

+89
-28
lines changed

12 files changed

+89
-28
lines changed

libc/src/__support/CPP/algorithm.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ template <class T = void> struct bit_and {};
2424
template <class T = void> struct bit_or {};
2525
template <class T = void> struct bit_xor {};
2626

27-
template <class T> LIBC_INLINE constexpr const T &max(const T &a, const T &b) {
27+
template <class T>
28+
LIBC_INLINE constexpr const T &max(LIBC_LIFETIMEBOUND const T &a,
29+
LIBC_LIFETIMEBOUND const T &b) {
2830
return (a < b) ? b : a;
2931
}
3032

31-
template <class T> LIBC_INLINE constexpr const T &min(const T &a, const T &b) {
33+
template <class T>
34+
LIBC_INLINE constexpr const T &min(LIBC_LIFETIMEBOUND const T &a,
35+
LIBC_LIFETIMEBOUND const T &b) {
3236
return (a < b) ? a : b;
3337
}
3438

libc/src/__support/CPP/array.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ template <class T, size_t N> struct array {
3131
LIBC_INLINE constexpr T *data() { return Data; }
3232
LIBC_INLINE constexpr const T *data() const { return Data; }
3333

34-
LIBC_INLINE constexpr T &front() { return Data[0]; }
35-
LIBC_INLINE constexpr const T &front() const { return Data[0]; }
34+
LIBC_INLINE constexpr T &front() LIBC_LIFETIMEBOUND { return Data[0]; }
35+
LIBC_INLINE constexpr const T &front() const LIBC_LIFETIMEBOUND {
36+
return Data[0];
37+
}
3638

37-
LIBC_INLINE constexpr T &back() { return Data[N - 1]; }
38-
LIBC_INLINE constexpr const T &back() const { return Data[N - 1]; }
39+
LIBC_INLINE constexpr T &back() LIBC_LIFETIMEBOUND { return Data[N - 1]; }
40+
LIBC_INLINE constexpr const T &back() const LIBC_LIFETIMEBOUND {
41+
return Data[N - 1];
42+
}
3943

40-
LIBC_INLINE constexpr T &operator[](size_t Index) { return Data[Index]; }
44+
LIBC_INLINE constexpr T &operator[](size_t Index) LIBC_LIFETIMEBOUND {
45+
return Data[Index];
46+
}
4147

42-
LIBC_INLINE constexpr const T &operator[](size_t Index) const {
48+
LIBC_INLINE constexpr const T &
49+
operator[](size_t Index) const LIBC_LIFETIMEBOUND {
4350
return Data[Index];
4451
}
4552

libc/src/__support/CPP/atomic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ template <typename T> struct Atomic {
256256
LIBC_INLINE void set(T rhs) { val = rhs; }
257257
};
258258

259-
template <typename T> struct AtomicRef {
259+
template <typename T> struct LIBC_GSL_POINTER AtomicRef {
260260
static_assert(is_trivially_copyable_v<T> && is_copy_constructible_v<T> &&
261261
is_move_constructible_v<T> && is_copy_assignable_v<T> &&
262262
is_move_assignable_v<T>,

libc/src/__support/CPP/mutex.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H
1010
#define LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H
1111

12+
#include "src/__support/macros/attributes.h"
1213
#include "src/__support/macros/config.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
@@ -28,14 +29,17 @@ template <typename MutexType> class lock_guard {
2829

2930
public:
3031
// Calls `m.lock()` upon resource acquisition.
31-
explicit lock_guard(MutexType &m) : mutex(m) { mutex.lock(); }
32+
LIBC_INLINE explicit lock_guard(LIBC_LIFETIMEBOUND MutexType &m) : mutex(m) {
33+
mutex.lock();
34+
}
3235

3336
// Acquires ownership of the mutex object `m` without attempting to lock
3437
// it. The behavior is undefined if the current thread does not hold the
3538
// lock on `m`. Does not call `m.lock()` upon resource acquisition.
36-
lock_guard(MutexType &m, adopt_lock_t /* t */) : mutex(m) {}
39+
LIBC_INLINE lock_guard(LIBC_LIFETIMEBOUND MutexType &m, adopt_lock_t /* t */)
40+
: mutex(m) {}
3741

38-
~lock_guard() { mutex.unlock(); }
42+
LIBC_INLINE ~lock_guard() { mutex.unlock(); }
3943

4044
// non-copyable
4145
lock_guard &operator=(const lock_guard &) = delete;

libc/src/__support/CPP/optional.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ template <typename T> class optional {
108108

109109
LIBC_INLINE constexpr void reset() { storage.reset(); }
110110

111-
LIBC_INLINE constexpr const T &value() const & {
111+
LIBC_INLINE constexpr const T &value() const &LIBC_LIFETIMEBOUND {
112112
return storage.stored_value;
113113
}
114114

115-
LIBC_INLINE constexpr T &value() & { return storage.stored_value; }
115+
LIBC_INLINE constexpr T &value() & LIBC_LIFETIMEBOUND {
116+
return storage.stored_value;
117+
}
116118

117119
LIBC_INLINE constexpr explicit operator bool() const {
118120
return storage.in_use;
@@ -122,10 +124,12 @@ template <typename T> class optional {
122124
return &storage.stored_value;
123125
}
124126
LIBC_INLINE constexpr T *operator->() { return &storage.stored_value; }
125-
LIBC_INLINE constexpr const T &operator*() const & {
127+
LIBC_INLINE constexpr const T &operator*() const &LIBC_LIFETIMEBOUND {
128+
return storage.stored_value;
129+
}
130+
LIBC_INLINE constexpr T &operator*() & LIBC_LIFETIMEBOUND {
126131
return storage.stored_value;
127132
}
128-
LIBC_INLINE constexpr T &operator*() & { return storage.stored_value; }
129133

130134
LIBC_INLINE constexpr T &&value() && { return move(storage.stored_value); }
131135
LIBC_INLINE constexpr T &&operator*() && {

libc/src/__support/CPP/span.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace cpp {
2828
// - No implicit type conversion (e.g. Span<B>, initialized with As where A
2929
// inherits from B),
3030
// - No reverse iterators
31-
template <typename T> class span {
31+
template <typename T> class LIBC_GSL_POINTER span {
3232
template <typename U>
3333
LIBC_INLINE_VAR static constexpr bool is_const_view_v =
3434
!cpp::is_const_v<U> && cpp::is_const_v<T> &&
@@ -64,11 +64,12 @@ template <typename T> class span {
6464

6565
template <typename U, size_t N,
6666
cpp::enable_if_t<is_compatible_v<U>, bool> = true>
67-
LIBC_INLINE constexpr span(U (&arr)[N]) : span_data(arr), span_size(N) {}
67+
LIBC_INLINE constexpr span(LIBC_LIFETIMEBOUND U (&arr)[N])
68+
: span_data(arr), span_size(N) {}
6869

6970
template <typename U, size_t N,
7071
cpp::enable_if_t<is_compatible_v<U>, bool> = true>
71-
LIBC_INLINE constexpr span(array<U, N> &arr)
72+
LIBC_INLINE constexpr span(LIBC_LIFETIMEBOUND array<U, N> &arr)
7273
: span_data(arr.data()), span_size(arr.size()) {}
7374

7475
template <typename U, cpp::enable_if_t<is_compatible_v<U>, bool> = true>

libc/src/__support/CPP/string.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,23 @@ class string {
106106
LIBC_INLINE constexpr const char *end() const { return data() + size_; }
107107
LIBC_INLINE char *end() { return data() + size_; }
108108

109-
LIBC_INLINE constexpr const char &front() const { return data()[0]; }
109+
LIBC_INLINE constexpr const char &front() const LIBC_LIFETIMEBOUND {
110+
return data()[0];
111+
}
110112
LIBC_INLINE char &front() { return data()[0]; }
111113

112-
LIBC_INLINE constexpr const char &back() const { return data()[size_ - 1]; }
114+
LIBC_INLINE constexpr const char &back() const LIBC_LIFETIMEBOUND {
115+
return data()[size_ - 1];
116+
}
113117
LIBC_INLINE char &back() { return data()[size_ - 1]; }
114118

115-
LIBC_INLINE constexpr const char &operator[](size_t index) const {
119+
LIBC_INLINE constexpr const char &
120+
operator[](size_t index) const LIBC_LIFETIMEBOUND {
121+
return data()[index];
122+
}
123+
LIBC_INLINE char &operator[](size_t index) LIBC_LIFETIMEBOUND {
116124
return data()[index];
117125
}
118-
LIBC_INLINE char &operator[](size_t index) { return data()[index]; }
119126

120127
LIBC_INLINE const char *c_str() const { return data(); }
121128

libc/src/__support/CPP/string_view.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
namespace LIBC_NAMESPACE_DECL {
1919
namespace cpp {
2020

21+
class string;
2122
// This is very simple alternate of the std::string_view class. There is no
2223
// bounds check performed in any of the methods. The callers are expected to
2324
// do the checks before invoking the methods.
2425
//
2526
// This class will be extended as needed in future.
26-
class string_view {
27+
class LIBC_GSL_POINTER string_view {
2728
private:
2829
const char *Data;
2930
size_t Len;
@@ -77,6 +78,10 @@ class string_view {
7778
LIBC_INLINE constexpr string_view(const char *Str, size_t N)
7879
: Data(Str), Len(N) {}
7980

81+
template <size_t N>
82+
LIBC_INLINE constexpr string_view(LIBC_LIFETIMEBOUND const char (&Str)[N])
83+
: Data(Str), Len(N) {}
84+
8085
LIBC_INLINE constexpr const char *data() const { return Data; }
8186

8287
// Returns the size of the string_view.

libc/src/__support/CPP/stringstream.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class StringStream {
4848
// null terminator was not explicitly written, then the return value
4949
// will not include one. In order to produce a string_view to a null
5050
// terminated string, write ENDS explicitly.
51-
string_view str() const { return string_view(data.data(), write_ptr); }
51+
[[nodiscard]] LIBC_INLINE string_view str() const {
52+
return string_view(data.data(), write_ptr);
53+
}
5254

5355
// Write the characters from |str| to the stream.
5456
StringStream &operator<<(string_view str) {

libc/src/__support/CPP/utility/forward.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ namespace cpp {
1818

1919
// forward
2020
template <typename T>
21-
LIBC_INLINE constexpr T &&forward(remove_reference_t<T> &value) {
21+
LIBC_INLINE constexpr T &&
22+
forward(LIBC_LIFETIMEBOUND remove_reference_t<T> &value) {
2223
return static_cast<T &&>(value);
2324
}
2425

2526
template <typename T>
26-
LIBC_INLINE constexpr T &&forward(remove_reference_t<T> &&value) {
27+
LIBC_INLINE constexpr T &&
28+
forward(LIBC_LIFETIMEBOUND remove_reference_t<T> &&value) {
2729
static_assert(!is_lvalue_reference_v<T>,
2830
"cannot forward an rvalue as an lvalue");
2931
return static_cast<T &&>(value);

0 commit comments

Comments
 (0)