diff options
author | Anders Carlsson <andersca@mac.com> | 2011-04-14 00:41:11 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2011-04-14 00:41:11 +0000 |
commit | b8fc45f8d0fdcc7908590115942d425bf4a924f1 (patch) | |
tree | 6fce29d2c5f4a58558dd13cd9ab306e9f84d17ea | |
parent | 3e2193ce5feb2feb092e5ae615e85148e06e9fd2 (diff) |
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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129490 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Sema/SemaInit.cpp | 9 | ||||
-rw-r--r-- | test/SemaCXX/pascal-strings.cpp | 4 |
2 files changed, 13 insertions, 0 deletions
diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 1dff64e855..84ab23e584 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -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(), diff --git a/test/SemaCXX/pascal-strings.cpp b/test/SemaCXX/pascal-strings.cpp index db80b68b37..89194b54aa 100644 --- a/test/SemaCXX/pascal-strings.cpp +++ b/test/SemaCXX/pascal-strings.cpp @@ -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}} |