Commit d3742a4b authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Add NO_UNIQUE_ADDRESS attribute

This change adds the HAS_CPP_ATTRIBUTE and NO_UNIQUE_ADDRESS macros to
base/compiler_specific.h. This allows using the C++20 no_unique_address
attribute in prior dialects if the compiler supports it.

Bug: None
Change-Id: I6aff82e7b195f5eaf93551049c9dfa6a3fd1fbc6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2510396Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822768}
parent f48e55e3
...@@ -11,6 +11,19 @@ ...@@ -11,6 +11,19 @@
#error "Only clang-cl is supported on Windows, see https://crbug.com/988071" #error "Only clang-cl is supported on Windows, see https://crbug.com/988071"
#endif #endif
// This is a wrapper around `__has_cpp_attribute`, which can be used to test for
// the presence of an attribute. In case the compiler does not support this
// macro it will simply evaluate to 0.
//
// References:
// https://wg21.link/sd6#testing-for-the-presence-of-an-attribute-__has_cpp_attribute
// https://wg21.link/cpp.cond#:__has_cpp_attribute
#if defined(__has_cpp_attribute)
#define HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#else
#define HAS_CPP_ATTRIBUTE(x) 0
#endif
// Annotate a variable indicating it's ok if the variable is not used. // Annotate a variable indicating it's ok if the variable is not used.
// (Typically used to silence a compiler warning when the assignment // (Typically used to silence a compiler warning when the assignment
// is important for some other reason.) // is important for some other reason.)
...@@ -99,6 +112,20 @@ ...@@ -99,6 +112,20 @@
#define WARN_UNUSED_RESULT #define WARN_UNUSED_RESULT
#endif #endif
// In case the compiler supports it NO_UNIQUE_ADDRESS evaluates to the C++20
// attribute [[no_unique_address]]. This allows annotating data members so that
// they need not have an address distinct from all other non-static data members
// of its class.
//
// References:
// * https://en.cppreference.com/w/cpp/language/attributes/no_unique_address
// * https://wg21.link/dcl.attr.nouniqueaddr
#if HAS_CPP_ATTRIBUTE(no_unique_address)
#define NO_UNIQUE_ADDRESS [[no_unique_address]]
#else
#define NO_UNIQUE_ADDRESS
#endif
// Tell the compiler a function is using a printf-style format string. // Tell the compiler a function is using a printf-style format string.
// |format_param| is the one-based index of the format string parameter; // |format_param| is the one-based index of the format string parameter;
// |dots_param| is the one-based index of the "..." parameter. // |dots_param| is the one-based index of the "..." parameter.
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include "base/compiler_specific.h"
#include "base/functional/identity.h" #include "base/functional/identity.h"
#include "base/functional/invoke.h" #include "base/functional/invoke.h"
#include "base/ranges/functional.h" #include "base/ranges/functional.h"
...@@ -150,14 +151,13 @@ using range_category_t = iterator_category_t<ranges::iterator_t<Range>>; ...@@ -150,14 +151,13 @@ using range_category_t = iterator_category_t<ranges::iterator_t<Range>>;
namespace ranges { namespace ranges {
// C++14 implementation of std::ranges::in_fun_result. Note the because C++14 // C++14 implementation of std::ranges::in_fun_result.
// lacks the `no_unique_address` attribute it is commented out.
// //
// Reference: https://wg21.link/algorithms.results#:~:text=in_fun_result // Reference: https://wg21.link/algorithms.results#:~:text=in_fun_result
template <typename I, typename F> template <typename I, typename F>
struct in_fun_result { struct in_fun_result {
/* [[no_unique_address]] */ I in; NO_UNIQUE_ADDRESS I in;
/* [[no_unique_address]] */ F fun; NO_UNIQUE_ADDRESS F fun;
template <typename I2, template <typename I2,
typename F2, typename F2,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment