diff options
| author | Fariborz Jahanian <fjahanian@apple.com> | 2011-02-02 19:36:18 +0000 |
|---|---|---|
| committer | Fariborz Jahanian <fjahanian@apple.com> | 2011-02-02 19:36:18 +0000 |
| commit | 09b2331cf7a03a8f372edfb5123048a86d8b4df7 (patch) | |
| tree | a84fcc949483268aa29683ad0d8c8a2d30b6b357 | |
| parent | ac18130050830f4632e8d703c97572c322a56ce2 (diff) | |
| download | bcm5719-llvm-09b2331cf7a03a8f372edfb5123048a86d8b4df7.tar.gz bcm5719-llvm-09b2331cf7a03a8f372edfb5123048a86d8b4df7.zip | |
For gcc compatibility, size of a class which is zero
but has non-empty data fields, such as array of zero length,
remains zero.
// rdar://8945175
llvm-svn: 124741
| -rw-r--r-- | clang/lib/AST/RecordLayoutBuilder.cpp | 13 | ||||
| -rw-r--r-- | clang/test/CodeGenCXX/non-empty-class-size-zero.cpp | 21 |
2 files changed, 32 insertions, 2 deletions
diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp index 730ec213580..a871c3d1766 100644 --- a/clang/lib/AST/RecordLayoutBuilder.cpp +++ b/clang/lib/AST/RecordLayoutBuilder.cpp @@ -1469,8 +1469,17 @@ void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) { // In C++, records cannot be of size 0. - if (Context.getLangOptions().CPlusPlus && Size == 0) - Size = 8; + if (Context.getLangOptions().CPlusPlus && Size == 0) { + if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { + // Compatibility with gcc requires a class (pod or non-pod) + // which is not empty but of size 0; such as having fields of + // array of zero-length, remains of Size 0 + if (RD->isEmpty()) + Size = 8; + } + else + Size = 8; + } // Finally, round the size of the record up to the alignment of the // record itself. uint64_t UnpaddedSize = Size - UnfilledBitsInLastByte; diff --git a/clang/test/CodeGenCXX/non-empty-class-size-zero.cpp b/clang/test/CodeGenCXX/non-empty-class-size-zero.cpp new file mode 100644 index 00000000000..810717bb505 --- /dev/null +++ b/clang/test/CodeGenCXX/non-empty-class-size-zero.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s +// rdar://8945175 + +struct X { + int array[0]; + int array1[0]; + int array2[0]; + X(); + ~X(); +}; + +struct Y { + int first; + X padding; + int second; +}; + +int main() { +// CHECK: store i32 0, i32* [[RETVAL:%.*]] + return sizeof(Y) -8 ; +} |

