Skip to content

Commit d48d6ba

Browse files
authored
[clang][NFC] Factor out VLA checks in type traits (#88646)
This is a follow-up to #88473 suggested by @cor3ntin in #88473 (comment).
1 parent c6f9c84 commit d48d6ba

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5012,6 +5012,20 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
50125012
return From;
50135013
}
50145014

5015+
/// Checks that type T is not a VLA.
5016+
///
5017+
/// @returns @c true if @p T is VLA and a diagnostic was emitted,
5018+
/// @c false otherwise.
5019+
static bool DiagnoseVLAInCXXTypeTrait(Sema &S, const TypeSourceInfo *T,
5020+
clang::tok::TokenKind TypeTraitID) {
5021+
if (!T->getType()->isVariableArrayType())
5022+
return false;
5023+
5024+
S.Diag(T->getTypeLoc().getBeginLoc(), diag::err_vla_unsupported)
5025+
<< 1 << TypeTraitID;
5026+
return true;
5027+
}
5028+
50155029
/// Check the completeness of a type in a unary type trait.
50165030
///
50175031
/// If the particular type trait requires a complete type, tries to complete
@@ -5188,7 +5202,9 @@ static bool HasNoThrowOperator(const RecordType *RT, OverloadedOperatorKind Op,
51885202
}
51895203

51905204
static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
5191-
SourceLocation KeyLoc, QualType T) {
5205+
SourceLocation KeyLoc,
5206+
TypeSourceInfo *TInfo) {
5207+
QualType T = TInfo->getType();
51925208
assert(!T->isDependentType() && "Cannot evaluate traits of dependent type");
51935209

51945210
ASTContext &C = Self.Context;
@@ -5205,21 +5221,13 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
52055221
case UTT_IsArray:
52065222
return T->isArrayType();
52075223
case UTT_IsBoundedArray:
5208-
if (!T->isVariableArrayType()) {
5209-
return T->isArrayType() && !T->isIncompleteArrayType();
5210-
}
5211-
5212-
Self.Diag(KeyLoc, diag::err_vla_unsupported)
5213-
<< 1 << tok::kw___is_bounded_array;
5214-
return false;
5224+
if (DiagnoseVLAInCXXTypeTrait(Self, TInfo, tok::kw___is_bounded_array))
5225+
return false;
5226+
return T->isArrayType() && !T->isIncompleteArrayType();
52155227
case UTT_IsUnboundedArray:
5216-
if (!T->isVariableArrayType()) {
5217-
return T->isIncompleteArrayType();
5218-
}
5219-
5220-
Self.Diag(KeyLoc, diag::err_vla_unsupported)
5221-
<< 1 << tok::kw___is_unbounded_array;
5222-
return false;
5228+
if (DiagnoseVLAInCXXTypeTrait(Self, TInfo, tok::kw___is_unbounded_array))
5229+
return false;
5230+
return T->isIncompleteArrayType();
52235231
case UTT_IsPointer:
52245232
return T->isAnyPointerType();
52255233
case UTT_IsNullPointer:
@@ -5631,7 +5639,7 @@ static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind,
56315639
return false;
56325640

56335641
if (Kind <= UTT_Last)
5634-
return EvaluateUnaryTypeTrait(S, Kind, KWLoc, Args[0]->getType());
5642+
return EvaluateUnaryTypeTrait(S, Kind, KWLoc, Args[0]);
56355643

56365644
// Evaluate ReferenceBindsToTemporary and ReferenceConstructsFromTemporary
56375645
// alongside the IsConstructible traits to avoid duplication.
@@ -6093,12 +6101,9 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
60936101
Self.RequireCompleteType(Rhs->getTypeLoc().getBeginLoc(), RhsT,
60946102
diag::err_incomplete_type);
60956103

6096-
if (LhsT->isVariableArrayType())
6097-
Self.Diag(Lhs->getTypeLoc().getBeginLoc(), diag::err_vla_unsupported)
6098-
<< 1 << tok::kw___is_layout_compatible;
6099-
if (RhsT->isVariableArrayType())
6100-
Self.Diag(Rhs->getTypeLoc().getBeginLoc(), diag::err_vla_unsupported)
6101-
<< 1 << tok::kw___is_layout_compatible;
6104+
DiagnoseVLAInCXXTypeTrait(Self, Lhs, tok::kw___is_layout_compatible);
6105+
DiagnoseVLAInCXXTypeTrait(Self, Rhs, tok::kw___is_layout_compatible);
6106+
61026107
return Self.IsLayoutCompatible(LhsT, RhsT);
61036108
}
61046109
case BTT_IsPointerInterconvertibleBaseOf: {
@@ -6108,12 +6113,10 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
61086113
diag::err_incomplete_type);
61096114
}
61106115

6111-
if (LhsT->isVariableArrayType())
6112-
Self.Diag(Lhs->getTypeLoc().getBeginLoc(), diag::err_vla_unsupported)
6113-
<< 1 << tok::kw___is_pointer_interconvertible_base_of;
6114-
if (RhsT->isVariableArrayType())
6115-
Self.Diag(Rhs->getTypeLoc().getBeginLoc(), diag::err_vla_unsupported)
6116-
<< 1 << tok::kw___is_pointer_interconvertible_base_of;
6116+
DiagnoseVLAInCXXTypeTrait(Self, Lhs,
6117+
tok::kw___is_pointer_interconvertible_base_of);
6118+
DiagnoseVLAInCXXTypeTrait(Self, Rhs,
6119+
tok::kw___is_pointer_interconvertible_base_of);
61176120

61186121
return Self.IsPointerInterconvertibleBaseOf(Lhs, Rhs);
61196122
}

0 commit comments

Comments
 (0)