|
| 1 | +// RUN: %clang_cc1 -verify=expected,c2y -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 -pedantic -std=c23 %s |
| 4 | +// RUN: %clang_cc1 -verify=expected,ext -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 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 14 | + */ |
| 15 | + |
| 16 | +static_assert(i == 156); |
| 17 | +static_assert(j == 156); |
| 18 | + |
| 19 | +// Show that 0O is the same as Oo (tested above) |
| 20 | +static_assert(0O1234 == 0o1234); /* ext-warning 2 {{octal integer literals are a C2y extension}} |
| 21 | + compat-warning 2 {{octal integer literals are incompatible with standards before C2y}} |
| 22 | + */ |
| 23 | + |
| 24 | +// Demonstrate that it works fine in the preprocessor. |
| 25 | +#if 0o123 != 0x53 /* ext-warning {{octal integer literals are a C2y extension}} |
| 26 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 27 | + */ |
| 28 | +#error "oh no, math stopped working!" |
| 29 | +#endif |
| 30 | + |
| 31 | +// 0 by itself is not deprecated, of course. |
| 32 | +int k = 0; |
| 33 | + |
| 34 | +// Make sure there are no surprises with auto and type deduction. Promotion |
| 35 | +// turns this into an 'int', and 'constexpr' implies 'const'. |
| 36 | +constexpr auto l = 0o1234567; /* ext-warning {{octal integer literals are a C2y extension}} |
| 37 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 38 | + */ |
| 39 | +static_assert(l == 0x53977); |
| 40 | +static_assert(__extension__ _Generic(typeof(0o1), typeof(01) : 1, default : 0)); /* c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 41 | + compat-warning {{passing a type argument as the first operand to '_Generic' is incompatible with C standards before C2y}} |
| 42 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 43 | + */ |
| 44 | +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}} |
| 45 | + |
| 46 | +// Note that 0o by itself is an invalid literal. |
| 47 | +int m = 0o; /* expected-error {{invalid suffix 'o' on integer constant}} |
| 48 | + c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 49 | + */ |
| 50 | + |
| 51 | +// Ensure negation works as expected. |
| 52 | +static_assert(-0o1234 == -668); /* ext-warning {{octal integer literals are a C2y extension}} |
| 53 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 54 | + */ |
| 55 | + |
| 56 | +// FIXME: it would be better to not diagnose the compat and ext warnings when |
| 57 | +// the octal literal is invalid. |
| 58 | +// We expect diagnostics for non-octal digits. |
| 59 | +int n = 0o18; /* expected-error {{invalid digit '8' in octal constant}} |
| 60 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 61 | + ext-warning {{octal integer literals are a C2y extension}} |
| 62 | + */ |
| 63 | +int o1 = 0o8; /* expected-error {{invalid suffix 'o8' on integer constant}} |
| 64 | + c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 65 | + */ |
| 66 | +// FIXME: however, it matches the behavior for hex literals in terms of the |
| 67 | +// error reported. Unfortunately, we then go on to think 0 is an octal literal |
| 68 | +// without a prefix, which is again a bit confusing. |
| 69 | +int o2 = 0xG; /* expected-error {{invalid suffix 'xG' on integer constant}} |
| 70 | + c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 71 | + */ |
| 72 | + |
| 73 | +// Ensure digit separators work as expected. |
| 74 | +constexpr int p = 0o0'1'2'3'4'5'6'7; /* compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 75 | + ext-warning {{octal integer literals are a C2y extension}} |
| 76 | + */ |
| 77 | +static_assert(p == 01234567); // c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 78 | +int q = 0o'0'1; /* expected-error {{invalid suffix 'o'0'1' on integer constant}} |
| 79 | + c2y-warning {{octal literals without a '0o' prefix are deprecated}} |
| 80 | + */ |
| 81 | + |
| 82 | +#ifdef __cplusplus |
| 83 | +template <unsigned N> |
| 84 | +struct S { |
| 85 | + static_assert(N == 0o567); /* ext-warning {{octal integer literals are a C2y extension}} |
| 86 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 87 | + */ |
| 88 | +}; |
| 89 | + |
| 90 | +void foo() { |
| 91 | + S<0o567> s; /* ext-warning {{octal integer literals are a C2y extension}} |
| 92 | + compat-warning {{octal integer literals are incompatible with standards before C2y}} |
| 93 | + */ |
| 94 | +} |
| 95 | +#endif |
0 commit comments