From 9b36209e31ce6ddba8ed52547b34da6fd98e06cb Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sat, 9 Mar 2013 23:56:02 +0000 Subject: [PATCH] When lexing in C11 mode, accept unicode character and string literals, per C11 6.4.4.4/1 and 6.4.5/1. llvm-svn: 176780 --- clang/lib/Lex/Lexer.cpp | 22 ++++++----- clang/test/CodeGen/string-literal.c | 72 ++++++++++++++++++++++------------ clang/test/Lexer/char-literal.cpp | 18 ++++++++- clang/test/Lexer/string_concat.cpp | 15 +++++++ clang/test/Lexer/utf8-char-literal.cpp | 1 + 5 files changed, 92 insertions(+), 36 deletions(-) diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index e18953d..e6cc8b9 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -1629,7 +1629,8 @@ void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, 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 &Result, const char *CurPtr, 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), diff --git a/clang/test/CodeGen/string-literal.c b/clang/test/CodeGen/string-literal.c index 962b19d..8bc97f1 100644 --- a/clang/test/CodeGen/string-literal.c +++ b/clang/test/CodeGen/string-literal.c @@ -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 +#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 } diff --git a/clang/test/Lexer/char-literal.cpp b/clang/test/Lexer/char-literal.cpp index 8556d46..b2fab34 100644 --- a/clang/test/Lexer/char-literal.cpp +++ b/clang/test/Lexer/char-literal.cpp @@ -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 {{multi-character character constant}} 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 {{Unicode character literals may not conta 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@-2 {{too long}} +#else +// FIXME: The above should be accepted in C11 mode. +// expected-error@-6 {{must be an initializer list}} +// expected-error@-6 {{must be an initializer list}} +#endif diff --git a/clang/test/Lexer/string_concat.cpp b/clang/test/Lexer/string_concat.cpp index 7e78a63..a7eb396 100644 --- a/clang/test/Lexer/string_concat.cpp +++ b/clang/test/Lexer/string_concat.cpp @@ -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 } diff --git a/clang/test/Lexer/utf8-char-literal.cpp b/clang/test/Lexer/utf8-char-literal.cpp index 12b001e..7a4d126 100644 --- a/clang/test/Lexer/utf8-char-literal.cpp +++ b/clang/test/Lexer/utf8-char-literal.cpp @@ -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]; -- 2.7.4