Unnamed bitfields don't block constant evaluation of constexpr ctors

C++14 [dcl.constexpr]p4 states that in the body of a constexpr
constructor,

> every non-variant non-static data member and base class sub-object
  shall be initialized

However, [class.bit]p2 notes that

> Unnamed bit-fields are not members and cannot be initialized.

Therefore, we should make sure to filter them out of the check that
all fields are initialized.

Fixing this makes the constant evaluator a bit smarter, and
specifically allows constexpr constructors to avoid tripping
-Wglobal-constructors when the type contains unnamed bitfields.

Reviewed at https://reviews.llvm.org/D39035.

llvm-svn: 316408
This commit is contained in:
Jordan Rose
2017-10-24 02:17:07 +00:00
parent bd83ad4a87
commit d4503da40c
3 changed files with 38 additions and 0 deletions

View File

@@ -1818,6 +1818,9 @@ static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
}
}
for (const auto *I : RD->fields()) {
if (I->isUnnamedBitfield())
continue;
if (!CheckConstantExpression(Info, DiagLoc, I->getType(),
Value.getStructField(I->getFieldIndex())))
return false;

View File

@@ -1931,6 +1931,22 @@ namespace Bitfields {
};
static_assert(X::f(3) == -1, "3 should truncate to -1");
}
struct HasUnnamedBitfield {
unsigned a;
unsigned : 20;
unsigned b;
constexpr HasUnnamedBitfield() : a(), b() {}
constexpr HasUnnamedBitfield(unsigned a, unsigned b) : a(a), b(b) {}
};
void testUnnamedBitfield() {
const HasUnnamedBitfield zero{};
int a = 1 / zero.b; // expected-warning {{division by zero is undefined}}
const HasUnnamedBitfield oneZero{1, 0};
int b = 1 / oneZero.b; // expected-warning {{division by zero is undefined}}
}
}
namespace ZeroSizeTypes {

View File

@@ -126,3 +126,22 @@ namespace pr20420 {
void *array_storage[1];
const int &global_reference = *(int *)array_storage;
}
namespace bitfields {
struct HasUnnamedBitfield {
unsigned a;
unsigned : 20;
unsigned b;
constexpr HasUnnamedBitfield() : a(), b() {}
constexpr HasUnnamedBitfield(unsigned a, unsigned b) : a(a), b(b) {}
explicit HasUnnamedBitfield(unsigned a) {}
};
const HasUnnamedBitfield zeroConst{};
HasUnnamedBitfield zeroMutable{};
const HasUnnamedBitfield explicitConst{1, 2};
HasUnnamedBitfield explicitMutable{1, 2};
const HasUnnamedBitfield nonConstexprConst{1}; // expected-warning {{global constructor}}
HasUnnamedBitfield nonConstexprMutable{1}; // expected-warning {{global constructor}}
}