Skip to content

Commit

Permalink
Add ABSL_ATTRIBUTE_LIFETIME_BOUND for remaining pointers and file d…
Browse files Browse the repository at this point in the history
…escriptors

passed as function and constructor parameters, where the pointed to object must
outlive the function result or the constructed object.

PiperOrigin-RevId: 679114168
  • Loading branch information
QrczakMK committed Sep 26, 2024
1 parent 6e996c3 commit 12ad44a
Show file tree
Hide file tree
Showing 23 changed files with 87 additions and 47 deletions.
7 changes: 4 additions & 3 deletions riegeli/base/chain_details.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ class Chain::BlockIterator : public WithCompare<BlockIterator> {

BlockIterator() = default;

explicit BlockIterator(const Chain* chain, size_t block_index);
explicit BlockIterator(const Chain* chain ABSL_ATTRIBUTE_LIFETIME_BOUND,
size_t block_index);

BlockIterator(const BlockIterator& that) = default;
BlockIterator& operator=(const BlockIterator& that) = default;
Expand Down Expand Up @@ -768,8 +769,8 @@ inline const T* Chain::BlockRef::external_object() const {
}
}

inline Chain::BlockIterator::BlockIterator(const Chain* chain,
size_t block_index)
inline Chain::BlockIterator::BlockIterator(
const Chain* chain ABSL_ATTRIBUTE_LIFETIME_BOUND, size_t block_index)
: chain_(chain),
ptr_((ABSL_PREDICT_FALSE(chain_ == nullptr) ? kBeginShortData
: chain_->begin_ == chain_->end_
Expand Down
6 changes: 4 additions & 2 deletions riegeli/base/intrusive_shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ class
// Creates an `IntrusiveSharedPtr` holding `ptr`.
//
// Takes ownership of `ptr` unless the second parameter is `kShareOwnership`.
explicit IntrusiveSharedPtr(T* ptr, PassOwnership = kPassOwnership) noexcept
explicit IntrusiveSharedPtr(T* ptr ABSL_ATTRIBUTE_LIFETIME_BOUND,
PassOwnership = kPassOwnership) noexcept
: ptr_(ptr) {}
explicit IntrusiveSharedPtr(T* ptr, ShareOwnership) noexcept
explicit IntrusiveSharedPtr(T* ptr ABSL_ATTRIBUTE_LIFETIME_BOUND,
ShareOwnership) noexcept
: ptr_(Ref(ptr)) {}

// Creates an `IntrusiveSharedPtr` holding a constructed value.
Expand Down
5 changes: 3 additions & 2 deletions riegeli/bytes/absl_stringify_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AbslStringifyWriter : public BufferedWriter {
explicit AbslStringifyWriter(Closed) noexcept : BufferedWriter(kClosed) {}

// Will write to `*dest`.
explicit AbslStringifyWriter(Dest dest)
explicit AbslStringifyWriter(Dest dest ABSL_ATTRIBUTE_LIFETIME_BOUND)
: dest_(std::move(RIEGELI_ASSERT_NOTNULL(dest))) {}

AbslStringifyWriter(AbslStringifyWriter&& that) = default;
Expand Down Expand Up @@ -81,7 +81,8 @@ class AbslStringifyWriter<WriterAbslStringifySink*>
: PrefixLimitingWriter(kClosed) {}

// Will write to `*dest`.
explicit AbslStringifyWriter(WriterAbslStringifySink* dest)
explicit AbslStringifyWriter(
WriterAbslStringifySink* dest ABSL_ATTRIBUTE_LIFETIME_BOUND)
: PrefixLimitingWriter(RIEGELI_ASSERT_NOTNULL(dest)->dest()),
dest_(dest) {}

Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/array_backward_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class ArrayBackwardWriter : public ArrayBackwardWriterBase {
template <typename DependentDest = Dest,
std::enable_if_t<
std::is_same<DependentDest, absl::Span<char>>::value, int> = 0>
explicit ArrayBackwardWriter(char* dest, size_t size);
explicit ArrayBackwardWriter(char* dest ABSL_ATTRIBUTE_LIFETIME_BOUND,
size_t size);

ArrayBackwardWriter(ArrayBackwardWriter&& that) = default;
ArrayBackwardWriter& operator=(ArrayBackwardWriter&& that) = default;
Expand Down Expand Up @@ -153,7 +154,8 @@ explicit ArrayBackwardWriter(Dest&& dest)
absl::negation<std::is_pointer<std::remove_reference_t<Dest>>>>::
value,
DeleteCtad<Dest&&>, InitializerTargetT<Dest>>>;
explicit ArrayBackwardWriter(char* dest, size_t size)
explicit ArrayBackwardWriter(char* dest ABSL_ATTRIBUTE_LIFETIME_BOUND,
size_t size)
-> ArrayBackwardWriter<absl::Span<char>>;
#endif

Expand Down
5 changes: 3 additions & 2 deletions riegeli/bytes/array_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ArrayWriter : public ArrayWriterBase {
template <typename DependentDest = Dest,
std::enable_if_t<
std::is_same<DependentDest, absl::Span<char>>::value, int> = 0>
explicit ArrayWriter(char* dest, size_t size);
explicit ArrayWriter(char* dest ABSL_ATTRIBUTE_LIFETIME_BOUND, size_t size);

ArrayWriter(ArrayWriter&& that) = default;
ArrayWriter& operator=(ArrayWriter&& that) = default;
Expand Down Expand Up @@ -258,7 +258,8 @@ template <typename Dest>
template <
typename DependentDest,
std::enable_if_t<std::is_same<DependentDest, absl::Span<char>>::value, int>>
inline ArrayWriter<Dest>::ArrayWriter(char* dest, size_t size)
inline ArrayWriter<Dest>::ArrayWriter(char* dest ABSL_ATTRIBUTE_LIFETIME_BOUND,
size_t size)
: ArrayWriter(absl::MakeSpan(dest, size)) {}

template <typename Dest>
Expand Down
9 changes: 6 additions & 3 deletions riegeli/bytes/cfile_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class
OwnedCFile() = default;

// Creates an `OwnedCFile` which owns `file`.
explicit OwnedCFile(FILE* file) noexcept : file_(file) {}
explicit OwnedCFile(FILE* file ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept
: file_(file) {}

// The moved-from `FILE*` is left absent.
OwnedCFile(OwnedCFile&& that) = default;
Expand Down Expand Up @@ -139,7 +140,8 @@ class
UnownedCFile() = default;

// Creates an `UnownedCFile` which stores `file`.
explicit UnownedCFile(FILE* file) noexcept : file_(file) {}
explicit UnownedCFile(FILE* file ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept
: file_(file) {}

UnownedCFile(const UnownedCFile& that) = default;
UnownedCFile& operator=(const UnownedCFile& that) = default;
Expand Down Expand Up @@ -233,7 +235,8 @@ class

// Creates a `CFileHandle` which points to `target`.
template <typename T, std::enable_if_t<IsValidCFileTarget<T>::value, int> = 0>
explicit CFileHandle(T* target) : methods_(&kMethods<T>), target_(target) {}
explicit CFileHandle(T* target ABSL_ATTRIBUTE_LIFETIME_BOUND)
: methods_(&kMethods<T>), target_(target) {}

CFileHandle(const CFileHandle& that) = default;
CFileHandle& operator=(const CFileHandle& that) = default;
Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/cfile_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ class CFileReader : public CFileReaderBase {
template <typename DependentSrc = Src,
std::enable_if_t<std::is_constructible<DependentSrc, FILE*>::value,
int> = 0>
explicit CFileReader(FILE* src, Options options = Options());
explicit CFileReader(FILE* src ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options = Options());

// Opens a file for reading.
//
Expand Down Expand Up @@ -400,7 +401,8 @@ template <typename Src>
template <
typename DependentSrc,
std::enable_if_t<std::is_constructible<DependentSrc, FILE*>::value, int>>
inline CFileReader<Src>::CFileReader(FILE* src, Options options)
inline CFileReader<Src>::CFileReader(FILE* src ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options)
: CFileReader(riegeli::Maker(src), std::move(options)) {}

template <typename Src>
Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/cfile_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ class CFileWriter : public CFileWriterBase {
template <typename DependentDest = Dest,
std::enable_if_t<std::is_constructible<DependentDest, FILE*>::value,
int> = 0>
explicit CFileWriter(FILE* dest, Options options = Options());
explicit CFileWriter(FILE* dest ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options = Options());

// Opens a file for writing.
//
Expand Down Expand Up @@ -515,7 +516,8 @@ template <typename Dest>
template <
typename DependentDest,
std::enable_if_t<std::is_constructible<DependentDest, FILE*>::value, int>>
inline CFileWriter<Dest>::CFileWriter(FILE* dest, Options options)
inline CFileWriter<Dest>::CFileWriter(FILE* dest ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options)
: CFileWriter(riegeli::Maker(dest), std::move(options)) {}

template <typename Dest>
Expand Down
7 changes: 4 additions & 3 deletions riegeli/bytes/fd_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class
OwnedFd() = default;

// Creates an `OwnedFd` which owns `fd`.
explicit OwnedFd(int fd) noexcept : fd_(fd) {}
explicit OwnedFd(int fd ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept : fd_(fd) {}

// The moved-from fd is left absent.
OwnedFd(OwnedFd&& that) noexcept : fd_(that.Release()) {}
Expand Down Expand Up @@ -144,7 +144,7 @@ class UnownedFd : public WithEqual<UnownedFd> {
UnownedFd() = default;

// Creates an `UnownedFd` which stores `fd`.
explicit UnownedFd(int fd) noexcept : fd_(fd) {}
explicit UnownedFd(int fd ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept : fd_(fd) {}

UnownedFd(const UnownedFd& that) = default;
UnownedFd& operator=(const UnownedFd& that) = default;
Expand Down Expand Up @@ -249,7 +249,8 @@ class

// Creates an `FdHandle` which points to `target`.
template <typename T, std::enable_if_t<IsValidFdTarget<T>::value, int> = 0>
explicit FdHandle(T* target) : methods_(&kMethods<T>), target_(target) {}
explicit FdHandle(T* target ABSL_ATTRIBUTE_LIFETIME_BOUND)
: methods_(&kMethods<T>), target_(target) {}

FdHandle(const FdHandle& that) = default;
FdHandle& operator=(const FdHandle& that) = default;
Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/fd_mmap_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ class FdMMapReader : public FdMMapReaderBase {
template <typename DependentSrc = Src,
std::enable_if_t<std::is_constructible<DependentSrc, int>::value,
int> = 0>
explicit FdMMapReader(int src, Options options = Options());
explicit FdMMapReader(int src ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options = Options());

// Opens a file for reading.
//
Expand Down Expand Up @@ -386,7 +387,8 @@ template <typename Src>
template <
typename DependentSrc,
std::enable_if_t<std::is_constructible<DependentSrc, int>::value, int>>
inline FdMMapReader<Src>::FdMMapReader(int src, Options options)
inline FdMMapReader<Src>::FdMMapReader(int src ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options)
: FdMMapReader(riegeli::Maker(src), std::move(options)) {}

template <typename Src>
Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/fd_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ class FdReader : public FdReaderBase {
template <typename DependentSrc = Src,
std::enable_if_t<std::is_constructible<DependentSrc, int>::value,
int> = 0>
explicit FdReader(int src, Options options = Options());
explicit FdReader(int src ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options = Options());

// Opens a file for reading.
//
Expand Down Expand Up @@ -501,7 +502,8 @@ template <typename Src>
template <
typename DependentSrc,
std::enable_if_t<std::is_constructible<DependentSrc, int>::value, int>>
inline FdReader<Src>::FdReader(int src, Options options)
inline FdReader<Src>::FdReader(int src ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options)
: FdReader(riegeli::Maker(src), std::move(options)) {}

template <typename Src>
Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/fd_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,8 @@ class FdWriter : public FdWriterBase {
template <typename DependentDest = Dest,
std::enable_if_t<std::is_constructible<DependentDest, int>::value,
int> = 0>
explicit FdWriter(int dest, Options options = Options());
explicit FdWriter(int dest ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options = Options());

// Opens a file for writing.
//
Expand Down Expand Up @@ -687,7 +688,8 @@ template <typename Dest>
template <
typename DependentDest,
std::enable_if_t<std::is_constructible<DependentDest, int>::value, int>>
inline FdWriter<Dest>::FdWriter(int dest, Options options)
inline FdWriter<Dest>::FdWriter(int dest ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options)
: FdWriter(riegeli::Maker(dest), std::move(options)) {}

template <typename Dest>
Expand Down
7 changes: 5 additions & 2 deletions riegeli/bytes/limiting_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ class ScopedLimiter {
// would require an unusual behavior of failing when the limit is exceeded.
// That behavior would not be useful in practice because reading would never
// end cleanly.
explicit ScopedLimiter(LimitingReaderBase* reader, Options options);
explicit ScopedLimiter(LimitingReaderBase* reader
ABSL_ATTRIBUTE_LIFETIME_BOUND,
Options options);

ScopedLimiter(const ScopedLimiter&) = delete;
ScopedLimiter& operator=(const ScopedLimiter&) = delete;
Expand Down Expand Up @@ -584,7 +586,8 @@ bool LimitingReader<Src>::SyncImpl(SyncType sync_type) {
return sync_ok;
}

inline ScopedLimiter::ScopedLimiter(LimitingReaderBase* reader, Options options)
inline ScopedLimiter::ScopedLimiter(
LimitingReaderBase* reader ABSL_ATTRIBUTE_LIFETIME_BOUND, Options options)
: reader_(RIEGELI_ASSERT_NOTNULL(reader)),
old_max_pos_(reader_->max_pos()),
old_exact_(reader_->exact()),
Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/pullable_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <memory>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "absl/functional/function_ref.h"
#include "absl/strings/cord.h"
Expand Down Expand Up @@ -167,7 +168,7 @@ class PullableReader : public Reader {
// position reflects what has been read from the source and must not be changed.
class PullableReader::BehindScratch {
public:
explicit BehindScratch(PullableReader* context);
explicit BehindScratch(PullableReader* context ABSL_ATTRIBUTE_LIFETIME_BOUND);

BehindScratch(BehindScratch&& that) = default;
BehindScratch& operator=(BehindScratch&&) = delete;
Expand Down Expand Up @@ -209,7 +210,8 @@ inline bool PullableReader::scratch_used() const {
return scratch_ != nullptr && !scratch_->buffer.empty();
}

inline PullableReader::BehindScratch::BehindScratch(PullableReader* context)
inline PullableReader::BehindScratch::BehindScratch(
PullableReader* context ABSL_ATTRIBUTE_LIFETIME_BOUND)
: context_(context) {
if (ABSL_PREDICT_FALSE(context_->scratch_used())) Enter();
}
Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/pushable_backward_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <memory>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
Expand Down Expand Up @@ -177,7 +178,8 @@ class PushableBackwardWriter : public BackwardWriter {
// destination and must not be changed.
class PushableBackwardWriter::BehindScratch {
public:
explicit BehindScratch(PushableBackwardWriter* context);
explicit BehindScratch(
PushableBackwardWriter* context ABSL_ATTRIBUTE_LIFETIME_BOUND);

BehindScratch(BehindScratch&& that) = default;
BehindScratch& operator=(BehindScratch&&) = delete;
Expand Down Expand Up @@ -222,7 +224,7 @@ inline bool PushableBackwardWriter::scratch_used() const {
}

inline PushableBackwardWriter::BehindScratch::BehindScratch(
PushableBackwardWriter* context)
PushableBackwardWriter* context ABSL_ATTRIBUTE_LIFETIME_BOUND)
: context_(context) {
if (ABSL_PREDICT_FALSE(context_->scratch_used())) Enter();
}
Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/pushable_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <memory>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
Expand Down Expand Up @@ -183,7 +184,7 @@ class PushableWriter : public Writer {
// destination and must not be changed.
class PushableWriter::BehindScratch {
public:
explicit BehindScratch(PushableWriter* context);
explicit BehindScratch(PushableWriter* context ABSL_ATTRIBUTE_LIFETIME_BOUND);

BehindScratch(BehindScratch&& that) = default;
BehindScratch& operator=(BehindScratch&&) = delete;
Expand Down Expand Up @@ -225,7 +226,8 @@ inline bool PushableWriter::scratch_used() const {
return scratch_ != nullptr && !scratch_->buffer.empty();
}

inline PushableWriter::BehindScratch::BehindScratch(PushableWriter* context)
inline PushableWriter::BehindScratch::BehindScratch(
PushableWriter* context ABSL_ATTRIBUTE_LIFETIME_BOUND)
: context_(context) {
if (ABSL_PREDICT_FALSE(context_->scratch_used())) Enter();
}
Expand Down
6 changes: 4 additions & 2 deletions riegeli/bytes/string_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class StringReader : public StringReaderBase {
template <typename DependentSrc = Src,
std::enable_if_t<
std::is_same<DependentSrc, absl::string_view>::value, int> = 0>
explicit StringReader(const char* src, size_t size);
explicit StringReader(const char* src ABSL_ATTRIBUTE_LIFETIME_BOUND,
size_t size);

StringReader(StringReader&& that) = default;
StringReader& operator=(StringReader&& that) = default;
Expand Down Expand Up @@ -221,7 +222,8 @@ template <typename Src>
template <
typename DependentSrc,
std::enable_if_t<std::is_same<DependentSrc, absl::string_view>::value, int>>
inline StringReader<Src>::StringReader(const char* src, size_t size)
inline StringReader<Src>::StringReader(
const char* src ABSL_ATTRIBUTE_LIFETIME_BOUND, size_t size)
: StringReader(absl::string_view(src, size)) {}

template <typename Src>
Expand Down
2 changes: 1 addition & 1 deletion riegeli/bytes/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class WriterAbslStringifySink {
WriterAbslStringifySink() = default;

// Will write to `*dest`.
explicit WriterAbslStringifySink(Writer* dest)
explicit WriterAbslStringifySink(Writer* dest ABSL_ATTRIBUTE_LIFETIME_BOUND)
: dest_(RIEGELI_ASSERT_NOTNULL(dest)) {}

WriterAbslStringifySink(WriterAbslStringifySink&& that) = default;
Expand Down
Loading

0 comments on commit 12ad44a

Please sign in to comment.