llvm-project/clang/test/Sema/offsetof-ucrt.c
A. Jiang 6abe19ac58
[clang] Predefine _CRT_USE_BUILTIN_OFFSETOF in MS-compatible modes (#127568)
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.

Fixes #59689.
2025-03-13 14:02:44 +08:00

29 lines
851 B
C

// 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)))
#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, "");