1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 17:47:45 +00:00

AK+Everywhere: Add AK_COMPILER_{GCC,CLANG} and use them most places

Doesn't use them in libc headers so that those don't have to pull in
AK/Platform.h.

AK_COMPILER_GCC is set _only_ for gcc, not for clang too. (__GNUC__ is
defined in clang builds as well.) Using AK_COMPILER_GCC simplifies
things some.

AK_COMPILER_CLANG isn't as much of a win, other than that it's
consistent with AK_COMPILER_GCC.
This commit is contained in:
Nico Weber 2022-10-04 15:04:13 -04:00 committed by Linus Groh
parent ff4b912b7c
commit 2af028132a
35 changed files with 64 additions and 49 deletions

View file

@ -11,7 +11,7 @@
template<Unsigned IntType>
inline constexpr int popcount(IntType value)
{
#if defined(__GNUC__) || defined(__clang__)
#if defined(AK_COMPILER_CLANG) || defined(AK_COMPILER_GCC)
static_assert(sizeof(IntType) <= sizeof(unsigned long long));
if constexpr (sizeof(IntType) <= sizeof(unsigned int))
return __builtin_popcount(value);
@ -39,7 +39,7 @@ inline constexpr int popcount(IntType value)
template<Unsigned IntType>
inline constexpr int count_trailing_zeroes(IntType value)
{
#if defined(__GNUC__) || defined(__clang__)
#if defined(AK_COMPILER_CLANG) || defined(AK_COMPILER_GCC)
static_assert(sizeof(IntType) <= sizeof(unsigned long long));
if constexpr (sizeof(IntType) <= sizeof(unsigned int))
return __builtin_ctz(value);
@ -77,7 +77,7 @@ inline constexpr int count_trailing_zeroes_safe(IntType value)
template<Unsigned IntType>
inline constexpr int count_leading_zeroes(IntType value)
{
#if defined(__GNUC__) || defined(__clang__)
#if defined(AK_COMPILER_CLANG) || defined(AK_COMPILER_GCC)
static_assert(sizeof(IntType) <= sizeof(unsigned long long));
if constexpr (sizeof(IntType) <= sizeof(unsigned int))
return __builtin_clz(value) - (32 - (8 * sizeof(IntType)));
@ -114,7 +114,7 @@ inline constexpr int count_leading_zeroes_safe(IntType value)
template<Integral IntType>
inline constexpr int bit_scan_forward(IntType value)
{
#if defined(__GNUC__) || defined(__clang__)
#if defined(AK_COMPILER_CLANG) || defined(AK_COMPILER_GCC)
static_assert(sizeof(IntType) <= sizeof(unsigned long long));
if constexpr (sizeof(IntType) <= sizeof(unsigned int))
return __builtin_ffs(value);

View file

@ -302,7 +302,7 @@ public:
template<typename U, typename V>
[[nodiscard]] static constexpr bool addition_would_overflow(U u, V v)
{
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
Checked checked;
checked = u;
checked += v;
@ -315,7 +315,7 @@ public:
template<typename U, typename V>
[[nodiscard]] static constexpr bool multiplication_would_overflow(U u, V v)
{
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
Checked checked;
checked = u;
checked *= v;

View file

@ -15,7 +15,7 @@
#ifdef ENABLE_COMPILETIME_FORMAT_CHECK
// FIXME: Seems like clang doesn't like calling 'consteval' functions inside 'consteval' functions quite the same way as GCC does,
// it seems to entirely forget that it accepted that parameters to a 'consteval' function to begin with.
# if defined(__clang__) || defined(__CLION_IDE__) || defined(__CLION_IDE_)
# if defined(AK_COMPILER_CLANG) || defined(__CLION_IDE__) || defined(__CLION_IDE_)
# undef ENABLE_COMPILETIME_FORMAT_CHECK
# endif
#endif

View file

@ -168,7 +168,7 @@ public:
// Note: For some reason, clang does not consider `member` as declared here, and as declared above (`SubstitutedIntrusiveListNode<T, Container> T::*`)
// to be of equal types. so for now, just make the members public on clang.
#ifndef __clang__
#if !defined(AK_COMPILER_CLANG)
private:
template<class T_, typename Container_, SubstitutedIntrusiveListNode<T_, Container_> T_::*member>
friend class ::AK::Detail::IntrusiveList;

View file

@ -198,7 +198,7 @@ public:
static constexpr bool IsRaw = IsPointer<Container>;
#ifndef __clang__
#if !defined(AK_COMPILER_CLANG)
private:
template<Integral TK, typename TV, typename TContainer, SubstitutedIntrusiveRedBlackTreeNode<TK, TV, TContainer> TV::*member>
friend class ::AK::Detail::IntrusiveRedBlackTree;

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -24,6 +25,12 @@
# define AK_ARCH_32_BIT
#endif
#if defined(__clang__)
# define AK_COMPILER_CLANG
#elif defined(__GNUC__)
# define AK_COMPILER_GCC
#endif
#if defined(__serenity__)
# define AK_OS_SERENITY
#endif
@ -84,7 +91,7 @@
# define VALIDATE_IS_X86() static_assert(false, "Trying to include x86 only header on non x86 platform");
#endif
#if !defined(__clang__) && !defined(__CLION_IDE_) && !defined(__CLION_IDE__)
#if !defined(AK_COMPILER_CLANG) && !defined(__CLION_IDE_) && !defined(__CLION_IDE__)
# define AK_HAS_CONDITIONALLY_TRIVIAL
#endif
@ -121,7 +128,7 @@
#ifdef DISALLOW
# undef DISALLOW
#endif
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# define DISALLOW(message) __attribute__((diagnose_if(1, message, "error")))
#else
# define DISALLOW(message) __attribute__((error(message)))

View file

@ -8,6 +8,8 @@
#pragma once
#include <AK/Platform.h>
namespace AK::Detail {
template<class T, T v>
@ -530,7 +532,7 @@ template<typename T>
inline constexpr bool IsDestructible = requires { declval<T>().~T(); };
template<typename T>
#if defined(__clang__)
#if defined(AK_COMPILER_CLANG)
inline constexpr bool IsTriviallyDestructible = __is_trivially_destructible(T);
#else
inline constexpr bool IsTriviallyDestructible = __has_trivial_destructor(T) && IsDestructible<T>;

View file

@ -6,7 +6,9 @@
#pragma once
#if defined(__clang__) || defined(__CLION_IDE__)
#include <AK/Platform.h>
#if defined(AK_COMPILER_CLANG) || defined(__CLION_IDE__)
# pragma clang diagnostic ignored "-Wunqualified-std-cast-call"
#endif

View file

@ -338,7 +338,7 @@ struct CaseInsensitiveStringViewTraits : public Traits<StringView> {
// FIXME: Remove this when clang fully supports consteval (specifically in the context of default parameter initialization).
// See: https://stackoverflow.com/questions/68789984/immediate-function-as-default-function-argument-initializer-in-clang
#if defined(__clang__)
#if defined(AK_COMPILER_CLANG)
# define AK_STRING_VIEW_LITERAL_CONSTEVAL constexpr
#else
# define AK_STRING_VIEW_LITERAL_CONSTEVAL consteval