[clang] af189c8 - Fix constant evaluation of zero-initialization of a union whose first
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 22 17:04:11 PDT 2020
Author: Richard Smith
Date: 2020-10-22T17:03:59-07:00
New Revision: af189c8ab113668434eae5c71f174613a9e64625
URL: https://github.com/llvm/llvm-project/commit/af189c8ab113668434eae5c71f174613a9e64625
DIFF: https://github.com/llvm/llvm-project/commit/af189c8ab113668434eae5c71f174613a9e64625.diff
LOG: Fix constant evaluation of zero-initialization of a union whose first
FieldDecl is an unamed bitfield.
Unnamed bitfields aren't non-static data member, so such a bitfield
isn't actually the first non-static data member.
Added:
Modified:
clang/lib/AST/ExprConstant.cpp
clang/test/SemaCXX/constant-expression-cxx11.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e978a190a195..c6fc02a6fe91 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9630,6 +9630,8 @@ bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) {
// C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
// object's first non-static named data member is zero-initialized
RecordDecl::field_iterator I = RD->field_begin();
+ while (I != RD->field_end() && (*I)->isUnnamedBitfield())
+ ++I;
if (I == RD->field_end()) {
Result = APValue((const FieldDecl*)nullptr);
return true;
diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index 0d67b747a0f4..7d51a48eac42 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -2021,6 +2021,14 @@ namespace Bitfields {
const HasUnnamedBitfield oneZero{1, 0};
int b = 1 / oneZero.b; // expected-warning {{division by zero is undefined}}
}
+
+ union UnionWithUnnamedBitfield {
+ int : 3;
+ int n;
+ };
+ static_assert(UnionWithUnnamedBitfield().n == 0, "");
+ static_assert(UnionWithUnnamedBitfield{}.n == 0, "");
+ static_assert(UnionWithUnnamedBitfield{1}.n == 1, "");
}
namespace ZeroSizeTypes {
More information about the cfe-commits
mailing list