From d198c50abe9bf2132d2e65f814cf9d0d48e49cc0 Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Wed, 1 Oct 2014 12:29:01 -0700 Subject: [PATCH] Convert COMPILE_ASSERT to static_assert (Perhaps I should have just left it in mini_chromium, but anyway.) R=mark@chromium.org BUG=crashpad:1 Review URL: https://codereview.chromium.org/615923004 --- client/simple_string_dictionary.cc | 5 +-- crashpad.doxy | 3 +- minidump/minidump_system_info_writer.cc | 13 +++--- util/file/file_writer.cc | 10 ++--- util/file/string_file_writer_test.cc | 4 +- util/mac/mach_o_image_reader.cc | 2 +- util/mac/mach_o_image_segment_reader.cc | 6 +-- util/mach/exc_server_variants_test.cc | 2 +- util/mach/mach_message_server_test.cc | 4 +- util/mach/symbolic_constants_mach.cc | 4 +- util/misc/uuid.cc | 6 +-- util/numeric/checked_range.h | 4 +- util/numeric/int128.h | 3 +- util/numeric/int128_test.cc | 2 +- util/posix/symbolic_constants_posix.cc | 4 +- util/stdlib/string_number_conversion.cc | 57 ++++++++++++------------- util/stdlib/strlcpy_test.cc | 1 + util/test/multiprocess_exec.cc | 6 +-- 18 files changed, 66 insertions(+), 70 deletions(-) diff --git a/client/simple_string_dictionary.cc b/client/simple_string_dictionary.cc index c8e9c98d..171a8526 100644 --- a/client/simple_string_dictionary.cc +++ b/client/simple_string_dictionary.cc @@ -14,7 +14,6 @@ #include "client/simple_string_dictionary.h" -#include "base/basictypes.h" #include "util/stdlib/cxx.h" #if CXX_LIBRARY_VERSION >= 2011 @@ -29,9 +28,9 @@ typedef TSimpleStringDictionary<1, 1, 1> SimpleStringDictionaryForAssertion; #if CXX_LIBRARY_VERSION >= 2011 // In C++11, check that TSimpleStringDictionary has standard layout, which is // what is actually important. -COMPILE_ASSERT( +static_assert( std::is_standard_layout::value, - SimpleStringDictionary_must_be_standard_layout); + "SimpleStringDictionary must be standard layout"); #else // In C++98 (ISO 14882), section 9.5.1 says that a union cannot have a member // with a non-trivial ctor, copy ctor, dtor, or assignment operator. Use this diff --git a/crashpad.doxy b/crashpad.doxy index 45f714ca..fe59ed14 100644 --- a/crashpad.doxy +++ b/crashpad.doxy @@ -1943,8 +1943,7 @@ INCLUDE_FILE_PATTERNS = # recursively expanded use the := operator instead of the = operator. # This tag requires that the tag ENABLE_PREPROCESSING is set to YES. -PREDEFINED = COMPILE_ASSERT(a,b)= \ - DOXYGEN \ +PREDEFINED = DOXYGEN \ __attribute__(x)= # If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this diff --git a/minidump/minidump_system_info_writer.cc b/minidump/minidump_system_info_writer.cc index f59e3d37..a8cabb7b 100644 --- a/minidump/minidump_system_info_writer.cc +++ b/minidump/minidump_system_info_writer.cc @@ -47,8 +47,8 @@ void MinidumpSystemInfoWriter::SetCPUX86Vendor(uint32_t ebx, system_info_.ProcessorArchitecture == kMinidumpCPUArchitectureX86Win64); - COMPILE_ASSERT(arraysize(system_info_.Cpu.X86CpuInfo.VendorId) == 3, - vendor_id_must_have_3_elements); + static_assert(arraysize(system_info_.Cpu.X86CpuInfo.VendorId) == 3, + "VendorId must have 3 elements"); system_info_.Cpu.X86CpuInfo.VendorId[0] = ebx; system_info_.Cpu.X86CpuInfo.VendorId[1] = edx; @@ -61,9 +61,9 @@ void MinidumpSystemInfoWriter::SetCPUX86VendorString( CHECK_EQ(vendor.size(), sizeof(system_info_.Cpu.X86CpuInfo.VendorId)); uint32_t registers[3]; - COMPILE_ASSERT( + static_assert( sizeof(registers) == sizeof(system_info_.Cpu.X86CpuInfo.VendorId), - vendor_id_sizes_must_be_equal); + "VendorId sizes must be equal"); for (size_t index = 0; index < arraysize(registers); ++index) { memcpy(®isters[index], @@ -105,9 +105,8 @@ void MinidumpSystemInfoWriter::SetCPUOtherFeatures(uint64_t features_0, system_info_.ProcessorArchitecture != kMinidumpCPUArchitectureX86Win64); - COMPILE_ASSERT( - arraysize(system_info_.Cpu.OtherCpuInfo.ProcessorFeatures) == 2, - processor_features_must_have_2_elements); + static_assert(arraysize(system_info_.Cpu.OtherCpuInfo.ProcessorFeatures) == 2, + "ProcessorFeatures must have 2 elements"); system_info_.Cpu.OtherCpuInfo.ProcessorFeatures[0] = features_0; system_info_.Cpu.OtherCpuInfo.ProcessorFeatures[1] = features_1; diff --git a/util/file/file_writer.cc b/util/file/file_writer.cc index 703a8829..5c7c25dc 100644 --- a/util/file/file_writer.cc +++ b/util/file/file_writer.cc @@ -25,11 +25,11 @@ namespace crashpad { // Ensure type compatibility between WritableIoVec and iovec. -COMPILE_ASSERT(sizeof(WritableIoVec) == sizeof(iovec), WritableIoVec_size); -COMPILE_ASSERT(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base), - WritableIoVec_base_offset); -COMPILE_ASSERT(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len), - WritableIoVec_len_offset); +static_assert(sizeof(WritableIoVec) == sizeof(iovec), "WritableIoVec size"); +static_assert(offsetof(WritableIoVec, iov_base) == offsetof(iovec, iov_base), + "WritableIoVec base offset"); +static_assert(offsetof(WritableIoVec, iov_len) == offsetof(iovec, iov_len), + "WritableIoVec len offset"); FileWriter::FileWriter() : fd_() { } diff --git a/util/file/string_file_writer_test.cc b/util/file/string_file_writer_test.cc index f0ec72f7..b55c778e 100644 --- a/util/file/string_file_writer_test.cc +++ b/util/file/string_file_writer_test.cc @@ -354,8 +354,8 @@ TEST(StringFileWriter, SeekInvalid) { EXPECT_EQ(1, writer.Seek(0, SEEK_CUR)); EXPECT_TRUE(writer.string().empty()); - COMPILE_ASSERT(SEEK_SET != 3 && SEEK_CUR != 3 && SEEK_END != 3, - three_must_be_invalid_for_whence); + static_assert(SEEK_SET != 3 && SEEK_CUR != 3 && SEEK_END != 3, + "3 must be invalid for whence"); EXPECT_LT(writer.Seek(0, 3), 0); writer.Reset(); diff --git a/util/mac/mach_o_image_reader.cc b/util/mac/mach_o_image_reader.cc index 09e35e59..25e279c7 100644 --- a/util/mac/mach_o_image_reader.cc +++ b/util/mac/mach_o_image_reader.cc @@ -328,7 +328,7 @@ const process_types::section* MachOImageReader::GetSectionAtIndex( mach_vm_address_t* address) const { INITIALIZATION_STATE_DCHECK_VALID(initialized_); - COMPILE_ASSERT(NO_SECT == 0, no_sect_must_be_zero); + static_assert(NO_SECT == 0, "NO_SECT must be zero"); if (index == NO_SECT) { LOG(WARNING) << "section index " << index << " out of range"; return NULL; diff --git a/util/mac/mach_o_image_segment_reader.cc b/util/mac/mach_o_image_segment_reader.cc index 0625f392..3a2c37bf 100644 --- a/util/mac/mach_o_image_segment_reader.cc +++ b/util/mac/mach_o_image_segment_reader.cc @@ -237,9 +237,9 @@ std::string MachOImageSegmentReader::SegmentNameString( const char* segment_name_c) { // This is used to interpret the segname field of both the segment_command and // section structures, so be sure that they’re identical. - COMPILE_ASSERT(sizeof(process_types::segment_command::segname) == - sizeof(process_types::section::segname), - sizes_must_be_equal); + static_assert(sizeof(process_types::segment_command::segname) == + sizeof(process_types::section::segname), + "sizes must be equal"); return SizeLimitedCString(segment_name_c, sizeof(process_types::segment_command::segname)); diff --git a/util/mach/exc_server_variants_test.cc b/util/mach/exc_server_variants_test.cc index 9f4963a3..5daa81ba 100644 --- a/util/mach/exc_server_variants_test.cc +++ b/util/mach/exc_server_variants_test.cc @@ -1114,7 +1114,7 @@ TEST(ExcServerVariants, ExcCrashRecoverOriginalException) { // Now make sure that ExcCrashRecoverOriginalException() properly ignores // optional arguments. - COMPILE_ASSERT(arraysize(kTestData) >= 1, must_have_something_to_test); + static_assert(arraysize(kTestData) >= 1, "must have something to test"); const TestData& test_data = kTestData[0]; EXPECT_EQ(test_data.exception, ExcCrashRecoverOriginalException(test_data.code_0, NULL, NULL)); diff --git a/util/mach/mach_message_server_test.cc b/util/mach/mach_message_server_test.cc index cb97e5ba..3d0d8bd7 100644 --- a/util/mach/mach_message_server_test.cc +++ b/util/mach/mach_message_server_test.cc @@ -696,8 +696,8 @@ TEST(MachMessageServer, PersistentNonblockingFourMessages) { // child_wait_for_parent_pipe_early is used to make the child wait until the // parent is ready. const size_t kTransactionCount = 4; - COMPILE_ASSERT(kTransactionCount <= MACH_PORT_QLIMIT_DEFAULT, - must_not_exceed_queue_limit); + static_assert(kTransactionCount <= MACH_PORT_QLIMIT_DEFAULT, + "must not exceed queue limit"); TestMachMessageServer::Options options; options.parent_wait_for_child_pipe = true; diff --git a/util/mach/symbolic_constants_mach.cc b/util/mach/symbolic_constants_mach.cc index 4bba92af..827555d0 100644 --- a/util/mach/symbolic_constants_mach.cc +++ b/util/mach/symbolic_constants_mach.cc @@ -42,8 +42,8 @@ const char* kExceptionNames[] = { "RESOURCE", "GUARD", }; -COMPILE_ASSERT(arraysize(kExceptionNames) == EXC_TYPES_COUNT, - kExceptionNames_length); +static_assert(arraysize(kExceptionNames) == EXC_TYPES_COUNT, + "kExceptionNames length"); const char kExcPrefix[] = "EXC_"; const char kExcMaskPrefix[] = "EXC_MASK_"; diff --git a/util/misc/uuid.cc b/util/misc/uuid.cc index a7ba4a8f..939c0b8e 100644 --- a/util/misc/uuid.cc +++ b/util/misc/uuid.cc @@ -27,11 +27,11 @@ namespace crashpad { -COMPILE_ASSERT(sizeof(UUID) == 16, UUID_must_be_16_bytes); +static_assert(sizeof(UUID) == 16, "UUID must be 16 bytes"); #if CXX_LIBRARY_VERSION >= 2011 -COMPILE_ASSERT(std::is_standard_layout::value, - UUID_must_be_standard_layout); +static_assert(std::is_standard_layout::value, + "UUID must be standard layout"); #endif UUID::UUID() : data_1(0), data_2(0), data_3(0), data_4(), data_5() { diff --git a/util/numeric/checked_range.h b/util/numeric/checked_range.h index b0f12227..df3e395f 100644 --- a/util/numeric/checked_range.h +++ b/util/numeric/checked_range.h @@ -30,8 +30,8 @@ template class CheckedRange { public: CheckedRange(ValueType base, SizeType size) { - COMPILE_ASSERT(!std::numeric_limits::is_signed, - SizeType_must_be_unsigned); + static_assert(!std::numeric_limits::is_signed, + "SizeType must be unsigned"); SetRange(base, size); } diff --git a/util/numeric/int128.h b/util/numeric/int128.h index 75b8b6a8..c57bed6d 100644 --- a/util/numeric/int128.h +++ b/util/numeric/int128.h @@ -17,7 +17,6 @@ #include -#include "base/basictypes.h" #include "build/build_config.h" namespace crashpad { @@ -46,7 +45,7 @@ struct uint128_struct { #endif }; -COMPILE_ASSERT(sizeof(uint128_struct) == 16, uint128_must_be_16_bytes); +static_assert(sizeof(uint128_struct) == 16, "uint128 must be 16 bytes"); } // namespace crashpad diff --git a/util/numeric/int128_test.cc b/util/numeric/int128_test.cc index bf42c0cf..fc18de46 100644 --- a/util/numeric/int128_test.cc +++ b/util/numeric/int128_test.cc @@ -30,7 +30,7 @@ TEST(Int128, UInt128) { #endif uint128_struct uint128; - COMPILE_ASSERT(sizeof(uint128) == sizeof(kBytes), sizes_must_be_equal); + static_assert(sizeof(uint128) == sizeof(kBytes), "sizes must be equal"); uint128 = bit_cast(kBytes); diff --git a/util/posix/symbolic_constants_posix.cc b/util/posix/symbolic_constants_posix.cc index 6846c5d2..c5a4968b 100644 --- a/util/posix/symbolic_constants_posix.cc +++ b/util/posix/symbolic_constants_posix.cc @@ -99,9 +99,9 @@ const char* kSignalNames[] = { }; #if defined(OS_LINUX) // NSIG is 64 to account for real-time signals. -COMPILE_ASSERT(arraysize(kSignalNames) == 32, kSignalNames_length); +static_assert(arraysize(kSignalNames) == 32, "kSignalNames length"); #else -COMPILE_ASSERT(arraysize(kSignalNames) == NSIG, kSignalNames_length); +static_assert(arraysize(kSignalNames) == NSIG, "kSignalNames length"); #endif const char kSigPrefix[] = "SIG"; diff --git a/util/stdlib/string_number_conversion.cc b/util/stdlib/string_number_conversion.cc index 76c80fdd..0c16c038 100644 --- a/util/stdlib/string_number_conversion.cc +++ b/util/stdlib/string_number_conversion.cc @@ -21,20 +21,19 @@ #include -#include "base/basictypes.h" #include "base/logging.h" #include "util/stdlib/cxx.h" -// CONSTEXPR_COMPILE_ASSERT will be a normal COMPILE_ASSERT if the C++ library -// is the C++11 library. If using an older C++ library when compiling C++11 -// code, the std::numeric_limits<>::min() and max() functions will not be -// marked as constexpr, and thus won’t be usable with C++11’s static_assert(). -// In that case, a run-time CHECK() will have to do. +// CONSTEXPR_STATIC_ASSERT will be a normal static_assert if the C++ library is +// the C++11 library. If using an older C++ library when compiling C++11 code, +// the std::numeric_limits<>::min() and max() functions will not be marked as +// constexpr, and thus won’t be usable with C++11’s static_assert(). In that +// case, a run-time CHECK() will have to do. #if CXX_LIBRARY_VERSION >= 2011 -#define CONSTEXPR_COMPILE_ASSERT(condition, message) \ - COMPILE_ASSERT(condition, message) +#define CONSTEXPR_STATIC_ASSERT(condition, message) \ + static_assert(condition, message) #else -#define CONSTEXPR_COMPILE_ASSERT(condition, message) CHECK(condition) +#define CONSTEXPR_STATIC_ASSERT(condition, message) CHECK(condition) << message #endif namespace { @@ -44,22 +43,22 @@ struct StringToIntegerTraits { typedef TIntType IntType; typedef TLongType LongType; static void TypeCheck() { - COMPILE_ASSERT(std::numeric_limits::is_integer && - std::numeric_limits::is_integer, - IntType_and_LongType_must_be_integer); - COMPILE_ASSERT(std::numeric_limits::is_signed == - std::numeric_limits::is_signed, - IntType_and_LongType_signedness_must_agree); - CONSTEXPR_COMPILE_ASSERT(std::numeric_limits::min() >= - std::numeric_limits::min() && - std::numeric_limits::min() < - std::numeric_limits::max(), - IntType_min_must_be_in_LongType_range); - CONSTEXPR_COMPILE_ASSERT(std::numeric_limits::max() > - std::numeric_limits::min() && - std::numeric_limits::max() <= - std::numeric_limits::max(), - IntType_max_must_be_in_LongType_range); + static_assert(std::numeric_limits::is_integer && + std::numeric_limits::is_integer, + "IntType and LongType must be integer"); + static_assert(std::numeric_limits::is_signed == + std::numeric_limits::is_signed, + "IntType and LongType signedness must agree"); + CONSTEXPR_STATIC_ASSERT(std::numeric_limits::min() >= + std::numeric_limits::min() && + std::numeric_limits::min() < + std::numeric_limits::max(), + "IntType min must be in LongType range"); + CONSTEXPR_STATIC_ASSERT(std::numeric_limits::max() > + std::numeric_limits::min() && + std::numeric_limits::max() <= + std::numeric_limits::max(), + "IntType max must be in LongType range"); } }; @@ -67,8 +66,8 @@ template struct StringToSignedIntegerTraits : public StringToIntegerTraits { static void TypeCheck() { - COMPILE_ASSERT(std::numeric_limits::is_signed, - StringToSignedTraits_IntType_must_be_signed); + static_assert(std::numeric_limits::is_signed, + "StringToSignedTraits IntType must be signed"); return super::TypeCheck(); } static bool IsNegativeOverflow(TLongType value) { @@ -83,8 +82,8 @@ template struct StringToUnsignedIntegerTraits : public StringToIntegerTraits { static void TypeCheck() { - COMPILE_ASSERT(!std::numeric_limits::is_signed, - StringToUnsignedTraits_IntType_must_be_unsigned); + static_assert(!std::numeric_limits::is_signed, + "StringToUnsignedTraits IntType must be unsigned"); return super::TypeCheck(); } static bool IsNegativeOverflow(TLongType value) { return false; } diff --git a/util/stdlib/strlcpy_test.cc b/util/stdlib/strlcpy_test.cc index 9dfa5ac7..1cf187c2 100644 --- a/util/stdlib/strlcpy_test.cc +++ b/util/stdlib/strlcpy_test.cc @@ -18,6 +18,7 @@ #include +#include "base/basictypes.h" #include "base/strings/string16.h" #include "base/strings/stringprintf.h" #include "gtest/gtest.h" diff --git a/util/test/multiprocess_exec.cc b/util/test/multiprocess_exec.cc index 4cad7e0d..4888a809 100644 --- a/util/test/multiprocess_exec.cc +++ b/util/test/multiprocess_exec.cc @@ -72,9 +72,9 @@ void MultiprocessExec::PreFork() { void MultiprocessExec::MultiprocessChild() { // Make sure that stdin, stdout, and stderr are FDs 0, 1, and 2, respectively. // All FDs above this will be closed. - COMPILE_ASSERT(STDIN_FILENO == 0, stdin_must_be_fd_0); - COMPILE_ASSERT(STDOUT_FILENO == 1, stdout_must_be_fd_1); - COMPILE_ASSERT(STDERR_FILENO == 2, stderr_must_be_fd_2); + static_assert(STDIN_FILENO == 0, "stdin must be fd 0"); + static_assert(STDOUT_FILENO == 1, "stdout must be fd 1"); + static_assert(STDERR_FILENO == 2, "stderr must be fd 2"); // Move the read pipe to stdin. int read_fd = ReadPipeFD();