From 12f18049ccee4935659da61ba0bf80d9cbd5cf18 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 | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/MdePkg/Include/Base.h b/MdePkg/Include/Base.h index 45deb4cec0..210586954d 100644 --- a/MdePkg/Include/Base.h +++ b/MdePkg/Include/Base.h @@ -765,16 +765,28 @@ typedef UINTN *BASE_LIST; @return Alignment requirement, in Bytes, of TYPE. **/ -#if defined(__GNUC__) || defined(__clang__) || (defined(_MSC_VER) && _MSC_VER >= 1900) +#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. + // 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, support the - // vendor-extension __alignof. + // 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