On Wed, 16 Oct 2024, Alejandro Colomar wrote:
> + if (type_code != ARRAY_TYPE)
> + {
> + error_at (loc, "invalid application of %<countof%> to type %qT", type);
> + return error_mark_node;
> + }
> + if (!COMPLETE_TYPE_P (type))
> + {
> + error_at (loc,
> + "invalid application of %<countof%> to incomplete type %qT",
It is never appropriate, regardless of the actual operator naming, for a
diagnostic to refer to an operator name that doesn't exist at all (such as
countof instead of __countof__ in this patch).
> @@ -8992,12 +8992,17 @@ start_struct (location_t loc, enum tree_code code,
> tree name,
> within a statement expr used within sizeof, et. al. This is not
> terribly serious as C++ doesn't permit statement exprs within
> sizeof anyhow. */
> - if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
> + if (warn_cxx_compat
> + && (in_sizeof || in_typeof || in_alignof || in_countof))
> warning_at (loc, OPT_Wc___compat,
> "defining type in %qs expression is invalid in C++",
> (in_sizeof
> ? "sizeof"
> - : (in_typeof ? "typeof" : "alignof")));
> + : (in_typeof
> + ? "typeof"
> + : (in_alignof
> + ? "alignof"
> + : "countof"))));
Likewise.
> @@ -10135,12 +10140,17 @@ start_enum (location_t loc, struct c_enum_contents
> *the_enum, tree name,
> /* FIXME: This will issue a warning for a use of a type defined
> within sizeof in a statement expr. This is not terribly serious
> as C++ doesn't permit statement exprs within sizeof anyhow. */
> - if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
> + if (warn_cxx_compat
> + && (in_sizeof || in_typeof || in_alignof || in_countof))
> warning_at (loc, OPT_Wc___compat,
> "defining type in %qs expression is invalid in C++",
> (in_sizeof
> ? "sizeof"
> - : (in_typeof ? "typeof" : "alignof")));
> + : (in_typeof
> + ? "typeof"
> + : (in_alignof
> + ? "alignof"
> + : "countof"))));
Likewise.
> static struct c_expr
> -c_parser_sizeof_expression (c_parser *parser)
> +c_parser_sizeof_or_countof_expression (c_parser *parser, enum rid rid)
> {
> + const char *op_name = (rid == RID_COUNTOF) ? "countof" : "sizeof";
Likewise.
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index 302c3299ede..82f31668e37 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -10555,6 +10555,36 @@ If the operand of the @code{__alignof__} expression
> is a function,
> the expression evaluates to the alignment of the function which may
> be specified by attribute @code{aligned} (@pxref{Common Function
> Attributes}).
>
> +@node countof
> +@section Determining the Number of Elements of Arrays
> +@cindex countof
> +@cindex number of elements
Likewise, for node name and index entry.
--
Joseph S. Myers
[email protected]