|
| 1 | +// RUN: %clang_cc1 -verify=expected,c2y,c -pedantic -std=c2y %s |
| 2 | +// RUN: %clang_cc1 -verify=expected,c2y,compat -Wpre-c2y-compat -std=c2y %s |
| 3 | +// RUN: %clang_cc1 -verify=expected,ext,c -pedantic -std=c23 %s |
| 4 | +// RUN: %clang_cc1 -verify=expected,cpp -pedantic -x c++ -Wno-c11-extensions %s |
| 5 | + |
| 6 | + |
| 7 | +/* WG14 N3353: Clang 21 |
| 8 | + * Obsolete implicitly octal literals and add delimited escape sequences |
| 9 | + */ |
| 10 | + |
| 11 | +constexpr int i = 0234; // c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 12 | +constexpr int j = 0o234; /* ext-warning {{octal integer literals are a C2y extension}} |
| 13 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 14 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 15 | + */ |
| 16 | + |
| 17 | +static_assert(i == 156); |
| 18 | +static_assert(j == 156); |
| 19 | + |
| 20 | +// Show that 0O is the same as Oo (tested above) |
| 21 | +static_assert(0O1234 == 0o1234); /* ext-warning 2 {{octal integer literals are a C2y extension}} |
| 22 | + cpp-warning 2 {{octal integer literals are a Clang extension}} |
| 23 | + compat-warning 2 {{octal integer literals are incompatible with standards before C2y}} |
| 24 | + */ |
| 25 | + |
| 26 | +// Show that you can use them with the usual integer literal suffixes. |
| 27 | +static_assert(0o234ull == 156); /* ext-warning {{octal integer literals are a C2y extension}} |
| 28 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 29 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 30 | + */ |
| 31 | + |
| 32 | +// And it's still a valid null pointer constant. |
| 33 | +static const void *ptr = 0o0; /* ext-warning {{octal integer literals are a C2y extension}} |
| 34 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 35 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 36 | + */ |
| 37 | + |
| 38 | +// Demonstrate that it works fine in the preprocessor. |
| 39 | +#if 0o123 != 0x53 /* ext-warning {{octal integer literals are a C2y extension}} |
| 40 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 41 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 42 | + */ |
| 43 | +#error "oh no, math stopped working!" |
| 44 | +#endif |
| 45 | + |
| 46 | +// 0 by itself is not deprecated, of course. |
| 47 | +int k = 0; |
| 48 | + |
| 49 | +// Make sure there are no surprises with auto and type deduction. Promotion |
| 50 | +// turns this into an 'int', and 'constexpr' implies 'const'. |
| 51 | +constexpr auto l = 0o1234567; /* ext-warning {{octal integer literals are a C2y extension}} |
| 52 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 53 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 54 | + */ |
| 55 | +static_assert(l == 0x53977); |
| 56 | +static_assert(__extension__ _Generic(typeof(0o1), typeof(01) : 1, default : 0)); /* c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 57 | + compat-warning {{passing a type argument as the first operand to '_Generic' is incompatible with C standards before C2y}} |
| 58 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 59 | + */ |
| 60 | +static_assert(__extension__ _Generic(typeof(l), const int : 1, default : 0)); // compat-warning {{passing a type argument as the first operand to '_Generic' is incompatible with C standards before C2y}} |
| 61 | + |
| 62 | +// Note that 0o by itself is an invalid literal. |
| 63 | +int m = 0o; /* expected-error {{invalid suffix 'o' on integer constant}} |
| 64 | + c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 65 | + */ |
| 66 | + |
| 67 | +// Ensure negation works as expected. |
| 68 | +static_assert(-0o1234 == -668); /* ext-warning {{octal integer literals are a C2y extension}} |
| 69 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 70 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 71 | + */ |
| 72 | + |
| 73 | +// FIXME: it would be better to not diagnose the compat and ext warnings when |
| 74 | +// the octal literal is invalid. |
| 75 | +// We expect diagnostics for non-octal digits. |
| 76 | +int n = 0o18; /* expected-error {{invalid digit '8' in octal constant}} |
| 77 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 78 | + ext-warning {{octal integer literals are a C2y extension}} |
| 79 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 80 | + */ |
| 81 | +int o1 = 0o8; /* expected-error {{invalid suffix 'o8' on integer constant}} |
| 82 | + c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 83 | + */ |
| 84 | +// FIXME: however, it matches the behavior for hex literals in terms of the |
| 85 | +// error reported. Unfortunately, we then go on to think 0 is an octal literal |
| 86 | +// without a prefix, which is again a bit confusing. |
| 87 | +int o2 = 0xG; /* expected-error {{invalid suffix 'xG' on integer constant}} |
| 88 | + c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 89 | + */ |
| 90 | + |
| 91 | +// Ensure digit separators work as expected. |
| 92 | +constexpr int p = 0o0'1'2'3'4'5'6'7; /* compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 93 | + ext-warning {{octal integer literals are a C2y extension}} |
| 94 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 95 | + */ |
| 96 | +static_assert(p == 01234567); // c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 97 | +int q = 0o'0'1; /* expected-error {{invalid suffix 'o'0'1' on integer constant}} |
| 98 | + c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 99 | + */ |
| 100 | + |
| 101 | +#define M 0o123 |
| 102 | +int r = M; /* compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 103 | + ext-warning {{octal integer literals are a C2y extension}} |
| 104 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 105 | + */ |
| 106 | + |
| 107 | +// Also, test delimited escape sequences. Note, this paper added a delimited |
| 108 | +// escape sequence for octal *and* hex. |
| 109 | +auto a = "\x{12}\o{12}\N{SPARKLES}"; /* compat-warning 2 {{delimited escape sequences are incompatible with C standards before C2y}} |
| 110 | + ext-warning 2 {{delimited escape sequences are a C2y extension}} |
| 111 | + cpp-warning 2 {{delimited escape sequences are a C++23 extension}} |
| 112 | + cpp-warning {{named escape sequences are a C++23 extension}} |
| 113 | + c-warning {{named escape sequences are a Clang extension}} |
| 114 | + */ |
| 115 | + |
| 116 | +#ifdef __cplusplus |
| 117 | +template <unsigned N> |
| 118 | +struct S { |
| 119 | + static_assert(N == 0o567); /* ext-warning {{octal integer literals are a C2y extension}} |
| 120 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 121 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 122 | + */ |
| 123 | +}; |
| 124 | + |
| 125 | +void foo() { |
| 126 | + S<0o567> s; /* ext-warning {{octal integer literals are a C2y extension}} |
| 127 | + cpp-warning {{octal integer literals are a Clang extension}} |
| 128 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 129 | + */ |
| 130 | +} |
| 131 | +#endif |
| 132 | + |
| 133 | +#line 0123 // expected-warning {{#line directive interprets number as decimal, not octal}} |
| 134 | +#line 0o123 // expected-error {{#line directive requires a simple digit sequence}} |
0 commit comments