From efee2e57e9ed56a596ba91069ff7e6c728470fb0 Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Wed, 1 Oct 2025 14:00:44 -0700 Subject: [PATCH] Fix __declspec support for ABSL_DECLARE_FLAG() Fix #1817 PiperOrigin-RevId: 813902689 Change-Id: Ic3508c02d5ed2e7dc220cdf02526680ae0adbbc1 --- absl/flags/declare.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/absl/flags/declare.h b/absl/flags/declare.h index 8d2a856e..a0b12bb5 100644 --- a/absl/flags/declare.h +++ b/absl/flags/declare.h @@ -59,10 +59,19 @@ ABSL_NAMESPACE_END // Internal implementation of ABSL_DECLARE_FLAG to allow macro expansion of its // arguments. Clients must use ABSL_DECLARE_FLAG instead. +// +// The non-MSVC implementation declares the flag twice. This is to allow +// applying attributes to the second declaration. However, this causes a +// compile error (C4273) in MSVC if a `__declspec` is prepended to the macro. +#if defined(_MSC_VER) +#define ABSL_DECLARE_FLAG_INTERNAL(type, name) \ + extern absl::Flag FLAGS_##name +#else #define ABSL_DECLARE_FLAG_INTERNAL(type, name) \ extern absl::Flag FLAGS_##name; \ namespace absl /* block flags in namespaces */ {} \ /* second redeclaration is to allow applying attributes */ \ extern absl::Flag FLAGS_##name +#endif // _MSC_VER #endif // ABSL_FLAGS_DECLARE_H_