================
@@ -222,3 +222,49 @@ struct on_void_ty {
   // expected-error@+1{{field has incomplete type 'void'}}
   void wrong_ty __counted_by_or_null(count);
 };
+
+//==============================================================================
+// __counted_by_or_null on pointer members in unions
+//==============================================================================
+
+// Pointer in anonymous union with count in parent struct - OK
+struct ptr_in_anon_union_count_in_parent {
+  int count;
+  union {
+    int a;
+    struct size_known *buf __counted_by_or_null(count);
+  };
+};
+
+// Pointer in named union - ERROR
+union ptr_in_named_union {
+  int count;
+  struct size_known *buf __counted_by_or_null(count); // expected-error 
{{'counted_by_or_null' cannot be applied to a union member}}
+};
+
+// Both pointer and count in same anonymous union - ERROR (they share storage)
+struct ptr_and_count_in_same_anon_union {
+  union {
+    int count;
+    struct size_known *buf __counted_by_or_null(count); // expected-error 
{{'counted_by_or_null' cannot be applied to a union member}}
+  };
+};
+
+// Count in anonymous union, pointer in parent struct - ERROR (count in union)
+struct count_in_anon_union_ptr_in_parent {
+  union {
+    int count;
+    int x;
+  };
+  struct size_known *buf __counted_by_or_null(count); // expected-error 
{{'counted_by_or_null' argument cannot refer to a union member}}
+};
+
+// Count in anonymous union, but hidden by struct - ERROR (count in union)
+struct count_in_deep_anon_union {
+  union {
+    struct {
+      int count;
+    };
+  };
+  struct size_known *buf __counted_by_or_null(count); // expected-error 
{{'counted_by_or_null' argument cannot refer to a union member}}
+};
----------------
ojhunt wrote:

Can we also add a test for
```c
struct count_in_deep_anon_union {
  union {
    struct {
      int count;
      struct size_known *buf1 __counted_by_or_null(count); // Should this be 
valid or invalid?
    };
  };
  struct size_known *buf2 __counted_by_or_null(count); // This should still be 
invalid
};
```

From the code I _think_ that the behavior that will fall out of 
`HasUnionInAncestors` is that the buf1 annotation will be considered invalid 
but it would worth adding a test for this. Assuming it does get reported as 
invalid I think that's fine as rejecting such obtuse code seems more than 
reasonable.

https://github.com/llvm/llvm-project/pull/171996
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to