Commit d21c89bb authored by Anton Bikineev's avatar Anton Bikineev Committed by Commit Bot

cppgc: Support compile-time typename computation

Bug: chromium:1056170
Change-Id: If4d4d08b4a50312b7a3cd1d11bb2cccc2272c96b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2461733Reviewed-by: 's avatarOmer Katz <omerkatz@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Anton Bikineev <bikineev@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70414}
parent 0b96f711
...@@ -5,12 +5,50 @@ ...@@ -5,12 +5,50 @@
#ifndef INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_ #ifndef INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_
#define INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_ #define INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_
#include <cstddef>
#include "cppgc/name-provider.h" #include "cppgc/name-provider.h"
#include "v8config.h" // NOLINT(build/include_directory) #include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc { namespace cppgc {
namespace internal { namespace internal {
#if CPPGC_SUPPORTS_OBJECT_NAMES && defined(__clang__)
#define CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME 1
template <size_t Size>
struct NameBuffer {
char name[Size]{};
static constexpr NameBuffer FromCString(const char* str) {
NameBuffer result;
for (size_t i = 0; i < Size; ++i) result.name[i] = str[i];
return result;
}
};
template <typename T>
const char* GetTypename() {
static constexpr char kSelfPrefix[] =
"const char *cppgc::internal::GetTypename() [T =";
static_assert(__builtin_strncmp(__PRETTY_FUNCTION__, kSelfPrefix,
sizeof(kSelfPrefix) - 1) == 0,
"The prefix must match");
static constexpr const char* kTypenameStart =
__PRETTY_FUNCTION__ + sizeof(kSelfPrefix);
static constexpr size_t kTypenameSize =
__builtin_strlen(__PRETTY_FUNCTION__) - sizeof(kSelfPrefix) - 1;
// NameBuffer is an indirection that is needed to make sure that only a
// substring of __PRETTY_FUNCTION__ gets materialized in the binary.
static constexpr auto buffer =
NameBuffer<kTypenameSize>::FromCString(kTypenameStart);
return buffer.name;
}
#else
#define CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME 0
#endif
struct HeapObjectName { struct HeapObjectName {
const char* value; const char* value;
bool name_was_hidden; bool name_was_hidden;
...@@ -36,7 +74,9 @@ class NameTrait final : public NameTraitBase { ...@@ -36,7 +74,9 @@ class NameTrait final : public NameTraitBase {
} }
static HeapObjectName GetNameFor(...) { static HeapObjectName GetNameFor(...) {
#if CPPGC_SUPPORTS_OBJECT_NAMES #if CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME
return {GetTypename<T>(), false};
#elif CPPGC_SUPPORTS_OBJECT_NAMES
#if defined(V8_CC_GNU) #if defined(V8_CC_GNU)
#define PRETTY_FUNCTION_VALUE __PRETTY_FUNCTION__ #define PRETTY_FUNCTION_VALUE __PRETTY_FUNCTION__
...@@ -63,4 +103,6 @@ using NameCallback = HeapObjectName (*)(const void*); ...@@ -63,4 +103,6 @@ using NameCallback = HeapObjectName (*)(const void*);
} // namespace internal } // namespace internal
} // namespace cppgc } // namespace cppgc
#undef CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME
#endif // INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_ #endif // INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_
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