]> granicus.if.org Git - clang/commitdiff
Fix for PR4285: allow intializing a const wchar_t array with a wide
authorEli Friedman <eli.friedman@gmail.com>
Sun, 31 May 2009 10:54:53 +0000 (10:54 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sun, 31 May 2009 10:54:53 +0000 (10:54 +0000)
string.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72663 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaInit.cpp
test/Sema/init.c

index 4976bd466fcc4b01b542dedda87b6f6cb693fdae..4e0eb1d431fe2de8dbd635fce99550a5bb6e0031 100644 (file)
@@ -44,17 +44,19 @@ static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
   // 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;
-
-  // 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";
+    return ElemTy->isCharType() ? Init : 0;
+
+  // 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;
index 7938ec5568dc8c9661ac581dcde23db9b6276f7c..1cbcbb7e36f8af2ad55fcf0d1b1cb65df3647ef8 100644 (file)
@@ -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 @@ ivector4 vtest4 = __imag__ (ivector4){1};
 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";