From fcbf3d2b628da205a5eef10321033f8232ab7b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20Ha=CC=88user?= <8659494+mhaeuser@users.noreply.github.com> Date: Sun, 12 Mar 2023 22:08:46 +0100 Subject: [PATCH] MdePkg/Base.h: Add support for C++ alignof MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In preparation of including Base.h in C++ BaseTools programs, support the C++ 11 alignof operator. The C _Alignof operator is not universally supported in C++ mode across compiler toolchains and thus the current definition may lead to compilation errors with said programs. Signed-off-by: Marvin Häuser --- MdePkg/Include/Base.h | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/MdePkg/Include/Base.h b/MdePkg/Include/Base.h index 03e84f3f3a..d3f21a03ff 100644 --- a/MdePkg/Include/Base.h +++ b/MdePkg/Include/Base.h @@ -765,23 +765,30 @@ typedef UINTN *BASE_LIST; @return Alignment requirement, in Bytes, of TYPE. **/ -#if defined (__cplusplus) -// -// Standard C++ operator. -// -#define ALIGNOF(TYPE) alignof (TYPE) -#elif defined (__GNUC__) || defined (__clang__) || (defined (_MSC_VER) && _MSC_VER >= 1900) -// -// All supported versions of GCC and Clang, as well as MSVC 2015 and later, -// support the standard operator _Alignof. -// -#define ALIGNOF(TYPE) _Alignof (TYPE) -#elif defined (_MSC_EXTENSIONS) -// -// Earlier versions of MSVC, at least MSVC 2008 and later, support the vendor -// extension __alignof. -// -#define ALIGNOF(TYPE) __alignof (TYPE) +#if defined(__cplusplus) && __cplusplus >= 201103L + // + // C++ 11 and later support the standard operator alignof. + // + #define ALIGNOF(TYPE) alignof (TYPE) +#elif ((defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1900)) && !defined (__cplusplus)) || defined(__clang__) + // + // All supported versions of GCC and Clang, as well as MSVC 2015 and later, + // support the standard operator _Alignof in C mode. GCC and MSVC do not + // support it in C++ mode though. + // + #define ALIGNOF(TYPE) _Alignof (TYPE) +#elif defined(__GNUC__) + // + // GCC does not support _Alignof in C++ mode, unlike Clang. The vendor- + // extenstion is supported in both C and C++ mode. + // + #define ALIGNOF(TYPE) __alignof__ (TYPE) +#elif defined(_MSC_EXTENSIONS) + // + // Earlier versions of MSVC, at least MSVC 2008 and later, as well as current + // versions in C++ mode support the vendor-extension __alignof. + // + #define ALIGNOF(TYPE) __alignof (TYPE) #else // // For compilers that do not support inbuilt alignof operators, use OFFSET_OF.