[Clang] allow ` @$ `` in raw string delimiters in C++26 (#93216)

And as an extension in older language modes.

Per https://eel.is/c++draft/lex.string#nt:d-char

Fixes #93130
This commit is contained in:
cor3ntin 2024-05-28 15:38:02 +02:00 committed by GitHub
parent 8995ccc446
commit 2ace7bdcfe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 49 additions and 18 deletions

View File

@ -802,6 +802,7 @@ Bug Fixes to C++ Support
- Fixed a regression introduced in Clang 18 causing a static function overloading a non-static function
with the same parameters not to be diagnosed. (Fixes #GH93456).
- Clang now diagnoses unexpanded parameter packs in attributes. (Fixes #GH93269).
- Clang now allows ``@$``` in raw string literals. Fixes (#GH93130).
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -28,8 +28,7 @@ namespace charinfo {
CHAR_LOWER = 0x0040, // a-z
CHAR_UNDER = 0x0080, // _
CHAR_PERIOD = 0x0100, // .
CHAR_RAWDEL = 0x0200, // {}[]#<>%:;?*+-/^&|~!=,"'
CHAR_PUNCT = 0x0400 // `$@()
CHAR_PUNCT = 0x0200, // {}[]#<>%:;?*+-/^&|~!=,"'`$@()
};
enum {
@ -152,7 +151,8 @@ LLVM_READONLY inline bool isHexDigit(unsigned char c) {
/// Note that '_' is both a punctuation character and an identifier character!
LLVM_READONLY inline bool isPunctuation(unsigned char c) {
using namespace charinfo;
return (InfoTable[c] & (CHAR_UNDER|CHAR_PERIOD|CHAR_RAWDEL|CHAR_PUNCT)) != 0;
return (InfoTable[c] &
(CHAR_UNDER | CHAR_PERIOD | CHAR_PUNCT | CHAR_PUNCT)) != 0;
}
/// Return true if this character is an ASCII printable character; that is, a
@ -160,8 +160,8 @@ LLVM_READONLY inline bool isPunctuation(unsigned char c) {
/// terminal.
LLVM_READONLY inline bool isPrintable(unsigned char c) {
using namespace charinfo;
return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_PERIOD|CHAR_PUNCT|
CHAR_DIGIT|CHAR_UNDER|CHAR_RAWDEL|CHAR_SPACE)) != 0;
return (InfoTable[c] & (CHAR_UPPER | CHAR_LOWER | CHAR_PERIOD | CHAR_PUNCT |
CHAR_DIGIT | CHAR_UNDER | CHAR_SPACE)) != 0;
}
/// Return true if this is the body character of a C preprocessing number,
@ -175,8 +175,9 @@ LLVM_READONLY inline bool isPreprocessingNumberBody(unsigned char c) {
/// Return true if this is the body character of a C++ raw string delimiter.
LLVM_READONLY inline bool isRawStringDelimBody(unsigned char c) {
using namespace charinfo;
return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_PERIOD|
CHAR_DIGIT|CHAR_UNDER|CHAR_RAWDEL)) != 0;
return (InfoTable[c] & (CHAR_UPPER | CHAR_LOWER | CHAR_PERIOD | CHAR_DIGIT |
CHAR_UNDER | CHAR_PUNCT)) != 0 &&
c != '(' && c != ')';
}
enum class EscapeChar {

View File

@ -111,6 +111,14 @@ def warn_cxx98_compat_raw_string_literal : Warning<
"raw string literals are incompatible with C++98">,
InGroup<CXX98Compat>, DefaultIgnore;
def warn_cxx26_compat_raw_string_literal_character_set : Warning<
" '%0' in a raw string literal delimiter is incompatible "
"with standards before C++2c">,
InGroup<CXXPre26Compat>, DefaultIgnore;
def ext_cxx26_raw_string_literal_character_set : Extension<
" '%0' in a raw string literal delimiter is a C++2c extension">,
InGroup<CXX26>, DefaultIgnore;
def warn_multichar_character_literal : Warning<
"multi-character character constant">, InGroup<MultiChar>;
def warn_four_char_character_literal : Warning<

View File

@ -31,20 +31,20 @@ const uint16_t clang::charinfo::InfoTable[256] = {
0 , 0 , 0 , 0 ,
//32 SP 33 ! 34 " 35 #
//36 $ 37 % 38 & 39 '
CHAR_SPACE , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL ,
CHAR_PUNCT , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL ,
CHAR_SPACE , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT ,
CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT ,
//40 ( 41 ) 42 * 43 +
//44 , 45 - 46 . 47 /
CHAR_PUNCT , CHAR_PUNCT , CHAR_RAWDEL , CHAR_RAWDEL ,
CHAR_RAWDEL , CHAR_RAWDEL , CHAR_PERIOD , CHAR_RAWDEL ,
CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT ,
CHAR_PUNCT , CHAR_PUNCT , CHAR_PERIOD , CHAR_PUNCT ,
//48 0 49 1 50 2 51 3
//52 4 53 5 54 6 55 7
CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT ,
CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT , CHAR_DIGIT ,
//56 8 57 9 58 : 59 ;
//60 < 61 = 62 > 63 ?
CHAR_DIGIT , CHAR_DIGIT , CHAR_RAWDEL , CHAR_RAWDEL ,
CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL ,
CHAR_DIGIT , CHAR_DIGIT , CHAR_PUNCT , CHAR_PUNCT ,
CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT ,
//64 @ 65 A 66 B 67 C
//68 D 69 E 70 F 71 G
CHAR_PUNCT , CHAR_XUPPER , CHAR_XUPPER , CHAR_XUPPER ,
@ -59,8 +59,8 @@ const uint16_t clang::charinfo::InfoTable[256] = {
CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_UPPER ,
//88 X 89 Y 90 Z 91 [
//92 \ 93 ] 94 ^ 95 _
CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_RAWDEL ,
CHAR_PUNCT , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_UNDER ,
CHAR_UPPER , CHAR_UPPER , CHAR_UPPER , CHAR_PUNCT ,
CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , CHAR_UNDER ,
//96 ` 97 a 98 b 99 c
//100 d 101 e 102 f 103 g
CHAR_PUNCT , CHAR_XLOWER , CHAR_XLOWER , CHAR_XLOWER ,
@ -75,6 +75,6 @@ const uint16_t clang::charinfo::InfoTable[256] = {
CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_LOWER ,
//120 x 121 y 122 z 123 {
//124 | 125 } 126 ~ 127 DEL
CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_RAWDEL ,
CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 0
CHAR_LOWER , CHAR_LOWER , CHAR_LOWER , CHAR_PUNCT ,
CHAR_PUNCT , CHAR_PUNCT , CHAR_PUNCT , 0
};

View File

@ -2261,8 +2261,17 @@ bool Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr,
unsigned PrefixLen = 0;
while (PrefixLen != 16 && isRawStringDelimBody(CurPtr[PrefixLen]))
while (PrefixLen != 16 && isRawStringDelimBody(CurPtr[PrefixLen])) {
++PrefixLen;
if (!isLexingRawMode() &&
llvm::is_contained({'$', '@', '`'}, CurPtr[PrefixLen])) {
const char *Pos = &CurPtr[PrefixLen];
Diag(Pos, LangOpts.CPlusPlus26
? diag::warn_cxx26_compat_raw_string_literal_character_set
: diag::ext_cxx26_raw_string_literal_character_set)
<< StringRef(Pos, 1);
}
}
// If the last character was not a '(', then we didn't lex a valid delimiter.
if (CurPtr[PrefixLen] != '(') {

View File

@ -0,0 +1,12 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Wc++26-extensions %s
// RUN: %clang_cc1 -std=c++2c -fsyntax-only -verify=cxx26 -Wpre-c++26-compat %s
int main() {
(void) R"abc`@$(foobar)abc`@$";
//expected-warning@-1 {{'`' in a raw string literal delimiter is a C++2c extension}}
//expected-warning@-2 {{'@' in a raw string literal delimiter is a C++2c extension}}
//expected-warning@-3 {{'$' in a raw string literal delimiter is a C++2c extension}}
//cxx26-warning@-4 {{'`' in a raw string literal delimiter is incompatible with standards before C++2c}}
//cxx26-warning@-5 {{'@' in a raw string literal delimiter is incompatible with standards before C++2c}}
//cxx26-warning@-6 {{'$' in a raw string literal delimiter is incompatible with standards before C++2c}}
}