[cfe-dev] [PATCH] C++0x unicode string and character literals now with test cases

Douglas Gregor dgregor at apple.com
Mon Jul 25 11:44:54 PDT 2011


On Jul 24, 2011, at 10:28 PM, Craig Topper wrote:

> Previous patch doesn't apply cleanly anymore so here's a new version.
> 
> Chris or Doug, if you're reading this can I get a review as suggested by Eric?


Index: lib/Sema/SemaExprCXX.cpp
===================================================================
--- lib/Sema/SemaExprCXX.cpp	(revision 135897)
+++ lib/Sema/SemaExprCXX.cpp	(working copy)
@@ -2026,12 +2026,21 @@
           = ToPtrType->getPointeeType()->getAs<BuiltinType>()) {
         // This conversion is considered only when there is an
         // explicit appropriate pointer target type (C++ 4.2p2).
-        if (!ToPtrType->getPointeeType().hasQualifiers() &&
-            ((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
-             (!StrLit->isWide() &&
-              (ToPointeeType->getKind() == BuiltinType::Char_U ||
-               ToPointeeType->getKind() == BuiltinType::Char_S))))
-          return true;
+        if (!ToPtrType->getPointeeType().hasQualifiers()) {
+          switch (StrLit->getKind()) {
+            default: assert(0 && "Unknown case");
+            case StringLiteral::Ascii:
+            case StringLiteral::UTF8:
+              return (ToPointeeType->getKind() == BuiltinType::Char_U ||
+                      ToPointeeType->getKind() == BuiltinType::Char_S);
+            case StringLiteral::Wide:
+              return ToPointeeType->isWideCharType();
+            case StringLiteral::UTF16:
+              return ToPointeeType->getKind() == BuiltinType::Char16;
+            case StringLiteral::UTF32:
+              return ToPointeeType->getKind() == BuiltinType::Char32;
+          }
+        }
       }

This routine handles the C++98/03 deprecated string literal -> non-const character* conversion, which should not apply to the new C++0x character types. Or did I miss something in the C++0x FDIS?

Also, please remove the 'default' case; it's enough that the switch statement is exhaustive.

Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp	(revision 135897)
+++ lib/Sema/SemaInit.cpp	(working copy)
@@ -48,20 +48,29 @@
   if (SL == 0) return 0;
 
   QualType ElemTy = Context.getCanonicalType(AT->getElementType());
-  // char array can be initialized with a narrow string.
-  // Only allow char x[] = "foo";  not char x[] = L"foo";
-  if (!SL->isWide())
+
+  switch (SL->getKind()) {
+  default: assert(0 && "Unknown string literal kind");
+  case StringLiteral::Ascii:
+  case StringLiteral::UTF8:
+    // char array can be initialized with a narrow string.
+    // Only allow char x[] = "foo";  not char x[] = L"foo";
     return ElemTy->isCharType() ? Init : 0;
+  case StringLiteral::UTF16:
+    return ElemTy->isChar16Type() ? Init : 0;
+  case StringLiteral::UTF32:
+    return ElemTy->isChar32Type() ? Init : 0;

Please remove the "default" here. (And the other "default" cases when the switch statement is exhaustive).

Overall, this patch is looking great to me. Once we resolve the deprecated conversion stuff, I think we'll be ready to put it in. Thanks! (and sorry for the delay)

	- Doug



More information about the cfe-dev mailing list