Skip to content

Commit

Permalink
AK+Everywhere: Add AK_COMPILER_{GCC,CLANG} and use them most places
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nico authored and linusg committed Oct 4, 2022
1 parent ff4b912 commit 2af0281
Show file tree
Hide file tree
Showing 35 changed files with 64 additions and 49 deletions.
8 changes: 4 additions & 4 deletions AK/BuiltinWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions AK/Checked.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class Checked {
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;
Expand All @@ -315,7 +315,7 @@ class Checked {
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;
Expand Down
2 changes: 1 addition & 1 deletion AK/CheckedFormatString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion AK/IntrusiveList.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class IntrusiveListNode {

// 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;
Expand Down
2 changes: 1 addition & 1 deletion AK/IntrusiveRedBlackTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class IntrusiveRedBlackTreeNode : public BaseRedBlackTree<K>::Node {

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;
Expand Down
11 changes: 9 additions & 2 deletions AK/Platform.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2022, Nico Weber <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)))
Expand Down
4 changes: 3 additions & 1 deletion AK/StdLibExtraDetails.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#pragma once

#include <AK/Platform.h>

namespace AK::Detail {

template<class T, T v>
Expand Down Expand Up @@ -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>;
Expand Down
4 changes: 3 additions & 1 deletion AK/StdLibExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion AK/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Arch/x86/i386/Atomics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

extern "C" {

#if defined(__GNUC__) && !defined(__clang__) // FIXME: Remove this file once GCC supports 8-byte atomics on i686
#if defined(AK_COMPILER_GCC) // FIXME: Remove this file once GCC supports 8-byte atomics on i686

u64 kernel__atomic_compare_exchange_8(u64 volatile*, u64*, u64, int, int);
# pragma redefine_extname kernel__atomic_compare_exchange_8 __atomic_compare_exchange_8
Expand Down
2 changes: 1 addition & 1 deletion Kernel/StdLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ ErrorOr<void> memset_user(void* dest_ptr, int c, size_t n)
return {};
}

#if defined(__clang__) && defined(ENABLE_KERNEL_LTO)
#if defined(AK_COMPILER_CLANG) && defined(ENABLE_KERNEL_LTO)
// Due to a chicken-and-egg situation, certain linker-defined symbols that are added on-demand (like the GOT)
// need to be present before LTO bitcode files are compiled. And since we don't link to any native object files,
// the linker does not know that _GLOBAL_OFFSET_TABLE_ is needed, so it doesn't define it, so linking as a PIE fails.
Expand Down
4 changes: 2 additions & 2 deletions Meta/Lagom/Tools/CodeGenerators/IPCCompiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ class @endpoint.name@Stub : public IPC::Stub {
private:
};
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
#pragma clang diagnostic pop
#endif)~~~");
}
Expand Down Expand Up @@ -781,7 +781,7 @@ void build(StringBuilder& builder, Vector<Endpoint> const& endpoints)
#include <LibIPC/Message.h>
#include <LibIPC/Stub.h>
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdefaulted-function-deleted"
#endif)~~~");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ static CodePointRange parse_code_point_range(StringView list)
}

// gcc-11, gcc-12 have a codegen bug on (at least) intel macOS 10.15, see #15449.
#if defined(__GNUC__) && !defined(__clang__) && defined(AK_OS_MACOS)
#if defined(AK_COMPILER_GCC) && defined(AK_OS_MACOS)
# pragma GCC push_options
# pragma GCC optimize("O0")
#endif
Expand Down Expand Up @@ -649,7 +649,7 @@ static ErrorOr<void> parse_unicode_data(Core::Stream::BufferedFile& file, Unicod

return {};
}
#if defined(__GNUC__) && !defined(__clang__) && defined(AK_OS_MACOS)
#if defined(AK_COMPILER_GCC) && defined(AK_OS_MACOS)
# pragma GCC pop_options
#endif

Expand Down
8 changes: 4 additions & 4 deletions Tests/AK/TestRefPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ TEST_CASE(assign_moved_self)
{
RefPtr<Object> object = adopt_ref(*new Object);
EXPECT_EQ(object->ref_count(), 1u);
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wself-move"
#endif
object = move(object);
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic pop
#endif
EXPECT_EQ(object->ref_count(), 1u);
Expand All @@ -113,12 +113,12 @@ TEST_CASE(assign_copy_self)
RefPtr<Object> object = adopt_ref(*new Object);
EXPECT_EQ(object->ref_count(), 1u);

#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
#endif
object = object;
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic pop
#endif

Expand Down
4 changes: 2 additions & 2 deletions Tests/AK/TestWeakPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <AK/WeakPtr.h>
#include <AK/Weakable.h>

#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-private-field"
#endif
Expand All @@ -24,7 +24,7 @@ class SimpleWeakable : public Weakable<SimpleWeakable>
int m_member { 123 };
};

#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic pop
#endif

Expand Down
4 changes: 3 additions & 1 deletion Tests/Kernel/TestSigAltStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#ifdef __clang__
#include <AK/Platform.h>

#if defined(AK_COMPILER_CLANG)
# pragma clang optimize off
#else
# pragma GCC optimize("O0")
Expand Down
2 changes: 1 addition & 1 deletion Tests/Kernel/crash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

using Test::Crash;

#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang optimize off
#else
# pragma GCC optimize("O0")
Expand Down
4 changes: 3 additions & 1 deletion Tests/LibC/TestMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#ifdef __clang__
#include <AK/Platform.h>

#if defined(AK_COMPILER_CLANG)
# pragma clang optimize off
#else
# pragma GCC optimize("O0")
Expand Down
4 changes: 2 additions & 2 deletions Tests/LibC/TestMemalign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ TEST_CASE(aligned_alloc_fuzz)

TEST_CASE(aligned_alloc_not_power2)
{
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wnon-power-of-two-alignment"
#endif
EXPECT_EQ(aligned_alloc(7, 256), nullptr);
EXPECT_EQ(errno, EINVAL);
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic pop
#endif
}
4 changes: 2 additions & 2 deletions Userland/Applications/KeyboardMapper/KeyPositions.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct KeyPosition {

#define KEY_COUNT 63

#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wc99-designator"
#endif
Expand Down Expand Up @@ -99,6 +99,6 @@ struct KeyPosition keys[KEY_COUNT] = {
// clang-format on
};

#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic pop
#endif
2 changes: 1 addition & 1 deletion Userland/DevTools/UserspaceEmulator/Emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <syscall.h>
#include <unistd.h>

#if defined(__GNUC__) && !defined(__clang__)
#if defined(AK_COMPILER_GCC)
# pragma GCC optimize("O3")
#endif

Expand Down
2 changes: 1 addition & 1 deletion Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <syscall.h>
#include <termios.h>

#if defined(__GNUC__) && !defined(__clang__)
#if defined(AK_COMPILER_GCC)
# pragma GCC optimize("O3")
#endif

Expand Down
2 changes: 1 addition & 1 deletion Userland/DevTools/UserspaceEmulator/SoftCPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <string.h>
#include <unistd.h>

#if defined(__GNUC__) && !defined(__clang__)
#if defined(AK_COMPILER_GCC)
# pragma GCC optimize("O3")
#endif

Expand Down
2 changes: 1 addition & 1 deletion Userland/DevTools/UserspaceEmulator/SoftFPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <unistd.h>

#if defined(__GNUC__) && !defined(__clang__)
#if defined(AK_COMPILER_GCC)
# pragma GCC optimize("O3")
#endif

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibC/arch/x86_64/memset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace {
}
}

#if !defined(__clang__) && !defined(_DYNAMIC_LOADER)
#if !defined(AK_COMPILER_CLANG) && !defined(_DYNAMIC_LOADER)
[[gnu::ifunc("resolve_memset")]] void* memset(void*, int, size_t);
#else
// DynamicLoader can't self-relocate IFUNCs.
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibC/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <stdint.h>
#include <stdlib.h>

#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdouble-promotion"
#endif
Expand Down Expand Up @@ -1148,6 +1148,6 @@ float nearbyintf(float value) NOEXCEPT
}
}

#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic pop
#endif
2 changes: 1 addition & 1 deletion Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void DwarfInfo::populate_compilation_units()
// HACK: Clang generates line programs for embedded resource assembly files, but not compile units.
// Meaning that for graphical applications, some line info data would be unread, triggering the assertion below.
// As a fix, we don't create compilation units for line programs that come from resource files.
#ifdef __clang__
#if defined(AK_COMPILER_CLANG)
if (line_program->source_files().size() == 1 && line_program->source_files()[0].name.view().contains("serenity_icon_"sv)) {
debug_info_stream.seek(unit_offset);
} else
Expand Down
Loading

0 comments on commit 2af0281

Please sign in to comment.