Skip to content

Commit b63b010

Browse files
authored
[Clang] enhance diagnostic message for __builtin_bit_cast size mismatch (#115940)
Fixes #115870
1 parent a33ae1b commit b63b010

File tree

6 files changed

+9
-6
lines changed

6 files changed

+9
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ Improvements to Clang's diagnostics
531531

532532
- Clang now diagnoses ``[[deprecated]]`` attribute usage on local variables (#GH90073).
533533

534+
- Improved diagnostic message for ``__builtin_bit_cast`` size mismatch (#GH115870).
535+
534536
Improvements to Clang's time-trace
535537
----------------------------------
536538

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12368,7 +12368,7 @@ def err_preserve_enum_value_not_const: Error<
1236812368
def err_bit_cast_non_trivially_copyable : Error<
1236912369
"__builtin_bit_cast %select{source|destination}0 type must be trivially copyable">;
1237012370
def err_bit_cast_type_size_mismatch : Error<
12371-
"__builtin_bit_cast source size does not equal destination size (%0 vs %1)">;
12371+
"size of '__builtin_bit_cast' source type %0 does not match destination type %1 (%2 vs %3 bytes)">;
1237212372

1237312373
// SYCL-specific diagnostics
1237412374
def warn_sycl_kernel_num_of_template_params : Warning<

clang/lib/Sema/SemaCast.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3283,7 +3283,8 @@ void CastOperation::CheckBuiltinBitCast() {
32833283
CharUnits SourceSize = Self.Context.getTypeSizeInChars(SrcType);
32843284
if (DestSize != SourceSize) {
32853285
Self.Diag(OpRange.getBegin(), diag::err_bit_cast_type_size_mismatch)
3286-
<< (int)SourceSize.getQuantity() << (int)DestSize.getQuantity();
3286+
<< SrcType << DestType << (int)SourceSize.getQuantity()
3287+
<< (int)DestSize.getQuantity();
32873288
SrcExpr = ExprError();
32883289
return;
32893290
}

clang/test/AST/ByteCode/builtin-bit-cast.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ typedef bool bool32 __attribute__((ext_vector_type(32)));
496496
typedef bool bool128 __attribute__((ext_vector_type(128)));
497497

498498
static_assert(bit_cast<unsigned char>(bool8{1,0,1,0,1,0,1,0}) == (LITTLE_END ? 0x55 : 0xAA), "");
499-
constexpr bool8 b8 = __builtin_bit_cast(bool8, 0x55); // both-error {{__builtin_bit_cast source size does not equal destination size (4 vs 1)}}
499+
constexpr bool8 b8 = __builtin_bit_cast(bool8, 0x55); // both-error {{'__builtin_bit_cast' source type 'int' does not match destination type 'bool8' (vector of 8 'bool' values) (4 vs 1 bytes)}}
500500
#if 0
501501
static_assert(check_round_trip<bool8>(static_cast<unsigned char>(0)), "");
502502
static_assert(check_round_trip<bool8>(static_cast<unsigned char>(1)), "");
@@ -535,5 +535,5 @@ namespace test_complex {
535535
static_assert(TF.A == 1.0f && TF.B == 2.0f);
536536

537537
constexpr double D = __builtin_bit_cast(double, test_float_complex);
538-
constexpr int M = __builtin_bit_cast(int, test_int_complex); // both-error {{__builtin_bit_cast source size does not equal destination size}}
538+
constexpr int M = __builtin_bit_cast(int, test_int_complex); // both-error {{size of '__builtin_bit_cast' source type 'const _Complex unsigned int' does not match destination type 'int' (8 vs 4 bytes)}}
539539
}

clang/test/SemaCXX/builtin-bit-cast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void test1() {
2424

2525
void test2() {
2626
constexpr int i = 0;
27-
// expected-error@+1{{__builtin_bit_cast source size does not equal destination size (4 vs 1)}}
27+
// expected-error@+1{{size of '__builtin_bit_cast' source type 'const int' does not match destination type 'char' (4 vs 1 bytes)}}
2828
constexpr char c = __builtin_bit_cast(char, i);
2929
}
3030

clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,5 +525,5 @@ namespace test_complex {
525525
static_assert(TF.A == 1.0f && TF.B == 2.0f);
526526

527527
constexpr double D = __builtin_bit_cast(double, test_float_complex);
528-
constexpr int M = __builtin_bit_cast(int, test_int_complex); // expected-error {{__builtin_bit_cast source size does not equal destination size}}
528+
constexpr int M = __builtin_bit_cast(int, test_int_complex); // expected-error {{size of '__builtin_bit_cast' source type 'const _Complex unsigned int' does not match destination type 'int' (8 vs 4 bytes)}}
529529
}

0 commit comments

Comments
 (0)