Skip to content

Commit

Permalink
Replace ABSL_ATTRIBUTE_* with base/ macros.
Browse files Browse the repository at this point in the history
Approved on
https://groups.google.com/a/chromium.org/g/cxx/c/lVQOJTng1RU.

Bug: none
Change-Id: Ibe0f8f16bce55b3ede4c1e1d2720e83e8dd4faf0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5852373
Commit-Queue: Peter Kasting <[email protected]>
Reviewed-by: danakj <[email protected]>
Auto-Submit: Peter Kasting <[email protected]>
Code-Coverage: [email protected] <[email protected]>
Owners-Override: danakj <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1356840}
  • Loading branch information
pkasting authored and Chromium LUCI CQ committed Sep 18, 2024
1 parent 5edbc5a commit 4b18d0c
Show file tree
Hide file tree
Showing 108 changed files with 356 additions and 262 deletions.
1 change: 1 addition & 0 deletions DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -4707,6 +4707,7 @@ include_rules = [
# //styleguide/c++/c++-features.md.
'+third_party/abseil-cpp',
'-third_party/abseil-cpp/absl/algorithm/container.h',
'-third_party/abseil-cpp/absl/base/attributes.h',
'-third_party/abseil-cpp/absl/base/no_destructor.h',
'-third_party/abseil-cpp/absl/base/nullability.h',
'-third_party/abseil-cpp/absl/container',
Expand Down
1 change: 1 addition & 0 deletions ash/assistant/ui/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ include_rules = [
# Please keep this section in sync with //DEPS.
'+third_party/abseil-cpp',
'-third_party/abseil-cpp/absl/algorithm/container.h',
'-third_party/abseil-cpp/absl/base/attributes.h',
'-third_party/abseil-cpp/absl/base/nullability.h',
'-third_party/abseil-cpp/absl/container',
'+third_party/abseil-cpp/absl/container/inlined_vector.h',
Expand Down
1 change: 1 addition & 0 deletions ash/constants/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include_rules = [
# Please keep this section in sync with //DEPS.
'+third_party/abseil-cpp',
'-third_party/abseil-cpp/absl/algorithm/container.h',
'-third_party/abseil-cpp/absl/base/attributes.h',
'-third_party/abseil-cpp/absl/base/nullability.h',
'-third_party/abseil-cpp/absl/container',
'+third_party/abseil-cpp/absl/container/inlined_vector.h',
Expand Down
1 change: 0 additions & 1 deletion base/android/jni_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "build/robolectric_buildflags.h"
#include "third_party/abseil-cpp/absl/base/attributes.h"
#include "third_party/jni_zero/jni_zero.h"

#if BUILDFLAG(IS_ROBOLECTRIC)
Expand Down
3 changes: 1 addition & 2 deletions base/check_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ inline char* CheckOpValueStr(const T& v) {
// use with CheckOpValueStr() which allocates these strings using strdup().
// Returns allocated string (with strdup) for passing into
// ::logging::CheckError::(D)CheckOp methods.
// TODO(pbos): Annotate this ABSL_ATTRIBUTE_RETURNS_NONNULL after solving
// compile failure.
// TODO(pbos): Annotate this RETURNS_NONNULL after solving compile failure.
BASE_EXPORT char* CreateCheckOpLogMessageString(const char* expr_str,
char* v1_str,
char* v2_str);
Expand Down
149 changes: 149 additions & 0 deletions base/compiler_specific.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,155 @@ inline constexpr bool AnalyzerAssumeTrue(bool arg) {
#define LIFETIME_BOUND
#endif

// Annotates a function or variable to indicate that it should have weak
// linkage. Useful for library code that wants code linking against it to be
// able to override its functionality; inside a single target, this is better
// accomplished via virtual methods and other more standard mechanisms.
//
// Any weak definition of a symbol will be overridden at link time by a non-weak
// definition. Marking a `const` or `constexpr` variable weak makes it no longer
// be considered a compile-time constant, since its value may be different after
// linking.
//
// Multiple weak definitions of a symbol may exist, in which case the linker is
// free to select any when there are no non-weak definitions. Like with symbols
// marked `inline`, this can lead to subtle, difficult-to-diagnose bugs if not
// all definitions are identical.
//
// A weak declaration that has no definitions at link time will be linked as if
// the corresponding address is null. Therefore library code can use weak
// declarations and conditionals to allow consumers to provide optional
// customizations.
//
// See also:
// https://clang.llvm.org/docs/AttributeReference.html#weak
//
// Usage:
// ```
// // The following definition defaults `x` to 10, but allows other object
// // files to override its value. Thus, despite `constexpr`, `x` is not
// // considered a compile-time constant (and cannot be used in a `constexpr`
// // context).
// extern const int x;
// WEAK_SYMBOL constexpr int x = 10;
//
// // The following declaration allows linking to occur whether a definition
// // of `Func()` is provided or not; if none is present, `&Func` will
// // evaluate to `nullptr` at runtime.
// WEAK_SYMBOL void Func();
//
// // The following definition provides a default implementation of `Func2()`,
// // but allows other object files to override.
// WEAK_SYMBOL void Func2() { ... }
// ```
#if __has_cpp_attribute(gnu::weak)
#define WEAK_SYMBOL [[gnu::weak]]
#else
#define WEAK_SYMBOL
#endif

// Annotates a function indicating that the compiler should not convert calls
// within it to tail calls.
//
// For a callee-side version of this, see `NOT_TAIL_CALLED`.
//
// See also:
// https://clang.llvm.org/docs/AttributeReference.html#disable-tail-calls
// Usage:
// ```
// DISABLE_TAIL_CALLS void Func() {
// // Function calls in this body will not be tail calls.
// }
// ```
#if __has_cpp_attribute(clang::disable_tail_calls)
#define DISABLE_TAIL_CALLS [[clang::disable_tail_calls]]
#else
#define DISABLE_TAIL_CALLS
#endif

// Annotates a type or member indicating the minimum possible alignment (one bit
// for bitfields, one byte otherwise) should be used. This can be used to
// eliminate padding inside objects, at the cost of potentially pessimizing
// code, or even generating invalid code (depending on platform restrictions) if
// underaligned objects have their addresses taken and passed elsewhere.
//
// This is similar to the more-broadly-supported `#pragma pack(1)`.
//
// See also:
// https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute
//
// Usage:
// ```
// struct PACKED_OBJ S1 {
// int8_t a; // Alignment 1, offset 0, size 1
// int32_t b; // Alignment 1, offset 1 (0 bytes padding), size 4
// }; // Overall alignment 1, 0 bytes trailing padding, overall size 5
//
// struct S2 {
// int8_t a; // Alignment 1, offset 0, size 1
// int32_t b; // Alignment 4, offset 4 (3 bytes padding), size 4
// int8_t c; // Alignment 1, offset 8 (0 bytes padding), size 1
// PACKED_OBJ int32_t d; // Alignment 1, offset 9 (0 bytes padding), size 4
// }; // Overall alignment 4, 3 bytes trailing padding, overall size 16
// ```
#if __has_cpp_attribute(gnu::packed)
#define PACKED_OBJ [[gnu::packed]]
#else
#define PACKED_OBJ
#endif

// Annotates a function indicating that the returned pointer will never be null.
// This may allow the compiler to assume null checks on the caller side are
// unnecessary.
//
// In practice, this is usually better-handled by returning a value or
// reference, which enforce such guarantees at the type level.
//
// See also:
// https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-returns_005fnonnull-function-attribute
// https://clang.llvm.org/docs/AttributeReference.html#nullability-attributes
//
// Usage:
// ```
// // The following function will never return `nullptr`.
// RETURNS_NONNULL int* Func();
// ```
#if __has_cpp_attribute(gnu::returns_nonnull)
#define RETURNS_NONNULL [[gnu::returns_nonnull]]
#else
#define RETURNS_NONNULL
#endif

// Annotates a function indicating it is const, meaning that it has no
// observable side effects and its return value depends only on its arguments.
// Const functions may not read external memory other than unchanging objects
// (e.g. non-volatile constants), and the compiler is free to replace calls to
// them with the return values of earlier calls with the same arguments no
// matter what other state might have changed in the meantime.
//
// This is a much stronger restriction than `const`-qualified functions, and is
// rarely appropriate outside small local helpers, which are frequently
// inlineable anyway and would not really benefit.
//
// WARNING: Misusing this attribute can lead to silent miscompilation, UB, and
// difficult-to-diagnose bugs. For this and the above reason, usage should be
// very rare.
//
// See also:
// https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute
//
// Usage:
// ```
// // The compiler may replace calls to this function with values returned
// // from earlier calls, assuming the args match.
// CONST_FUNCTION int Func(int);
// ```
#if __has_cpp_attribute(gnu::const)
#define CONST_FUNCTION [[gnu::const]]
#else
#define CONST_FUNCTION
#endif

// Annotates a function indicating it is pure, meaning that it has no observable
// side effects. Unlike functions annotated `CONST_FUNCTION`, pure functions may
// still read external memory, and thus their return values may change between
Expand Down
41 changes: 17 additions & 24 deletions base/containers/heap_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "third_party/abseil-cpp/absl/base/attributes.h"

namespace base {

Expand Down Expand Up @@ -117,36 +116,30 @@ class TRIVIAL_ABI GSL_OWNER HeapArray {
// Prefer span-based methods below over data() where possible. The data()
// method exists primarily to allow implicit constructions of spans.
// Returns nullptr for a zero-sized (or moved-from) array.
T* data() ABSL_ATTRIBUTE_LIFETIME_BOUND { return data_.get(); }
const T* data() const ABSL_ATTRIBUTE_LIFETIME_BOUND { return data_.get(); }
T* data() LIFETIME_BOUND { return data_.get(); }
const T* data() const LIFETIME_BOUND { return data_.get(); }

iterator begin() ABSL_ATTRIBUTE_LIFETIME_BOUND { return as_span().begin(); }
const_iterator begin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return as_span().begin();
}
iterator begin() LIFETIME_BOUND { return as_span().begin(); }
const_iterator begin() const LIFETIME_BOUND { return as_span().begin(); }

iterator end() ABSL_ATTRIBUTE_LIFETIME_BOUND { return as_span().end(); }
const_iterator end() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return as_span().end();
}
iterator end() LIFETIME_BOUND { return as_span().end(); }
const_iterator end() const LIFETIME_BOUND { return as_span().end(); }

T& operator[](size_t idx) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return as_span()[idx];
}
const T& operator[](size_t idx) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
T& operator[](size_t idx) LIFETIME_BOUND { return as_span()[idx]; }
const T& operator[](size_t idx) const LIFETIME_BOUND {
return as_span()[idx];
}

// Access the HeapArray via spans. Note that span<T> is implicilty
// constructible from HeapArray<T>, so an explicit call to .as_span() is
// most useful, say, when the compiler can't deduce a template
// argument type.
base::span<T> as_span() ABSL_ATTRIBUTE_LIFETIME_BOUND {
base::span<T> as_span() LIFETIME_BOUND {
// SAFETY: `size_` is the number of elements in the `data_` allocation` at
// all times.
return UNSAFE_BUFFERS(base::span<T>(data_.get(), size_));
}
base::span<const T> as_span() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
base::span<const T> as_span() const LIFETIME_BOUND {
// SAFETY: `size_` is the number of elements in the `data_` allocation` at
// all times.
return UNSAFE_BUFFERS(base::span<const T>(data_.get(), size_));
Expand All @@ -169,31 +162,31 @@ class TRIVIAL_ABI GSL_OWNER HeapArray {
// If `count` is unspecified, all remaining elements are included. A CHECK()
// occurs if any of the parameters results in an out-of-range position in
// the HeapArray.
base::span<T> subspan(size_t offset, size_t count = base::dynamic_extent)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
base::span<T> subspan(size_t offset,
size_t count = base::dynamic_extent) LIFETIME_BOUND {
return as_span().subspan(offset, count);
}
base::span<const T> subspan(size_t offset,
size_t count = base::dynamic_extent) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
LIFETIME_BOUND {
return as_span().subspan(offset, count);
}

// Returns a span over the first `count` elements of the HeapArray. A CHECK()
// occurs if the `count` is larger than size of the HeapArray.
base::span<T> first(size_t count) ABSL_ATTRIBUTE_LIFETIME_BOUND {
base::span<T> first(size_t count) LIFETIME_BOUND {
return as_span().first(count);
}
base::span<const T> first(size_t count) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
base::span<const T> first(size_t count) const LIFETIME_BOUND {
return as_span().first(count);
}

// Returns a span over the last `count` elements of the HeapArray. A CHECK()
// occurs if the `count` is larger than size of the HeapArray.
base::span<T> last(size_t count) ABSL_ATTRIBUTE_LIFETIME_BOUND {
base::span<T> last(size_t count) LIFETIME_BOUND {
return as_span().last(count);
}
base::span<const T> last(size_t count) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
base::span<const T> last(size_t count) const LIFETIME_BOUND {
return as_span().last(count);
}

Expand Down
22 changes: 10 additions & 12 deletions base/containers/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "base/containers/dynamic_extent.h"
#include "base/numerics/safe_conversions.h"
#include "base/types/to_address.h"
#include "third_party/abseil-cpp/absl/base/attributes.h"

namespace base {

Expand Down Expand Up @@ -1521,8 +1520,7 @@ constexpr auto make_span(Container&& container) noexcept {
// non-std helper that is inspired by the `std::slice::from_ref()` function from
// Rust.
template <typename T>
constexpr span<T, 1u> span_from_ref(
T& single_object ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept {
constexpr span<T, 1u> span_from_ref(T& single_object LIFETIME_BOUND) noexcept {
// SAFETY: Given a valid reference to `single_object` the span of size 1 will
// be a valid span that points to the `single_object`.
return UNSAFE_BUFFERS(span<T, 1u>(std::addressof(single_object), 1u));
Expand All @@ -1536,12 +1534,12 @@ constexpr span<T, 1u> span_from_ref(
// references are turned into a `span<T, sizeof(T)>`.
template <typename T>
constexpr span<const uint8_t, sizeof(T)> byte_span_from_ref(
const T& single_object ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept {
const T& single_object LIFETIME_BOUND) noexcept {
return as_bytes(span_from_ref(single_object));
}
template <typename T>
constexpr span<uint8_t, sizeof(T)> byte_span_from_ref(
T& single_object ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept {
T& single_object LIFETIME_BOUND) noexcept {
return as_writable_bytes(span_from_ref(single_object));
}

Expand All @@ -1559,7 +1557,7 @@ constexpr span<uint8_t, sizeof(T)> byte_span_from_ref(
// always preserved.
template <class CharT, size_t N>
constexpr span<const CharT, N - 1> span_from_cstring(
const CharT (&lit ABSL_ATTRIBUTE_LIFETIME_BOUND)[N])
const CharT (&lit LIFETIME_BOUND)[N])
ENABLE_IF_ATTR(lit[N - 1u] == CharT{0},
"requires string literal as input") {
return span(lit).template first<N - 1>();
Expand All @@ -1582,7 +1580,7 @@ constexpr span<const CharT, N - 1> span_from_cstring(
// always preserved.
template <class CharT, size_t N>
constexpr span<const CharT, N> span_with_nul_from_cstring(
const CharT (&lit ABSL_ATTRIBUTE_LIFETIME_BOUND)[N])
const CharT (&lit LIFETIME_BOUND)[N])
ENABLE_IF_ATTR(lit[N - 1u] == CharT{0},
"requires string literal as input") {
return span(lit);
Expand All @@ -1602,7 +1600,7 @@ constexpr span<const CharT, N> span_with_nul_from_cstring(
// always preserved.
template <size_t N>
constexpr span<const uint8_t, N - 1> byte_span_from_cstring(
const char (&lit ABSL_ATTRIBUTE_LIFETIME_BOUND)[N])
const char (&lit LIFETIME_BOUND)[N])
ENABLE_IF_ATTR(lit[N - 1u] == '\0', "requires string literal as input") {
return as_bytes(span(lit).template first<N - 1>());
}
Expand All @@ -1624,7 +1622,7 @@ constexpr span<const uint8_t, N - 1> byte_span_from_cstring(
// always preserved.
template <size_t N>
constexpr span<const uint8_t, N> byte_span_with_nul_from_cstring(
const char (&lit ABSL_ATTRIBUTE_LIFETIME_BOUND)[N])
const char (&lit LIFETIME_BOUND)[N])
ENABLE_IF_ATTR(lit[N - 1u] == '\0', "requires string literal as input") {
return as_bytes(span(lit));
}
Expand All @@ -1642,7 +1640,7 @@ constexpr auto as_byte_span(const Spannable& arg) {

template <int&... ExplicitArgumentBarrier, typename T, size_t N>
constexpr span<const uint8_t, N * sizeof(T)> as_byte_span(
const T (&arr ABSL_ATTRIBUTE_LIFETIME_BOUND)[N]) {
const T (&arr LIFETIME_BOUND)[N]) {
return as_bytes(make_span(arr));
}

Expand All @@ -1664,13 +1662,13 @@ constexpr auto as_writable_byte_span(Spannable&& arg) {
// the span type signature span<uint8_t, N>.
template <int&... ExplicitArgumentBarrier, typename T, size_t N>
constexpr span<uint8_t, N * sizeof(T)> as_writable_byte_span(
T (&arr ABSL_ATTRIBUTE_LIFETIME_BOUND)[N]) {
T (&arr LIFETIME_BOUND)[N]) {
return as_writable_bytes(make_span(arr));
}

template <int&... ExplicitArgumentBarrier, typename T, size_t N>
constexpr span<uint8_t, N * sizeof(T)> as_writable_byte_span(
T (&&arr ABSL_ATTRIBUTE_LIFETIME_BOUND)[N]) {
T (&&arr LIFETIME_BOUND)[N]) {
return as_writable_bytes(make_span(arr));
}

Expand Down
Loading

0 comments on commit 4b18d0c

Please sign in to comment.