r176780 - When lexing in C11 mode, accept unicode character and string literals, per C11

Jordan Rose jordan_rose at apple.com
Sat Mar 9 23:44:48 PST 2013


I added a c99-compat warning group for UCNs, but it's pretty empty right now. Should we just have two variations of these warnings?


On Mar 9, 2013, at 15:56 , Richard Smith <richard-llvm at metafoo.co.uk> wrote:

> Author: rsmith
> Date: Sat Mar  9 17:56:02 2013
> New Revision: 176780
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=176780&view=rev
> Log:
> When lexing in C11 mode, accept unicode character and string literals, per C11
> 6.4.4.4/1 and 6.4.5/1.
> 
> Modified:
>    cfe/trunk/lib/Lex/Lexer.cpp
>    cfe/trunk/test/CodeGen/string-literal.c
>    cfe/trunk/test/Lexer/char-literal.cpp
>    cfe/trunk/test/Lexer/string_concat.cpp
>    cfe/trunk/test/Lexer/utf8-char-literal.cpp
> 
> Modified: cfe/trunk/lib/Lex/Lexer.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Lexer.cpp?rev=176780&r1=176779&r2=176780&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Lex/Lexer.cpp (original)
> +++ cfe/trunk/lib/Lex/Lexer.cpp Sat Mar  9 17:56:02 2013
> @@ -1629,7 +1629,8 @@ void Lexer::LexStringLiteral(Token &Resu
>   if (!isLexingRawMode() &&
>       (Kind == tok::utf8_string_literal ||
>        Kind == tok::utf16_string_literal ||
> -       Kind == tok::utf32_string_literal))
> +       Kind == tok::utf32_string_literal) &&
> +      getLangOpts().CPlusPlus)
>     Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal);
> 
>   char C = getAndAdvanceChar(CurPtr, Result);
> @@ -1794,7 +1795,8 @@ void Lexer::LexCharConstant(Token &Resul
>   const char *NulCharacter = 0; // Does this character contain the \0 character?
> 
>   if (!isLexingRawMode() &&
> -      (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant))
> +      (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant) &&
> +      getLangOpts().CPlusPlus)
>     Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal);
> 
>   char C = getAndAdvanceChar(CurPtr, Result);
> @@ -2848,11 +2850,11 @@ LexNextToken:
>     MIOpt.ReadToken();
>     return LexNumericConstant(Result, CurPtr);
> 
> -  case 'u':   // Identifier (uber) or C++0x UTF-8 or UTF-16 string literal
> +  case 'u':   // Identifier (uber) or C11/C++11 UTF-8 or UTF-16 string literal
>     // Notify MIOpt that we read a non-whitespace/non-comment token.
>     MIOpt.ReadToken();
> 
> -    if (LangOpts.CPlusPlus11) {
> +    if (LangOpts.CPlusPlus11 || LangOpts.C11) {
>       Char = getCharAndSize(CurPtr, SizeTmp);
> 
>       // UTF-16 string literal
> @@ -2866,7 +2868,8 @@ LexNextToken:
>                                tok::utf16_char_constant);
> 
>       // UTF-16 raw string literal
> -      if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
> +      if (Char == 'R' && LangOpts.CPlusPlus11 &&
> +          getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
>         return LexRawStringLiteral(Result,
>                                ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
>                                            SizeTmp2, Result),
> @@ -2882,7 +2885,7 @@ LexNextToken:
>                                            SizeTmp2, Result),
>                                tok::utf8_string_literal);
> 
> -        if (Char2 == 'R') {
> +        if (Char2 == 'R' && LangOpts.CPlusPlus11) {
>           unsigned SizeTmp3;
>           char Char3 = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3);
>           // UTF-8 raw string literal
> @@ -2900,11 +2903,11 @@ LexNextToken:
>     // treat u like the start of an identifier.
>     return LexIdentifier(Result, CurPtr);
> 
> -  case 'U':   // Identifier (Uber) or C++0x UTF-32 string literal
> +  case 'U':   // Identifier (Uber) or C11/C++11 UTF-32 string literal
>     // Notify MIOpt that we read a non-whitespace/non-comment token.
>     MIOpt.ReadToken();
> 
> -    if (LangOpts.CPlusPlus11) {
> +    if (LangOpts.CPlusPlus11 || LangOpts.C11) {
>       Char = getCharAndSize(CurPtr, SizeTmp);
> 
>       // UTF-32 string literal
> @@ -2918,7 +2921,8 @@ LexNextToken:
>                                tok::utf32_char_constant);
> 
>       // UTF-32 raw string literal
> -      if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
> +      if (Char == 'R' && LangOpts.CPlusPlus11 &&
> +          getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
>         return LexRawStringLiteral(Result,
>                                ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
>                                            SizeTmp2, Result),
> 
> Modified: cfe/trunk/test/CodeGen/string-literal.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/string-literal.c?rev=176780&r1=176779&r2=176780&view=diff
> ==============================================================================
> --- cfe/trunk/test/CodeGen/string-literal.c (original)
> +++ cfe/trunk/test/CodeGen/string-literal.c Sat Mar  9 17:56:02 2013
> @@ -1,87 +1,107 @@
> -// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck -check-prefix=C %s
> -// RUN: %clang_cc1 -x c++ -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck -check-prefix=C %s
> -// RUN: %clang_cc1 -x c++ -std=c++11 -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck -check-prefix=CPP0X %s
> +// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-C %s
> +// RUN: %clang_cc1 -x c++ -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-C %s
> +// RUN: %clang_cc1 -x c++ -std=c++11 -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-CXX11 %s
> +// RUN: %clang_cc1 -x c -std=c11 -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-C11 %s
> 
> #include <stddef.h>
> 
> +#ifndef __cplusplus
> +typedef __WCHAR_TYPE__ wchar_t;
> +typedef __CHAR16_TYPE__ char16_t;
> +typedef __CHAR32_TYPE__ char32_t;
> +#endif
> +
> int main() {
>   // CHECK-C: private unnamed_addr constant [10 x i8] c"abc\00\00\00\00\00\00\00", align 1
> -  // CHECK-CPP0X: private unnamed_addr constant [10 x i8] c"abc\00\00\00\00\00\00\00", align 1
> +  // CHECK-C11: private unnamed_addr constant [10 x i8] c"abc\00\00\00\00\00\00\00", align 1
> +  // CHECK-CXX11: private unnamed_addr constant [10 x i8] c"abc\00\00\00\00\00\00\00", align 1
>   char a[10] = "abc";
> 
>   // This should convert to utf8.
>   // CHECK-C: private unnamed_addr constant [10 x i8] c"\E1\84\A0\C8\A0\F4\82\80\B0\00", align 1
> -  // CHECK-CPP0X: private unnamed_addr constant [10 x i8] c"\E1\84\A0\C8\A0\F4\82\80\B0\00", align 1
> +  // CHECK-C11: private unnamed_addr constant [10 x i8] c"\E1\84\A0\C8\A0\F4\82\80\B0\00", align 1
> +  // CHECK-CXX11: private unnamed_addr constant [10 x i8] c"\E1\84\A0\C8\A0\F4\82\80\B0\00", align 1
>   char b[10] = "\u1120\u0220\U00102030";
> 
>   // CHECK-C: private unnamed_addr constant [3 x i32] [i32 65, i32 66, i32 0], align 4
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i32] [i32 65, i32 66, i32 0], align 4
> +  // CHECK-C11: private unnamed_addr constant [3 x i32] [i32 65, i32 66, i32 0], align 4
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i32] [i32 65, i32 66, i32 0], align 4
>   const wchar_t *foo = L"AB";
> 
>   // CHECK-C: private unnamed_addr constant [3 x i32] [i32 4660, i32 1110027, i32 0], align 4
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i32] [i32 4660, i32 1110027, i32 0], align 4
> +  // CHECK-C11: private unnamed_addr constant [3 x i32] [i32 4660, i32 1110027, i32 0], align 4
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i32] [i32 4660, i32 1110027, i32 0], align 4
>   const wchar_t *bar = L"\u1234\U0010F00B";
> 
>   // CHECK-C: private unnamed_addr constant [3 x i32] [i32 4660, i32 1110028, i32 0], align 4
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i32] [i32 4660, i32 1110028, i32 0], align 4
> +  // CHECK-C11: private unnamed_addr constant [3 x i32] [i32 4660, i32 1110028, i32 0], align 4
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i32] [i32 4660, i32 1110028, i32 0], align 4
>   const wchar_t *baz = L"\u1234" "\U0010F00C";
> 
> -#if __cplusplus >= 201103L
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i32] [i32 67, i32 68, i32 0], align 4
> +#if __cplusplus >= 201103L || __STDC_VERSION__ >= 201112L
> +  // CHECK-C11: private unnamed_addr constant [3 x i32] [i32 67, i32 68, i32 0], align 4
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i32] [i32 67, i32 68, i32 0], align 4
>   const char32_t *c = U"CD";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i32] [i32 4661, i32 1110028, i32 0], align 4
> +  // CHECK-C11: private unnamed_addr constant [3 x i32] [i32 4661, i32 1110028, i32 0], align 4
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i32] [i32 4661, i32 1110028, i32 0], align 4
>   const char32_t *d = U"\u1235\U0010F00C";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i32] [i32 4661, i32 1110027, i32 0], align 4
> +  // CHECK-C11: private unnamed_addr constant [3 x i32] [i32 4661, i32 1110027, i32 0], align 4
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i32] [i32 4661, i32 1110027, i32 0], align 4
>   const char32_t *o = "\u1235" U"\U0010F00B";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i16] [i16 69, i16 70, i16 0], align 2
> +  // CHECK-C11: private unnamed_addr constant [3 x i16] [i16 69, i16 70, i16 0], align 2
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i16] [i16 69, i16 70, i16 0], align 2
>   const char16_t *e = u"EF";
> 
>   // This should convert to utf16.
> -  // CHECK-CPP0X: private unnamed_addr constant [5 x i16] [i16 4384, i16 544, i16 -9272, i16 -9168, i16 0], align 2
> +  // CHECK-C11: private unnamed_addr constant [5 x i16] [i16 4384, i16 544, i16 -9272, i16 -9168, i16 0], align 2
> +  // CHECK-CXX11: private unnamed_addr constant [5 x i16] [i16 4384, i16 544, i16 -9272, i16 -9168, i16 0], align 2
>   const char16_t *f = u"\u1120\u0220\U00102030";
> 
>   // This should convert to utf16.
> -  // CHECK-CPP0X: private unnamed_addr constant [5 x i16] [i16 4384, i16 800, i16 -9272, i16 -9168, i16 0], align 2
> +  // CHECK-C11: private unnamed_addr constant [5 x i16] [i16 4384, i16 800, i16 -9272, i16 -9168, i16 0], align 2
> +  // CHECK-CXX11: private unnamed_addr constant [5 x i16] [i16 4384, i16 800, i16 -9272, i16 -9168, i16 0], align 2
>   const char16_t *p = u"\u1120\u0320" "\U00102030";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [4 x i8] c"def\00", align 1
> +  // CHECK-C11: private unnamed_addr constant [4 x i8] c"def\00", align 1
> +  // CHECK-CXX11: private unnamed_addr constant [4 x i8] c"def\00", align 1
>   const char *g = u8"def";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [4 x i8] c"ghi\00", align 1
> +#ifdef __cplusplus
> +  // CHECK-CXX11: private unnamed_addr constant [4 x i8] c"ghi\00", align 1
>   const char *h = R"foo(ghi)foo";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [4 x i8] c"jkl\00", align 1
> +  // CHECK-CXX11: private unnamed_addr constant [4 x i8] c"jkl\00", align 1
>   const char *i = u8R"bar(jkl)bar";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i16] [i16 71, i16 72, i16 0], align 2
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i16] [i16 71, i16 72, i16 0], align 2
>   const char16_t *j = uR"foo(GH)foo";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i32] [i32 73, i32 74, i32 0], align 4
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i32] [i32 73, i32 74, i32 0], align 4
>   const char32_t *k = UR"bar(IJ)bar";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [3 x i32] [i32 75, i32 76, i32 0], align 4
> +  // CHECK-CXX11: private unnamed_addr constant [3 x i32] [i32 75, i32 76, i32 0], align 4
>   const wchar_t *l = LR"bar(KL)bar";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [9 x i8] c"abc\5Cndef\00", align 1
> +  // CHECK-CXX11: private unnamed_addr constant [9 x i8] c"abc\5Cndef\00", align 1
>   const char *m = R"(abc\ndef)";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [8 x i8] c"abc\0Adef\00", align 1
> +  // CHECK-CXX11: private unnamed_addr constant [8 x i8] c"abc\0Adef\00", align 1
>   const char *n = R"(abc
> def)";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [11 x i8] c"abc\0Adefghi\00", align 1
> +  // CHECK-CXX11: private unnamed_addr constant [11 x i8] c"abc\0Adefghi\00", align 1
>   const char *q = R"(abc
> def)" "ghi";
> 
> -  // CHECK-CPP0X: private unnamed_addr constant [13 x i8] c"abc\5C\0A??=\0Adef\00", align 1
> +  // CHECK-CXX11: private unnamed_addr constant [13 x i8] c"abc\5C\0A??=\0Adef\00", align 1
>   const char *r = R\
> "(abc\
> ??=
> def)";
> 
> -
> +#endif
> #endif
> }
> 
> Modified: cfe/trunk/test/Lexer/char-literal.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/char-literal.cpp?rev=176780&r1=176779&r2=176780&view=diff
> ==============================================================================
> --- cfe/trunk/test/Lexer/char-literal.cpp (original)
> +++ cfe/trunk/test/Lexer/char-literal.cpp Sat Mar  9 17:56:02 2013
> @@ -1,4 +1,11 @@
> // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -Wfour-char-constants -fsyntax-only -verify %s
> +// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c11 -x c -Wfour-char-constants -fsyntax-only -verify %s
> +
> +#ifndef __cplusplus
> +typedef __WCHAR_TYPE__ wchar_t;
> +typedef __CHAR16_TYPE__ char16_t;
> +typedef __CHAR32_TYPE__ char32_t;
> +#endif
> 
> int a = 'ab'; // expected-warning {{multi-character character constant}}
> int b = '\xFF\xFF'; // expected-warning {{multi-character character constant}}
> @@ -7,7 +14,9 @@ int c = 'APPS'; // expected-warning {{mu
> char d = '⌘'; // expected-error {{character too large for enclosing character literal type}}
> char e = '\u2318'; // expected-error {{character too large for enclosing character literal type}}
> 
> +#ifdef __cplusplus
> auto f = '\xE2\x8C\x98'; // expected-warning {{multi-character character constant}}
> +#endif
> 
> char16_t g = u'ab'; // expected-error {{Unicode character literals may not contain multiple characters}}
> char16_t h = u'\U0010FFFD'; // expected-error {{character too large for enclosing character literal type}}
> @@ -24,4 +33,11 @@ char32_t n = U'ab'; // expected-error {{
> char16_t o = '👽'; // expected-error {{character too large for enclosing character literal type}}
> 
> char16_t p[2] = u"\U0000FFFF";
> -char16_t q[2] = u"\U00010000"; // expected-error {{too long}}
> +char16_t q[2] = u"\U00010000";
> +#ifdef __cplusplus
> +// expected-error at -2 {{too long}}
> +#else
> +// FIXME: The above should be accepted in C11 mode.
> +// expected-error at -6 {{must be an initializer list}}
> +// expected-error at -6 {{must be an initializer list}}
> +#endif
> 
> Modified: cfe/trunk/test/Lexer/string_concat.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/string_concat.cpp?rev=176780&r1=176779&r2=176780&view=diff
> ==============================================================================
> --- cfe/trunk/test/Lexer/string_concat.cpp (original)
> +++ cfe/trunk/test/Lexer/string_concat.cpp Sat Mar  9 17:56:02 2013
> @@ -1,33 +1,48 @@
> // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
> +// RUN: %clang_cc1 -std=c11 -x c -fsyntax-only -verify %s
> +
> +#ifndef __cplusplus
> +typedef __WCHAR_TYPE__ wchar_t;
> +typedef __CHAR16_TYPE__ char16_t;
> +typedef __CHAR32_TYPE__ char32_t;
> +#endif
> 
> void f() {
> 
>   const char* a = u8"abc" u"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char* b = u8"abc" U"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char* c = u8"abc" L"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
> +#ifdef __cplusplus
>   const char* d = u8"abc" uR"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char* e = u8"abc" UR"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char* f = u8"abc" LR"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
> +#endif
> 
>   const char16_t* g = u"abc" u8"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char16_t* h = u"abc" U"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char16_t* i = u"abc" L"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
> +#ifdef __cplusplus
>   const char16_t* j = u"abc" u8R"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char16_t* k = u"abc" UR"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char16_t* l = u"abc" LR"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
> +#endif
> 
>   const char32_t* m = U"abc" u8"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char32_t* n = U"abc" u"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char32_t* o = U"abc" L"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
> +#ifdef __cplusplus
>   const char32_t* p = U"abc" u8R"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char32_t* q = U"abc" uR"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const char32_t* r = U"abc" LR"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
> +#endif
> 
>   const wchar_t* s = L"abc" u8"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const wchar_t* t = L"abc" u"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const wchar_t* u = L"abc" U"abc"; // expected-error {{unsupported non-standard concatenation of string literals}}
> +#ifdef __cplusplus
>   const wchar_t* v = L"abc" u8R"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const wchar_t* w = L"abc" uR"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
>   const wchar_t* x = L"abc" UR"(abc)"; // expected-error {{unsupported non-standard concatenation of string literals}}
> +#endif
> }
> 
> 
> Modified: cfe/trunk/test/Lexer/utf8-char-literal.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/utf8-char-literal.cpp?rev=176780&r1=176779&r2=176780&view=diff
> ==============================================================================
> --- cfe/trunk/test/Lexer/utf8-char-literal.cpp (original)
> +++ cfe/trunk/test/Lexer/utf8-char-literal.cpp Sat Mar  9 17:56:02 2013
> @@ -1,4 +1,5 @@
> // 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
> 
> int array0[u'ñ' == u'\xf1'? 1 : -1];
> int array1['\xF1' !=  u'\xf1'? 1 : -1];
> 
> 
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits





More information about the cfe-commits mailing list