[clang] Add more tests for _Countof (PR #133333)

2025-03-27 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-27 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar created 
https://github.com/llvm/llvm-project/pull/13

Cc: @AaronBallman 

I haven't yet tried to run these tests.  I've pasted and adapted them from the 
ones I wrote for my own implementation.

>From c9ce60fef4e3af6faaea2a7e7f41b3866856d237 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar 
Date: Thu, 27 Mar 2025 23:51:18 +0100
Subject: [PATCH] Add more tests for _Countof

Signed-off-by: Alejandro Colomar 
---
 clang/test/C/C2y/n3369.c | 45 +++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/clang/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index 389828b52b6a2..5f7792962ad21 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -18,6 +18,8 @@
 #endif
 
 int global_array[12];
+int global_multi_array[12][34];
+int global_num;
 
 void test_parsing_failures() {
   (void)_Countof; // expected-error {{expected expression}}
@@ -36,6 +38,12 @@ void test_semantic_failures() {
 expected-note {{forward declaration of 'struct 
S'}}
   struct T { int x; };
   (void)_Countof(struct T);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct T' invalid}}
+  struct U { int x[3]; };
+  (void)_Countof(struct U);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct U' invalid}}
+  int a[3];
+  (void)_Countof(&a);  // expected-error {{'_Countof' requires an argument of 
array type; 'int (*)[3]' invalid}}
+  int *p;
+  (void)_Countof(p);  // expected-error {{'_Countof' requires an argument of 
array type; 'int *' invalid}}
 }
 
 void test_constant_expression_behavior(int n) {
@@ -81,6 +89,22 @@ void test_with_function_param(int array[12], int 
(*array_ptr)[12], int static_ar
   (void)_Countof(static_array); // expected-error {{'_Countof' requires an 
argument of array type; 'int *' invalid}}
 }
 
+void test_func_fix_fix(int i, char (*a)[3][5], int (*x)[_Countof(*a)]);
+void test_func_fix_var(int i, char (*a)[3][i], int (*x)[_Countof(*a)]);
+void test_func_fix_uns(int i, char (*a)[3][*], int (*x)[_Countof(*a)]);
+
+void test_funcs() {
+  int i3[3];
+  int i5[5];
+  char c35[3][5];
+  test_func_fix_fix(5, &c35, &i3);
+  test_func_fix_fix(5, &c35, &i5); // expected-error 
{{incompatible-pointer-types}}
+  test_func_fix_var(5, &c35, &i3);
+  test_func_fix_var(5, &c35, &i5); // expected-error 
{{incompatible-pointer-types}}
+  test_func_fix_uns(5, &c35, &i3);
+  test_func_fix_uns(5, &c35, &i5); // expected-error 
{{incompatible-pointer-types}}
+}
+
 void test_multidimensional_arrays() {
   int array[12][7];
   static_assert(_Countof(array) == 12);
@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, errno};
+  static_assert(_Countof(a) == 3);
+}
+
 // Test that the return type of _Countof is what you'd expect (size_t).
 void test_return_type() {
   static_assert(_Generic(typeof(_Countof global_array), typeof(sizeof(0)) : 1, 
default : 0));
@@ -121,10 +150,14 @@ void test_typedefs() {
   static_assert(_Countof(*x) == 12);
 }
 
-void test_zero_size_arrays() {
+void test_zero_size_arrays(int n) {
   int array[0]; // expected-warning {{zero size arrays are an extension}}
   static_assert(_Countof(array) == 0);
   static_assert(_Countof(int[0]) == 0); // expected-warning {{zero size arrays 
are an extension}}
+  int multi_array[0][n]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(multi_array) == 0);
+  int another_one[0][3]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(another_one) == 0);
 }
 
 void test_struct_members() {
@@ -144,3 +177,13 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+static int test_f1();
+static int test_f2();  // expected-warning {{never defined}}
+
+void test_symbols() {
+  int a[global_num][global_num];
+
+  static_assert(_Countof(global_multi_array[test_f1()]) == 34);
+  (void)_Countof(a[test_f2()]);
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-27 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-27 Thread Alejandro Colomar via cfe-commits

alejandro-colomar wrote:

> > Thanks @AaronBallman !! I am still working on my patch, so just for 
> > learning about Clang internals, I'll try to finish mine, and compare it to 
> > yours. :-)
> 
> Sorry for stealing this one out from under you!

No problem.  :-)

> It turns out it was a bit more involved than I expected because I didn't 
> realize how we represented multidimensional VLAs in the type system.

Yep, the GCC patches were also non-trivial; at least for some corner cases.  I 
had to wait for Martin to patch something unrelated to make it work.

BTW, I'll check all the tests I wrote for GCC, and contribute anything that 
might be missing here (if any).

> 
> > Sorry for being so slow!
> 
> No worries! I wasn't certain if you had gotten busy or not, and I had some 
> spare bandwidth and wanted to write code for a bit.

:)

https://github.com/llvm/llvm-project/pull/133125
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-27 Thread Alejandro Colomar via cfe-commits


@@ -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}}  
+}
+
+void test_constant_expression_behavior(int n) {
+  static_assert(_Countof(global_array) == 12);
+  static_assert(_Countof global_array == 12);  
+  static_assert(_Countof(int[12]) == 12);
+
+  // Use of a VLA makes it not a constant expression, same as with sizeof.
+  int array[n];
+  static_assert(_Countof(array)); // expected-error {{static assertion 
expression is not an integral constant expression}}
+  static_assert(sizeof(array));   // expected-error {{static assertion 
expression is not an integral constant expression}}
+  static_assert(_Countof(int[n]));// expected-error {{static assertion 
expression is not an integral constant expression}}
+  static_assert(sizeof(int[n]));  // expected-error {{static assertion 
expression is not an integral constant expression}}
+  
+  // Constant folding works the same way as sizeof, too.
+  const int m = 12;
+  int other_array[m];
+  static_assert(sizeof(other_array));   // expected-error {{static assertion 
expression is not an integral constant expression}}
+  static_assert(_Countof(other_array)); // expected-error {{static assertion 
expression is not an integral constant expression}}
+  static_assert(sizeof(int[m]));// expected-error {{static assertion 
expression is not an integral constant expression}}
+  static_assert(_Countof(int[m]));  // expected-error {{static assertion 
expression is not an integral constant expression}}
+  
+  // Note that this applies to each array dimension.
+  int another_array[n][7];
+  static_assert(_Countof(another_array)); // expected-error {{static assertion 
expression is not an integral constant expression}}
+  static_assert(_Countof(*another_array) == 7);
+
+  // Only the first dimension is needed for constant evaluation; other
+  // dimensions can be ignored.
+  int yet_another_array[7][n];
+  static_assert(_Countof(yet_another_array) == 7);
+  static_assert(_Countof(*yet_another_array)); // expected-error {{static 
assertion expression is not an integral constant expression}}
+  
+  int one_more_time[n][n][7];
+  static_assert(_Countof(one_more_time));  // expected-error {{static 
assertion expression is not an integral constant expression}}
+  static_assert(_Countof(*one_more_time)); // expected-error {{static 
assertion expression is not an integral constant expression}}
+  static_assert(_Countof(**one_more_time) == 7);
+}
+
+void test_with_function_param(int array[12], int (*array_ptr)[12], int 
static_array[static 12]) {
+  (void)_Countof(array); // expected-error {{'_Countof' requires an argument 
of array type; 'int *' invalid}}
+  static_assert(_Countof(*array_ptr) == 12);
+  (void)_Countof(static_array); // expected-error {{'_Countof' requires an 
argument of array type; 'int *' invalid}}
+}
+
+void test_multidimensional_arrays() {
+  int array[12][7];
+  static_assert(_Countof(array) == 12);
+  static_assert(_Countof(*array) == 7);
+
+  int mdarray[12][7][100][3];
+  static_assert(_Countof(mdarray) == 12);
+  static_assert(_Countof(*mdarray) == 7);
+  static_assert(_Countof(**mdarray) == 100);
+  static_assert(_Countof(***mdarray) == 3);
+}
+
+void test_unspecified_array_length() {
+  static_assert(_Countof(int[])); // expected-error {{invalid application of 
'_Countof' to an incomplete type 'int[]'}}
+
+  extern int x[][6][3];
+  static_assert(_Countof(x)); // expected-error {{invalid application of 
'_Countof' to an incomplete type 'int[][6][3]'}}
+  static_assert(_Countof(*x) == 6);
+  static_assert(_Countof(**x) == 3);
+}
+
+// Test that the return type of _Countof is what you'd expect (size_t).
+void test_return_type() {
+  static_assert(_Gen

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

2025-03-27 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/133125
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-27 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/133125
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-27 Thread Alejandro Colomar via cfe-commits

alejandro-colomar wrote:

Thanks @AaronBallman !!  I am still working on my patch, so just for learning 
about Clang internals, I'll try to finish mine, and compare it to yours.  :-)

Sorry for being so slow!

Cheers,
Alex

https://github.com/llvm/llvm-project/pull/133125
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar updated 
https://github.com/llvm/llvm-project/pull/13

>From a651447d3faca50423afa7fe528960afd58d8711 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar 
Date: Thu, 27 Mar 2025 23:51:18 +0100
Subject: [PATCH] Add more tests for _Countof

Link: 
Link: 
Cc: Aaron Ballman 
Signed-off-by: Alejandro Colomar 
---
 clang/test/C/C2y/n3369.c | 45 +++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/clang/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index 389828b52b6a2..0a4917a74f63e 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -18,6 +18,8 @@
 #endif
 
 int global_array[12];
+int global_multi_array[12][34];
+int global_num;
 
 void test_parsing_failures() {
   (void)_Countof; // expected-error {{expected expression}}
@@ -36,6 +38,12 @@ void test_semantic_failures() {
 expected-note {{forward declaration of 'struct 
S'}}
   struct T { int x; };
   (void)_Countof(struct T);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct T' invalid}}
+  struct U { int x[3]; };
+  (void)_Countof(struct U);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct U' invalid}}
+  int a[3];
+  (void)_Countof(&a);  // expected-error {{'_Countof' requires an argument of 
array type; 'int (*)[3]' invalid}}
+  int *p;
+  (void)_Countof(p);  // expected-error {{'_Countof' requires an argument of 
array type; 'int *' invalid}}
 }
 
 void test_constant_expression_behavior(int n) {
@@ -81,6 +89,22 @@ void test_with_function_param(int array[12], int 
(*array_ptr)[12], int static_ar
   (void)_Countof(static_array); // expected-error {{'_Countof' requires an 
argument of array type; 'int *' invalid}}
 }
 
+void test_func_fix_fix(int i, char (*a)[3][5], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_var(int i, char (*a)[3][i], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_uns(int i, char (*a)[3][*], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+
+void test_funcs() {
+  int i3[3];
+  int i5[5];
+  char c35[3][5];
+  test_func_fix_fix(5, &c35, &i3);
+  test_func_fix_fix(5, &c35, &i5); // expected-error {{incompatible pointer 
types passing}}
+  test_func_fix_var(5, &c35, &i3);
+  test_func_fix_var(5, &c35, &i5); // expected-error {{incompatible pointer 
types passing}}
+  test_func_fix_uns(5, &c35, &i3);
+  test_func_fix_uns(5, &c35, &i5); // expected-error {{incompatible pointer 
types passing}}
+}
+
 void test_multidimensional_arrays() {
   int array[12][7];
   static_assert(_Countof(array) == 12);
@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, global_num};
+  static_assert(_Countof(a) == 3);
+}
+
 // Test that the return type of _Countof is what you'd expect (size_t).
 void test_return_type() {
   static_assert(_Generic(typeof(_Countof global_array), typeof(sizeof(0)) : 1, 
default : 0));
@@ -121,10 +150,14 @@ void test_typedefs() {
   static_assert(_Countof(*x) == 12);
 }
 
-void test_zero_size_arrays() {
+void test_zero_size_arrays(int n) {
   int array[0]; // expected-warning {{zero size arrays are an extension}}
   static_assert(_Countof(array) == 0);
   static_assert(_Countof(int[0]) == 0); // expected-warning {{zero size arrays 
are an extension}}
+  int multi_array[0][n]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(multi_array) == 0);
+  int another_one[0][3]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(another_one) == 0);
 }
 
 void test_struct_members() {
@@ -144,3 +177,13 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+static int test_f1();
+static int test_f2();  // expected-warning {{never defined}}
+
+void test_symbols() {
+  int a[global_num][global_num];
+
+  static_assert(_Countof(global_multi_array[test_f1()]) == 34);
+  (void)_Countof(a[test_f2()]);
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits


@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, errno};

alejandro-colomar wrote:

I wanted something that wasn't an ICE, and which the compiler wasn't allowed to 
transform into an ICE through optimization, to make sure that even with a 
run-time value, we have _Countof result in an ICE.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits


@@ -144,3 +177,13 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+static int test_f1();
+static int test_f2();  // expected-warning {{never defined}}
+
+void test_symbols() {
+  int a[global_num][global_num];
+
+  static_assert(_Countof(global_multi_array[test_f1()]) == 34);
+  (void)_Countof(a[test_f2()]);

alejandro-colomar wrote:

That the symbol `test_f1` is not used at all.


We don't get a diagnostic due to having a declaration without a definition.  
(But we get it in line 188 for `test_f2` because that's not an ICE.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-07 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar updated 
https://github.com/llvm/llvm-project/pull/13

>From 3660f83ad4beb674127a2033d1662c3cb15ace67 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar 
Date: Thu, 27 Mar 2025 23:51:18 +0100
Subject: [PATCH] Add more tests for _Countof

Link: 
Link: 
Cc: Aaron Ballman 
Signed-off-by: Alejandro Colomar 
---
 clang/test/C/C2y/n3369.c | 50 +++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/clang/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index 389828b52b6a2..8443ed56b7ab2 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -18,6 +18,8 @@
 #endif
 
 int global_array[12];
+int global_multi_array[12][34];
+int global_num;
 
 void test_parsing_failures() {
   (void)_Countof; // expected-error {{expected expression}}
@@ -36,6 +38,12 @@ void test_semantic_failures() {
 expected-note {{forward declaration of 'struct 
S'}}
   struct T { int x; };
   (void)_Countof(struct T);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct T' invalid}}
+  struct U { int x[3]; };
+  (void)_Countof(struct U);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct U' invalid}}
+  int a[3];
+  (void)_Countof(&a);  // expected-error {{'_Countof' requires an argument of 
array type; 'int (*)[3]' invalid}}
+  int *p;
+  (void)_Countof(p);  // expected-error {{'_Countof' requires an argument of 
array type; 'int *' invalid}}
 }
 
 void test_constant_expression_behavior(int n) {
@@ -81,6 +89,22 @@ void test_with_function_param(int array[12], int 
(*array_ptr)[12], int static_ar
   (void)_Countof(static_array); // expected-error {{'_Countof' requires an 
argument of array type; 'int *' invalid}}
 }
 
+void test_func_fix_fix(int i, char (*a)[3][5], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_var(int i, char (*a)[3][i], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_uns(int i, char (*a)[3][*], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+
+void test_funcs() {
+  int i3[3];
+  int i5[5];
+  char c35[3][5];
+  test_func_fix_fix(5, &c35, &i3);
+  test_func_fix_fix(5, &c35, &i5); // expected-warning {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_var(5, &c35, &i3);
+  test_func_fix_var(5, &c35, &i5); // expected-warning {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_uns(5, &c35, &i3);
+  test_func_fix_uns(5, &c35, &i5); // expected-warning {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+}
+
 void test_multidimensional_arrays() {
   int array[12][7];
   static_assert(_Countof(array) == 12);
@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, global_num};
+  static_assert(_Countof(a) == 3);
+}
+
 // Test that the return type of _Countof is what you'd expect (size_t).
 void test_return_type() {
   static_assert(_Generic(typeof(_Countof global_array), typeof(sizeof(0)) : 1, 
default : 0));
@@ -121,10 +150,14 @@ void test_typedefs() {
   static_assert(_Countof(*x) == 12);
 }
 
-void test_zero_size_arrays() {
+void test_zero_size_arrays(int n) {
   int array[0]; // expected-warning {{zero size arrays are an extension}}
   static_assert(_Countof(array) == 0);
   static_assert(_Countof(int[0]) == 0); // expected-warning {{zero size arrays 
are an extension}}
+  int multi_array[0][n]; // FIXME: Should trigger -Wzero-length-array
+  static_assert(_Countof(multi_array) == 0);
+  int another_one[0][3]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(another_one) == 0);
 }
 
 void test_struct_members() {
@@ -144,3 +177,18 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+/* We don't get a diagnostic for test_f1(), because it ends up unused
+ * as _Countof() results in an integer constant expression, which is not
+ * evaluated.  However, test_f2() ends up being evaluated, since 'a' is
+ * a VLA.
+ */
+static int test_f1();
+static int test_f2(); // expected-warning {{function 'test_f2' has internal 
linkage but is not defined}}
+
+void test_symbols() {
+  int a[global_num][global_num];
+
+  static_assert(_Countof(global_multi_array[test_f1()]) == 34);
+  (void)_Countof(a[test_f2()]);
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-06 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar updated 
https://github.com/llvm/llvm-project/pull/13

>From b8f95e71efec49e90ed20bc1f78fe2055caccece Mon Sep 17 00:00:00 2001
From: Alejandro Colomar 
Date: Thu, 27 Mar 2025 23:51:18 +0100
Subject: [PATCH] Add more tests for _Countof

Link: 
Link: 
Cc: Aaron Ballman 
Signed-off-by: Alejandro Colomar 
---
 clang/test/C/C2y/n3369.c | 45 +++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/clang/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index 389828b52b6a2..a4ae6749fb898 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -18,6 +18,8 @@
 #endif
 
 int global_array[12];
+int global_multi_array[12][34];
+int global_num;
 
 void test_parsing_failures() {
   (void)_Countof; // expected-error {{expected expression}}
@@ -36,6 +38,12 @@ void test_semantic_failures() {
 expected-note {{forward declaration of 'struct 
S'}}
   struct T { int x; };
   (void)_Countof(struct T);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct T' invalid}}
+  struct U { int x[3]; };
+  (void)_Countof(struct U);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct U' invalid}}
+  int a[3];
+  (void)_Countof(&a);  // expected-error {{'_Countof' requires an argument of 
array type; 'int (*)[3]' invalid}}
+  int *p;
+  (void)_Countof(p);  // expected-error {{'_Countof' requires an argument of 
array type; 'int *' invalid}}
 }
 
 void test_constant_expression_behavior(int n) {
@@ -81,6 +89,22 @@ void test_with_function_param(int array[12], int 
(*array_ptr)[12], int static_ar
   (void)_Countof(static_array); // expected-error {{'_Countof' requires an 
argument of array type; 'int *' invalid}}
 }
 
+void test_func_fix_fix(int i, char (*a)[3][5], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_var(int i, char (*a)[3][i], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_uns(int i, char (*a)[3][*], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+
+void test_funcs() {
+  int i3[3];
+  int i5[5];
+  char c35[3][5];
+  test_func_fix_fix(5, &c35, &i3);
+  test_func_fix_fix(5, &c35, &i5); // expected-warning {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_var(5, &c35, &i3);
+  test_func_fix_var(5, &c35, &i5); // expected-warning {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_uns(5, &c35, &i3);
+  test_func_fix_uns(5, &c35, &i5); // expected-warning {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+}
+
 void test_multidimensional_arrays() {
   int array[12][7];
   static_assert(_Countof(array) == 12);
@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, global_num};
+  static_assert(_Countof(a) == 3);
+}
+
 // Test that the return type of _Countof is what you'd expect (size_t).
 void test_return_type() {
   static_assert(_Generic(typeof(_Countof global_array), typeof(sizeof(0)) : 1, 
default : 0));
@@ -121,10 +150,14 @@ void test_typedefs() {
   static_assert(_Countof(*x) == 12);
 }
 
-void test_zero_size_arrays() {
+void test_zero_size_arrays(int n) {
   int array[0]; // expected-warning {{zero size arrays are an extension}}
   static_assert(_Countof(array) == 0);
   static_assert(_Countof(int[0]) == 0); // expected-warning {{zero size arrays 
are an extension}}
+  int multi_array[0][n]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(multi_array) == 0);
+  int another_one[0][3]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(another_one) == 0);
 }
 
 void test_struct_members() {
@@ -144,3 +177,13 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+static int test_f1();
+static int test_f2();  // expected-warning {{never defined}}
+
+void test_symbols() {
+  int a[global_num][global_num];
+
+  static_assert(_Countof(global_multi_array[test_f1()]) == 34);
+  (void)_Countof(a[test_f2()]);
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-06 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-06 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-07 Thread Alejandro Colomar via cfe-commits


@@ -144,3 +177,13 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+static int test_f1();
+static int test_f2();  // expected-warning {{never defined}}

alejandro-colomar wrote:

It seems it's not triggering anything at all.  :|

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-07 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar updated 
https://github.com/llvm/llvm-project/pull/13

>From eb63ad49de452c88fe7739172da9c17eadfce2d4 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar 
Date: Thu, 27 Mar 2025 23:51:18 +0100
Subject: [PATCH] Add more tests for _Countof

Link: 
Link: 
Cc: Aaron Ballman 
Signed-off-by: Alejandro Colomar 
---
 clang/test/C/C2y/n3369.c | 45 +++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/clang/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index 389828b52b6a2..2fc926a588fdc 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -18,6 +18,8 @@
 #endif
 
 int global_array[12];
+int global_multi_array[12][34];
+int global_num;
 
 void test_parsing_failures() {
   (void)_Countof; // expected-error {{expected expression}}
@@ -36,6 +38,12 @@ void test_semantic_failures() {
 expected-note {{forward declaration of 'struct 
S'}}
   struct T { int x; };
   (void)_Countof(struct T);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct T' invalid}}
+  struct U { int x[3]; };
+  (void)_Countof(struct U);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct U' invalid}}
+  int a[3];
+  (void)_Countof(&a);  // expected-error {{'_Countof' requires an argument of 
array type; 'int (*)[3]' invalid}}
+  int *p;
+  (void)_Countof(p);  // expected-error {{'_Countof' requires an argument of 
array type; 'int *' invalid}}
 }
 
 void test_constant_expression_behavior(int n) {
@@ -81,6 +89,22 @@ void test_with_function_param(int array[12], int 
(*array_ptr)[12], int static_ar
   (void)_Countof(static_array); // expected-error {{'_Countof' requires an 
argument of array type; 'int *' invalid}}
 }
 
+void test_func_fix_fix(int i, char (*a)[3][5], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_var(int i, char (*a)[3][i], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_uns(int i, char (*a)[3][*], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+
+void test_funcs() {
+  int i3[3];
+  int i5[5];
+  char c35[3][5];
+  test_func_fix_fix(5, &c35, &i3);
+  test_func_fix_fix(5, &c35, &i5); // expected-warning {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_var(5, &c35, &i3);
+  test_func_fix_var(5, &c35, &i5); // expected-warning {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_uns(5, &c35, &i3);
+  test_func_fix_uns(5, &c35, &i5); // expected-warning {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+}
+
 void test_multidimensional_arrays() {
   int array[12][7];
   static_assert(_Countof(array) == 12);
@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, global_num};
+  static_assert(_Countof(a) == 3);
+}
+
 // Test that the return type of _Countof is what you'd expect (size_t).
 void test_return_type() {
   static_assert(_Generic(typeof(_Countof global_array), typeof(sizeof(0)) : 1, 
default : 0));
@@ -121,10 +150,14 @@ void test_typedefs() {
   static_assert(_Countof(*x) == 12);
 }
 
-void test_zero_size_arrays() {
+void test_zero_size_arrays(int n) {
   int array[0]; // expected-warning {{zero size arrays are an extension}}
   static_assert(_Countof(array) == 0);
   static_assert(_Countof(int[0]) == 0); // expected-warning {{zero size arrays 
are an extension}}
+  int multi_array[0][n]; // FIXME: Should trigger -Wzero-length-array
+  static_assert(_Countof(multi_array) == 0);
+  int another_one[0][3]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(another_one) == 0);
 }
 
 void test_struct_members() {
@@ -144,3 +177,13 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+static int test_f1();
+static int test_f2(); // expected-warning {{function 'f' has internal linkage 
but is not defined}}
+
+void test_symbols() {
+  int a[global_num][global_num];
+
+  static_assert(_Countof(global_multi_array[test_f1()]) == 34);
+  (void)_Countof(a[test_f2()]);
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-07 Thread Alejandro Colomar via cfe-commits


@@ -121,10 +150,14 @@ void test_typedefs() {
   static_assert(_Countof(*x) == 12);
 }
 
-void test_zero_size_arrays() {
+void test_zero_size_arrays(int n) {
   int array[0]; // expected-warning {{zero size arrays are an extension}}
   static_assert(_Countof(array) == 0);
   static_assert(_Countof(int[0]) == 0); // expected-warning {{zero size arrays 
are an extension}}
+  int multi_array[0][n]; // expected-warning {{zero size arrays are an 
extension}}

alejandro-colomar wrote:

Thanks!  I've marked it with a FIXME comment.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-07 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-07 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-04-07 Thread Alejandro Colomar via cfe-commits


@@ -144,3 +177,13 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+static int test_f1();
+static int test_f2();  // expected-warning {{never defined}}
+
+void test_symbols() {
+  int a[global_num][global_num];
+
+  static_assert(_Countof(global_multi_array[test_f1()]) == 34);
+  (void)_Countof(a[test_f2()]);

alejandro-colomar wrote:

Done.  Feel free to reopen if the comment is unclear.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar updated 
https://github.com/llvm/llvm-project/pull/13



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits


@@ -144,3 +177,13 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+static int test_f1();
+static int test_f2();  // expected-warning {{never defined}}

alejandro-colomar wrote:

done.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar updated 
https://github.com/llvm/llvm-project/pull/13

>From f4f1f5661b0bdb9a11dbeafd9c7242592c40ff4a Mon Sep 17 00:00:00 2001
From: Alejandro Colomar 
Date: Thu, 27 Mar 2025 23:51:18 +0100
Subject: [PATCH] Add more tests for _Countof

Link: 
Link: 
Cc: Aaron Ballman 
Signed-off-by: Alejandro Colomar 
---
 clang/test/C/C2y/n3369.c | 52 +++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/clang/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index 389828b52b6a2..db26040d8cf44 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -17,7 +17,11 @@
 #error "Expected to have _Countof support"
 #endif
 
+#define NULL  ((void *) 0)
+
 int global_array[12];
+int global_multi_array[12][34];
+int global_num;
 
 void test_parsing_failures() {
   (void)_Countof; // expected-error {{expected expression}}
@@ -36,6 +40,12 @@ void test_semantic_failures() {
 expected-note {{forward declaration of 'struct 
S'}}
   struct T { int x; };
   (void)_Countof(struct T);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct T' invalid}}
+  struct U { int x[3]; };
+  (void)_Countof(struct U);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct U' invalid}}
+  int a[3];
+  (void)_Countof(&a);  // expected-error {{'_Countof' requires an argument of 
array type; 'int (*)[3]' invalid}}
+  int *p;
+  (void)_Countof(p);  // expected-error {{'_Countof' requires an argument of 
array type; 'int *' invalid}}
 }
 
 void test_constant_expression_behavior(int n) {
@@ -81,6 +91,22 @@ void test_with_function_param(int array[12], int 
(*array_ptr)[12], int static_ar
   (void)_Countof(static_array); // expected-error {{'_Countof' requires an 
argument of array type; 'int *' invalid}}
 }
 
+void test_func_fix_fix(int i, char (*a)[3][5], int (*x)[_Countof(*a)], char 
(*)[_Generic(x, int (*)[3]: 1)]);  // expected-note {{passing argument to 
parameter}}
+void test_func_fix_var(int i, char (*a)[3][i], int (*x)[_Countof(*a)], char 
(*)[_Generic(x, int (*)[3]: 1)]);  // expected-note {{passing argument to 
parameter}}
+void test_func_fix_uns(int i, char (*a)[3][*], int (*x)[_Countof(*a)], char 
(*)[_Generic(x, int (*)[3]: 1)]);  // expected-note {{passing argument to 
parameter}}
+
+void test_funcs() {
+  int i3[3];
+  int i5[5];
+  char c35[3][5];
+  test_func_fix_fix(5, &c35, &i3, NULL);
+  test_func_fix_fix(5, &c35, &i5, NULL); // expected-warning {{incompatible 
pointer types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_var(5, &c35, &i3, NULL);
+  test_func_fix_var(5, &c35, &i5, NULL); // expected-warning {{incompatible 
pointer types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_uns(5, &c35, &i3, NULL);
+  test_func_fix_uns(5, &c35, &i5, NULL); // expected-warning {{incompatible 
pointer types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+}
+
 void test_multidimensional_arrays() {
   int array[12][7];
   static_assert(_Countof(array) == 12);
@@ -102,6 +128,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, global_num};
+  static_assert(_Countof(a) == 3);
+}
+
 // Test that the return type of _Countof is what you'd expect (size_t).
 void test_return_type() {
   static_assert(_Generic(typeof(_Countof global_array), typeof(sizeof(0)) : 1, 
default : 0));
@@ -121,10 +152,14 @@ void test_typedefs() {
   static_assert(_Countof(*x) == 12);
 }
 
-void test_zero_size_arrays() {
+void test_zero_size_arrays(int n) {
   int array[0]; // expected-warning {{zero size arrays are an extension}}
   static_assert(_Countof(array) == 0);
   static_assert(_Countof(int[0]) == 0); // expected-warning {{zero size arrays 
are an extension}}
+  int multi_array[0][n]; // FIXME: Should trigger -Wzero-length-array
+  static_assert(_Countof(multi_array) == 0);
+  int another_one[0][3]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(another_one) == 0);
 }
 
 void test_struct_members() {
@@ -144,3 +179,18 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+/* We don't get a diagnostic for test_f1(), because it ends up unused
+ * as _Countof() results in an integer constant expression, which is not
+ * evaluated.  However, test_f2() ends up being evaluated, since 'a' is
+ * a VLA.
+ */
+static int test_f1();
+static int test_f2(); // FIXME: Should trigger function 'test_f2' has internal 
linkage but is not defined
+
+void test_symbols() {
+  int a[global_num][global_num];
+
+  static_assert(_Countof(global_multi_array[test_f1()]) == 34);
+  (void)_Countof(a[test_f2()]);
+}

__

[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar ready_for_review 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits

alejandro-colomar wrote:

@AaronBallman This is ready.  Still, my patches for GCC had another set of 
tests that are completely missing here: run-time tests (assert(3)) for non-ICE 
(so, VLA).  Maybe you could add a set of tests that are run (not just compiled).

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar updated 
https://github.com/llvm/llvm-project/pull/13



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-05 Thread Alejandro Colomar via cfe-commits


@@ -81,6 +89,22 @@ void test_with_function_param(int array[12], int 
(*array_ptr)[12], int static_ar
   (void)_Countof(static_array); // expected-error {{'_Countof' requires an 
argument of array type; 'int *' invalid}}
 }
 
+void test_func_fix_fix(int i, char (*a)[3][5], int (*x)[_Countof(*a)]);
+void test_func_fix_var(int i, char (*a)[3][i], int (*x)[_Countof(*a)]);
+void test_func_fix_uns(int i, char (*a)[3][*], int (*x)[_Countof(*a)]);

alejandro-colomar wrote:

Done.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits


@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, errno};

alejandro-colomar wrote:

I could use the global_num here.  I wrote that one after having used errno, so 
I didn't think of it.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits


@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, global_num};
+  static_assert(_Countof(a) == 3);

alejandro-colomar wrote:

Or maybe it's because it was using `errno`, which didn't allow it to be 
completed.  Maybe it now works.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits


@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, global_num};
+  static_assert(_Countof(a) == 3);

alejandro-colomar wrote:

Actually, I don't see it anymore.  Good then.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar updated 
https://github.com/llvm/llvm-project/pull/13

>From 6c5c502ca376f6097ee783ed1f4f8bb1e2a3b2a0 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar 
Date: Thu, 27 Mar 2025 23:51:18 +0100
Subject: [PATCH] Add more tests for _Countof

Link: 
Link: 
Cc: Aaron Ballman 
Signed-off-by: Alejandro Colomar 
---
 clang/test/C/C2y/n3369.c | 45 +++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/clang/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index 389828b52b6a2..ccaea206d2d09 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -18,6 +18,8 @@
 #endif
 
 int global_array[12];
+int global_multi_array[12][34];
+int global_num;
 
 void test_parsing_failures() {
   (void)_Countof; // expected-error {{expected expression}}
@@ -36,6 +38,12 @@ void test_semantic_failures() {
 expected-note {{forward declaration of 'struct 
S'}}
   struct T { int x; };
   (void)_Countof(struct T);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct T' invalid}}
+  struct U { int x[3]; };
+  (void)_Countof(struct U);  // expected-error {{'_Countof' requires an 
argument of array type; 'struct U' invalid}}
+  int a[3];
+  (void)_Countof(&a);  // expected-error {{'_Countof' requires an argument of 
array type; 'int (*)[3]' invalid}}
+  int *p;
+  (void)_Countof(p);  // expected-error {{'_Countof' requires an argument of 
array type; 'int *' invalid}}
 }
 
 void test_constant_expression_behavior(int n) {
@@ -81,6 +89,22 @@ void test_with_function_param(int array[12], int 
(*array_ptr)[12], int static_ar
   (void)_Countof(static_array); // expected-error {{'_Countof' requires an 
argument of array type; 'int *' invalid}}
 }
 
+void test_func_fix_fix(int i, char (*a)[3][5], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_var(int i, char (*a)[3][i], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+void test_func_fix_uns(int i, char (*a)[3][*], int (*x)[_Countof(*a)]);  // 
expected-note {{passing argument to parameter}}
+
+void test_funcs() {
+  int i3[3];
+  int i5[5];
+  char c35[3][5];
+  test_func_fix_fix(5, &c35, &i3);
+  test_func_fix_fix(5, &c35, &i5); // expected-error {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_var(5, &c35, &i3);
+  test_func_fix_var(5, &c35, &i5); // expected-error {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+  test_func_fix_uns(5, &c35, &i3);
+  test_func_fix_uns(5, &c35, &i5); // expected-error {{incompatible pointer 
types passing 'int (*)[5]' to parameter of type 'int (*)[3]'}}
+}
+
 void test_multidimensional_arrays() {
   int array[12][7];
   static_assert(_Countof(array) == 12);
@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, global_num};
+  static_assert(_Countof(a) == 3);
+}
+
 // Test that the return type of _Countof is what you'd expect (size_t).
 void test_return_type() {
   static_assert(_Generic(typeof(_Countof global_array), typeof(sizeof(0)) : 1, 
default : 0));
@@ -121,10 +150,14 @@ void test_typedefs() {
   static_assert(_Countof(*x) == 12);
 }
 
-void test_zero_size_arrays() {
+void test_zero_size_arrays(int n) {
   int array[0]; // expected-warning {{zero size arrays are an extension}}
   static_assert(_Countof(array) == 0);
   static_assert(_Countof(int[0]) == 0); // expected-warning {{zero size arrays 
are an extension}}
+  int multi_array[0][n]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(multi_array) == 0);
+  int another_one[0][3]; // expected-warning {{zero size arrays are an 
extension}}
+  static_assert(_Countof(another_one) == 0);
 }
 
 void test_struct_members() {
@@ -144,3 +177,13 @@ void test_compound_literals() {
   static_assert(_Countof((int[2]){}) == 2);
   static_assert(_Countof((int[]){1, 2, 3, 4}) == 4);   
 }
+
+static int test_f1();
+static int test_f2();  // expected-warning {{never defined}}
+
+void test_symbols() {
+  int a[global_num][global_num];
+
+  static_assert(_Countof(global_multi_array[test_f1()]) == 34);
+  (void)_Countof(a[test_f2()]);
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits


@@ -102,6 +126,11 @@ void test_unspecified_array_length() {
   static_assert(_Countof(**x) == 3);
 }
 
+void test_completed_array() {
+  int a[] = {1, 2, global_num};
+  static_assert(_Countof(a) == 3);

alejandro-colomar wrote:

@AaronBallman This test seems to have caught a bug in the implementation.

I see the following report in the CI:

```
File 
/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-bt7k7-1/llvm-project/github-pull-requests/clang/test/C/C2y/n3369.c
 Line 131: invalid application of '_Countof' to an incomplete type 'int[]'
```

But this shouldn't happen.  The type should be completed.  In my GCC 
implementation, this test passes, AFAIR.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-03-28 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar edited 
https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add more tests for _Countof (PR #133333)

2025-05-07 Thread Alejandro Colomar via cfe-commits

alejandro-colomar wrote:

> > @AaronBallman This is ready. Still, my patches for GCC had another set of 
> > tests that are completely missing here: run-time tests (assert(3)) for 
> > non-ICE (so, VLA). Maybe you could add a set of tests that are run (not 
> > just compiled).
> 
> We don't do end-to-end testing in Clang directly (those kinds of tests are 
> sometimes more fragile because of differences between host platforms); we 
> usually shunt that sort of thing off into llvm-test-suite, which gets less 
> visibility and has less post-commit bot coverage, so we typically only do 
> that for special cases. Instead we test what LLVM IR we emit and we rely on 
> the backend tests to verify that the given LLVM IR instructions behave as 
> intended.

Thanks!  Makes sense.

https://github.com/llvm/llvm-project/pull/13
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [C2y] Add stdcountof.h (PR #140890)

2025-05-21 Thread Alejandro Colomar via cfe-commits

alejandro-colomar wrote:

> > Thanks!
> > For the first commit:
> > ```
> > Suggested-by: Alejandro Colomar 
> > ```
> 
> Happy to add it,

Thanks!  :)

> but for my own education: what's that do? :-D



Quoting that:

```
A Suggested-by: tag indicates that the patch idea is suggested by
the person named and ensures credit to the person for the idea:
if we diligently credit our idea reporters, they will, hopefully,
be inspired to help us again in the future. Note, this is one of
only three tags you might be able to use without explicit
permission of the person named (see ‘Tagging people requires
permission’ below for details).
```

https://github.com/llvm/llvm-project/pull/140890
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [C2y] Add stdcountof.h (PR #140890)

2025-05-21 Thread Alejandro Colomar via cfe-commits

https://github.com/alejandro-colomar approved this pull request.

Thanks!

For the first commit:
```
Suggested-by: Alejandro Colomar 
```

https://github.com/llvm/llvm-project/pull/140890
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Make the result type of sizeof/pointer subtraction/size_t lit… (PR #136542)

2025-05-21 Thread Alejandro Colomar via cfe-commits

alejandro-colomar wrote:

Hi!

I don't see it mentioned, so I'll note it down here.  The standard allows this:

```c
typedef typeof(sizeof(0))  size_t;
```

as a valid implementation of `size_t`.

It would be good to keep this in mind to not accidentally break it.  (Not 
saying this would break it; just to be cautious.)

https://github.com/llvm/llvm-project/pull/136542
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-05-21 Thread Alejandro Colomar via cfe-commits

alejandro-colomar wrote:

@AaronBallman 

I'm not sure who's responsible for `` in LLVM.  In GCC it's a 
header provided by the compiler.  I see that this PR does not add the header, 
so I was wondering if that's to be added in a separate commit, or if it's not 
supported by Clang and depends on library support.

https://github.com/llvm/llvm-project/pull/133125
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-05-21 Thread Alejandro Colomar via cfe-commits

alejandro-colomar wrote:

> > @AaronBallman
> > I'm not sure who's responsible for `` in LLVM.
> 
> Yeah, it's never really clear who is expected to provide these headers.
> 
> > In GCC it's a header provided by the compiler. I see that this PR does not 
> > add the header, so I was wondering if that's to be added in a separate 
> > commit, or if it's not supported by Clang and depends on library support.
> 
> I think Clang should probably provide the header as well. We do so for 
> stdalign.h, stdbool.h, stdnoreturn.h and this is morally equivalent to those. 
> Thanks for bringing this up!

Okay, I'll see if I can write this patch.  I'm much less busy these days.  :)

https://github.com/llvm/llvm-project/pull/133125
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-05-21 Thread Alejandro Colomar via cfe-commits

alejandro-colomar wrote:

No problem.  Thanks!  :-D

https://github.com/llvm/llvm-project/pull/133125
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits