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 <iostream>
#include <memory>
#include <type_traits>

struct A {
    std::unique_ptr<int> m1;
};

int main()
{
    std::cout << std::boolalpha;
    std::cout << std::is_standard_layout<A>::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
This commit is contained in:
Yanqin Jin 2022-01-10 11:06:40 -08:00 committed by Facebook GitHub Bot
parent 255aefb628
commit 55a2105258
6 changed files with 18 additions and 9 deletions

View File

@ -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.

View File

@ -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.,

View File

@ -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

View File

@ -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;
};
/*

View File

@ -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

View File

@ -206,22 +206,31 @@ struct SimpleOptions {
TestCustomizable* cp = nullptr;
};
static SimpleOptions dummy_simple_options;
template <typename T1>
int offset_of(T1 SimpleOptions::*member) {
return static_cast<int>(
reinterpret_cast<uintptr_t>(
std::addressof(dummy_simple_options.*member)) -
reinterpret_cast<uintptr_t>(std::addressof(dummy_simple_options)));
}
static std::unordered_map<std::string, OptionTypeInfo> 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<TestCustomizable>(
offsetof(struct SimpleOptions, cu), OptionVerificationType::kNormal,
offset_of(&SimpleOptions::cu), OptionVerificationType::kNormal,
OptionTypeFlags::kAllowNull)},
{"shared",
OptionTypeInfo::AsCustomSharedPtr<TestCustomizable>(
offsetof(struct SimpleOptions, cs), OptionVerificationType::kNormal,
offset_of(&SimpleOptions::cs), OptionVerificationType::kNormal,
OptionTypeFlags::kAllowNull)},
{"pointer",
OptionTypeInfo::AsCustomRawPtr<TestCustomizable>(
offsetof(struct SimpleOptions, cp), OptionVerificationType::kNormal,
offset_of(&SimpleOptions::cp), OptionVerificationType::kNormal,
OptionTypeFlags::kAllowNull)},
#endif // ROCKSDB_LITE
};