Bug 102613 - [C++23] P2246R1 - Character encoding of diagnostic text
Summary: [C++23] P2246R1 - Character encoding of diagnostic text
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: 16.0
Assignee: Jakub Jelinek
URL:
Keywords:
Depends on:
Blocks: c++23-core
  Show dependency tree / graph
 
Reported: 2021-10-05 16:34 UTC by Marek Polacek
Modified: 2026-01-28 09:28 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2025-11-14 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marek Polacek 2021-10-05 16:34:21 UTC
See https://wg21.link/p2246r1

This might be a noop, but we should include some tests probably.
Comment 1 Jakub Jelinek 2023-08-22 15:30:31 UTC
Is there anything we need to do here?
From what I can see, e.g. for static assertions which is one of the two changed points
we reencode the message from -finput-charset= charset if supplied to UTF-8 and then emit the UTF-8 chars to the terminal regardless of locale's charset unobfuscated, so say given:
static_assert (false, "��c");
and -finput-charset=ISO-8859-1
00.C:1:16: error: static assertion failed: áæc
    1 | static_assert (false, "áæc");
      |                ^~~~~
is emitted, while without that -finput-charset= (i.e. when it is invalid UTF-8), we emit
00.C:1:16: error: static assertion failed: ��c
    1 | static_assert (false, "��c");
      |                ^~~~~
On valid UTF-8 emoji:
static_assert (false, "😀");
we emit
01.C:1:16: error: static assertion failed: 😀
    1 | static_assert (false, "😀");
      |                ^~~~~
and with -finput-charset=ISO-8859-1
01.C:1:16: error: static assertion failed: ð
    1 | static_assert (false, "ð");
      |                ^~~~~
Not really sure what to do for our testsuite for this though, because while we can use -finput-charset= in dg-options, we have no control on what the pseudo terminal encoding actually is.
Comment 2 Jakub Jelinek 2023-08-22 15:33:08 UTC
Looking at what clang++ does, they emit
01.C:1:1: error: static_assert failed "\360\237\230\200"
etc. and don't really support -finput-charset=ISO-8859-1 etc. (-finput-charset=utf-8 works).
Comment 3 Drea Pinski 2025-11-14 20:04:56 UTC
.
Comment 4 GCC Commits 2026-01-28 09:26:40 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:bb5ebc937329196eca404385b4388352ae568a86

commit r16-7104-gbb5ebc937329196eca404385b4388352ae568a86
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 28 10:23:20 2026 +0100

    c++: Implement C++23 P2246R1 - Character encoding of diagnostic text
    
    The following patch attempts to implement the C++23 P2246R1
    Character encoding of diagnostic text paper.
    Initially I thought there is nothing to do, but this patch shows
    that there is (and I wonder if we shouldn't backport it to release
    branches).  Though the patch is on top of the cpp_translate_string
    libcpp addition from the reflection patchset (though, that is
    quite small change that could be backported too).
    
    We have various different encodings in play in GCC.
    There is -finput-charset= defaulting to SOURCE_CHARSET, which is
    almost always UTF-8 (but in theory could be UTF-EBCDIC if that really
    works).  libcpp converts source from the input charset to SOURCE_CHARSET
    initially.  And then we have -fexec-charset=, again defaulting to
    SOURCE_CHARSET, -fwide-exec-charset=, then UTF-8, UTF-16 and UTF-32
    for u8, u and U string literals and constants and finally user uses
    some character set in the terminal in which gcc is running.
    
    Now, I think we mostly just emit diagnostics in SOURCE_CHARSET,
    there is identifier_to_locale function which uses UCNs if LC_CTYPE
    CODESET is not UTF-8-ish, but I think we don't use it all the time.
    Even then, there is really no support for outputing from SOURCE_CHARSET
    UTF-8 to non-ASCII compatible terminal charsets.
    So for now let's pretend that we are emitting diagnostics to UTF-8
    capable terminal.
    
    When reporting errors about identifiers in the source (which are in
    SOURCE_CHARSET), we just emit those.  The paper talks about
    deprecated & nodiscard attribute msgs, static_assert, #error (and for
    C++26 it would talk about #warning, delete (reason) and static_assert
    with constexpr user messages).  #error/#warning works fine on UTF-8
    terminals, delete (reason) too (we don't translate the string literal
    from SOURCE_CHARSET to exec-charset in that case), static_assert
    with a string literal too (again, notranslate), __attribute__ form
    of deprecated attribute too (again, !parser->translate_strings_p).
    What doesn't work properly are C++11 attributes (standard or gnu::),
    we do translate those to exec charset, except for C++26
    standard deprecated/nodiscard (which aren't translated).  And static_assert
    with user messages doesn't work, those really have to be in exec-charset
    because we have no control on how user constructs the messages during
    constexpr evaluation.
    
    So, this patch for C++11 attributes if they have the first argument
    of a CPP_STRING temporarily disables translation of that string, which
    fixes [[gnu::deprecated ("foo")]], [[gnu::unavailable ("foo")]]
    and for C++ < 26 also [[deprecated ("foo")]] and [[nodiscard ("foo")]].
    And another change is convert back from exec-charset to SOURCE_CHARSET
    the custom user static_assert messages (and also inline asm strings).
    For diagnostics without this patch worst case we show garbage, but
    for inline asm we actually then fail to assemble stuff when users
    use the constexpr created string views with non-ASCII exec charsets.
    
    2026-01-28  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/102613
            * parser.cc: Implement C++23 P2246R1 - Character encoding of
            diagnostic text.
            (cp_parser_parenthesized_expression_list): For std attribute
            argument where the first argument is CPP_STRING, ensure the
            string is not translated.
            * semantics.cc: Include c-family/c-pragma.h.
            (cexpr_str::extract): Use cpp_translate_string to translate
            string from ordinary literal encoding to SOURCE_CHARSET.
    
            * g++.dg/cpp1z/constexpr-asm-6.C: New test.
            * g++.dg/cpp23/charset2.C: New test.
            * g++.dg/cpp23/charset3.C: New test.
            * g++.dg/cpp23/charset4.C: New test.
            * g++.dg/cpp23/charset5.C: New test.
Comment 5 Jakub Jelinek 2026-01-28 09:28:06 UTC
Implemented for GCC 16.