From 55a2105258b63d17879db005391dea81c53e3212 Mon Sep 17 00:00:00 2001 From: Yanqin Jin Date: Mon, 10 Jan 2022 11:06:40 -0800 Subject: [PATCH] Make RocksDB codebase compatible with newer compilers like clang-12 (#9370) Summary: Pull Request resolved: https://github.com/facebook/rocksdb/pull/9370 GCC and newer clang, e.g. clang-12 treat `std::unique_ptr` slightly differently. For the following code ``` #include #include #include struct A { std::unique_ptr m1; }; int main() { std::cout << std::boolalpha; std::cout << std::is_standard_layout::value << '\n'; return 0; } ``` GCC11(C++20) (tested on https://en.cppreference.com/w/cpp/types/is_standard_layout) will print "true", while newer clang, e.g. clang-12 will print "false". This breaks the usage of `offsetof()` on structs with non-static members of type `std::unique_ptr`. Fixing this by replacing the builtin `offsetof` with a trick documented at https://gist.github.com/graphitemaster/494f21190bb2c63c5516. Reviewed By: jay-zhuang Differential Revision: D33420840 fbshipit-source-id: 02bde281dfa28809bec787ad0f7019e85dd9c607 --- include/rocksdb/comparator.h | 2 +- include/rocksdb/file_checksum.h | 2 +- include/rocksdb/memtablerep.h | 2 +- include/rocksdb/sst_partitioner.h | 2 +- include/rocksdb/table_properties.h | 2 +- options/customizable_test.cc | 17 +++++++++++++---- 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/rocksdb/comparator.h b/include/rocksdb/comparator.h index c8c720b2f..9c0694cc8 100644 --- a/include/rocksdb/comparator.h +++ b/include/rocksdb/comparator.h @@ -75,7 +75,7 @@ class Comparator : public Customizable { // // Names starting with "rocksdb." are reserved and should not be used // by any clients of this package. - virtual const char* Name() const override = 0; + const char* Name() const override = 0; // Advanced functions: these are used to reduce the space requirements // for internal data structures like index blocks. diff --git a/include/rocksdb/file_checksum.h b/include/rocksdb/file_checksum.h index e993690b4..0645299e1 100644 --- a/include/rocksdb/file_checksum.h +++ b/include/rocksdb/file_checksum.h @@ -85,7 +85,7 @@ class FileChecksumGenFactory : public Customizable { const FileChecksumGenContext& context) = 0; // Return the name of this FileChecksumGenFactory. - virtual const char* Name() const override = 0; + const char* Name() const override = 0; }; // FileChecksumList stores the checksum information of a list of files (e.g., diff --git a/include/rocksdb/memtablerep.h b/include/rocksdb/memtablerep.h index 64ebaf966..66332978d 100644 --- a/include/rocksdb/memtablerep.h +++ b/include/rocksdb/memtablerep.h @@ -311,7 +311,7 @@ class MemTableRepFactory : public Customizable { return CreateMemTableRep(key_cmp, allocator, slice_transform, logger); } - virtual const char* Name() const override = 0; + const char* Name() const override = 0; // Return true if the current MemTableRep supports concurrent inserts // Default: false diff --git a/include/rocksdb/sst_partitioner.h b/include/rocksdb/sst_partitioner.h index 9765f61ec..e2370132c 100644 --- a/include/rocksdb/sst_partitioner.h +++ b/include/rocksdb/sst_partitioner.h @@ -93,7 +93,7 @@ class SstPartitionerFactory : public Customizable { const SstPartitioner::Context& context) const = 0; // Returns a name that identifies this partitioner factory. - virtual const char* Name() const override = 0; + const char* Name() const override = 0; }; /* diff --git a/include/rocksdb/table_properties.h b/include/rocksdb/table_properties.h index a6813e9e8..e26b43d77 100644 --- a/include/rocksdb/table_properties.h +++ b/include/rocksdb/table_properties.h @@ -162,7 +162,7 @@ class TablePropertiesCollectorFactory : public Customizable { TablePropertiesCollectorFactory::Context context) = 0; // The name of the properties collector can be used for debugging purpose. - virtual const char* Name() const override = 0; + const char* Name() const override = 0; // Can be overridden by sub-classes to return the Name, followed by // configuration info that will // be logged to the info log when the diff --git a/options/customizable_test.cc b/options/customizable_test.cc index ea6e73fb2..2973f3c62 100644 --- a/options/customizable_test.cc +++ b/options/customizable_test.cc @@ -206,22 +206,31 @@ struct SimpleOptions { TestCustomizable* cp = nullptr; }; +static SimpleOptions dummy_simple_options; +template +int offset_of(T1 SimpleOptions::*member) { + return static_cast( + reinterpret_cast( + std::addressof(dummy_simple_options.*member)) - + reinterpret_cast(std::addressof(dummy_simple_options))); +} + static std::unordered_map simple_option_info = { #ifndef ROCKSDB_LITE {"bool", - {offsetof(struct SimpleOptions, b), OptionType::kBoolean, + {offset_of(&SimpleOptions::b), OptionType::kBoolean, OptionVerificationType::kNormal, OptionTypeFlags::kNone}}, {"unique", OptionTypeInfo::AsCustomUniquePtr( - offsetof(struct SimpleOptions, cu), OptionVerificationType::kNormal, + offset_of(&SimpleOptions::cu), OptionVerificationType::kNormal, OptionTypeFlags::kAllowNull)}, {"shared", OptionTypeInfo::AsCustomSharedPtr( - offsetof(struct SimpleOptions, cs), OptionVerificationType::kNormal, + offset_of(&SimpleOptions::cs), OptionVerificationType::kNormal, OptionTypeFlags::kAllowNull)}, {"pointer", OptionTypeInfo::AsCustomRawPtr( - offsetof(struct SimpleOptions, cp), OptionVerificationType::kNormal, + offset_of(&SimpleOptions::cp), OptionVerificationType::kNormal, OptionTypeFlags::kAllowNull)}, #endif // ROCKSDB_LITE };