[clang] 997ffce - [C23] Implement N2490, Remove trigraphs??!

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 21 10:21:09 PST 2024


Author: Aaron Ballman
Date: 2024-01-21T13:20:56-05:00
New Revision: 997ffce43c6d2d3f647eb091c732665049b1f47f

URL: https://github.com/llvm/llvm-project/commit/997ffce43c6d2d3f647eb091c732665049b1f47f
DIFF: https://github.com/llvm/llvm-project/commit/997ffce43c6d2d3f647eb091c732665049b1f47f.diff

LOG: [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.

Added: 
    clang/test/C/C2x/n2940.c

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Frontend/CompilerInvocation.cpp
    clang/test/C/drs/dr3xx.c
    clang/test/Preprocessor/ucn-pp-identifier.c
    clang/www/c_status.html

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8bb26fcae18d6b6..28fd79abbe782a4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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
 -------------------------------------------------

diff  --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index ed9cd1299eae282..7edea7798af1ef0 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -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);
 

diff  --git a/clang/test/C/C2x/n2940.c b/clang/test/C/C2x/n2940.c
new file mode 100644
index 000000000000000..ee92b6ec94a8c7c
--- /dev/null
+++ b/clang/test/C/C2x/n2940.c
@@ -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 at -1 {{trigraph ignored}} \
+   no-trigraphs-error at -1 {{expected identifier or '('}} \
+   trigraphs-warning at -1 {{trigraph converted to '#' character}}
+

diff  --git a/clang/test/C/drs/dr3xx.c b/clang/test/C/drs/dr3xx.c
index 4d6617184d9a4be..0f67470b013a5d0 100644
--- a/clang/test/C/drs/dr3xx.c
+++ b/clang/test/C/drs/dr3xx.c
@@ -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.
+ */

diff  --git a/clang/test/Preprocessor/ucn-pp-identifier.c b/clang/test/Preprocessor/ucn-pp-identifier.c
index 19cfc2589efddb7..5efcfe48f638ab4 100644
--- a/clang/test/Preprocessor/ucn-pp-identifier.c
+++ b/clang/test/Preprocessor/ucn-pp-identifier.c
@@ -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

diff  --git a/clang/www/c_status.html b/clang/www/c_status.html
index b9e0650ddca2429..3955a1d79639764 100644
--- a/clang/www/c_status.html
+++ b/clang/www/c_status.html
@@ -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>


        


More information about the cfe-commits mailing list