[clang] Rewrite offsetof with __builtin_offsetof with -fms-kernel (PR #193804)

via cfe-commits cfe-commits at lists.llvm.org
Wed May 6 10:31:25 PDT 2026


eleviant wrote:

> I'm pretty sure CONTAINING_RECORD can't be constexpr-evaluated anyway

Very true. The problem is that MSVC headers have a whole bunch of such macros, here are the few:
```
#define FIELD_OFFSET(type, field)    ((LONG)(LONG_PTR)&(((type *)0)->field))
#define RTL_SIZEOF_THROUGH_FIELD(type, field) \
    (FIELD_OFFSET(type, field) + RTL_FIELD_SIZE(type, field))
#define RTL_CONTAINS_FIELD(Struct, Size, Field) \
    ( (((PCHAR)(&(Struct)->Field)) + sizeof((Struct)->Field)) <= (((PCHAR)(Struct))+(Size)) )
#define NdrFieldOffset(s,f)     (LONG_PTR)(& (((s  *)0)->f))
```

> My biggest concern that this is introducing an incompatibility with MSVC; this is not how MSVC handles these constructs.

May be. However MSVC allows evaluating those as constexpr, while clang doesn't. Another problem is that there can be some constructs, not handled by this patch at all, for example:

```
#define RTL_FIELD_SIZE(type, field) (sizeof(((type *)0)->field))
```
What do you thing about allowing reinterpret_cast from nullptr if LangOpts.NullPointerIsValid is set? Just for -fms-kernel, of course

https://github.com/llvm/llvm-project/pull/193804


More information about the cfe-commits mailing list