From 8ab822fdffe8472c052d90d9c1fbd23f8671dfbd Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Sat, 17 Mar 2018 21:08:40 +0000 Subject: [PATCH] Implement DR2229, which prohibits unnamed bit-fields from having qualifiers in C++. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@327781 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaDecl.cpp | 7 +++++++ test/CXX/class/class.bit/p2.cpp | 2 +- test/CXX/drs/dr22xx.cpp | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/CXX/drs/dr22xx.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index e58d1c7465..99c340775f 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1586,6 +1586,8 @@ def err_not_integral_type_bitfield : Error< "bit-field %0 has non-integral type %1">; def err_not_integral_type_anon_bitfield : Error< "anonymous bit-field has non-integral type %0">; +def err_anon_bitfield_qualifiers : Error< + "anonymous bit-field cannot have qualifiers">; def err_member_function_initialization : Error< "initializer on function does not look like a pure-specifier">; def err_non_virtual_pure : Error< diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 612761177c..ea97135303 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -14856,6 +14856,13 @@ FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, InvalidDecl = true; } + // Anonymous bit-fields cannot be cv-qualified (CWG 2229). + if (!InvalidDecl && getLangOpts().CPlusPlus && !II && BitWidth && + T.hasQualifiers()) { + InvalidDecl = true; + Diag(Loc, diag::err_anon_bitfield_qualifiers); + } + // C99 6.7.2.1p8: A member of a structure or union may have any type other // than a variably modified type. if (!InvalidDecl && T->isVariablyModifiedType()) { diff --git a/test/CXX/class/class.bit/p2.cpp b/test/CXX/class/class.bit/p2.cpp index 7962330d16..8b2436a869 100644 --- a/test/CXX/class/class.bit/p2.cpp +++ b/test/CXX/class/class.bit/p2.cpp @@ -9,7 +9,7 @@ A a = { }; A a2 = { 1 }; // expected-error{{excess elements in struct initializer}} struct B { - const int : 0; + const int : 0; // expected-error{{anonymous bit-field cannot have qualifiers}} }; B b; diff --git a/test/CXX/drs/dr22xx.cpp b/test/CXX/drs/dr22xx.cpp new file mode 100644 index 0000000000..55b3d78207 --- /dev/null +++ b/test/CXX/drs/dr22xx.cpp @@ -0,0 +1,17 @@ +// RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors +// RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors +// RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors +// RUN: %clang_cc1 -std=c++1z -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors + +namespace dr2229 { // dr2229: yes +struct AnonBitfieldQualifiers { + const unsigned : 1; // expected-error {{anonymous bit-field cannot have qualifiers}} + volatile unsigned : 1; // expected-error {{anonymous bit-field cannot have qualifiers}} + const volatile unsigned : 1; // expected-error {{anonymous bit-field cannot have qualifiers}} + + unsigned : 1; + const unsigned i1 : 1; + volatile unsigned i2 : 1; + const volatile unsigned i3 : 1; +}; +} -- 2.40.0