Skip to content

Commit 997ffce

Browse files
committed
[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.
1 parent bc82cfb commit 997ffce

File tree

6 files changed

+59
-17
lines changed

6 files changed

+59
-17
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ C23 Feature Support
271271
previously implemented allowing a label at the end of a compound statement,
272272
and now we've implemented allowing a label to be followed by a declaration
273273
instead of a statement.
274+
- Implemented
275+
`N2940 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2940.pdf>`_ which
276+
removes support for trigraphs in C23 and later. In earlier language modes,
277+
trigraphs remain enabled by default in conforming modes (e.g. ``-std=c17``)
278+
and disabled by default in GNU and Microsoft modes (e.g., ``-std=gnu17`` or
279+
``-fms-compatibility``). If needed, you can enable trigraphs by passing
280+
``-ftrigraphs``.
274281

275282
Non-comprehensive list of changes in this release
276283
-------------------------------------------------

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,7 +3480,8 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
34803480
Twine(Major) + "." + Twine(Minor) + "." + Twine(Subminor));
34813481
}
34823482

3483-
if ((!Opts.GNUMode && !Opts.MSVCCompat && !Opts.CPlusPlus17) || T.isOSzOS()) {
3483+
if ((!Opts.GNUMode && !Opts.MSVCCompat && !Opts.CPlusPlus17 && !Opts.C23) ||
3484+
T.isOSzOS()) {
34843485
if (!Opts.Trigraphs)
34853486
GenerateArg(Consumer, OPT_fno_trigraphs);
34863487
} else {
@@ -3876,10 +3877,11 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
38763877

38773878
// Mimicking gcc's behavior, trigraphs are only enabled if -trigraphs
38783879
// is specified, or -std is set to a conforming mode.
3879-
// Trigraphs are disabled by default in c++1z onwards.
3880+
// Trigraphs are disabled by default in C++17 and C23 onwards.
38803881
// For z/OS, trigraphs are enabled by default (without regard to the above).
38813882
Opts.Trigraphs =
3882-
(!Opts.GNUMode && !Opts.MSVCCompat && !Opts.CPlusPlus17) || T.isOSzOS();
3883+
(!Opts.GNUMode && !Opts.MSVCCompat && !Opts.CPlusPlus17 && !Opts.C23) ||
3884+
T.isOSzOS();
38833885
Opts.Trigraphs =
38843886
Args.hasFlag(OPT_ftrigraphs, OPT_fno_trigraphs, Opts.Trigraphs);
38853887

clang/test/C/C2x/n2940.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_cc1 -verify=no-trigraphs -std=c23 %s
2+
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu23 %s
3+
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu17 %s
4+
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu11 %s
5+
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu99 %s
6+
// RUN: %clang_cc1 -verify=no-trigraphs -std=gnu89 %s
7+
// RUN: %clang_cc1 -verify=trigraphs -std=c17 %s
8+
// RUN: %clang_cc1 -verify=trigraphs -std=c11 %s
9+
// RUN: %clang_cc1 -verify=trigraphs -std=c99 %s
10+
// RUN: %clang_cc1 -verify=trigraphs -std=c89 %s
11+
// RUN: %clang_cc1 -verify=trigraphs -std=c23 -ftrigraphs %s
12+
13+
/* WG14 N2940: Clang 18
14+
* Removing trigraphs??!
15+
*/
16+
17+
// Trigraphs are enabled by default in any conforming C mode before C23, but
18+
// are otherwise disabled (in all GNU modes, and in C23 or later).
19+
// The ??= trigraph, if supported, will become the # character, which is a null
20+
// preprocessor directive that does nothing.
21+
22+
??=
23+
// no-trigraphs-warning@-1 {{trigraph ignored}} \
24+
no-trigraphs-error@-1 {{expected identifier or '('}} \
25+
trigraphs-warning@-1 {{trigraph converted to '#' character}}
26+

clang/test/C/drs/dr3xx.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/* RUN: %clang_cc1 -std=c89 -fsyntax-only -Wvla -verify=expected,c89only -pedantic -Wno-c11-extensions %s
2-
RUN: %clang_cc1 -std=c99 -fsyntax-only -Wvla -verify=expected,c99andup -pedantic -Wno-c11-extensions %s
3-
RUN: %clang_cc1 -std=c11 -fsyntax-only -Wvla -verify=expected,c99andup -pedantic %s
4-
RUN: %clang_cc1 -std=c17 -fsyntax-only -Wvla -verify=expected,c99andup -pedantic %s
5-
RUN: %clang_cc1 -std=c2x -fsyntax-only -Wvla -verify=expected,c99andup -pedantic %s
1+
/* RUN: %clang_cc1 -std=c89 -fsyntax-only -Wvla -verify=expected,c89only,c17andearlier -pedantic -Wno-c11-extensions %s
2+
RUN: %clang_cc1 -std=c99 -fsyntax-only -Wvla -verify=expected,c99andup,c17andearlier -pedantic -Wno-c11-extensions %s
3+
RUN: %clang_cc1 -std=c11 -fsyntax-only -Wvla -verify=expected,c99andup,c17andearlier -pedantic %s
4+
RUN: %clang_cc1 -std=c17 -fsyntax-only -Wvla -verify=expected,c99andup,c17andearlier -pedantic %s
5+
RUN: %clang_cc1 -std=c2x -fsyntax-only -Wvla -verify=expected,c99andup,c23andup -pedantic %s
66
*/
77

88
/* The following are DRs which do not require tests to demonstrate
@@ -70,13 +70,6 @@
7070
#error "We definitely should not have gotten here"
7171
#endif
7272

73-
/* WG14 DR309: yes
74-
* Clarifying trigraph substitution
75-
*/
76-
int dr309??(1??) = { 1 }; /* expected-warning {{trigraph converted to '[' character}}
77-
expected-warning {{trigraph converted to ']' character}}
78-
*/
79-
8073
/* WG14 DR311: yes
8174
* Definition of variably modified types
8275
*/
@@ -292,3 +285,17 @@ void f(long double f,
292285
char (**a)[10 * sizeof f]) {
293286
_Static_assert(sizeof **a == sizeof(long double) * 10, "");
294287
}
288+
289+
/* WG14 DR309: yes
290+
* Clarifying trigraph substitution
291+
*/
292+
int dr309??(1??) = { 1 }; /* c17andearlier-warning {{trigraph converted to '[' character}}
293+
c17andearlier-warning {{trigraph converted to ']' character}}
294+
c23andup-warning 2 {{trigraph ignored}}
295+
c23andup-error {{expected ';' after top level declarator}}
296+
*/
297+
298+
/* NOTE: Due to interactions with the diagnostic system, dr309 should be the
299+
* last test case in this file because subsequent diagnostics may not be
300+
* generated as expected.
301+
*/

clang/test/Preprocessor/ucn-pp-identifier.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang_cc1 %s -fsyntax-only -std=c99 -pedantic -verify=expected,ext -Wundef -DTRIGRAPHS=1
2-
// RUN: %clang_cc1 %s -fsyntax-only -std=c2x -pedantic -verify=expected,ext -Wundef -DTRIGRAPHS=1
2+
// RUN: %clang_cc1 %s -fsyntax-only -std=c23 -pedantic -verify=expected,ext -Wundef -ftrigraphs -DTRIGRAPHS=1
33
// RUN: %clang_cc1 %s -fsyntax-only -x c++ -pedantic -verify=expected,ext -Wundef -fno-trigraphs
44
// RUN: %clang_cc1 %s -fsyntax-only -x c++ -std=c++23 -pedantic -ftrigraphs -DTRIGRAPHS=1 -verify=expected,cxx23 -Wundef -Wpre-c++23-compat
55
// RUN: %clang_cc1 %s -fsyntax-only -x c++ -pedantic -verify=expected,ext -Wundef -ftrigraphs -DTRIGRAPHS=1

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ <h2 id="c2x">C23 implementation status</h2>
11561156
<tr>
11571157
<td>Remove trigraphs??!</td>
11581158
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2940.pdf">N2940</a></td>
1159-
<td class="full" align="center">Yes</td>
1159+
<td class="unreleased" align="center">Clang 18</td>
11601160
</tr>
11611161
<tr>
11621162
<td>Improved normal enumerations</td>

0 commit comments

Comments
 (0)