[C23] Implement N2490, Remove trigraphs??!

This follows the same implementation logic as with C++ and is
compatible with the GCC behavior in C.

Trigraphs are enabled by default in -std=c* conformance modes before
C23, but are disabled in GNU and Microsoft modes as well as in C23 or
later.
This commit is contained in:
Aaron Ballman 2024-01-21 13:18:51 -05:00
parent bc82cfb38d
commit 997ffce43c
6 changed files with 59 additions and 17 deletions

View File

@ -271,6 +271,13 @@ C23 Feature Support
previously implemented allowing a label at the end of a compound statement,
and now we've implemented allowing a label to be followed by a declaration
instead of a statement.
- Implemented
`N2940 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2940.pdf>`_ which
removes support for trigraphs in C23 and later. In earlier language modes,
trigraphs remain enabled by default in conforming modes (e.g. ``-std=c17``)
and disabled by default in GNU and Microsoft modes (e.g., ``-std=gnu17`` or
``-fms-compatibility``). If needed, you can enable trigraphs by passing
``-ftrigraphs``.
Non-comprehensive list of changes in this release
-------------------------------------------------

View File

@ -3480,7 +3480,8 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
Twine(Major) + "." + Twine(Minor) + "." + Twine(Subminor));
}
if ((!Opts.GNUMode && !Opts.MSVCCompat && !Opts.CPlusPlus17) || T.isOSzOS()) {
if ((!Opts.GNUMode && !Opts.MSVCCompat && !Opts.CPlusPlus17 && !Opts.C23) ||
T.isOSzOS()) {
if (!Opts.Trigraphs)
GenerateArg(Consumer, OPT_fno_trigraphs);
} else {
@ -3876,10 +3877,11 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
// Mimicking gcc's behavior, trigraphs are only enabled if -trigraphs
// is specified, or -std is set to a conforming mode.
// Trigraphs are disabled by default in c++1z onwards.
// Trigraphs are disabled by default in C++17 and C23 onwards.
// For z/OS, trigraphs are enabled by default (without regard to the above).
Opts.Trigraphs =
(!Opts.GNUMode && !Opts.MSVCCompat && !Opts.CPlusPlus17) || T.isOSzOS();
(!Opts.GNUMode && !Opts.MSVCCompat && !Opts.CPlusPlus17 && !Opts.C23) ||
T.isOSzOS();
Opts.Trigraphs =
Args.hasFlag(OPT_ftrigraphs, OPT_fno_trigraphs, Opts.Trigraphs);

26
clang/test/C/C2x/n2940.c Normal file
View File

@ -0,0 +1,26 @@
// RUN: %clang_cc1 -verify=no-trigraphs -std=c23 %s
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu23 %s
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu17 %s
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu11 %s
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu99 %s
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu89 %s
// RUN: %clang_cc1 -verify=trigraphs -std=c17 %s
// RUN: %clang_cc1 -verify=trigraphs -std=c11 %s
// RUN: %clang_cc1 -verify=trigraphs -std=c99 %s
// RUN: %clang_cc1 -verify=trigraphs -std=c89 %s
// RUN: %clang_cc1 -verify=trigraphs -std=c23 -ftrigraphs %s
/* WG14 N2940: Clang 18
* Removing trigraphs??!
*/
// Trigraphs are enabled by default in any conforming C mode before C23, but
// are otherwise disabled (in all GNU modes, and in C23 or later).
// The ??= trigraph, if supported, will become the # character, which is a null
// preprocessor directive that does nothing.
??=
// no-trigraphs-warning@-1 {{trigraph ignored}} \
no-trigraphs-error@-1 {{expected identifier or '('}} \
trigraphs-warning@-1 {{trigraph converted to '#' character}}

View File

@ -1,8 +1,8 @@
/* RUN: %clang_cc1 -std=c89 -fsyntax-only -Wvla -verify=expected,c89only -pedantic -Wno-c11-extensions %s
RUN: %clang_cc1 -std=c99 -fsyntax-only -Wvla -verify=expected,c99andup -pedantic -Wno-c11-extensions %s
RUN: %clang_cc1 -std=c11 -fsyntax-only -Wvla -verify=expected,c99andup -pedantic %s
RUN: %clang_cc1 -std=c17 -fsyntax-only -Wvla -verify=expected,c99andup -pedantic %s
RUN: %clang_cc1 -std=c2x -fsyntax-only -Wvla -verify=expected,c99andup -pedantic %s
/* RUN: %clang_cc1 -std=c89 -fsyntax-only -Wvla -verify=expected,c89only,c17andearlier -pedantic -Wno-c11-extensions %s
RUN: %clang_cc1 -std=c99 -fsyntax-only -Wvla -verify=expected,c99andup,c17andearlier -pedantic -Wno-c11-extensions %s
RUN: %clang_cc1 -std=c11 -fsyntax-only -Wvla -verify=expected,c99andup,c17andearlier -pedantic %s
RUN: %clang_cc1 -std=c17 -fsyntax-only -Wvla -verify=expected,c99andup,c17andearlier -pedantic %s
RUN: %clang_cc1 -std=c2x -fsyntax-only -Wvla -verify=expected,c99andup,c23andup -pedantic %s
*/
/* The following are DRs which do not require tests to demonstrate
@ -70,13 +70,6 @@
#error "We definitely should not have gotten here"
#endif
/* WG14 DR309: yes
* Clarifying trigraph substitution
*/
int dr309??(1??) = { 1 }; /* expected-warning {{trigraph converted to '[' character}}
expected-warning {{trigraph converted to ']' character}}
*/
/* WG14 DR311: yes
* Definition of variably modified types
*/
@ -292,3 +285,17 @@ void f(long double f,
char (**a)[10 * sizeof f]) {
_Static_assert(sizeof **a == sizeof(long double) * 10, "");
}
/* WG14 DR309: yes
* Clarifying trigraph substitution
*/
int dr309??(1??) = { 1 }; /* c17andearlier-warning {{trigraph converted to '[' character}}
c17andearlier-warning {{trigraph converted to ']' character}}
c23andup-warning 2 {{trigraph ignored}}
c23andup-error {{expected ';' after top level declarator}}
*/
/* NOTE: Due to interactions with the diagnostic system, dr309 should be the
* last test case in this file because subsequent diagnostics may not be
* generated as expected.
*/

View File

@ -1,5 +1,5 @@
// RUN: %clang_cc1 %s -fsyntax-only -std=c99 -pedantic -verify=expected,ext -Wundef -DTRIGRAPHS=1
// RUN: %clang_cc1 %s -fsyntax-only -std=c2x -pedantic -verify=expected,ext -Wundef -DTRIGRAPHS=1
// RUN: %clang_cc1 %s -fsyntax-only -std=c23 -pedantic -verify=expected,ext -Wundef -ftrigraphs -DTRIGRAPHS=1
// RUN: %clang_cc1 %s -fsyntax-only -x c++ -pedantic -verify=expected,ext -Wundef -fno-trigraphs
// RUN: %clang_cc1 %s -fsyntax-only -x c++ -std=c++23 -pedantic -ftrigraphs -DTRIGRAPHS=1 -verify=expected,cxx23 -Wundef -Wpre-c++23-compat
// RUN: %clang_cc1 %s -fsyntax-only -x c++ -pedantic -verify=expected,ext -Wundef -ftrigraphs -DTRIGRAPHS=1

View File

@ -1156,7 +1156,7 @@ conformance.</p>
<tr>
<td>Remove trigraphs??!</td>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2940.pdf">N2940</a></td>
<td class="full" align="center">Yes</td>
<td class="unreleased" align="center">Clang 18</td>
</tr>
<tr>
<td>Improved normal enumerations</td>