[cfe-commits] r65585 - /cfe/trunk/lib/Sema/SemaInit.cpp

Chris Lattner sabre at nondot.org
Thu Feb 26 15:26:43 PST 2009


Author: lattner
Date: Thu Feb 26 17:26:43 2009
New Revision: 65585

URL: http://llvm.org/viewvc/llvm-project?rev=65585&view=rev
Log:
allow wide strings to initialize arrays compatible with wchar_t.  
Unfortunately this doesn't work yet because wchar_t is completely
broken in C.

Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Thu Feb 26 17:26:43 2009
@@ -27,12 +27,31 @@
 //===----------------------------------------------------------------------===//
 
 static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
-  if (const ArrayType *AT = Context.getAsArrayType(DeclType))
-    if (AT->getElementType()->isCharType()) {
-      Init = Init->IgnoreParens();
-      if (isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init))
-        return Init;
-    }
+  const ArrayType *AT = Context.getAsArrayType(DeclType);
+  if (!AT) return 0;
+
+  // See if this is a string literal or @encode.
+  Init = Init->IgnoreParens();
+  
+  // Handle @encode, which is a narrow string.
+  if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
+    return Init;
+
+  // Otherwise we can only handle string literals.
+  StringLiteral *SL = dyn_cast<StringLiteral>(Init);
+  
+  // 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.WCharTy, AT->getElementType()))
+    // Only allow wchar_t x[] = L"foo";  not wchar_t x[] = "foo";
+    return Init;
+  
   return 0;
 }
 





More information about the cfe-commits mailing list