Blame chromium-84-base-has_bultin.patch

d155912
From f2076c6a4c9c14538d71a3ef9c9e4fd40ee6ee00 Mon Sep 17 00:00:00 2001
d155912
From: Stephan Hartmann <stha09@googlemail.com>
d155912
Date: Sun, 31 May 2020 11:46:05 +0000
d155912
Subject: [PATCH] GCC: fix __has_builtin defines for non-clang compilers
d155912
d155912
Defining __has_builtin to 0 for non-clang compilers in base headers can
d155912
lead to wrong detection of features. For example in base/location.h
d155912
checking for __has_builtin macros succeeds for non-clang compilers,
d155912
because base/check_op.h defines __has_builtin to 0 and is included
d155912
before base/location.h. Instead of defining __has_builtin to 0 for
d155912
non-clang compilers, define an independent preprocessor symbol that
d155912
reflects support for requested feature. Undefine the symbol to avoid
d155912
collision.
d155912
d155912
While we're at it fix base/memory/aligned_memory.h too.
d155912
d155912
Bug: 819294
d155912
Change-Id: Iac40dc44e7356b600e7d06aa4ccd1294bb09ebce
d155912
---
d155912
d155912
diff --git a/base/check_op.h b/base/check_op.h
d155912
index 04b0c6f..28f4263a 100644
d155912
--- a/base/check_op.h
d155912
+++ b/base/check_op.h
d155912
@@ -48,8 +48,10 @@
d155912
                                  void (*stream_func)(std::ostream&,
d155912
                                                      const void*));
d155912
 
d155912
-#ifndef __has_builtin
d155912
-#define __has_builtin(x) 0  // Compatibility with non-clang compilers.
d155912
+#ifdef __has_builtin
d155912
+#define SUPPORTS_BUILTIN_ADDRESSOF (__has_builtin(__builtin_addressof))
d155912
+#else
d155912
+#define SUPPORTS_BUILTIN_ADDRESSOF 0
d155912
 #endif
d155912
 
d155912
 template <typename T>
d155912
@@ -65,7 +67,7 @@
d155912
   // operator& might be overloaded, so do the std::addressof dance.
d155912
   // __builtin_addressof is preferred since it also handles Obj-C ARC pointers.
d155912
   // Some casting is still needed, because T might be volatile.
d155912
-#if __has_builtin(__builtin_addressof)
d155912
+#if SUPPORTS_BUILTIN_ADDRESSOF
d155912
   const void* vp = const_cast<const void*>(
d155912
       reinterpret_cast<const volatile void*>(__builtin_addressof(v)));
d155912
 #else
d155912
@@ -75,6 +77,8 @@
d155912
   return StreamValToStr(vp, f);
d155912
 }
d155912
 
d155912
+#undef SUPPORTS_BUILTIN_ADDRESSOF
d155912
+
d155912
 // Overload for types that have no operator<< but do have .ToString() defined.
d155912
 template <typename T>
d155912
 inline typename std::enable_if<
d155912
diff --git a/base/memory/aligned_memory.h b/base/memory/aligned_memory.h
d155912
index d1cba0c..a0d9f13 100644
d155912
--- a/base/memory/aligned_memory.h
d155912
+++ b/base/memory/aligned_memory.h
d155912
@@ -57,13 +57,15 @@
d155912
   }
d155912
 };
d155912
 
d155912
-#ifndef __has_builtin
d155912
-#define __has_builtin(x) 0  // Compatibility with non-clang compilers.
d155912
+#ifdef __has_builtin
d155912
+#define SUPPORTS_BUILTIN_IS_ALIGNED (__has_builtin(__builtin_is_aligned))
d155912
+#else
d155912
+#define SUPPORTS_BUILTIN_IS_ALIGNED 0
d155912
 #endif
d155912
 
d155912
 inline bool IsAligned(uintptr_t val, size_t alignment) {
d155912
   // If the compiler supports builtin alignment checks prefer them.
d155912
-#if __has_builtin(__builtin_is_aligned)
d155912
+#if SUPPORTS_BUILTIN_IS_ALIGNED
d155912
   return __builtin_is_aligned(val, alignment);
d155912
 #else
d155912
   DCHECK(!((alignment - 1) & alignment))
d155912
@@ -72,6 +74,8 @@
d155912
 #endif
d155912
 }
d155912
 
d155912
+#undef SUPPORTS_BUILTIN_IS_ALIGNED
d155912
+
d155912
 inline bool IsAligned(void* val, size_t alignment) {
d155912
   return IsAligned(reinterpret_cast<uintptr_t>(val), alignment);
d155912
 }