https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/127568
>From 2c9e6e45944891af54cba9648297a996bb4d8cca Mon Sep 17 00:00:00 2001 From: "A. Jiang" <d...@live.cn> Date: Tue, 18 Feb 2025 14:03:35 +0800 Subject: [PATCH 1/3] [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 | 17 +++++++++++++++++ clang/test/SemaCXX/offsetof-ucrt.cpp | 17 +++++++++++++++++ 3 files changed, 36 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..60c9ae3b5650e --- /dev/null +++ b/clang/test/Sema/offsetof-ucrt.c @@ -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(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, ""); >From 4dc9bed47d7b4802e91b1774d8371e169f3f942c Mon Sep 17 00:00:00 2001 From: "A. Jiang" <d...@live.cn> Date: Fri, 21 Feb 2025 17:00:37 +0800 Subject: [PATCH 2/3] Address @AaronBallman's review comments --- clang/lib/Basic/Targets/OSTargets.cpp | 2 +- clang/test/Sema/offsetof-ucrt.c | 11 +++++++++++ clang/test/SemaCXX/offsetof-ucrt.cpp | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/clang/lib/Basic/Targets/OSTargets.cpp b/clang/lib/Basic/Targets/OSTargets.cpp index 2e57c286c9a16..e744e84a5b079 100644 --- a/clang/lib/Basic/Targets/OSTargets.cpp +++ b/clang/lib/Basic/Targets/OSTargets.cpp @@ -220,7 +220,7 @@ 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 + // Exposed by MSVC, used in their stddef.h. Builder.defineMacro("_CRT_USE_BUILTIN_OFFSETOF", Twine(1)); if (Opts.CPlusPlus11 && Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)) diff --git a/clang/test/Sema/offsetof-ucrt.c b/clang/test/Sema/offsetof-ucrt.c index 60c9ae3b5650e..05c3d87ce66ca 100644 --- a/clang/test/Sema/offsetof-ucrt.c +++ b/clang/test/Sema/offsetof-ucrt.c @@ -1,8 +1,19 @@ // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -verify -fms-compatibility +// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -verify // expected-no-diagnostics typedef __typeof__(sizeof(0)) size_t; +#ifdef _MSC_VER +#ifndef _CRT_USE_BUILTIN_OFFSETOF +#error _CRT_USE_BUILTIN_OFFSETOF should be predefined in MSVC-compatible modes. +#endif +#else +#ifdef _CRT_USE_BUILTIN_OFFSETOF +#error _CRT_USE_BUILTIN_OFFSETOF should not be predefined in non-MSVC-compatible modes. +#endif +#endif + #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))) diff --git a/clang/test/SemaCXX/offsetof-ucrt.cpp b/clang/test/SemaCXX/offsetof-ucrt.cpp index a679fa01133f4..95437554c4665 100644 --- a/clang/test/SemaCXX/offsetof-ucrt.cpp +++ b/clang/test/SemaCXX/offsetof-ucrt.cpp @@ -1,8 +1,19 @@ // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -verify -fms-compatibility +// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -verify // expected-no-diagnostics typedef __typeof__(sizeof(0)) size_t; +#ifdef _MSC_VER +#ifndef _CRT_USE_BUILTIN_OFFSETOF +#error _CRT_USE_BUILTIN_OFFSETOF should be predefined in MSVC-compatible modes. +#endif +#else +#ifdef _CRT_USE_BUILTIN_OFFSETOF +#error _CRT_USE_BUILTIN_OFFSETOF should not be predefined in non-MSVC-compatible modes. +#endif +#endif + #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))) >From d974358e1490945156a8d51c19fcd38d50d33efb Mon Sep 17 00:00:00 2001 From: "A. Jiang" <d...@live.cn> Date: Sat, 22 Feb 2025 16:03:18 +0800 Subject: [PATCH 3/3] Release notes for `_CRT_USE_BUILTIN_OFFSETOF` --- clang/docs/ReleaseNotes.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index ec9cab2eb657f..00823a035ad38 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -227,6 +227,10 @@ Android Support Windows Support ^^^^^^^^^^^^^^^ +- Clang now defines ``_CRT_USE_BUILTIN_OFFSETOF`` macro in MSVC-compatible mode, + which makes ``offsetof`` provided by Microsoft's ``<stddef.h>`` to be defined + correctly. (#GH59689) + LoongArch Support ^^^^^^^^^^^^^^^^^ _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits