Skip to content

Commit 6582efc

Browse files
authored
[Clang] Fix __is_array returning true for zero-sized arrays (llvm#86652)
Fixes llvm#54705
1 parent a027bea commit 6582efc

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,9 @@ Bug Fixes in This Version
586586
- Clang now correctly disallows VLA type compound literals, e.g. ``(int[size]){}``,
587587
as the C standard mandates. (#GH89835)
588588

589+
- ``__is_array`` and ``__is_bounded_array`` no longer return ``true`` for
590+
zero-sized arrays. Fixes (#GH54705).
591+
589592
Bug Fixes to Compiler Builtins
590593
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
591594

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5217,10 +5217,18 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
52175217
case UTT_IsFloatingPoint:
52185218
return T->isFloatingType();
52195219
case UTT_IsArray:
5220+
// Zero-sized arrays aren't considered arrays in partial specializations,
5221+
// so __is_array shouldn't consider them arrays either.
5222+
if (const auto *CAT = C.getAsConstantArrayType(T))
5223+
return CAT->getSize() != 0;
52205224
return T->isArrayType();
52215225
case UTT_IsBoundedArray:
52225226
if (DiagnoseVLAInCXXTypeTrait(Self, TInfo, tok::kw___is_bounded_array))
52235227
return false;
5228+
// Zero-sized arrays aren't considered arrays in partial specializations,
5229+
// so __is_bounded_array shouldn't consider them arrays either.
5230+
if (const auto *CAT = C.getAsConstantArrayType(T))
5231+
return CAT->getSize() != 0;
52245232
return T->isArrayType() && !T->isIncompleteArrayType();
52255233
case UTT_IsUnboundedArray:
52265234
if (DiagnoseVLAInCXXTypeTrait(Self, TInfo, tok::kw___is_unbounded_array))

clang/test/SemaCXX/type-traits.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ typedef Empty EmptyArMB[1][2];
2525
typedef int Int;
2626
typedef Int IntAr[10];
2727
typedef Int IntArNB[];
28+
typedef Int IntArZero[0];
2829
class Statics { static int priv; static NonPOD np; };
2930
union EmptyUnion {};
3031
union IncompleteUnion; // expected-note {{forward declaration of 'IncompleteUnion'}}
@@ -685,6 +686,7 @@ void is_array()
685686
{
686687
static_assert(__is_array(IntAr));
687688
static_assert(__is_array(IntArNB));
689+
static_assert(!__is_array(IntArZero));
688690
static_assert(__is_array(UnionAr));
689691

690692
static_assert(!__is_array(void));
@@ -714,6 +716,7 @@ void is_array()
714716
void is_bounded_array(int n) {
715717
static_assert(__is_bounded_array(IntAr));
716718
static_assert(!__is_bounded_array(IntArNB));
719+
static_assert(!__is_bounded_array(IntArZero));
717720
static_assert(__is_bounded_array(UnionAr));
718721

719722
static_assert(!__is_bounded_array(void));
@@ -746,6 +749,7 @@ void is_bounded_array(int n) {
746749
void is_unbounded_array(int n) {
747750
static_assert(!__is_unbounded_array(IntAr));
748751
static_assert(__is_unbounded_array(IntArNB));
752+
static_assert(!__is_unbounded_array(IntArZero));
749753
static_assert(!__is_unbounded_array(UnionAr));
750754

751755
static_assert(!__is_unbounded_array(void));

0 commit comments

Comments
 (0)