Skip to content

Commit

Permalink
[libc] Clean up required LIBC_INLINE uses in src/string
Browse files Browse the repository at this point in the history
This was generated using clang-tidy and clang-apply-replacements,
on src/string/*.cpp for just the llvmlibc-inline-function-decl
check, after applying https://reviews.llvm.org/D157164, and then
some manual fixup.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D157169
  • Loading branch information
frobtech committed Aug 7, 2023
1 parent 9d4162f commit 019a477
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 76 deletions.
31 changes: 17 additions & 14 deletions libc/src/__support/CPP/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_ARRAY_H
#define LLVM_LIBC_SRC_SUPPORT_CPP_ARRAY_H

#include "src/__support/macros/attributes.h"
#include <stddef.h> // For size_t.

namespace __llvm_libc {
Expand All @@ -22,28 +23,30 @@ template <class T, size_t N> struct array {
using iterator = T *;
using const_iterator = const T *;

constexpr T *data() { return Data; }
constexpr const T *data() const { return Data; }
LIBC_INLINE constexpr T *data() { return Data; }
LIBC_INLINE constexpr const T *data() const { return Data; }

constexpr T &front() { return Data[0]; }
constexpr T &front() const { return Data[0]; }
LIBC_INLINE constexpr T &front() { return Data[0]; }
LIBC_INLINE constexpr T &front() const { return Data[0]; }

constexpr T &back() { return Data[N - 1]; }
constexpr T &back() const { return Data[N - 1]; }
LIBC_INLINE constexpr T &back() { return Data[N - 1]; }
LIBC_INLINE constexpr T &back() const { return Data[N - 1]; }

constexpr T &operator[](size_t Index) { return Data[Index]; }
LIBC_INLINE constexpr T &operator[](size_t Index) { return Data[Index]; }

constexpr const T &operator[](size_t Index) const { return Data[Index]; }
LIBC_INLINE constexpr const T &operator[](size_t Index) const {
return Data[Index];
}

constexpr size_t size() const { return N; }
LIBC_INLINE constexpr size_t size() const { return N; }

constexpr bool empty() const { return N == 0; }
LIBC_INLINE constexpr bool empty() const { return N == 0; }

constexpr iterator begin() { return Data; }
constexpr const_iterator begin() const { return Data; }
LIBC_INLINE constexpr iterator begin() { return Data; }
LIBC_INLINE constexpr const_iterator begin() const { return Data; }

constexpr iterator end() { return Data + N; }
const_iterator end() const { return Data + N; }
LIBC_INLINE constexpr iterator end() { return Data + N; }
LIBC_INLINE const_iterator end() const { return Data + N; }
};

} // namespace cpp
Expand Down
4 changes: 3 additions & 1 deletion libc/src/__support/CPP/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_LIBC_SUPPORT_CPP_BIT_H

#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN

namespace __llvm_libc::cpp {
Expand All @@ -24,7 +25,8 @@ namespace __llvm_libc::cpp {

// This function guarantees the bitcast to be optimized away by the compiler for
// GCC >= 8 and Clang >= 6.
template <class To, class From> constexpr To bit_cast(const From &from) {
template <class To, class From>
LIBC_INLINE constexpr To bit_cast(const From &from) {
static_assert(sizeof(To) == sizeof(From), "To and From must be of same size");
static_assert(cpp::is_trivially_copyable<To>::value &&
cpp::is_trivially_copyable<From>::value,
Expand Down
15 changes: 8 additions & 7 deletions libc/src/__support/CPP/bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_BITSET_H
#define LLVM_LIBC_SRC_SUPPORT_CPP_BITSET_H

#include "src/__support/macros/attributes.h"
#include <stddef.h> // For size_t.

namespace __llvm_libc::cpp {
Expand All @@ -17,27 +18,27 @@ template <size_t NumberOfBits> struct bitset {
static_assert(NumberOfBits != 0,
"Cannot create a __llvm_libc::cpp::bitset of size 0.");

constexpr void set(size_t Index) {
LIBC_INLINE constexpr void set(size_t Index) {
Data[Index / BITS_PER_UNIT] |= mask(Index);
}

constexpr void reset() {
LIBC_INLINE constexpr void reset() {
for (size_t i = 0; i < NUMBER_OF_UNITS; ++i)
Data[i] = 0;
}

constexpr bool test(size_t Index) const {
LIBC_INLINE constexpr bool test(size_t Index) const {
return Data[Index / BITS_PER_UNIT] & mask(Index);
}

constexpr void flip() {
LIBC_INLINE constexpr void flip() {
for (size_t i = 0; i < NUMBER_OF_UNITS; ++i)
Data[i] = ~Data[i];
}

// This function sets all bits in the range from Start to End (inclusive) to
// true. It assumes that Start <= End.
constexpr void set_range(size_t Start, size_t End) {
LIBC_INLINE constexpr void set_range(size_t Start, size_t End) {
size_t start_index = Start / BITS_PER_UNIT;
size_t end_index = End / BITS_PER_UNIT;

Expand All @@ -64,7 +65,7 @@ template <size_t NumberOfBits> struct bitset {
}
}

constexpr bool operator==(const bitset<NumberOfBits> &other) {
LIBC_INLINE constexpr bool operator==(const bitset<NumberOfBits> &other) {
for (size_t i = 0; i < NUMBER_OF_UNITS; ++i) {
if (Data[i] != other.Data[i])
return false;
Expand All @@ -78,7 +79,7 @@ template <size_t NumberOfBits> struct bitset {
static constexpr size_t NUMBER_OF_UNITS =
(NumberOfBits + BITS_PER_UNIT - 1) / BITS_PER_UNIT;

static constexpr size_t mask(size_t Index) {
LIBC_INLINE static constexpr size_t mask(size_t Index) {
return size_t{1} << (Index % BITS_PER_UNIT);
}
size_t Data[NUMBER_OF_UNITS] = {0};
Expand Down
31 changes: 19 additions & 12 deletions libc/src/__support/CPP/cstddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,59 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_BYTE_H
#define LLVM_LIBC_SRC_SUPPORT_CPP_BYTE_H

#include "src/__support/macros/attributes.h"
#include "type_traits.h" // For enable_if_t, is_integral_v.

namespace __llvm_libc::cpp {

enum class byte : unsigned char {};

template <class IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, byte>
LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte>
operator>>(byte b, IntegerType shift) noexcept {
return static_cast<byte>(static_cast<unsigned char>(b) >> shift);
}
template <class IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
operator>>=(byte &b, IntegerType shift) noexcept {
return b = b >> shift;
}
template <class IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, byte>
LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte>
operator<<(byte b, IntegerType shift) noexcept {
return static_cast<byte>(static_cast<unsigned char>(b) << shift);
}
template <class IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
operator<<=(byte &b, IntegerType shift) noexcept {
return b = b << shift;
}
constexpr byte operator|(byte l, byte r) noexcept {
LIBC_INLINE constexpr byte operator|(byte l, byte r) noexcept {
return static_cast<byte>(static_cast<unsigned char>(l) |
static_cast<unsigned char>(r));
}
constexpr byte &operator|=(byte &l, byte r) noexcept { return l = l | r; }
constexpr byte operator&(byte l, byte r) noexcept {
LIBC_INLINE constexpr byte &operator|=(byte &l, byte r) noexcept {
return l = l | r;
}
LIBC_INLINE constexpr byte operator&(byte l, byte r) noexcept {
return static_cast<byte>(static_cast<unsigned char>(l) &
static_cast<unsigned char>(r));
}
constexpr byte &operator&=(byte &l, byte r) noexcept { return l = l & r; }
constexpr byte operator^(byte l, byte r) noexcept {
LIBC_INLINE constexpr byte &operator&=(byte &l, byte r) noexcept {
return l = l & r;
}
LIBC_INLINE constexpr byte operator^(byte l, byte r) noexcept {
return static_cast<byte>(static_cast<unsigned char>(l) ^
static_cast<unsigned char>(r));
}
constexpr byte &operator^=(byte &l, byte r) noexcept { return l = l ^ r; }
constexpr byte operator~(byte b) noexcept {
LIBC_INLINE constexpr byte &operator^=(byte &l, byte r) noexcept {
return l = l ^ r;
}
LIBC_INLINE constexpr byte operator~(byte b) noexcept {
return static_cast<byte>(~static_cast<unsigned char>(b));
}
template <typename IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, IntegerType>
LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, IntegerType>
to_integer(byte b) noexcept {
return static_cast<IntegerType>(b);
}
Expand Down
8 changes: 5 additions & 3 deletions libc/src/__support/CPP/new.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ namespace __llvm_libc {

class AllocChecker {
bool success = false;
AllocChecker &operator=(bool status) {

LIBC_INLINE AllocChecker &operator=(bool status) {
success = status;
return *this;
}

public:
AllocChecker() = default;
operator bool() const { return success; }
LIBC_INLINE AllocChecker() = default;

LIBC_INLINE operator bool() const { return success; }

LIBC_INLINE static void *alloc(size_t s, AllocChecker &ac) {
void *mem = ::malloc(s);
Expand Down
4 changes: 2 additions & 2 deletions libc/src/__support/CPP/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ namespace cpp {

// Trivial in_place_t struct.
struct in_place_t {
LIBC_INLINE_VAR explicit in_place_t() = default;
LIBC_INLINE constexpr explicit in_place_t() = default;
};

// Trivial nullopt_t struct.
struct nullopt_t {
LIBC_INLINE_VAR explicit nullopt_t() = default;
LIBC_INLINE constexpr explicit nullopt_t() = default;
};

// nullopt that can be used and returned.
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ template <typename T> class span {
using const_reference = const T &;
using iterator = T *;

static constexpr size_type dynamic_extent = -1;
LIBC_INLINE_VAR static constexpr size_type dynamic_extent = -1;

LIBC_INLINE constexpr span() : span_data(nullptr), span_size(0) {}

Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class string_view {
const char *Data;
size_t Len;

static size_t min(size_t A, size_t B) { return A <= B ? A : B; }
LIBC_INLINE static size_t min(size_t A, size_t B) { return A <= B ? A : B; }

LIBC_INLINE static int compareMemory(const char *Lhs, const char *Rhs,
size_t Length) {
Expand Down
3 changes: 2 additions & 1 deletion libc/src/__support/CPP/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ template <typename T> struct remove_cvref {
template <typename T> using remove_cvref_t = typename remove_cvref<T>::type;

namespace details {
template <typename T, typename... Args> constexpr bool is_unqualified_any_of() {
template <typename T, typename... Args>
LIBC_INLINE constexpr bool is_unqualified_any_of() {
return (... || is_same_v<remove_cv_t<T>, Args>);
}
} // namespace details
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

namespace __llvm_libc {
namespace internal {
constexpr bool same_string(char const *lhs, char const *rhs) {
LIBC_INLINE constexpr bool same_string(char const *lhs, char const *rhs) {
for (; *lhs || *rhs; ++lhs, ++rhs)
if (*lhs != *rhs)
return false;
Expand Down
28 changes: 20 additions & 8 deletions libc/src/__support/ctype_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_CTYPE_UTILS_H
#define LLVM_LIBC_SRC_SUPPORT_CTYPE_UTILS_H

#include "src/__support/macros/attributes.h"

namespace __llvm_libc {
namespace internal {

Expand All @@ -18,25 +20,35 @@ namespace internal {
// of a function call by inlining them.
// ------------------------------------------------------

static constexpr bool isalpha(unsigned ch) { return (ch | 32) - 'a' < 26; }
LIBC_INLINE static constexpr bool isalpha(unsigned ch) {
return (ch | 32) - 'a' < 26;
}

static constexpr bool isdigit(unsigned ch) { return (ch - '0') < 10; }
LIBC_INLINE static constexpr bool isdigit(unsigned ch) {
return (ch - '0') < 10;
}

static constexpr bool isalnum(unsigned ch) {
LIBC_INLINE static constexpr bool isalnum(unsigned ch) {
return isalpha(ch) || isdigit(ch);
}

static constexpr bool isgraph(unsigned ch) { return 0x20 < ch && ch < 0x7f; }
LIBC_INLINE static constexpr bool isgraph(unsigned ch) {
return 0x20 < ch && ch < 0x7f;
}

static constexpr bool islower(unsigned ch) { return (ch - 'a') < 26; }
LIBC_INLINE static constexpr bool islower(unsigned ch) {
return (ch - 'a') < 26;
}

static constexpr bool isupper(unsigned ch) { return (ch - 'A') < 26; }
LIBC_INLINE static constexpr bool isupper(unsigned ch) {
return (ch - 'A') < 26;
}

static constexpr bool isspace(unsigned ch) {
LIBC_INLINE static constexpr bool isspace(unsigned ch) {
return ch == ' ' || (ch - '\t') < 5;
}

static constexpr int tolower(int ch) {
LIBC_INLINE static constexpr int tolower(int ch) {
if (isupper(ch))
return ch + ('a' - 'A');
return ch;
Expand Down
8 changes: 5 additions & 3 deletions libc/src/string/memory_utils/inline_memmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_INLINE_MEMMEM_H
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_INLINE_MEMMEM_H

#include "src/__support/macros/attributes.h"

#include <stddef.h>

namespace __llvm_libc {

template <typename Comp>
constexpr static void *inline_memmem(const void *haystack, size_t haystack_len,
const void *needle, size_t needle_len,
Comp &&comp) {
LIBC_INLINE constexpr static void *
inline_memmem(const void *haystack, size_t haystack_len, const void *needle,
size_t needle_len, Comp &&comp) {
// TODO: simple brute force implementation. This can be
// improved upon using well known string matching algorithms.
if (!needle_len)
Expand Down
Loading

0 comments on commit 019a477

Please sign in to comment.