From 559a83330416affb0e341a2c53800cbf924a5178 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sun, 22 Feb 2009 01:38:57 +0000 Subject: [PATCH] Correctly encode incomplete and variable length arrays. Fixes PR3639. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65255 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ASTContext.cpp | 27 +++++++++++++++++++-------- test/CodeGenObjC/encode-test-3.m | 11 +++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 test/CodeGenObjC/encode-test-3.m diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 605f46b44b..181ea90691 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -2214,16 +2214,27 @@ void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, } else if (const ArrayType *AT = // Ignore type qualifiers etc. dyn_cast(T->getCanonicalTypeInternal())) { - S += '['; + if (isa(AT)) { + // Incomplete arrays are encoded as a pointer to the array element. + S += '^'; + + getObjCEncodingForTypeImpl(AT->getElementType(), S, + false, ExpandStructures, FD); + } else { + S += '['; - if (const ConstantArrayType *CAT = dyn_cast(AT)) - S += llvm::utostr(CAT->getSize().getZExtValue()); - else - assert(0 && "Unhandled array type!"); + if (const ConstantArrayType *CAT = dyn_cast(AT)) + S += llvm::utostr(CAT->getSize().getZExtValue()); + else { + //Variable length arrays are encoded as a regular array with 0 elements. + assert(isa(AT) && "Unknown array type!"); + S += '0'; + } - getObjCEncodingForTypeImpl(AT->getElementType(), S, - false, ExpandStructures, FD); - S += ']'; + getObjCEncodingForTypeImpl(AT->getElementType(), S, + false, ExpandStructures, FD); + S += ']'; + } } else if (T->getAsFunctionType()) { S += '?'; } else if (const RecordType *RTy = T->getAsRecordType()) { diff --git a/test/CodeGenObjC/encode-test-3.m b/test/CodeGenObjC/encode-test-3.m new file mode 100644 index 0000000000..8bd4421d6c --- /dev/null +++ b/test/CodeGenObjC/encode-test-3.m @@ -0,0 +1,11 @@ +// RUN: clang -triple=i686-apple-darwin9 -fnext-runtime -emit-llvm -o %t %s && +// RUN: grep -e "\^i" %t | count 1 && +// RUN: grep -e "\[0i\]" %t | count 1 + +int main() +{ + int n; + + const char * inc = @encode(int[]); + const char * vla = @encode(int[n]); +} -- 2.40.0