globally replace COMPILE_ASSERT with static_assert

This commit is contained in:
Aliaksey Kandratsenka 2024-02-04 15:05:45 -05:00
parent c56764c3e6
commit de22f024fb
7 changed files with 15 additions and 78 deletions

View File

@ -118,66 +118,6 @@ const int64 kint64min = ( (((uint64) kint32min) << 32) | 0 );
// An alternate name that leaves out the moral judgment... :-)
#define DISALLOW_COPY_AND_ASSIGN(TypeName) DISALLOW_EVIL_CONSTRUCTORS(TypeName)
// The COMPILE_ASSERT macro can be used to verify that a compile time
// expression is true. For example, you could use it to verify the
// size of a static array:
//
// COMPILE_ASSERT(sizeof(num_content_type_names) == sizeof(int),
// content_type_names_incorrect_size);
//
// or to make sure a struct is smaller than a certain size:
//
// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
//
// The second argument to the macro is the name of the variable. If
// the expression is false, most compilers will issue a warning/error
// containing the name of the variable.
//
// Implementation details of COMPILE_ASSERT:
//
// - COMPILE_ASSERT works by defining an array type that has -1
// elements (and thus is invalid) when the expression is false.
//
// - The simpler definition
//
// #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
//
// does not work, as gcc supports variable-length arrays whose sizes
// are determined at run-time (this is gcc's extension and not part
// of the C++ standard). As a result, gcc fails to reject the
// following code with the simple definition:
//
// int foo;
// COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
// // not a compile-time constant.
//
// - By using the type CompileAssert<(bool(expr))>, we ensures that
// expr is a compile-time constant. (Template arguments must be
// determined at compile-time.)
//
// - The outter parentheses in CompileAssert<(bool(expr))> are necessary
// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
//
// CompileAssert<bool(expr)>
//
// instead, these compilers will refuse to compile
//
// COMPILE_ASSERT(5 > 0, some_message);
//
// (They seem to think the ">" in "5 > 0" marks the end of the
// template argument list.)
//
// - The array size is (bool(expr) ? 1 : -1), instead of simply
//
// ((expr) ? 1 : -1).
//
// This is to avoid running into a bug in MS VC 7.1, which
// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
template <bool>
struct CompileAssert {
};
#ifdef HAVE___ATTRIBUTE__
# define ATTRIBUTE_UNUSED __attribute__((unused))
#else
@ -190,9 +130,6 @@ struct CompileAssert {
#define ATTR_INITIAL_EXEC
#endif
#define COMPILE_ASSERT(expr, msg) \
typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED
#define arraysize(a) (sizeof(a) / sizeof(*(a)))
#define OFFSETOF_MEMBER(strct, field) \
@ -215,7 +152,7 @@ struct CompileAssert {
template <class Dest, class Source>
inline Dest bit_cast(const Source& source) {
COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), bitcasting_unequal_sizes);
static_assert(sizeof(Dest) == sizeof(Source), "bitcasting unequal sizes");
Dest dest;
memcpy(&dest, &source, sizeof(dest));
return dest;
@ -227,7 +164,7 @@ inline Dest bit_cast(const Source& source) {
// This prevents undefined behavior when the dest pointer is unaligned.
template <class Dest, class Source>
inline void bit_store(Dest *dest, const Source *source) {
COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), bitcasting_unequal_sizes);
static_assert(sizeof(Dest) == sizeof(Source), "bitcasting unequal sizes");
memcpy(dest, source, sizeof(Dest));
}

View File

@ -317,9 +317,9 @@ class WriterMutexLock {
};
// Catch bug where variable name is omitted, e.g. MutexLock (&mu);
#define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_decl_missing_var_name)
#define ReaderMutexLock(x) COMPILE_ASSERT(0, rmutex_lock_decl_missing_var_name)
#define WriterMutexLock(x) COMPILE_ASSERT(0, wmutex_lock_decl_missing_var_name)
#define MutexLock(x) static_assert(0)
#define ReaderMutexLock(x) static_assert(0)
#define WriterMutexLock(x) static_assert(0)
} // namespace MUTEX_NAMESPACE

View File

@ -129,7 +129,7 @@ class SCOPED_LOCKABLE SpinLockHolder {
}
};
// Catch bug where variable name is omitted, e.g. SpinLockHolder (&lock);
#define SpinLockHolder(x) COMPILE_ASSERT(0, spin_lock_decl_missing_var_name)
#define SpinLockHolder(x) static_assert(0)
namespace tcmalloc {

View File

@ -45,7 +45,7 @@
#include "base/logging.h"
#include "base/dynamic_annotations.h"
#include "base/basictypes.h" // for COMPILE_ASSERT
#include "base/basictypes.h"
#ifndef AT_SYSINFO_EHDR
#define AT_SYSINFO_EHDR 33
@ -88,8 +88,8 @@ const void *VDSOSupport::Init() {
ElfW(auxv_t) aux;
while (read(fd, &aux, sizeof(aux)) == sizeof(aux)) {
if (aux.a_type == AT_SYSINFO_EHDR) {
COMPILE_ASSERT(sizeof(vdso_base_) == sizeof(aux.a_un.a_val),
unexpected_sizeof_pointer_NE_sizeof_a_val);
static_assert(sizeof(vdso_base_) == sizeof(aux.a_un.a_val),
"unexpected sizeof(pointer) != sizeof(a_val)");
vdso_base_ = reinterpret_cast<void *>(aux.a_un.a_val);
break;
}

View File

@ -148,9 +148,9 @@ class PackedCache {
static const int kInvalidMask = 0x80;
explicit PackedCache() {
COMPILE_ASSERT(kKeybits + kValuebits + 1 <= 8 * sizeof(T), use_whole_keys);
COMPILE_ASSERT(kHashbits <= kKeybits, hash_function);
COMPILE_ASSERT(kHashbits >= kValuebits + 1, small_values_space);
static_assert(kKeybits + kValuebits + 1 <= 8 * sizeof(T));
static_assert(kHashbits <= kKeybits);
static_assert(kHashbits >= kValuebits + 1);
Clear();
}

View File

@ -86,7 +86,7 @@ PageHeap::PageHeap(Length smallest_span_size)
// Start scavenging at kMaxPages list
release_index_(kMaxPages),
aggressive_decommit_(false) {
COMPILE_ASSERT(kClassSizesMax <= (1 << PageMapCache::kValuebits), valuebits);
static_assert(kClassSizesMax <= (1 << PageMapCache::kValuebits));
// smallest_span_size needs to be power of 2.
CHECK_CONDITION((smallest_span_size_ & (smallest_span_size_-1)) == 0);
for (int i = 0; i < kMaxPages; i++) {

View File

@ -92,8 +92,8 @@ static bool CheckAddressBits(uintptr_t ptr) {
return always_ok || ((ptr >> shift_bits) == 0);
}
COMPILE_ASSERT(kAddressBits <= 8 * sizeof(void*),
address_bits_larger_than_pointer_size);
static_assert(kAddressBits <= 8 * sizeof(void*),
"address bits larger than pointer size");
static SpinLock spinlock(SpinLock::LINKER_INITIALIZED);