[PATCH] D133574: [C2x] reject type definitions in offsetof

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 26 08:16:19 PDT 2022


aaron.ballman added a subscriber: nickdesaulniers.
aaron.ballman added a comment.

In D133574#3815105 <https://reviews.llvm.org/D133574#3815105>, @inclyc wrote:

> Hi @aaron.ballman, I've noticed in the linux kernel, type alignment was implemented by a tricky way using offsetof.
>
>   #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
>
> Does this always has the same semantic with C11 `_Alignof`? If this is not true, I think unfortunately we may emit a switchable warning instead of an error to preserve semantics here.

Technically there's no requirement that they return the same value (the structure could insert arbitrary padding, including no padding), so it's theoretically possible they return different values. But I can't think of a situation in which you'd get a different answer from `TYPE_ALIGN` as you would get from `_Alignof`.

@nickdesaulniers any thoughts here since this can impact the Linux kernel?



================
Comment at: clang/include/clang/Parse/Parser.h:252-257
+    // Not parsing a type within __builtin_offsetof
+    Outside,
+    // Parsing a type within __builtin_offsetof
+    Builtin,
+    // Parsing a type within macro "offsetof", defined in __buitin_offsetof
+    // To improve our diagnostic message
----------------



================
Comment at: clang/include/clang/Sema/Sema.h:3279
                  bool IsTypeSpecifier, bool IsTemplateParamOrArg,
+                 bool IsWithinOffsetOf, bool IsOffsetOfInMacro,
                  SkipBodyInfo *SkipBody = nullptr);
----------------
Instead of passing two `bool`s, why not pass `Parser::OffsetOfStateKind` directly?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133574/new/

https://reviews.llvm.org/D133574



More information about the cfe-commits mailing list