File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 2
2
#define TEST_INTEROP_CXX_CLASS_INPUTS_MEMBER_VARIABLES_H
3
3
4
4
#include < cstddef>
5
+ #include < type_traits>
5
6
#include < optional>
6
7
7
8
class MyClass {
@@ -46,6 +47,25 @@ struct ReuseOptionalFieldPaddingWithTypedef {
46
47
int offset () const { return offsetof (ReuseOptionalFieldPadding, c); }
47
48
};
48
49
50
+ // Using a mix of private and public fields prevents this class from being
51
+ // standard-layout, which is necessary to allow clang to reuse its padding.
52
+ template <typename T>
53
+ struct NonStandardLayoutClass {
54
+ private:
55
+ T x;
56
+ public:
57
+ char pad_me;
58
+ };
59
+ static_assert (std::is_standard_layout_v<NonStandardLayoutClass<int >> == false );
60
+
61
+ struct ReuseNonStandardLayoutFieldPadding {
62
+ [[no_unique_address]] NonStandardLayoutClass<int > a;
63
+ char c;
64
+ char get_c () const { return c; }
65
+ void set_c (char c) { this ->c = c; }
66
+ // C-style implementation of offsetof() to avoid non-standard-layout warning
67
+ int offset () const { return (char *) &this ->c - (char *) this ; }
68
+ };
49
69
50
70
inline int takesZeroSizedInCpp (HasZeroSizedField x) {
51
71
return x.a ;
Original file line number Diff line number Diff line change @@ -54,4 +54,18 @@ FieldsTestSuite.test("Typedef'd optional field padding reused") {
54
54
expectEqual ( s2. get_c ( ) , 6 )
55
55
}
56
56
57
+ FieldsTestSuite . test ( " Non-standard-layout field padding reused " ) {
58
+ var s = ReuseNonStandardLayoutFieldPadding ( )
59
+ s. c = 5
60
+ expectEqual ( Int ( s. offset ( ) ) , MemoryLayout < ReuseNonStandardLayoutFieldPadding > . offset ( of: \. c) !)
61
+ expectEqual ( s. c, 5 )
62
+ expectEqual ( s. get_c ( ) , 5 )
63
+ s. set_c ( 6 )
64
+ expectEqual ( s. c, 6 )
65
+ expectEqual ( s. get_c ( ) , 6 )
66
+ let s2 = s
67
+ expectEqual ( s2. c, 6 )
68
+ expectEqual ( s2. get_c ( ) , 6 )
69
+ }
70
+
57
71
runAllTests ( )
You can’t perform that action at this time.
0 commit comments