Skip to content

Commit a0ab0ca

Browse files
committed
[C2y] Add diagnostics for alignof on an incomplete array
Clang implemented WG14 N3273 since Clang 3.5
1 parent 940ea5b commit a0ab0ca

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,6 +3547,12 @@ def warn_alignment_not_power_of_two : Warning<
35473547
InGroup<DiagGroup<"non-power-of-two-alignment">>;
35483548
def err_alignment_dependent_typedef_name : Error<
35493549
"requested alignment is dependent but declaration is not dependent">;
3550+
def ext_c2y_alignof_incomplete_array : Extension<
3551+
"'alignof' on an incomplete array type is a C2y extension">,
3552+
InGroup<C2y>;
3553+
def warn_c2y_compat_alignof_incomplete_array : Warning<
3554+
"'alignof' on an incomplete array type is incompatible with C standards "
3555+
"before C2y">, InGroup<CPre2yCompat>, DefaultIgnore;
35503556

35513557
def warn_alignment_builtin_useless : Warning<
35523558
"%select{aligning a value|the result of checking whether a value is aligned}0"

clang/lib/Sema/SemaExpr.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4486,8 +4486,16 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType,
44864486
// When alignof or _Alignof is applied to an array type, the result
44874487
// is the alignment of the element type.
44884488
if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf ||
4489-
ExprKind == UETT_OpenMPRequiredSimdAlign)
4489+
ExprKind == UETT_OpenMPRequiredSimdAlign) {
4490+
// If the trait is 'alignof' in C before C2y, the ability to apply the
4491+
// trait to an incomplete array is an extension.
4492+
if (ExprKind == UETT_AlignOf && !getLangOpts().CPlusPlus &&
4493+
ExprType->isIncompleteArrayType())
4494+
Diag(OpLoc, getLangOpts().C2y
4495+
? diag::warn_c2y_compat_alignof_incomplete_array
4496+
: diag::ext_c2y_alignof_incomplete_array);
44904497
ExprType = Context.getBaseElementType(ExprType);
4498+
}
44914499

44924500
if (ExprKind == UETT_VecStep)
44934501
return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange);

clang/test/C/C2y/n3273.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic -Wpre-c2y-compat %s
2+
// RUN: %clang_cc1 -verify=pre-c2y -std=c23 -Wall -pedantic %s
3+
4+
/* WG14 N3273: Clang 3.5
5+
* alignof of an incomplete array type
6+
*/
7+
8+
static_assert(
9+
alignof(int[]) == /* pre-c2y-warning {{'alignof' on an incomplete array type is a C2y extension}}
10+
expected-warning {{'alignof' on an incomplete array type is incompatible with C standards before C2y}}
11+
*/
12+
alignof(int)
13+
);
14+

0 commit comments

Comments
 (0)