Skip to content

Commit 8e24bc0

Browse files
authored
[C23] Do not diagnose binary literals as an extension (#81658)
We previously would diagnose them as a GNU extension in C mode, but they are now a feature of C23. The -Wgnu-binary-literal warning group no longer controls any diagnostics as this is no longer a GNU extension. The warning group is retained as a noop to help avoid "unknown warning" diagnostics. This also adds the companion compatibility warning which existed for C++ but not for C. Fixes #72017
1 parent bd8fcf7 commit 8e24bc0

File tree

6 files changed

+46
-22
lines changed

6 files changed

+46
-22
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ Clang Frontend Potentially Breaking Changes
5656
``ArrayRef<TemplateArgument>`` reduces AST memory usage by 0.4% when compiling clang, and is
5757
expected to show similar improvements on other workloads.
5858

59+
- The ``-Wgnu-binary-literal`` diagnostic group no longer controls any
60+
diagnostics. Binary literals are no longer a GNU extension, they're now a C23
61+
extension which is controlled via ``-pedantic`` or ``-Wc23-extensions``. Use
62+
of ``-Wno-gnu-binary-literal`` will no longer silence this pedantic warning,
63+
which may break existing uses with ``-Werror``.
64+
5965
Target OS macros extension
6066
^^^^^^^^^^^^^^^^^^^^^^^^^^
6167
A new Clang extension (see :ref:`here <target_os_detail>`) is enabled for
@@ -113,6 +119,8 @@ C Language Changes
113119

114120
C23 Feature Support
115121
^^^^^^^^^^^^^^^^^^^
122+
- No longer diagnose use of binary literals as an extension in C23 mode. Fixes
123+
`#72017 <https://github.com/llvm/llvm-project/issues/72017>`_.
116124

117125
Non-comprehensive list of changes in this release
118126
-------------------------------------------------

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ def DeprecatedModuleDotMap : DiagGroup<"deprecated-module-dot-map">;
4444
def FrameworkHdrAtImport : DiagGroup<"atimport-in-framework-header">;
4545
def CXX14BinaryLiteral : DiagGroup<"c++14-binary-literal">;
4646
def CXXPre14CompatBinaryLiteral : DiagGroup<"c++98-c++11-compat-binary-literal">;
47-
def GNUBinaryLiteral : DiagGroup<"gnu-binary-literal">;
4847
def BinaryLiteral : DiagGroup<"binary-literal", [CXX14BinaryLiteral,
49-
CXXPre14CompatBinaryLiteral,
50-
GNUBinaryLiteral]>;
48+
CXXPre14CompatBinaryLiteral]>;
5149
def GNUCompoundLiteralInitializer : DiagGroup<"gnu-compound-literal-initializer">;
5250
def SingleBitBitFieldConstantConversion :
5351
DiagGroup<"single-bit-bitfield-constant-conversion">;
@@ -1176,10 +1174,13 @@ def C23 : DiagGroup<"c23-extensions">;
11761174

11771175
def : DiagGroup<"c2x-extensions", [C23]>;
11781176

1177+
// Previously supported warning group which is no longer pertinent as binary
1178+
// literals are a C++14 and C23 extension now instead of a GNU extension.
1179+
def GNUBinaryLiteral : DiagGroup<"gnu-binary-literal">;
1180+
11791181
// A warning group for warnings about GCC extensions.
11801182
def GNU : DiagGroup<"gnu", [GNUAlignofExpression, GNUAnonymousStruct,
1181-
GNUAutoType,
1182-
GNUBinaryLiteral, GNUCaseRange,
1183+
GNUAutoType, GNUBinaryLiteral, GNUCaseRange,
11831184
GNUComplexInteger, GNUCompoundLiteralInitializer,
11841185
GNUConditionalOmittedOperand, GNUDesignator,
11851186
GNUEmptyStruct,

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ def warn_cxx17_hex_literal : Warning<
246246
"C++ standards before C++17">,
247247
InGroup<CXXPre17CompatPedantic>, DefaultIgnore;
248248
def ext_binary_literal : Extension<
249-
"binary integer literals are a GNU extension">, InGroup<GNUBinaryLiteral>;
249+
"binary integer literals are a C23 extension">, InGroup<C23>;
250+
def warn_c23_compat_binary_literal : Warning<
251+
"binary integer literals are incompatible with C standards before C23">,
252+
InGroup<CPre23Compat>, DefaultIgnore;
250253
def ext_binary_literal_cxx14 : Extension<
251254
"binary integer literals are a C++14 extension">, InGroup<CXX14BinaryLiteral>;
252255
def warn_cxx11_compat_binary_literal : Warning<

clang/lib/Lex/LiteralSupport.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,11 +1358,17 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
13581358

13591359
// Handle simple binary numbers 0b01010
13601360
if ((c1 == 'b' || c1 == 'B') && (s[1] == '0' || s[1] == '1')) {
1361-
// 0b101010 is a C++1y / GCC extension.
1362-
Diags.Report(TokLoc, LangOpts.CPlusPlus14
1363-
? diag::warn_cxx11_compat_binary_literal
1364-
: LangOpts.CPlusPlus ? diag::ext_binary_literal_cxx14
1365-
: diag::ext_binary_literal);
1361+
// 0b101010 is a C++14 and C23 extension.
1362+
unsigned DiagId;
1363+
if (LangOpts.CPlusPlus14)
1364+
DiagId = diag::warn_cxx11_compat_binary_literal;
1365+
else if (LangOpts.C23)
1366+
DiagId = diag::warn_c23_compat_binary_literal;
1367+
else if (LangOpts.CPlusPlus)
1368+
DiagId = diag::ext_binary_literal_cxx14;
1369+
else
1370+
DiagId = diag::ext_binary_literal;
1371+
Diags.Report(TokLoc, DiagId);
13661372
++s;
13671373
assert(s < ThisTokEnd && "didn't maximally munch?");
13681374
radix = 2;

clang/test/C/C2x/n2549.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -verify -std=c23 %s
2+
// RUN: %clang_cc1 -verify=pedantic -std=c17 -pedantic %s
3+
// RUN: %clang_cc1 -verify=compat -std=c23 -Wpre-c23-compat %s
4+
5+
// expected-no-diagnostics
6+
7+
/* WG14 N2549: Clang 9
8+
* Binary literals
9+
*/
10+
11+
int i = 0b01; /* pedantic-warning {{binary integer literals are a C23 extension}}
12+
compat-warning {{binary integer literals are incompatible with C standards before C23}}
13+
*/
14+

clang/test/Lexer/gnu-flags.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify %s -DNONE
2-
// RUN: %clang_cc1 -fsyntax-only -verify %s -DALL -Wgnu
2+
// RUN: %clang_cc1 -fsyntax-only -verify %s -DALL -Wgnu
33
// RUN: %clang_cc1 -fsyntax-only -verify %s -DALL \
44
// RUN: -Wgnu-zero-variadic-macro-arguments \
5-
// RUN: -Wgnu-imaginary-constant -Wgnu-binary-literal -Wgnu-zero-line-directive
5+
// RUN: -Wgnu-imaginary-constant -Wgnu-zero-line-directive
66
// RUN: %clang_cc1 -fsyntax-only -verify %s -DNONE -Wgnu \
77
// RUN: -Wno-gnu-zero-variadic-macro-arguments \
8-
// RUN: -Wno-gnu-imaginary-constant -Wno-gnu-binary-literal -Wno-gnu-zero-line-directive
8+
// RUN: -Wno-gnu-imaginary-constant -Wno-gnu-zero-line-directive
99
// Additional disabled tests:
1010
// %clang_cc1 -fsyntax-only -verify %s -DZEROARGS -Wgnu-zero-variadic-macro-arguments
1111
// %clang_cc1 -fsyntax-only -verify %s -DIMAGINARYCONST -Wgnu-imaginary-constant
12-
// %clang_cc1 -fsyntax-only -verify %s -DBINARYLITERAL -Wgnu-binary-literal
1312
// %clang_cc1 -fsyntax-only -verify %s -DLINE0 -Wgnu-zero-line-directive
1413

1514
#if NONE
@@ -38,13 +37,6 @@ void foo( const char* c )
3837
float _Complex c = 1.if;
3938

4039

41-
#if ALL || BINARYLITERAL
42-
// expected-warning@+3 {{binary integer literals are a GNU extension}}
43-
#endif
44-
45-
int b = 0b0101;
46-
47-
4840
// This case is handled differently because lit has a bug whereby #line 0 is reported to be on line 4294967295
4941
// http://llvm.org/bugs/show_bug.cgi?id=16952
5042
#if ALL || LINE0

0 commit comments

Comments
 (0)