[PATCH] D28427: Allow constexpr construction of subobjects unconditionally, not just in C++14.
This revision was automatically updated to reflect the committed changes. Closed by commit rL291480: Allow constexpr construction of subobjects unconditionally, not just in C++14. (authored by dlj). Changed prior to commit: https://reviews.llvm.org/D28427?vs=83650&id=83686#toc Repository: rL LLVM https://reviews.llvm.org/D28427 Files: cfe/trunk/lib/AST/ExprConstant.cpp cfe/trunk/test/CXX/basic/basic.start/basic.start.init/p2.cpp cfe/trunk/test/CodeGenCXX/global-array-destruction.cpp Index: cfe/trunk/test/CodeGenCXX/global-array-destruction.cpp === --- cfe/trunk/test/CodeGenCXX/global-array-destruction.cpp +++ cfe/trunk/test/CodeGenCXX/global-array-destruction.cpp @@ -39,17 +39,17 @@ T t[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @t to %struct.T*), i64 6 +// CHECK: getelementptr inbounds ({{.*}} getelementptr inbounds ([2 x [3 x {{.*}}]], [2 x [3 x {{.*}}]]* @t, i32 0, i32 0, i32 0), i64 6) // CHECK: call void @_ZN1TD1Ev // CHECK: icmp eq {{.*}} @t // CHECK: br i1 {{.*}} static T t2[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @_ZL2t2 to %struct.T*), i64 6 +// CHECK: getelementptr inbounds ({{.*}} getelementptr inbounds ([2 x [3 x {{.*}}]], [2 x [3 x {{.*}}]]* @_ZL2t2, i32 0, i32 0, i32 0), i64 6) // CHECK: call void @_ZN1TD1Ev -// CHECK: icmp eq {{.*}} @_ZL2t +// CHECK: icmp eq {{.*}} @_ZL2t2 // CHECK: br i1 {{.*}} using U = T[2][3]; Index: cfe/trunk/test/CXX/basic/basic.start/basic.start.init/p2.cpp === --- cfe/trunk/test/CXX/basic/basic.start/basic.start.init/p2.cpp +++ cfe/trunk/test/CXX/basic/basic.start/basic.start.init/p2.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++11 +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++14 +// expected-no-diagnostics + +struct foo_t { + union { +int i; +volatile int j; + } u; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_t x = {{0}}; + +union foo_u { + int i; + volatile int j; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_u y = {0}; Index: cfe/trunk/lib/AST/ExprConstant.cpp === --- cfe/trunk/lib/AST/ExprConstant.cpp +++ cfe/trunk/lib/AST/ExprConstant.cpp @@ -1627,8 +1627,17 @@ // C++1y: A constant initializer for an object o [...] may also invoke // constexpr constructors for o and its subobjects even if those objects // are of non-literal class types. - if (Info.getLangOpts().CPlusPlus14 && This && - Info.EvaluatingDecl == This->getLValueBase()) + // + // C++11 missed this detail for aggregates, so classes like this: + // struct foo_t { union { int i; volatile int j; } u; }; + // are not (obviously) initializable like so: + // __attribute__((__require_constant_initialization__)) + // static const foo_t x = {{0}}; + // because "i" is a subobject with non-literal initialization (due to the + // volatile member of the union). See: + // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 + // Therefore, we use the C++1y behavior. + if (This && Info.EvaluatingDecl == This->getLValueBase()) return true; // Prvalue constant expressions must be of literal types. Index: cfe/trunk/test/CodeGenCXX/global-array-destruction.cpp === --- cfe/trunk/test/CodeGenCXX/global-array-destruction.cpp +++ cfe/trunk/test/CodeGenCXX/global-array-destruction.cpp @@ -39,17 +39,17 @@ T t[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @t to %struct.T*), i64 6 +// CHECK: getelementptr inbounds ({{.*}} getelementptr inbounds ([2 x [3 x {{.*}}]], [2 x [3 x {{.*}}]]* @t, i32 0, i32 0, i32 0), i64 6) // CHECK: call void @_ZN1TD1Ev // CHECK: icmp eq {{.*}} @t // CHECK: br i1 {{.*}} static T t2[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @_ZL2t2 to %struct.T*), i64 6 +// CHECK: getelementptr inbounds ({{.*}} getelementptr inbounds ([2 x [3 x {{.*}}]], [2 x [3 x {{.*}}]]* @_ZL2t2, i32 0, i32 0, i32 0), i64 6) // CHECK: call void @_ZN1TD1Ev -// CHECK: icmp eq {{.*}} @_ZL2t +// CHECK: icmp eq {{.*}} @_ZL2t2 // CHECK: br i1 {{.*}} using U = T[2][3]; Index: cfe/trunk/test/CXX/basic/basic.start/basic.start.init/p2.cpp === --- cfe/trunk/test/CXX/basic/basic.start/basic.start.init/p2.cpp +++ cfe/trunk/test/CXX/basic/basic.start/basic
[PATCH] D28427: Allow constexpr construction of subobjects unconditionally, not just in C++14.
rsmith accepted this revision. rsmith added a comment. This revision is now accepted and ready to land. LGTM https://reviews.llvm.org/D28427 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D28427: Allow constexpr construction of subobjects unconditionally, not just in C++14.
dlj updated this revision to Diff 83650. dlj added a comment. - Fix lit checks. https://reviews.llvm.org/D28427 Files: lib/AST/ExprConstant.cpp test/CXX/basic/basic.start/basic.start.init/p2.cpp test/CodeGenCXX/global-array-destruction.cpp Index: test/CodeGenCXX/global-array-destruction.cpp === --- test/CodeGenCXX/global-array-destruction.cpp +++ test/CodeGenCXX/global-array-destruction.cpp @@ -39,17 +39,17 @@ T t[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @t to %struct.T*), i64 6 +// CHECK: getelementptr inbounds ({{.*}} getelementptr inbounds ([2 x [3 x {{.*}}]], [2 x [3 x {{.*}}]]* @t, i32 0, i32 0, i32 0), i64 6) // CHECK: call void @_ZN1TD1Ev // CHECK: icmp eq {{.*}} @t // CHECK: br i1 {{.*}} static T t2[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @_ZL2t2 to %struct.T*), i64 6 +// CHECK: getelementptr inbounds ({{.*}} getelementptr inbounds ([2 x [3 x {{.*}}]], [2 x [3 x {{.*}}]]* @_ZL2t2, i32 0, i32 0, i32 0), i64 6) // CHECK: call void @_ZN1TD1Ev -// CHECK: icmp eq {{.*}} @_ZL2t +// CHECK: icmp eq {{.*}} @_ZL2t2 // CHECK: br i1 {{.*}} using U = T[2][3]; Index: test/CXX/basic/basic.start/basic.start.init/p2.cpp === --- /dev/null +++ test/CXX/basic/basic.start/basic.start.init/p2.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++11 +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++14 +// expected-no-diagnostics + +struct foo_t { + union { +int i; +volatile int j; + } u; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_t x = {{0}}; + +union foo_u { + int i; + volatile int j; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_u y = {0}; Index: lib/AST/ExprConstant.cpp === --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -1627,8 +1627,17 @@ // C++1y: A constant initializer for an object o [...] may also invoke // constexpr constructors for o and its subobjects even if those objects // are of non-literal class types. - if (Info.getLangOpts().CPlusPlus14 && This && - Info.EvaluatingDecl == This->getLValueBase()) + // + // C++11 missed this detail for aggregates, so classes like this: + // struct foo_t { union { int i; volatile int j; } u; }; + // are not (obviously) initializable like so: + // __attribute__((__require_constant_initialization__)) + // static const foo_t x = {{0}}; + // because "i" is a subobject with non-literal initialization (due to the + // volatile member of the union). See: + // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 + // Therefore, we use the C++1y behavior. + if (This && Info.EvaluatingDecl == This->getLValueBase()) return true; // Prvalue constant expressions must be of literal types. Index: test/CodeGenCXX/global-array-destruction.cpp === --- test/CodeGenCXX/global-array-destruction.cpp +++ test/CodeGenCXX/global-array-destruction.cpp @@ -39,17 +39,17 @@ T t[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @t to %struct.T*), i64 6 +// CHECK: getelementptr inbounds ({{.*}} getelementptr inbounds ([2 x [3 x {{.*}}]], [2 x [3 x {{.*}}]]* @t, i32 0, i32 0, i32 0), i64 6) // CHECK: call void @_ZN1TD1Ev // CHECK: icmp eq {{.*}} @t // CHECK: br i1 {{.*}} static T t2[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @_ZL2t2 to %struct.T*), i64 6 +// CHECK: getelementptr inbounds ({{.*}} getelementptr inbounds ([2 x [3 x {{.*}}]], [2 x [3 x {{.*}}]]* @_ZL2t2, i32 0, i32 0, i32 0), i64 6) // CHECK: call void @_ZN1TD1Ev -// CHECK: icmp eq {{.*}} @_ZL2t +// CHECK: icmp eq {{.*}} @_ZL2t2 // CHECK: br i1 {{.*}} using U = T[2][3]; Index: test/CXX/basic/basic.start/basic.start.init/p2.cpp === --- /dev/null +++ test/CXX/basic/basic.start/basic.start.init/p2.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++11 +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++14 +// expected-no-diagnostics + +struct foo_t { + union { +int i; +volatile int j; + } u; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_t x = {{0}}; + +union foo_u { + int i; + volatile int j; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_u y = {0}; Index:
[PATCH] D28427: Allow constexpr construction of subobjects unconditionally, not just in C++14.
dlj added a comment. Test added, and fixed another one that I missed before. https://reviews.llvm.org/D28427 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D28427: Allow constexpr construction of subobjects unconditionally, not just in C++14.
dlj updated this revision to Diff 83647. dlj added a comment. - Add a test, and fix codegen test (the array init is now constant, even though there is a dtor). https://reviews.llvm.org/D28427 Files: lib/AST/ExprConstant.cpp test/CXX/basic/basic.start/basic.start.init/p2.cpp test/CodeGenCXX/global-array-destruction.cpp Index: test/CodeGenCXX/global-array-destruction.cpp === --- test/CodeGenCXX/global-array-destruction.cpp +++ test/CodeGenCXX/global-array-destruction.cpp @@ -39,15 +39,17 @@ T t[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @t to %struct.T*), i64 6 +// CHECK: getelementptr inbounds {{.*}} +// CHECK: bitcast {{.*}} to %struct.T* // CHECK: call void @_ZN1TD1Ev // CHECK: icmp eq {{.*}} @t // CHECK: br i1 {{.*}} static T t2[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @_ZL2t2 to %struct.T*), i64 6 +// CHECK: getelementptr inbounds {{.*}} +// CHECK: bitcast {{.*}} to %struct.T* // CHECK: call void @_ZN1TD1Ev // CHECK: icmp eq {{.*}} @_ZL2t // CHECK: br i1 {{.*}} Index: test/CXX/basic/basic.start/basic.start.init/p2.cpp === --- /dev/null +++ test/CXX/basic/basic.start/basic.start.init/p2.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++11 +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++14 +// expected-no-diagnostics + +struct foo_t { + union { +int i; +volatile int j; + } u; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_t x = {{0}}; + +union foo_u { + int i; + volatile int j; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_u y = {0}; Index: lib/AST/ExprConstant.cpp === --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -1627,8 +1627,17 @@ // C++1y: A constant initializer for an object o [...] may also invoke // constexpr constructors for o and its subobjects even if those objects // are of non-literal class types. - if (Info.getLangOpts().CPlusPlus14 && This && - Info.EvaluatingDecl == This->getLValueBase()) + // + // C++11 missed this detail for aggregates, so classes like this: + // struct foo_t { union { int i; volatile int j; } u; }; + // are not (obviously) initializable like so: + // __attribute__((__require_constant_initialization__)) + // static const foo_t x = {{0}}; + // because "i" is a subobject with non-literal initialization (due to the + // volatile member of the union). See: + // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 + // Therefore, we use the C++1y behavior. + if (This && Info.EvaluatingDecl == This->getLValueBase()) return true; // Prvalue constant expressions must be of literal types. Index: test/CodeGenCXX/global-array-destruction.cpp === --- test/CodeGenCXX/global-array-destruction.cpp +++ test/CodeGenCXX/global-array-destruction.cpp @@ -39,15 +39,17 @@ T t[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @t to %struct.T*), i64 6 +// CHECK: getelementptr inbounds {{.*}} +// CHECK: bitcast {{.*}} to %struct.T* // CHECK: call void @_ZN1TD1Ev // CHECK: icmp eq {{.*}} @t // CHECK: br i1 {{.*}} static T t2[2][3] = { 1.0, 2, 3.0, 4, 5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12 }; // CHECK: call {{.*}} @__cxa_atexit -// CHECK: getelementptr inbounds ({{.*}} bitcast {{.*}}* @_ZL2t2 to %struct.T*), i64 6 +// CHECK: getelementptr inbounds {{.*}} +// CHECK: bitcast {{.*}} to %struct.T* // CHECK: call void @_ZN1TD1Ev // CHECK: icmp eq {{.*}} @_ZL2t // CHECK: br i1 {{.*}} Index: test/CXX/basic/basic.start/basic.start.init/p2.cpp === --- /dev/null +++ test/CXX/basic/basic.start/basic.start.init/p2.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++11 +// RUN: %clang_cc1 -verify %s -pedantic-errors -std=c++14 +// expected-no-diagnostics + +struct foo_t { + union { +int i; +volatile int j; + } u; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_t x = {{0}}; + +union foo_u { + int i; + volatile int j; +}; + +__attribute__((__require_constant_initialization__)) +static const foo_u y = {0}; Index: lib/AST/ExprConstant.cpp === --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -1627,8 +1627,17 @@ // C++1y: A constant initializer for an object o [...] may also invoke // constexpr constructor
[PATCH] D28427: Allow constexpr construction of subobjects unconditionally, not just in C++14.
rsmith added a comment. Can you add a test please? https://reviews.llvm.org/D28427 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D28427: Allow constexpr construction of subobjects unconditionally, not just in C++14.
dlj created this revision. dlj added a reviewer: rsmith. dlj added subscribers: cfe-commits, EricWF. Per https://wg21.link/CWG1677, the C++11 standard did not clarify that constant initialization of an object allowed constexpr brace-or-equal initialization of subobjects: struct foo_t { union { int i; volatile int j; } u; }; __attribute__((__require_constant_initialization__)) static const foo_t x = {{0}}; Because foo_t::u has a volatile member, the initializer for x fails. However, there is really no good reason, because this: union foo_u { int i; volatile int j; }; __attribute__((__require_constant_initialization__)) static const foo_u x = {0}; does have a constant initializer. (This was triggered by musl's pthread_mutex_t type when building under C++11.) https://reviews.llvm.org/D28427 Files: lib/AST/ExprConstant.cpp Index: lib/AST/ExprConstant.cpp === --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -1627,8 +1627,17 @@ // C++1y: A constant initializer for an object o [...] may also invoke // constexpr constructors for o and its subobjects even if those objects // are of non-literal class types. - if (Info.getLangOpts().CPlusPlus14 && This && - Info.EvaluatingDecl == This->getLValueBase()) + // + // C++11 missed this detail for aggregates, so classes like this: + // struct foo_t { union { int i; volatile int j; } u; }; + // are not (obviously) initializable like so: + // __attribute__((__require_constant_initialization__)) + // static const foo_t x = {{0}}; + // because "i" is a subobject with non-literal initialization (due to the + // volatile member of the union). See: + // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 + // Therefore, we use the C++1y behavior. + if (This && Info.EvaluatingDecl == This->getLValueBase()) return true; // Prvalue constant expressions must be of literal types. Index: lib/AST/ExprConstant.cpp === --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -1627,8 +1627,17 @@ // C++1y: A constant initializer for an object o [...] may also invoke // constexpr constructors for o and its subobjects even if those objects // are of non-literal class types. - if (Info.getLangOpts().CPlusPlus14 && This && - Info.EvaluatingDecl == This->getLValueBase()) + // + // C++11 missed this detail for aggregates, so classes like this: + // struct foo_t { union { int i; volatile int j; } u; }; + // are not (obviously) initializable like so: + // __attribute__((__require_constant_initialization__)) + // static const foo_t x = {{0}}; + // because "i" is a subobject with non-literal initialization (due to the + // volatile member of the union). See: + // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 + // Therefore, we use the C++1y behavior. + if (This && Info.EvaluatingDecl == This->getLValueBase()) return true; // Prvalue constant expressions must be of literal types. ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits