commonlib: Use __builtin_offsetof with supported compilers

Use __builtin_offsetof (which is treated as a constant expression) with
Clang & GCC. This also allows check_member to work with Clang 9.

Signed-off-by: Alex James <theracermaster@gmail.com>
Change-Id: I8b5cb4110c13ee42114ecf65932d7f1e5636210e
Reviewed-on: https://review.coreboot.org/c/coreboot/+/36249
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Alex James 2019-10-22 18:32:22 -05:00 committed by Patrick Georgi
parent ddcfcb8ebc
commit 8119841ec0
1 changed files with 4 additions and 4 deletions

View File

@ -108,16 +108,16 @@
#define GHz (1000 * MHz) #define GHz (1000 * MHz)
#ifndef offsetof #ifndef offsetof
#ifdef __ROMCC__
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#else
#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
#endif
#endif #endif
#if !defined(__clang__)
#define check_member(structure, member, offset) _Static_assert( \ #define check_member(structure, member, offset) _Static_assert( \
offsetof(struct structure, member) == offset, \ offsetof(struct structure, member) == offset, \
"`struct " #structure "` offset for `" #member "` is not " #offset) "`struct " #structure "` offset for `" #member "` is not " #offset)
#else
#define check_member(structure, member, offset)
#endif
/** /**
* container_of - cast a member of a structure out to the containing structure * container_of - cast a member of a structure out to the containing structure