From eee944e7f9e60df44714eb547d0b876ee5dc7290 Mon Sep 17 00:00:00 2001 From: Erik Pilkington Date: Tue, 2 Jul 2019 18:28:13 +0000 Subject: [C++2a] Add __builtin_bit_cast, used to implement std::bit_cast This commit adds a new builtin, __builtin_bit_cast(T, v), which performs a bit_cast from a value v to a type T. This expression can be evaluated at compile time under specific circumstances. The compile time evaluation currently doesn't support bit-fields, but I'm planning on fixing this in a follow up (some of the logic for figuring this out is in CodeGen). I'm also planning follow-ups for supporting some more esoteric types that the constexpr evaluator supports, as well as extending __builtin_memcpy constexpr evaluation to use the same infrastructure. rdar://44987528 Differential revision: https://reviews.llvm.org/D62825 llvm-svn: 364954 --- clang/test/SemaCXX/builtin-bit-cast.cpp | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 clang/test/SemaCXX/builtin-bit-cast.cpp (limited to 'clang/test/SemaCXX/builtin-bit-cast.cpp') diff --git a/clang/test/SemaCXX/builtin-bit-cast.cpp b/clang/test/SemaCXX/builtin-bit-cast.cpp new file mode 100644 index 00000000000..3dddadc5617 --- /dev/null +++ b/clang/test/SemaCXX/builtin-bit-cast.cpp @@ -0,0 +1,39 @@ +// RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s +// RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s -fno-signed-char + +#if !__has_builtin(__builtin_bit_cast) +#error +#endif + +template +T instantiate() { + return __builtin_bit_cast(T, v); +} + +int x = instantiate(); + +struct secret_ctor { + char member; + +private: secret_ctor() = default; +}; + +void test1() { + secret_ctor c = __builtin_bit_cast(secret_ctor, (char)0); +} + +void test2() { + constexpr int i = 0; + // expected-error@+1{{__builtin_bit_cast source size does not equal destination size (4 vs 1)}} + constexpr char c = __builtin_bit_cast(char, i); +} + +struct not_trivially_copyable { + virtual void foo() {} +}; + +// expected-error@+1{{__builtin_bit_cast source type must be trivially copyable}} +constexpr unsigned long ul = __builtin_bit_cast(unsigned long, not_trivially_copyable{}); + +// expected-error@+1 {{__builtin_bit_cast destination type must be trivially copyable}} +constexpr long us = __builtin_bit_cast(unsigned long &, 0L); -- cgit v1.2.3