[cfe-commits] r129490 - in /cfe/trunk: lib/Sema/SemaInit.cpp test/SemaCXX/pascal-strings.cpp

Anders Carlsson andersca at mac.com
Wed Apr 13 17:41:11 PDT 2011


Author: andersca
Date: Wed Apr 13 19:41:11 2011
New Revision: 129490

URL: http://llvm.org/viewvc/llvm-project?rev=129490&view=rev
Log:
In C++, when initializing an array from a pascal string, it's OK if the array
is 1 element smaller than the string, because we can just strip off the last
null character. This matches GCC.

Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp
    cfe/trunk/test/SemaCXX/pascal-strings.cpp

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=129490&r1=129489&r2=129490&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Wed Apr 13 19:41:11 2011
@@ -96,6 +96,15 @@
   // 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(),

Modified: cfe/trunk/test/SemaCXX/pascal-strings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/pascal-strings.cpp?rev=129490&r1=129489&r2=129490&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/pascal-strings.cpp (original)
+++ cfe/trunk/test/SemaCXX/pascal-strings.cpp Wed Apr 13 19:41:11 2011
@@ -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}}





More information about the cfe-commits mailing list