c++: Implement C++23 P0330 - Literal Suffixes for ptrdiff_t and size_t.

Integer literal suffixes for signed size ('z') and unsigned size
(some permutation od 'zu') are provided as a language addition.

gcc/c-family/ChangeLog:

	* c-cppbuiltin.c (c_cpp_builtins): Define __cpp_size_t_suffix.
	* c-lex.c (interpret_integer): Set node type for size literal.

libcpp/ChangeLog:

	* expr.c (interpret_int_suffix): Detect 'z' integer suffix.
	(cpp_classify_number): Compat warning for use of 'z' suffix.
	* include/cpplib.h (struct cpp_options): New flag.
	(enum cpp_warning_reason): New flag.
	(CPP_N_USERDEF): Comment C++0x -> C++11.
	(CPP_N_SIZE_T): New flag for cpp_classify_number.
	* init.c (cpp_set_lang): Initialize new flag.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/udlit-shadow-neg.C: Test for 'z' and 'zu' shadowing.
	* g++.dg/cpp23/feat-cxx2b.C: New test.
	* g++.dg/cpp23/size_t-literals.C: New test.
	* g++.dg/warn/Wsize_t-literals.C: New test.
This commit is contained in:
Ed Smith-Rowland
2021-02-02 16:11:57 -05:00
parent 530203d6e3
commit 1f69e63cfc
9 changed files with 701 additions and 30 deletions
+24 -4
View File
@@ -313,13 +313,14 @@ static unsigned int
interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
{
size_t orig_len = len;
size_t u, l, i;
size_t u, l, i, z;
u = l = i = 0;
u = l = i = z = 0;
while (len--)
switch (s[len])
{
case 'z': case 'Z': z++; break;
case 'u': case 'U': u++; break;
case 'i': case 'I':
case 'j': case 'J': i++; break;
@@ -332,9 +333,17 @@ interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
return 0;
}
if (l > 2 || u > 1 || i > 1)
if (l > 2 || u > 1 || i > 1 || z > 1)
return 0;
if (z)
{
if (l > 0 || i > 0)
return 0;
if (!CPP_OPTION (pfile, cplusplus))
return 0;
}
if (i)
{
if (!CPP_OPTION (pfile, ext_numeric_literals))
@@ -352,7 +361,8 @@ interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
return ((i ? CPP_N_IMAGINARY : 0)
| (u ? CPP_N_UNSIGNED : 0)
| ((l == 0) ? CPP_N_SMALL
: (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
: (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE)
| (z ? CPP_N_SIZE_T : 0));
}
/* Return the classification flags for an int suffix. */
@@ -805,6 +815,16 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
virtual_location, 0, message);
}
if ((result & CPP_N_SIZE_T) == CPP_N_SIZE_T
&& !CPP_OPTION (pfile, size_t_literals))
{
const char *message = (result & CPP_N_UNSIGNED) == CPP_N_UNSIGNED
? N_("use of C++23 %<size_t%> integer constant")
: N_("use of C++23 %<make_signed<size_t>::type%> integer constant");
cpp_warning_with_line (pfile, CPP_W_SIZE_T_LITERALS,
virtual_location, 0, message);
}
result |= CPP_N_INTEGER;
}