[clang] [C2y] Implement WG14 N3369 and N3469 (_Countof) (PR #133125)

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 27 08:13:17 PDT 2025


================
@@ -0,0 +1,117 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c2y -pedantic -Wall -Wno-comment -verify %s
+// RUN: %clang_cc1 -fsyntax-only -std=c2y -pedantic -Wall -Wno-comment -fexperimental-new-constant-interpreter -verify %s
+
+/* WG14 N3369: Clang 21
+ * _Lengthof operator
+ *
+ * Adds an operator to get the length of an array. Note that WG14 N3469 renamed
+ * this operator to _Countof.
+ */
+
+#if !__has_feature(c_countof)
+#error "Expected to have _Countof support"
+#endif
+
+#if !__has_extension(c_countof)
+// __has_extension returns true if __has_feature returns true.
+#error "Expected to have _Countof support"
+#endif
+
+int global_array[12];
+
+void test_parsing_failures() {
+  (void)_Countof;     // expected-error {{expected expression}}
+  (void)_Countof(;    // expected-error {{expected expression}}
+  (void)_Countof();   // expected-error {{expected expression}}
+  (void)_Countof int; // expected-error {{expected expression}}
+}
+
+void test_semantic_failures() {
+  (void)_Countof(1);         // expected-error {{'_Countof' requires an argument of array type; 'int' invalid}}
+  int non_array;
+  (void)_Countof non_array;  // expected-error {{'_Countof' requires an argument of array type; 'int' invalid}}  
+  (void)_Countof(int);       // expected-error {{'_Countof' requires an argument of array type; 'int' invalid}}  
----------------
shafik wrote:

Maybe try on a struct and a function?

https://github.com/llvm/llvm-project/pull/133125


More information about the cfe-commits mailing list