171 lines · c
1// RUN: %clang_cc1 -verify=expected,c2y,c -pedantic -std=c2y %s2// RUN: %clang_cc1 -verify=expected,c2y,compat -Wpre-c2y-compat -std=c2y %s3// RUN: %clang_cc1 -verify=expected,ext,c -pedantic -std=c23 %s4// RUN: %clang_cc1 -verify=expected,cpp -pedantic -x c++ -Wno-c11-extensions %s5 6 7/* WG14 N3353: Clang 218 * Obsolete implicitly octal literals and add delimited escape sequences9 */10 11constexpr int i = 0234; // c2y-warning {{octal literals without a '0o' prefix are deprecated}}12constexpr 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 17static_assert(i == 156);18static_assert(j == 156);19 20// Show that 0O is the same as Oo (tested above)21static_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.27static_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.33static 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#endif45 46// 0 by itself is not deprecated, of course.47int k1 = 0;48unsigned int k2 = 0u;49long k3 = 0l;50unsigned long k4 = 0ul;51long long k5 = 0ll;52unsigned long long k6 = 0ull;53 54// Test a preprocessor use of 0 by itself, which is also not deprecated.55#if 056#endif57 58// Make sure there are no surprises with auto and type deduction. Promotion59// turns this into an 'int', and 'constexpr' implies 'const'.60constexpr auto l = 0o1234567; /* ext-warning {{octal integer literals are a C2y extension}}61 cpp-warning {{octal integer literals are a Clang extension}}62 compat-warning {{octal integer literals are incompatible with standards before C2y}}63 */64static_assert(l == 0x53977);65static_assert(__extension__ _Generic(typeof(0o1), typeof(01) : 1, default : 0)); /* c2y-warning {{octal literals without a '0o' prefix are deprecated}}66 compat-warning {{passing a type argument as the first operand to '_Generic' is incompatible with C standards before C2y}}67 compat-warning {{octal integer literals are incompatible with standards before C2y}}68 */69static_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}}70 71// Note that 0o by itself is an invalid literal.72int m = 0o; /* expected-error {{invalid suffix 'o' on integer constant}}73 */74 75// Ensure negation works as expected.76static_assert(-0o1234 == -668); /* ext-warning {{octal integer literals are a C2y extension}}77 cpp-warning {{octal integer literals are a Clang extension}}78 compat-warning {{octal integer literals are incompatible with standards before C2y}}79 */80 81// FIXME: it would be better to not diagnose the compat and ext warnings when82// the octal literal is invalid.83// We expect diagnostics for non-octal digits.84int n = 0o18; /* expected-error {{invalid digit '8' in octal constant}}85 compat-warning {{octal integer literals are incompatible with standards before C2y}}86 ext-warning {{octal integer literals are a C2y extension}}87 cpp-warning {{octal integer literals are a Clang extension}}88 */89int o1 = 0o8; /* expected-error {{invalid suffix 'o8' on integer constant}}90 */91// FIXME: however, it matches the behavior for hex literals in terms of the92// error reported. Unfortunately, we then go on to think 0 is an octal literal93// without a prefix, which is again a bit confusing.94int o2 = 0xG; /* expected-error {{invalid suffix 'xG' on integer constant}}95 */96 97// Show that floating-point suffixes on octal literals are rejected.98auto f1 = 0o0.; /* expected-error {{invalid suffix '.' on integer constant}}99 compat-warning {{octal integer literals are incompatible with standards before C2y}}100 ext-warning {{octal integer literals are a C2y extension}}101 cpp-warning {{octal integer literals are a Clang extension}}102 */103auto f2 = 0o0.1; /* expected-error {{invalid suffix '.1' on integer constant}}104 compat-warning {{octal integer literals are incompatible with standards before C2y}}105 ext-warning {{octal integer literals are a C2y extension}}106 cpp-warning {{octal integer literals are a Clang extension}}107 */108auto f3 = 0o0e1; /* expected-error {{invalid suffix 'e1' on integer constant}}109 compat-warning {{octal integer literals are incompatible with standards before C2y}}110 ext-warning {{octal integer literals are a C2y extension}}111 cpp-warning {{octal integer literals are a Clang extension}}112 */113auto f4 = 0o0E1; /* expected-error {{invalid suffix 'E1' on integer constant}}114 compat-warning {{octal integer literals are incompatible with standards before C2y}}115 ext-warning {{octal integer literals are a C2y extension}}116 cpp-warning {{octal integer literals are a Clang extension}}117 */118 119// Show that valid floating-point literals with a leading 0 do not produce octal-related warnings.120auto f5 = 0.;121auto f7 = 00.;122auto f8 = 01.;123auto f9 = 0e1;124auto f10 = 0E1;125auto f11 = 00e1;126auto f12 = 00E1;127 128// Ensure digit separators work as expected.129constexpr int p = 0o0'1'2'3'4'5'6'7; /* compat-warning {{octal integer literals are incompatible with standards before C2y}}130 ext-warning {{octal integer literals are a C2y extension}}131 cpp-warning {{octal integer literals are a Clang extension}}132 */133static_assert(p == 01234567); // c2y-warning {{octal literals without a '0o' prefix are deprecated}}134int q = 0o'0'1; /* expected-error {{invalid suffix 'o'0'1' on integer constant}}135 */136 137#define M 0o123138int r = M; /* compat-warning {{octal integer literals are incompatible with standards before C2y}}139 ext-warning {{octal integer literals are a C2y extension}}140 cpp-warning {{octal integer literals are a Clang extension}}141 */142 143// Also, test delimited escape sequences. Note, this paper added a delimited144// escape sequence for octal *and* hex.145auto a = "\x{12}\o{12}\N{SPARKLES}"; /* compat-warning 2 {{delimited escape sequences are incompatible with C standards before C2y}}146 ext-warning 2 {{delimited escape sequences are a C2y extension}}147 cpp-warning 2 {{delimited escape sequences are a C++23 extension}}148 cpp-warning {{named escape sequences are a C++23 extension}}149 c-warning {{named escape sequences are a Clang extension}}150 */151 152#ifdef __cplusplus153template <unsigned N>154struct S {155 static_assert(N == 0o567); /* ext-warning {{octal integer literals are a C2y extension}}156 cpp-warning {{octal integer literals are a Clang extension}}157 compat-warning {{octal integer literals are incompatible with standards before C2y}}158 */159};160 161void foo() {162 S<0o567> s; /* ext-warning {{octal integer literals are a C2y extension}}163 cpp-warning {{octal integer literals are a Clang extension}}164 compat-warning {{octal integer literals are incompatible with standards before C2y}}165 */166}167#endif168 169#line 0123 // expected-warning {{#line directive interprets number as decimal, not octal}}170#line 0o123 // expected-error {{#line directive requires a simple digit sequence}}171