Skip to content

Commit 9a64fa7

Browse files
[Support] Use CTLog2 from PointerLikeTypeTraits.h (NFC) (#157790)
This patch replaces ConstantLog2 with CTLog2. ConstantLog2 only operates on alignment values, making the two interchangeable in this context. CTLog2 also has the benefit of a static_assert that ensures its parameter is a power of two.
1 parent cac3802 commit 9a64fa7

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ class Expr : public ValueStmt {
10381038
// PointerLikeTypeTraits is specialized so it can be used with a forward-decl of
10391039
// Expr. Verify that we got it right.
10401040
static_assert(llvm::PointerLikeTypeTraits<Expr *>::NumLowBitsAvailable <=
1041-
llvm::detail::ConstantLog2<alignof(Expr)>::value,
1041+
llvm::CTLog2<alignof(Expr)>(),
10421042
"PointerLikeTypeTraits<Expr*> assumes too much alignment.");
10431043

10441044
using ConstantExprKind = Expr::ConstantExprKind;

llvm/include/llvm/Support/PointerLikeTypeTraits.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
1616

1717
#include "llvm/Support/DataTypes.h"
18+
#include "llvm/Support/MathExtras.h"
1819
#include <cassert>
19-
#include <type_traits>
2020

2121
namespace llvm {
2222

@@ -25,12 +25,6 @@ namespace llvm {
2525
template <typename T> struct PointerLikeTypeTraits;
2626

2727
namespace detail {
28-
/// A tiny meta function to compute the log2 of a compile time constant.
29-
template <size_t N>
30-
struct ConstantLog2
31-
: std::integral_constant<size_t, ConstantLog2<N / 2>::value + 1> {};
32-
template <> struct ConstantLog2<1> : std::integral_constant<size_t, 0> {};
33-
3428
// Provide a trait to check if T is pointer-like.
3529
template <typename T, typename U = void> struct HasPointerLikeTypeTraits {
3630
static const bool value = false;
@@ -57,8 +51,7 @@ template <typename T> struct PointerLikeTypeTraits<T *> {
5751
static inline void *getAsVoidPointer(T *P) { return P; }
5852
static inline T *getFromVoidPointer(void *P) { return static_cast<T *>(P); }
5953

60-
static constexpr int NumLowBitsAvailable =
61-
detail::ConstantLog2<alignof(T)>::value;
54+
static constexpr int NumLowBitsAvailable = CTLog2<alignof(T)>();
6255
};
6356

6457
template <> struct PointerLikeTypeTraits<void *> {
@@ -123,8 +116,7 @@ template <> struct PointerLikeTypeTraits<uintptr_t> {
123116
/// potentially use alignment attributes on functions to satisfy that.
124117
template <int Alignment, typename FunctionPointerT>
125118
struct FunctionPointerLikeTypeTraits {
126-
static constexpr int NumLowBitsAvailable =
127-
detail::ConstantLog2<Alignment>::value;
119+
static constexpr int NumLowBitsAvailable = CTLog2<Alignment>();
128120
static inline void *getAsVoidPointer(FunctionPointerT P) {
129121
assert((reinterpret_cast<uintptr_t>(P) &
130122
~((uintptr_t)-1 << NumLowBitsAvailable)) == 0 &&

0 commit comments

Comments
 (0)