[clang] a0ab0ca - [C2y] Add diagnostics for alignof on an incomplete array

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 2 07:34:47 PDT 2024


Author: Aaron Ballman
Date: 2024-07-02T10:34:34-04:00
New Revision: a0ab0ca7a733eb9e8e6442676a974ff8a2cdb930

URL: https://github.com/llvm/llvm-project/commit/a0ab0ca7a733eb9e8e6442676a974ff8a2cdb930
DIFF: https://github.com/llvm/llvm-project/commit/a0ab0ca7a733eb9e8e6442676a974ff8a2cdb930.diff

LOG: [C2y] Add diagnostics for alignof on an incomplete array

Clang implemented WG14 N3273 since Clang 3.5

Added: 
    clang/test/C/C2y/n3273.c

Modified: 
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaExpr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 3d3e98c7b751b..3df64b2ecef1b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3547,6 +3547,12 @@ def warn_alignment_not_power_of_two : Warning<
   InGroup<DiagGroup<"non-power-of-two-alignment">>;
 def err_alignment_dependent_typedef_name : Error<
   "requested alignment is dependent but declaration is not dependent">;
+def ext_c2y_alignof_incomplete_array : Extension<
+  "'alignof' on an incomplete array type is a C2y extension">,
+  InGroup<C2y>;
+def warn_c2y_compat_alignof_incomplete_array : Warning<
+  "'alignof' on an incomplete array type is incompatible with C standards "
+  "before C2y">, InGroup<CPre2yCompat>, DefaultIgnore;
 
 def warn_alignment_builtin_useless : Warning<
   "%select{aligning a value|the result of checking whether a value is aligned}0"

diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 1437fa08d2e7a..852344d895ffd 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -4486,8 +4486,16 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType,
   //   When alignof or _Alignof is applied to an array type, the result
   //   is the alignment of the element type.
   if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf ||
-      ExprKind == UETT_OpenMPRequiredSimdAlign)
+      ExprKind == UETT_OpenMPRequiredSimdAlign) {
+    // If the trait is 'alignof' in C before C2y, the ability to apply the
+    // trait to an incomplete array is an extension.
+    if (ExprKind == UETT_AlignOf && !getLangOpts().CPlusPlus &&
+        ExprType->isIncompleteArrayType())
+      Diag(OpLoc, getLangOpts().C2y
+                      ? diag::warn_c2y_compat_alignof_incomplete_array
+                      : diag::ext_c2y_alignof_incomplete_array);
     ExprType = Context.getBaseElementType(ExprType);
+  }
 
   if (ExprKind == UETT_VecStep)
     return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange);

diff  --git a/clang/test/C/C2y/n3273.c b/clang/test/C/C2y/n3273.c
new file mode 100644
index 0000000000000..84eeadd89ee62
--- /dev/null
+++ b/clang/test/C/C2y/n3273.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic -Wpre-c2y-compat %s
+// RUN: %clang_cc1 -verify=pre-c2y -std=c23 -Wall -pedantic %s
+
+/* WG14 N3273: Clang 3.5
+ * alignof of an incomplete array type
+ */
+
+static_assert(
+  alignof(int[]) == /* pre-c2y-warning {{'alignof' on an incomplete array type is a C2y extension}}
+                       expected-warning {{'alignof' on an incomplete array type is incompatible with C standards before C2y}}
+                     */
+  alignof(int)
+);
+


        


More information about the cfe-commits mailing list