+class empty { };
class A1 {
public:
int y;
};
+#if !defined (__GNUC__) || __GNUC__ > 7
+# define NO_UNIQUE_ADDRESS [[no_unique_address]]
+#else
+# define NO_UNIQUE_ADDRESS
+#endif
+
+class A4 {
+public:
+ NO_UNIQUE_ADDRESS empty x;
+};
+
class X : public A1, public A2 {
public:
int z;
int jva1v;
};
+class JE : public A1, public A4 {
+public:
+};
+
int main()
{
A1 a1;
JVA1 jva1;
JVA2 jva2;
JVA1V jva1v;
+ JE je;
int i;
jva1v.i = 4;
jva1v.jva1v = 5;
+ je.A1::x = 1;
+
return 0; /* set breakpoint here */
}
# JVA1V is derived from A1; A1 is a virtual base indirectly
# and also directly; must not report ambiguity when a JVA1V is cast to an A1.
gdb_test "print (A1)jva1v" " = {x = 1, y = 2}"
+
+# C++20 introduced a way to have ambiguous fields with the same byte offset.
+# This class explicitly tests for that.
+# if this is tested with a compiler that can't handle [[no_unique_address]]
+# the code should still correctly identify the ambiguity because of
+# different byte offsets.
+test_ambiguous "je.x" "x" "JE" {
+ "'int A1::x' (JE -> A1)"
+ "'empty A4::x' (JE -> A4)"
+}
space. */
if (m_fields.empty () || m_last_boffset != boffset)
m_fields.push_back ({m_struct_path, v});
+ else
+ {
+ /*Fields can occupy the same space and have the same name (be
+ ambiguous). This can happen when fields in two different base
+ classes are marked [[no_unique_address]] and have the same name.
+ The C++ standard says that such fields can only occupy the same
+ space if they are of different type, but we don't rely on that in
+ the following code. */
+ bool ambiguous = false, insert = true;
+ for (const found_field &field: m_fields)
+ {
+ if(field.path.back () != m_struct_path.back ())
+ {
+ /* Same boffset points to members of different classes.
+ We have found an ambiguity and should record it. */
+ ambiguous = true;
+ }
+ else
+ {
+ /* We don't need to insert this value again, because a
+ non-ambiguous path already leads to it. */
+ insert = false;
+ break;
+ }
+ }
+ if (ambiguous && insert)
+ m_fields.push_back ({m_struct_path, v});
+ }
}
}
}