Skip to content

Commit

Permalink
[C23] Implement N2490, Remove trigraphs??!
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AaronBallman committed Jan 21, 2024
1 parent bc82cfb commit 997ffce
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 17 deletions.
7 changes: 7 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------------------------
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
26 changes: 26 additions & 0 deletions clang/test/C/C2x/n2940.c
Original file line number Diff line number Diff line change
@@ -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}}

31 changes: 19 additions & 12 deletions clang/test/C/drs/dr3xx.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*/
2 changes: 1 addition & 1 deletion clang/test/Preprocessor/ucn-pp-identifier.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion clang/www/c_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ <h2 id="c2x">C23 implementation status</h2>
<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>
Expand Down

1 comment on commit 997ffce

@dwblaikie
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

End of an era... I still remember coming across this http://www.gotw.ca/gotw/086.htm back in the day.

Please sign in to comment.