[clang] [clang] Predefine `_CRT_USE_BUILTIN_OFFSETOF` in MS-compatible modes (PR #127568)
A. Jiang via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 17 21:57:44 PST 2025
https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/127568
>From f10028a540d277e255a3c12e4ce6deb20a4165b8 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Tue, 18 Feb 2025 13:57:30 +0800
Subject: [PATCH] [clang] Predefine `_CRT_USE_BUILTIN_OFFSETOF` in
MS-compatible modes
This patch makes Clang predefine `_CRT_USE_BUILTIN_OFFSETOF` in
MS-compatible modes. The macro can make the offsetof provided by MS
UCRT's `<stddef.h>` to select the `__builtin_offsetof` version,
so with it Clang (Clang-cl) can directly consume UCRT's `offsetof`.
MSVC predefines the macro as `1` since at least VS 2017 19.14, but I
think it's also OK to define it in "older" compatible modes.
---
clang/lib/Basic/Targets/OSTargets.cpp | 2 ++
clang/test/Sema/offsetof-ucrt.c | 16 ++++++++++++++++
clang/test/SemaCXX/offsetof-ucrt.cpp | 17 +++++++++++++++++
3 files changed, 35 insertions(+)
create mode 100644 clang/test/Sema/offsetof-ucrt.c
create mode 100644 clang/test/SemaCXX/offsetof-ucrt.cpp
diff --git a/clang/lib/Basic/Targets/OSTargets.cpp b/clang/lib/Basic/Targets/OSTargets.cpp
index 8af6623e5cb15..2e57c286c9a16 100644
--- a/clang/lib/Basic/Targets/OSTargets.cpp
+++ b/clang/lib/Basic/Targets/OSTargets.cpp
@@ -220,6 +220,8 @@ static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
Builder.defineMacro("_MSC_FULL_VER", Twine(Opts.MSCompatibilityVersion));
// FIXME We cannot encode the revision information into 32-bits
Builder.defineMacro("_MSC_BUILD", Twine(1));
+ // https://github.com/llvm/llvm-project/issues/59689
+ Builder.defineMacro("_CRT_USE_BUILTIN_OFFSETOF", Twine(1));
if (Opts.CPlusPlus11 && Opts.isCompatibleWithMSVC(LangOptions::MSVC2015))
Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", Twine(1));
diff --git a/clang/test/Sema/offsetof-ucrt.c b/clang/test/Sema/offsetof-ucrt.c
new file mode 100644
index 0000000000000..0f21b317c1ff2
--- /dev/null
+++ b/clang/test/Sema/offsetof-ucrt.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -verify -fms-compatibility
+
+typedef __typeof__(sizeof(0)) size_t;
+
+#if defined _MSC_VER && !defined _CRT_USE_BUILTIN_OFFSETOF
+#ifdef __cplusplus
+#define offsetof(s,m) ((::size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))
+#else
+#define offsetof(s,m) ((size_t)&(((s*)0)->m))
+#endif
+#else
+#define offsetof(s,m) __builtin_offsetof(s,m)
+#endif
+
+struct S { int a; };
+_Static_assert(offsetof(struct S, a) == 0, "");
diff --git a/clang/test/SemaCXX/offsetof-ucrt.cpp b/clang/test/SemaCXX/offsetof-ucrt.cpp
new file mode 100644
index 0000000000000..a679fa01133f4
--- /dev/null
+++ b/clang/test/SemaCXX/offsetof-ucrt.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -verify -fms-compatibility
+// expected-no-diagnostics
+
+typedef __typeof__(sizeof(0)) size_t;
+
+#if defined _MSC_VER && !defined _CRT_USE_BUILTIN_OFFSETOF
+#ifdef __cplusplus
+#define offsetof(s,m) ((::size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))
+#else
+#define offsetof(s,m) ((size_t)&(((s*)0)->m))
+#endif
+#else
+#define offsetof(s,m) __builtin_offsetof(s,m)
+#endif
+
+struct S { int a; };
+_Static_assert(offsetof(S, a) == 0, "");
More information about the cfe-commits
mailing list