[libc] [llvm] [libc][NFC] Fix missing LIBC_INLINE + style (PR #73659)

Guillaume Chatelet via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 28 08:17:49 PST 2023


https://github.com/gchatelet updated https://github.com/llvm/llvm-project/pull/73659

>From 95a5c4b461a23a92c24afd3ab4444c20d8b8fdf2 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Tue, 28 Nov 2023 15:34:12 +0000
Subject: [PATCH 1/2] [libc][NFC] Fix missing LIBC_INLINE + style

---
 libc/src/__support/CPP/limits.h               | 54 ++++++++++---------
 libc/src/__support/CPP/type_traits/invoke.h   |  5 +-
 libc/src/__support/CPP/utility/in_place.h     |  8 +--
 libc/src/__support/CPP/utility/move.h         |  4 +-
 libc/src/__support/FPUtil/FPBits.h            |  8 +--
 libc/src/__support/FPUtil/FloatProperties.h   |  3 +-
 .../__support/FPUtil/x86_64/LongDoubleBits.h  |  8 +--
 libc/src/__support/UInt.h                     | 18 ++++---
 libc/src/__support/integer_operations.h       |  6 ++-
 libc/src/string/memmem.cpp                    |  4 +-
 .../llvm-project-overlay/libc/BUILD.bazel     |  6 +++
 11 files changed, 72 insertions(+), 52 deletions(-)

diff --git a/libc/src/__support/CPP/limits.h b/libc/src/__support/CPP/limits.h
index e163f00c7320911..b1a1203df79674a 100644
--- a/libc/src/__support/CPP/limits.h
+++ b/libc/src/__support/CPP/limits.h
@@ -9,6 +9,8 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H
 #define LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H
 
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
+
 #include <limits.h>
 
 namespace LIBC_NAMESPACE {
@@ -24,74 +26,74 @@ constexpr unsigned long long ULLONG_MAX = ~0ULL;
 
 template <class T> class numeric_limits {
 public:
-  static constexpr T max();
-  static constexpr T min();
+  LIBC_INLINE static constexpr T max();
+  LIBC_INLINE static constexpr T min();
 };
 
 // TODO: Add numeric_limits specializations as needed for new types.
 
 template <> class numeric_limits<int> {
 public:
-  static constexpr int max() { return INT_MAX; }
-  static constexpr int min() { return INT_MIN; }
+  LIBC_INLINE static constexpr int max() { return INT_MAX; }
+  LIBC_INLINE static constexpr int min() { return INT_MIN; }
 };
 template <> class numeric_limits<unsigned int> {
 public:
-  static constexpr unsigned int max() { return UINT_MAX; }
-  static constexpr unsigned int min() { return 0; }
+  LIBC_INLINE static constexpr unsigned int max() { return UINT_MAX; }
+  LIBC_INLINE static constexpr unsigned int min() { return 0; }
 };
 template <> class numeric_limits<long> {
 public:
-  static constexpr long max() { return LONG_MAX; }
-  static constexpr long min() { return LONG_MIN; }
+  LIBC_INLINE static constexpr long max() { return LONG_MAX; }
+  LIBC_INLINE static constexpr long min() { return LONG_MIN; }
 };
 template <> class numeric_limits<unsigned long> {
 public:
-  static constexpr unsigned long max() { return ULONG_MAX; }
-  static constexpr unsigned long min() { return 0; }
+  LIBC_INLINE static constexpr unsigned long max() { return ULONG_MAX; }
+  LIBC_INLINE static constexpr unsigned long min() { return 0; }
 };
 template <> class numeric_limits<long long> {
 public:
-  static constexpr long long max() { return LLONG_MAX; }
-  static constexpr long long min() { return LLONG_MIN; }
+  LIBC_INLINE static constexpr long long max() { return LLONG_MAX; }
+  LIBC_INLINE static constexpr long long min() { return LLONG_MIN; }
 };
 template <> class numeric_limits<unsigned long long> {
 public:
-  static constexpr unsigned long long max() { return ULLONG_MAX; }
-  static constexpr unsigned long long min() { return 0; }
+  LIBC_INLINE static constexpr unsigned long long max() { return ULLONG_MAX; }
+  LIBC_INLINE static constexpr unsigned long long min() { return 0; }
 };
 template <> class numeric_limits<short> {
 public:
-  static constexpr short max() { return SHRT_MAX; }
-  static constexpr short min() { return SHRT_MIN; }
+  LIBC_INLINE static constexpr short max() { return SHRT_MAX; }
+  LIBC_INLINE static constexpr short min() { return SHRT_MIN; }
 };
 template <> class numeric_limits<unsigned short> {
 public:
-  static constexpr unsigned short max() { return USHRT_MAX; }
-  static constexpr unsigned short min() { return 0; }
+  LIBC_INLINE static constexpr unsigned short max() { return USHRT_MAX; }
+  LIBC_INLINE static constexpr unsigned short min() { return 0; }
 };
 template <> class numeric_limits<char> {
 public:
-  static constexpr char max() { return CHAR_MAX; }
-  static constexpr char min() { return CHAR_MIN; }
+  LIBC_INLINE static constexpr char max() { return CHAR_MAX; }
+  LIBC_INLINE static constexpr char min() { return CHAR_MIN; }
 };
 template <> class numeric_limits<signed char> {
 public:
-  static constexpr signed char max() { return SCHAR_MAX; }
-  static constexpr signed char min() { return SCHAR_MIN; }
+  LIBC_INLINE static constexpr signed char max() { return SCHAR_MAX; }
+  LIBC_INLINE static constexpr signed char min() { return SCHAR_MIN; }
 };
 template <> class numeric_limits<unsigned char> {
 public:
-  static constexpr unsigned char max() { return UCHAR_MAX; }
-  static constexpr unsigned char min() { return 0; }
+  LIBC_INLINE static constexpr unsigned char max() { return UCHAR_MAX; }
+  LIBC_INLINE static constexpr unsigned char min() { return 0; }
 };
 #ifdef __SIZEOF_INT128__
 // On platform where UInt128 resolves to __uint128_t, this specialization
 // provides the limits of UInt128.
 template <> class numeric_limits<__uint128_t> {
 public:
-  static constexpr __uint128_t max() { return ~__uint128_t(0); }
-  static constexpr __uint128_t min() { return 0; }
+  LIBC_INLINE static constexpr __uint128_t max() { return ~__uint128_t(0); }
+  LIBC_INLINE static constexpr __uint128_t min() { return 0; }
 };
 #endif
 
diff --git a/libc/src/__support/CPP/type_traits/invoke.h b/libc/src/__support/CPP/type_traits/invoke.h
index edf6a570c433e09..ea74fb7c36ceb2b 100644
--- a/libc/src/__support/CPP/type_traits/invoke.h
+++ b/libc/src/__support/CPP/type_traits/invoke.h
@@ -16,6 +16,7 @@
 #include "src/__support/CPP/type_traits/is_pointer.h"
 #include "src/__support/CPP/type_traits/is_same.h"
 #include "src/__support/CPP/utility/forward.h"
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
 
 namespace LIBC_NAMESPACE::cpp {
 
@@ -26,7 +27,7 @@ template <class FunctionPtrType> struct invoke_dispatcher {
   template <class T, class... Args,
             typename = cpp::enable_if_t<
                 cpp::is_same_v<cpp::decay_t<T>, FunctionPtrType>>>
-  static decltype(auto) call(T &&fun, Args &&...args) {
+  LIBC_INLINE static decltype(auto) call(T &&fun, Args &&...args) {
     return cpp::forward<T>(fun)(cpp::forward<Args>(args)...);
   }
 };
@@ -37,7 +38,7 @@ struct invoke_dispatcher<FunctionReturnType Class::*> {
   using FunctionPtrType = FunctionReturnType Class::*;
 
   template <class T, class... Args, class DecayT = cpp::decay_t<T>>
-  static decltype(auto) call(FunctionPtrType fun, T &&t1, Args &&...args) {
+  LIBC_INLINE static decltype(auto) call(FunctionPtrType fun, T &&t1, Args &&...args) {
     if constexpr (cpp::is_base_of_v<Class, DecayT>) {
       // T is a (possibly cv ref) type.
       return (cpp::forward<T>(t1).*fun)(cpp::forward<Args>(args)...);
diff --git a/libc/src/__support/CPP/utility/in_place.h b/libc/src/__support/CPP/utility/in_place.h
index f15703fad63b3ac..3aace22f211a8d0 100644
--- a/libc/src/__support/CPP/utility/in_place.h
+++ b/libc/src/__support/CPP/utility/in_place.h
@@ -8,7 +8,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_UTILITY_IN_PLACE_H
 #define LLVM_LIBC_SRC___SUPPORT_CPP_UTILITY_IN_PLACE_H
 
-#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/attributes.h" // LIBC_INLINE, LIBC_INLINE_VAR
 
 #include <stddef.h> // size_t
 
@@ -16,17 +16,17 @@ namespace LIBC_NAMESPACE::cpp {
 
 // in_place
 struct in_place_t {
-  explicit in_place_t() = default;
+  LIBC_INLINE explicit in_place_t() = default;
 };
 LIBC_INLINE_VAR constexpr in_place_t in_place{};
 
 template <class T> struct in_place_type_t {
-  explicit in_place_type_t() = default;
+  LIBC_INLINE explicit in_place_type_t() = default;
 };
 template <class T> LIBC_INLINE_VAR constexpr in_place_type_t<T> in_place_type{};
 
 template <size_t I> struct in_place_index_t {
-  explicit in_place_index_t() = default;
+  LIBC_INLINE explicit in_place_index_t() = default;
 };
 template <size_t I>
 LIBC_INLINE_VAR constexpr in_place_index_t<I> in_place_index{};
diff --git a/libc/src/__support/CPP/utility/move.h b/libc/src/__support/CPP/utility/move.h
index 2b4cdf0c4a160b1..4fec5e6af96e363 100644
--- a/libc/src/__support/CPP/utility/move.h
+++ b/libc/src/__support/CPP/utility/move.h
@@ -9,11 +9,13 @@
 #define LLVM_LIBC_SRC___SUPPORT_CPP_UTILITY_MOVE_H
 
 #include "src/__support/CPP/type_traits/remove_reference.h"
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
 
 namespace LIBC_NAMESPACE::cpp {
 
 // move
-template <class T> constexpr cpp::remove_reference_t<T> &&move(T &&t) {
+template <class T>
+LIBC_INLINE constexpr cpp::remove_reference_t<T> &&move(T &&t) {
   return static_cast<typename cpp::remove_reference_t<T> &&>(t);
 }
 
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 8bfb5a24161dae1..76a9fc6d772bf9c 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -13,6 +13,7 @@
 #include "src/__support/CPP/type_traits.h"
 #include "src/__support/bit.h"
 #include "src/__support/common.h"
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
 
 #include "FloatProperties.h"
 #include <stdint.h>
@@ -106,13 +107,14 @@ template <typename T> struct FPBits {
   // We don't want accidental type promotions/conversions, so we require exact
   // type match.
   template <typename XType, cpp::enable_if_t<cpp::is_same_v<T, XType>, int> = 0>
-  constexpr explicit FPBits(XType x) : bits(cpp::bit_cast<UIntType>(x)) {}
+  LIBC_INLINE constexpr explicit FPBits(XType x)
+      : bits(cpp::bit_cast<UIntType>(x)) {}
 
   template <typename XType,
             cpp::enable_if_t<cpp::is_same_v<XType, UIntType>, int> = 0>
-  constexpr explicit FPBits(XType x) : bits(x) {}
+  LIBC_INLINE constexpr explicit FPBits(XType x) : bits(x) {}
 
-  constexpr FPBits() : bits(0) {}
+  LIBC_INLINE constexpr FPBits() : bits(0) {}
 
   LIBC_INLINE constexpr T get_val() const { return cpp::bit_cast<T>(bits); }
 
diff --git a/libc/src/__support/FPUtil/FloatProperties.h b/libc/src/__support/FPUtil/FloatProperties.h
index 0e65a60ac654844..59e261e1047f1ad 100644
--- a/libc/src/__support/FPUtil/FloatProperties.h
+++ b/libc/src/__support/FPUtil/FloatProperties.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOATPROPERTIES_H
 
 #include "src/__support/UInt128.h"
+#include "src/__support/macros/attributes.h"       // LIBC_INLINE
 #include "src/__support/macros/properties/float.h" // LIBC_COMPILER_HAS_FLOAT128
 
 #include <stdint.h>
@@ -139,7 +140,7 @@ template <> struct FPProperties<FPType::IEEE754_Binary128> {
 };
 
 //-----------------------------------------------------------------------------
-template <typename FP> static constexpr FPType get_fp_type() {
+template <typename FP> LIBC_INLINE static constexpr FPType get_fp_type() {
   if constexpr (cpp::is_same_v<FP, float> && __FLT_MANT_DIG__ == 24)
     return FPType::IEEE754_Binary32;
   else if constexpr (cpp::is_same_v<FP, double> && __DBL_MANT_DIG__ == 53)
diff --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
index f2ac1b3f22933e2..2f07ff4a2c3e500 100644
--- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
+++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
@@ -12,6 +12,7 @@
 #include "src/__support/CPP/bit.h"
 #include "src/__support/UInt128.h"
 #include "src/__support/common.h"
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
 #include "src/__support/macros/properties/architectures.h"
 
 #if !defined(LIBC_TARGET_ARCH_IS_X86)
@@ -103,11 +104,12 @@ template <> struct FPBits<long double> {
     return bool((bits & FloatProp::SIGN_MASK) >> (FloatProp::BIT_WIDTH - 1));
   }
 
-  constexpr FPBits() : bits(0) {}
+  LIBC_INLINE constexpr FPBits() : bits(0) {}
 
   template <typename XType,
             cpp::enable_if_t<cpp::is_same_v<long double, XType>, int> = 0>
-  constexpr explicit FPBits(XType x) : bits(cpp::bit_cast<UIntType>(x)) {
+  LIBC_INLINE constexpr explicit FPBits(XType x)
+      : bits(cpp::bit_cast<UIntType>(x)) {
     // bits starts uninitialized, and setting it to a long double only
     // overwrites the first 80 bits. This clears those upper bits.
     bits = bits & ((UIntType(1) << 80) - 1);
@@ -115,7 +117,7 @@ template <> struct FPBits<long double> {
 
   template <typename XType,
             cpp::enable_if_t<cpp::is_same_v<XType, UIntType>, int> = 0>
-  constexpr explicit FPBits(XType x) : bits(x) {}
+  LIBC_INLINE constexpr explicit FPBits(XType x) : bits(x) {}
 
   LIBC_INLINE constexpr operator long double() {
     return cpp::bit_cast<long double>(bits);
diff --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index f46e31ae03db291..dbedfbc4c197ec6 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -15,6 +15,7 @@
 #include "src/__support/CPP/type_traits.h"
 #include "src/__support/bit.h" // unsafe_clz
 #include "src/__support/integer_utils.h"
+#include "src/__support/macros/attributes.h"   // LIBC_INLINE
 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
 #include "src/__support/math_extras.h"         // SumCarry, DiffBorrow
 #include "src/__support/number_pair.h"
@@ -28,13 +29,13 @@ template <size_t Bits, bool Signed> struct BigInt {
 
   static_assert(Bits > 0 && Bits % 64 == 0,
                 "Number of bits in BigInt should be a multiple of 64.");
-  static LIBC_INLINE_VAR constexpr size_t WORDCOUNT = Bits / 64;
+  LIBC_INLINE_VAR static constexpr size_t WORDCOUNT = Bits / 64;
   uint64_t val[WORDCOUNT]{};
 
-  static LIBC_INLINE_VAR constexpr uint64_t MASK32 = 0xFFFFFFFFu;
+  LIBC_INLINE_VAR static constexpr uint64_t MASK32 = 0xFFFFFFFFu;
 
-  static LIBC_INLINE constexpr uint64_t low(uint64_t v) { return v & MASK32; }
-  static LIBC_INLINE constexpr uint64_t high(uint64_t v) {
+  LIBC_INLINE static constexpr uint64_t low(uint64_t v) { return v & MASK32; }
+  LIBC_INLINE static constexpr uint64_t high(uint64_t v) {
     return (v >> 32) & MASK32;
   }
 
@@ -126,7 +127,8 @@ template <size_t Bits, bool Signed> struct BigInt {
 
   LIBC_INLINE constexpr explicit operator bool() const { return !is_zero(); }
 
-  BigInt<Bits, Signed> &operator=(const BigInt<Bits, Signed> &other) = default;
+  LIBC_INLINE BigInt<Bits, Signed> &
+  operator=(const BigInt<Bits, Signed> &other) = default;
 
   LIBC_INLINE constexpr bool is_zero() const {
     for (size_t i = 0; i < WORDCOUNT; ++i) {
@@ -324,8 +326,8 @@ template <size_t Bits, bool Signed> struct BigInt {
   //    196      3         9           6            2
   //    256      4        16          10            3
   //    512      8        64          36            7
-  constexpr BigInt<Bits, Signed>
-      LIBC_INLINE quick_mul_hi(const BigInt<Bits, Signed> &other) const {
+  LIBC_INLINE constexpr BigInt<Bits, Signed>
+  quick_mul_hi(const BigInt<Bits, Signed> &other) const {
     BigInt<Bits, Signed> result(0);
     BigInt<128, Signed> partial_sum(0);
     uint64_t carry = 0;
@@ -893,7 +895,7 @@ template <> class numeric_limits<UInt<128>> {
   LIBC_INLINE static constexpr UInt<128> max() {
     return UInt<128>({0xffff'ffff'ffff'ffff, 0xffff'ffff'ffff'ffff});
   }
-  static constexpr UInt<128> min() { return UInt<128>(0); }
+  LIBC_INLINE static constexpr UInt<128> min() { return UInt<128>(0); }
 };
 
 template <> class numeric_limits<Int<128>> {
diff --git a/libc/src/__support/integer_operations.h b/libc/src/__support/integer_operations.h
index 6253afed06b757a..390181f13b08bca 100644
--- a/libc/src/__support/integer_operations.h
+++ b/libc/src/__support/integer_operations.h
@@ -10,16 +10,18 @@
 #define LLVM_LIBC_SRC___SUPPORT_INTEGER_OPERATIONS_H
 
 #include "src/__support/CPP/type_traits.h"
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
 
 namespace LIBC_NAMESPACE {
 
 template <typename T>
-static constexpr cpp::enable_if_t<cpp::is_integral_v<T>, T> integer_abs(T n) {
+LIBC_INLINE static constexpr cpp::enable_if_t<cpp::is_integral_v<T>, T>
+integer_abs(T n) {
   return (n < 0) ? -n : n;
 }
 
 template <typename T>
-static constexpr cpp::enable_if_t<cpp::is_integral_v<T>, void>
+LIBC_INLINE static constexpr cpp::enable_if_t<cpp::is_integral_v<T>, void>
 integer_rem_quo(T x, T y, T &quot, T &rem) {
   quot = x / y;
   rem = x % y;
diff --git a/libc/src/string/memmem.cpp b/libc/src/string/memmem.cpp
index 067ea2ef9e7e51d..c695a8d4b8b4915 100644
--- a/libc/src/string/memmem.cpp
+++ b/libc/src/string/memmem.cpp
@@ -15,10 +15,10 @@ namespace LIBC_NAMESPACE {
 LLVM_LIBC_FUNCTION(void *, memmem,
                    (const void *haystack, size_t haystack_len,
                     const void *needle, size_t needle_len)) {
-  constexpr auto comp = [](unsigned char l, unsigned char r) -> int {
+  constexpr auto COMP = [](unsigned char l, unsigned char r) -> int {
     return l - r;
   };
-  return inline_memmem(haystack, haystack_len, needle, needle_len, comp);
+  return inline_memmem(haystack, haystack_len, needle, needle_len, COMP);
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e7dc978b0dbe847..ffb2652ed1419d0 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -205,6 +205,9 @@ libc_support_library(
 libc_support_library(
     name = "__support_cpp_limits",
     hdrs = ["src/__support/CPP/limits.h"],
+    deps = [
+        "__support_macros_attributes",
+    ],
 )
 
 libc_support_library(
@@ -672,6 +675,7 @@ libc_support_library(
     name = "__support_fputil_float_properties",
     hdrs = ["src/__support/FPUtil/FloatProperties.h"],
     deps = [
+        ":__support_macros_attributes",
         ":__support_macros_properties_float",
         ":__support_uint128",
     ],
@@ -687,6 +691,7 @@ libc_support_library(
         ":__support_cpp_bit",
         ":__support_cpp_type_traits",
         ":__support_fputil_float_properties",
+        ":__support_macros_attributes",
         ":__support_uint128",
     ],
 )
@@ -746,6 +751,7 @@ libc_support_library(
         ":__support_fputil_fenv_impl",
         ":__support_fputil_fp_bits",
         ":__support_fputil_rounding_mode",
+        ":__support_macros_attributes",
     ],
 )
 

>From 2bca7f106d82762e62e574df8555d6b67309cf06 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Tue, 28 Nov 2023 16:17:33 +0000
Subject: [PATCH 2/2] fix formatting

---
 libc/src/__support/CPP/type_traits/invoke.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/type_traits/invoke.h b/libc/src/__support/CPP/type_traits/invoke.h
index ea74fb7c36ceb2b..94351d849b9262f 100644
--- a/libc/src/__support/CPP/type_traits/invoke.h
+++ b/libc/src/__support/CPP/type_traits/invoke.h
@@ -38,7 +38,8 @@ struct invoke_dispatcher<FunctionReturnType Class::*> {
   using FunctionPtrType = FunctionReturnType Class::*;
 
   template <class T, class... Args, class DecayT = cpp::decay_t<T>>
-  LIBC_INLINE static decltype(auto) call(FunctionPtrType fun, T &&t1, Args &&...args) {
+  LIBC_INLINE static decltype(auto) call(FunctionPtrType fun, T &&t1,
+                                         Args &&...args) {
     if constexpr (cpp::is_base_of_v<Class, DecayT>) {
       // T is a (possibly cv ref) type.
       return (cpp::forward<T>(t1).*fun)(cpp::forward<Args>(args)...);



More information about the llvm-commits mailing list