Skip to content

Commit 36856a8

Browse files
Rajveer100copybara-github
authored andcommitted
[libc++] Diagnoses insufficiently aligned pointers for std::assume_aligned during constant evaluation (#73775)
This is a `libc++` enhancement when violating alignment assumption for `__builtin_assume_aligned`. Fixes #64078 NOKEYCHECK=True GitOrigin-RevId: 806f43e3cb9ca2bff7c2ae6f1324a062ddb83cac
1 parent 7a073e3 commit 36856a8

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

include/__memory/assume_aligned.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __ass
2727
static_assert(_Np != 0 && (_Np & (_Np - 1)) == 0, "std::assume_aligned<N>(p) requires N to be a power of two");
2828

2929
if (__libcpp_is_constant_evaluated()) {
30+
(void)__builtin_assume_aligned(__ptr, _Np);
3031
return __ptr;
3132
} else {
3233
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
11+
// template<size_t N, class T>
12+
// [[nodiscard]] constexpr T* assume_aligned(T* ptr);
13+
14+
#include <memory>
15+
#include <cstddef>
16+
17+
template <size_t Size>
18+
constexpr bool test() {
19+
char data[1];
20+
21+
[[maybe_unused]] auto data1 = std::assume_aligned<Size>(data);
22+
23+
return true;
24+
}
25+
26+
static_assert(test<2>());
27+
// expected-error@-1 {{static assertion expression is not an integral constant expression}}
28+
// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 2 bytes}}
29+
30+
static_assert(test<4>());
31+
// expected-error@-1 {{static assertion expression is not an integral constant expression}}
32+
// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 4 bytes}}
33+
34+
static_assert(test<8>());
35+
// expected-error@-1 {{static assertion expression is not an integral constant expression}}
36+
// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 8 bytes}}
37+
38+
static_assert(test<16>());
39+
// expected-error@-1 {{static assertion expression is not an integral constant expression}}
40+
// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 16 bytes}}
41+
42+
static_assert(test<32>());
43+
// expected-error@-1 {{static assertion expression is not an integral constant expression}}
44+
// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 32 bytes}}
45+
46+
static_assert(test<64>());
47+
// expected-error@-1 {{static assertion expression is not an integral constant expression}}
48+
// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 64 bytes}}

0 commit comments

Comments
 (0)