From 5712e1a746abe0786a62014a24840dd44533422a Mon Sep 17 00:00:00 2001 From: Kent Ross Date: Thu, 15 Dec 2022 17:37:04 -0800 Subject: [PATCH] place alignas() before lifetime specifiers (#11248) This patch fixes a compilation bug introduced in 821b0732f23af1c73155e71d4c46b8023664aacf. When the `constinit` keyword is available (such as in C++20), putting the `alignas` attribute after `ABSL_CONST_INIT` is an error: ``` ERROR: /home/widders/.cache/bazel/_bazel_widders/25f0d335ca0e01a2fd74c60e90175e8f/external/com_google_protobuf/src/google/protobuf/compiler/java/BUILD.bazel:44:11: Compiling src/google/protobuf/compiler/java/generator_factory.cc failed: (Exit 1): clang failed: error executing command /usr/lib/llvm-16/bin/clang -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer -g0 ... (remaining 64 arguments skipped) Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging In file included from external/com_google_protobuf/src/google/protobuf/compiler/java/generator_factory.cc:35: In file included from bazel-out/k8-opt-exec-2B5CBBC6/bin/external/com_google_protobuf/src/google/protobuf/compiler/java/_virtual_includes/java/google/protobuf/compiler/java/context.h:38: In file included from bazel-out/k8-opt-exec-2B5CBBC6/bin/external/com_google_protobuf/src/google/protobuf/compiler/java/_virtual_includes/names_internal/google/protobuf/compiler/java/helpers.h:45: In file included from bazel-out/k8-opt-exec-2B5CBBC6/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/protobuf_nowkt/google/protobuf/descriptor.pb.h:25: In file included from bazel-out/k8-opt-exec-2B5CBBC6/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/protobuf_lite/google/protobuf/arena.h:53: bazel-out/k8-opt-exec-2B5CBBC6/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/protobuf_lite/google/protobuf/arena_impl.h:584:19: error: an attribute list cannot appear here ABSL_CONST_INIT alignas( ^~~~~~~~ 1 error generated. ``` Therefore, it should be ordered as 1. always attribute-like (`alignas`) 2. sometimes attributes (`ABSL_CONST_INIT`, which may be a lifetime specifier or an attribute) 3. lifetime specifiers (`static`) 4. type etc. Where it currently is it may be in the midst of lifetime specifiers, and if it is moved to the right it syntactically applies to the type rather than the whole declaration which is also invalid. If this ordering could not be resolved for all cases it could also be moved to the end, after the name being declared, but we don't need to do that here. Closes #11248 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/11248 from mumbleskates:alignas-fix 496af774f898a5950c93c27c608ef1ecec637603 PiperOrigin-RevId: 495739359 --- src/google/protobuf/arena.cc | 2 +- src/google/protobuf/thread_safe_arena.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/arena.cc b/src/google/protobuf/arena.cc index 2ba9ff820d6f..b8ce7d8c97c8 100644 --- a/src/google/protobuf/arena.cc +++ b/src/google/protobuf/arena.cc @@ -443,7 +443,7 @@ ThreadSafeArena::SerialArenaChunk* ThreadSafeArena::SentrySerialArenaChunk() { } -ABSL_CONST_INIT alignas(kCacheAlignment) +alignas(kCacheAlignment) ABSL_CONST_INIT std::atomic ThreadSafeArena::lifecycle_id_{0}; #if defined(PROTOBUF_NO_THREADLOCAL) ThreadSafeArena::ThreadCache& ThreadSafeArena::thread_cache() { diff --git a/src/google/protobuf/thread_safe_arena.h b/src/google/protobuf/thread_safe_arena.h index a5ef1c2d993c..fa34d43576a1 100644 --- a/src/google/protobuf/thread_safe_arena.h +++ b/src/google/protobuf/thread_safe_arena.h @@ -269,8 +269,8 @@ class PROTOBUF_EXPORT ThreadSafeArena { #pragma warning(disable : 4324) #endif using LifecycleId = uint64_t; - ABSL_CONST_INIT alignas( - kCacheAlignment) static std::atomic lifecycle_id_; + alignas(kCacheAlignment) ABSL_CONST_INIT + static std::atomic lifecycle_id_; #if defined(PROTOBUF_NO_THREADLOCAL) // iOS does not support __thread keyword so we use a custom thread local // storage class we implemented.