Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -101,6 +101,7 @@ ------------------- - Implemented `WG14 N2674 The noreturn attribute `_. +- Implemented `WG14 N2418 Adding the u8 character prefix `_. C++ Language Changes in Clang ----------------------------- Index: clang/lib/Lex/Lexer.cpp =================================================================== --- clang/lib/Lex/Lexer.cpp +++ clang/lib/Lex/Lexer.cpp @@ -3459,7 +3459,10 @@ MIOpt.ReadToken(); return LexNumericConstant(Result, CurPtr); - case 'u': // Identifier (uber) or C11/C++11 UTF-8 or UTF-16 string literal + // Identifer (e.g., uber), or + // UTF-8 (C2x/C++17) or UTF-16 (C11/C++11) character literal, or + // UTF-8 or UTF-16 string literal (C11/C++11). + case 'u': // Notify MIOpt that we read a non-whitespace/non-comment token. MIOpt.ReadToken(); @@ -3493,7 +3496,7 @@ ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), SizeTmp2, Result), tok::utf8_string_literal); - if (Char2 == '\'' && LangOpts.CPlusPlus17) + if (Char2 == '\'' && (LangOpts.CPlusPlus17 || LangOpts.C2x)) return LexCharConstant( Result, ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), SizeTmp2, Result), @@ -3517,7 +3520,7 @@ // treat u like the start of an identifier. return LexIdentifierContinue(Result, CurPtr); - case 'U': // Identifier (Uber) or C11/C++11 UTF-32 string literal + case 'U': // Identifier (e.g. Uber) or C11/C++11 UTF-32 string literal // Notify MIOpt that we read a non-whitespace/non-comment token. MIOpt.ReadToken(); Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -3551,6 +3551,8 @@ QualType Ty; if (Literal.isWide()) Ty = Context.WideCharTy; // L'x' -> wchar_t in C and C++. + else if (Literal.isUTF8() && getLangOpts().C2x) + Ty = Context.UnsignedCharTy; // u8'x' -> unsigned char in C2x else if (Literal.isUTF8() && getLangOpts().Char8) Ty = Context.Char8Ty; // u8'x' -> char8_t when it exists. else if (Literal.isUTF16()) @@ -3560,7 +3562,8 @@ else if (!getLangOpts().CPlusPlus || Literal.isMultiChar()) Ty = Context.IntTy; // 'x' -> int in C, 'wxyz' -> int in C++. else - Ty = Context.CharTy; // 'x' -> char in C++ + Ty = Context.CharTy; // 'x' -> char in C++; + // u8'x' -> char in C11-C17 and in C++ without char8_t. CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; if (Literal.isWide()) Index: clang/test/Lexer/utf8-char-literal.cpp =================================================================== --- clang/test/Lexer/utf8-char-literal.cpp +++ clang/test/Lexer/utf8-char-literal.cpp @@ -1,5 +1,6 @@ // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -fsyntax-only -verify %s // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c11 -x c -fsyntax-only -verify %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c2x -DC2X -x c -fsyntax-only -verify %s // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++1z -fsyntax-only -verify %s int array0[u'ñ' == u'\xf1'? 1 : -1]; @@ -12,4 +13,16 @@ char d = u8'\u1234'; // expected-error {{character too large for enclosing character literal type}} char e = u8'ሴ'; // expected-error {{character too large for enclosing character literal type}} char f = u8'ab'; // expected-error {{Unicode character literals may not contain multiple characters}} +#elif defined(C2X) +char a = u8'ñ'; // expected-error {{character too large for enclosing character literal type}} +char b = u8'\x80'; // ok +char c = u8'\u0080'; // expected-error {{universal character name refers to a control character}} +char d = u8'\u1234'; // expected-error {{character too large for enclosing character literal type}} +char e = u8'ሴ'; // expected-error {{character too large for enclosing character literal type}} +char f = u8'ab'; // expected-error {{Unicode character literals may not contain multiple characters}} +_Static_assert( + _Generic(u8'a', + default : 0, + unsigned char : 1), + "Surprise!"); #endif Index: clang/www/c_status.html =================================================================== --- clang/www/c_status.html +++ clang/www/c_status.html @@ -720,7 +720,7 @@ Adding the u8 character prefix N2418 - No + Clang 15 Remove support for function definitions with identifier lists