[cfe-commits] r72663 - in /cfe/trunk: lib/Sema/SemaInit.cpp test/Sema/init.c

Eli Friedman eli.friedman at gmail.com
Sun May 31 03:54:56 PDT 2009


Author: efriedma
Date: Sun May 31 05:54:53 2009
New Revision: 72663

URL: http://llvm.org/viewvc/llvm-project?rev=72663&view=rev
Log:
Fix for PR4285: allow intializing a const wchar_t array with a wide 
string.


Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp
    cfe/trunk/test/Sema/init.c

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=72663&r1=72662&r2=72663&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Sun May 31 05:54:53 2009
@@ -44,17 +44,19 @@
   // Otherwise we can only handle string literals.
   StringLiteral *SL = dyn_cast<StringLiteral>(Init);
   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())
-    return AT->getElementType()->isCharType() ? Init : 0;
+    return ElemTy->isCharType() ? Init : 0;
 
-  // wchar_t array can be initialized with a wide string: C99 6.7.8p15:
-  // "An array with element type compatible with wchar_t may be initialized by a
-  // wide string literal, optionally enclosed in braces."
-  if (Context.typesAreCompatible(Context.getWCharType(), AT->getElementType()))
-    // Only allow wchar_t x[] = L"foo";  not wchar_t x[] = "foo";
+  // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with
+  // correction from DR343): "An array with element type compatible with a
+  // qualified or unqualified version of wchar_t may be initialized by a wide
+  // string literal, optionally enclosed in braces."
+  if (Context.typesAreCompatible(Context.getWCharType(),
+                                 ElemTy.getUnqualifiedType()))
     return Init;
   
   return 0;

Modified: cfe/trunk/test/Sema/init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/init.c?rev=72663&r1=72662&r2=72663&view=diff

==============================================================================
--- cfe/trunk/test/Sema/init.c (original)
+++ cfe/trunk/test/Sema/init.c Sun May 31 05:54:53 2009
@@ -1,5 +1,6 @@
 // RUN: clang-cc %s -verify -fsyntax-only
 
+#include <stddef.h>
 #include <stdint.h>
 
 typedef void (* fp)(void);
@@ -122,3 +123,6 @@
 uintptr_t ptrasintadd1 = (uintptr_t)&a - 4;
 uintptr_t ptrasintadd2 = (uintptr_t)&a + 4;
 uintptr_t ptrasintadd3 = 4 + (uintptr_t)&a;
+
+// PR4285
+const wchar_t widestr[] = L"asdf";





More information about the cfe-commits mailing list