]> granicus.if.org Git - clang/commitdiff
In C++, when initializing an array from a pascal string, it's OK if the array
authorAnders Carlsson <andersca@mac.com>
Thu, 14 Apr 2011 00:41:11 +0000 (00:41 +0000)
committerAnders Carlsson <andersca@mac.com>
Thu, 14 Apr 2011 00:41:11 +0000 (00:41 +0000)
is 1 element smaller than the string, because we can just strip off the last
null character. This matches GCC.

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

lib/Sema/SemaInit.cpp
test/SemaCXX/pascal-strings.cpp

index 1dff64e85579bb5ab759cf69e6364a7957e6dc99..84ab23e584c1c6642896b5afa576cd61e686f6f4 100644 (file)
@@ -96,6 +96,15 @@ static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT,
   // the size may be smaller or larger than the string we are initializing.
   // FIXME: Avoid truncation for 64-bit length strings.
   if (S.getLangOptions().CPlusPlus) {
+    if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) {
+      // For Pascal strings it's OK to strip off the terminating null character,
+      // so the example below is valid:
+      //
+      // unsigned char a[2] = "\pa";
+      if (SL->isPascal())
+        StrLength--;
+    }
+  
     // [dcl.init.string]p2
     if (StrLength > CAT->getSize().getZExtValue())
       S.Diag(Str->getSourceRange().getBegin(),
index db80b68b37e76caf489d5c1380e9ed6fee9b3ca2..89194b54aa8dd88fea6f9d88e13ad425c12b054c 100644 (file)
@@ -1,2 +1,6 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -fpascal-strings
 const wchar_t *pascalString = L"\pThis is a Pascal string";
+
+unsigned char a[3] = "\pa";
+unsigned char b[3] = "\pab";
+unsigned char c[3] = "\pabc"; // expected-error {{initializer-string for char array is too long}}