aboutsummaryrefslogtreecommitdiff
path: root/libcpp
diff options
context:
space:
mode:
authorEd Smith-Rowland <3dw4rd@verizon.net>2021-02-02 16:11:57 -0500
committerEd Smith-Rowland <3dw4rd@verizon.net>2021-02-03 12:12:31 -0500
commit1f69e63cfcc664fd7382dd877846007652a01dcf (patch)
tree044c232c236da131484baffa67d63b1cab0eac55 /libcpp
parent530203d6e3244c25eda4124f0fa5756ca9a5683e (diff)
downloadgcc-1f69e63cfcc664fd7382dd877846007652a01dcf.zip
gcc-1f69e63cfcc664fd7382dd877846007652a01dcf.tar.gz
gcc-1f69e63cfcc664fd7382dd877846007652a01dcf.tar.bz2
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.
Diffstat (limited to 'libcpp')
-rw-r--r--libcpp/expr.c28
-rw-r--r--libcpp/include/cpplib.h8
-rw-r--r--libcpp/init.c52
3 files changed, 58 insertions, 30 deletions
diff --git a/libcpp/expr.c b/libcpp/expr.c
index 474ea4d..42007f9 100644
--- a/libcpp/expr.c
+++ b/libcpp/expr.c
@@ -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;
}
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 4467c73..17feb64 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -500,6 +500,9 @@ struct cpp_options
/* Nonzero means tokenize C++20 module directives. */
unsigned char module_directives;
+ /* Nonzero for C++23 size_t literals. */
+ unsigned char size_t_literals;
+
/* Holds the name of the target (execution) character set. */
const char *narrow_charset;
@@ -626,6 +629,7 @@ enum cpp_warning_reason {
CPP_W_INVALID_PCH,
CPP_W_WARNING_DIRECTIVE,
CPP_W_LITERAL_SUFFIX,
+ CPP_W_SIZE_T_LITERALS,
CPP_W_DATE_TIME,
CPP_W_PEDANTIC,
CPP_W_C90_C99_COMPAT,
@@ -1211,7 +1215,9 @@ struct cpp_num
#define CPP_N_FLOATN 0x400000 /* _FloatN types. */
#define CPP_N_FLOATNX 0x800000 /* _FloatNx types. */
-#define CPP_N_USERDEF 0x1000000 /* C++0x user-defined literal. */
+#define CPP_N_USERDEF 0x1000000 /* C++11 user-defined literal. */
+
+#define CPP_N_SIZE_T 0x2000000 /* C++23 size_t literal. */
#define CPP_N_WIDTH_FLOATN_NX 0xF0000000 /* _FloatN / _FloatNx value
of N, divided by 16. */
diff --git a/libcpp/init.c b/libcpp/init.c
index ecd3d5b..17b0d25 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -94,34 +94,35 @@ struct lang_flags
char va_opt;
char scope;
char dfp_constants;
+ char size_t_literals;
};
static const struct lang_flags lang_defaults[] =
-{ /* c99 c++ xnum xid c11 std digr ulit rlit udlit bincst digsep trig u8chlit vaopt scope dfp */
- /* GNUC89 */ { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
- /* GNUC99 */ { 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0 },
- /* GNUC11 */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0 },
- /* GNUC17 */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0 },
- /* GNUC2X */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1 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, 1, 1, 1, 0 },
- /* GNUCXX23 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0 },
- /* CXX23 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0 },
- /* ASM */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
+{ /* c99 c++ xnum xid c11 std digr ulit rlit udlit bincst digsep trig u8chlit vaopt scope dfp szlit */
+ /* GNUC89 */ { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
+ /* GNUC99 */ { 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
+ /* GNUC11 */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
+ /* GNUC17 */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
+ /* GNUC2X */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0 },
+ /* STDC89 */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
+ /* STDC94 */ { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
+ /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
+ /* STDC11 */ { 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
+ /* STDC17 */ { 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
+ /* STDC2X */ { 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 },
+ /* GNUCXX */ { 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
+ /* CXX98 */ { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 },
+ /* GNUCXX11 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
+ /* CXX11 */ { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },
+ /* GNUCXX14 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0 },
+ /* CXX14 */ { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0 },
+ /* GNUCXX17 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0 },
+ /* CXX17 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0 },
+ /* GNUCXX20 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0 },
+ /* CXX20 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0 },
+ /* GNUCXX23 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1 },
+ /* CXX23 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1 },
+ /* ASM */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
/* Sets internal flags correctly for a given language. */
@@ -149,6 +150,7 @@ cpp_set_lang (cpp_reader *pfile, enum c_lang lang)
CPP_OPTION (pfile, va_opt) = l->va_opt;
CPP_OPTION (pfile, scope) = l->scope;
CPP_OPTION (pfile, dfp_constants) = l->dfp_constants;
+ CPP_OPTION (pfile, size_t_literals) = l->size_t_literals;
}
/* Initialize library global state. */