PR c/122591
gcc/c-family/ChangeLog:
* c-common.cc (c_countof_type): Convert return value to size_t.
gcc/testsuite/ChangeLog:
* gcc.dg/countof-compile.c (type): Test return type of _Countof.
Reported-by: Sam James <[email protected]>
Suggested-by: Andrew Pinski <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
---
Hi Joseph,
I've squashed both commits into one. Below the patch, you can find the
interdiff and range diff. There are absolutely no changes other than
squashing the commits from v1 (and thus I have not re-tested, as it's
unnecessary).
Have a lovely night!
Alex
gcc/c-family/c-common.cc | 8 +++++++-
gcc/testsuite/gcc.dg/countof-compile.c | 6 ++++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index f2eed0337065..3b418ffba25b 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -4112,6 +4112,7 @@ tree
c_countof_type (location_t loc, tree type)
{
enum tree_code type_code;
+ tree value;
type_code = TREE_CODE (type);
if (type_code != ARRAY_TYPE)
@@ -4127,7 +4128,12 @@ c_countof_type (location_t loc, tree type)
return error_mark_node;
}
- return array_type_nelts_top (type);
+ value = array_type_nelts_top (type);
+ /* VALUE will have the middle-end integer type sizetype.
+ However, we should really return a value of type `size_t',
+ which is just a typedef for an ordinary integer type. */
+ value = fold_convert_loc (loc, size_type_node, value);
+ return value;
}
/* Handle C and C++ default attributes. */
diff --git a/gcc/testsuite/gcc.dg/countof-compile.c
b/gcc/testsuite/gcc.dg/countof-compile.c
index afd5659618b4..ebbac16ed38d 100644
--- a/gcc/testsuite/gcc.dg/countof-compile.c
+++ b/gcc/testsuite/gcc.dg/countof-compile.c
@@ -122,3 +122,9 @@ const_expr(void)
_Static_assert (_Countof (int [3][n]) == 3);
_Static_assert (_Countof (int [n][3]) == 7); /* { dg-error "not constant" }
*/
}
+
+void
+type(void)
+{
+ _Generic (_Countof (w), __typeof__ (sizeof 0): 0);
+}
Interdiff against v1:
Range-diff against v1:
1: b891e6e55035 < -: ------------ testsuite: Test return type of _Countof
[PR112591]
2: 052e8b60c9c6 ! 1: 3eabdc5e6238 c: Fix return type of _Countof [PR112591]
@@ Commit message
* c-common.cc (c_countof_type): Convert return value to size_t.
+ gcc/testsuite/ChangeLog:
+
+ * gcc.dg/countof-compile.c (type): Test return type of
_Countof.
+
Reported-by: Sam James <[email protected]>
Suggested-by: Andrew Pinski <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
@@ gcc/c-family/c-common.cc: c_countof_type (location_t loc, tree type)
}
/* Handle C and C++ default attributes. */
+
+ ## gcc/testsuite/gcc.dg/countof-compile.c ##
+@@ gcc/testsuite/gcc.dg/countof-compile.c: const_expr(void)
+ _Static_assert (_Countof (int [3][n]) == 3);
+ _Static_assert (_Countof (int [n][3]) == 7); /* { dg-error "not
constant" } */
+ }
++
++void
++type(void)
++{
++ _Generic (_Countof (w), __typeof__ (sizeof 0): 0);
++}
--
2.51.0