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 | d162fb83f2c005484beb8ad71860c30a09f1907d (patch) | |
tree | 891045d00f54ff6299dd29de0075aeb940fae65c /clang | |
parent | e7e288c80552ef5176c1ed5a35df26be3a0129ea (diff) | |
download | bcm5719-llvm-d162fb83f2c005484beb8ad71860c30a09f1907d.tar.gz bcm5719-llvm-d162fb83f2c005484beb8ad71860c30a09f1907d.zip |
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.
llvm-svn: 129490
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/Sema/SemaInit.cpp | 9 | ||||
-rw-r--r-- | clang/test/SemaCXX/pascal-strings.cpp | 4 |
2 files changed, 13 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 1dff64e8557..84ab23e584c 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/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/clang/test/SemaCXX/pascal-strings.cpp b/clang/test/SemaCXX/pascal-strings.cpp index db80b68b37e..89194b54aa8 100644 --- a/clang/test/SemaCXX/pascal-strings.cpp +++ b/clang/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}} |