From 4142cebf70fe6c3855570c98b8042431797a65fd Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 26 May 2010 20:19:07 +0000 Subject: [PATCH] Fixes misc. flexible array bugs in c++ (PR7029). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@104733 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 + lib/Sema/SemaDecl.cpp | 9 +++++ lib/Sema/SemaDeclCXX.cpp | 13 ++++++- test/SemaCXX/flexible-array-test.cpp | 45 ++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 test/SemaCXX/flexible-array-test.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index bdbb36761b..a76757fc07 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1828,6 +1828,8 @@ def ext_variable_sized_type_in_struct : ExtWarn< def err_flexible_array_empty_struct : Error< "flexible array %0 not allowed in otherwise empty struct">; +def err_flexible_array_has_nonpod_type : Error< + "flexible array %0 must have a non-dependent, non-POD type">; def ext_flexible_array_in_struct : Extension< "%0 may not be nested in a struct due to flexible array member">; def ext_flexible_array_in_array : Extension< diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 19e31da6ca..dac070aaae 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -6136,6 +6136,15 @@ void Sema::ActOnFields(Scope* S, EnclosingDecl->setInvalidDecl(); continue; } + if (!FD->getType()->isDependentType() && + !Context.getBaseElementType(FD->getType())->isPODType()) { + Diag(FD->getLocation(), diag::err_flexible_array_has_nonpod_type) + << FD->getDeclName(); + FD->setInvalidDecl(); + EnclosingDecl->setInvalidDecl(); + continue; + } + // Okay, we have a legal flexible array member at the end of the struct. if (Record) Record->setHasFlexibleArrayMember(true); diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 29fb6d6eac..148d1463c2 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -1892,9 +1892,15 @@ Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, // Fields. for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), - E = ClassDecl->field_end(); Field != E; ++Field) + E = ClassDecl->field_end(); Field != E; ++Field) { + if ((*Field)->getType()->isIncompleteArrayType()) { + assert(ClassDecl->hasFlexibleArrayMember() && + "Incomplete array type is not valid"); + continue; + } if (CollectFieldInitializer(Info, *Field, *Field)) HadError = true; + } NumInitializers = Info.AllToInit.size(); if (NumInitializers > 0) { @@ -4577,6 +4583,11 @@ void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, } QualType FieldType = Field->getType().getNonReferenceType(); + if (FieldType->isIncompleteArrayType()) { + assert(ClassDecl->hasFlexibleArrayMember() && + "Incomplete array type is not valid"); + continue; + } // Build references to the field in the object we're copying from and to. CXXScopeSpec SS; // Intentionally empty diff --git a/test/SemaCXX/flexible-array-test.cpp b/test/SemaCXX/flexible-array-test.cpp new file mode 100644 index 0000000000..e58992bc93 --- /dev/null +++ b/test/SemaCXX/flexible-array-test.cpp @@ -0,0 +1,45 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +// pr7029 + +template struct QMap +{ + void insert(const Key &, const T &); + T v; +}; + + +template +void QMap::insert(const Key &, const T &avalue) +{ + v = avalue; +} + + +struct inotify_event +{ + int wd; + + // clang doesn't like '[]': + // cannot initialize a parameter of type 'void *' with an rvalue of type 'char (*)[]' + char name []; +}; + + +void foo() +{ + inotify_event event; + inotify_event* ptr = &event; + inotify_event event1 = *ptr; + *ptr = event; + QMap eventForId; + eventForId.insert(ptr->wd, *ptr); +} + +struct S { + virtual void foo(); +}; + +struct X { + int blah; + S strings[]; // expected-error {{flexible array 'strings' must have a non-dependent, non-POD type}} +}; -- 2.40.0