[clang] [clang] Add clang::preferred_type attribute for bitfields (PR #69104)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 19 07:15:43 PDT 2023


================
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -verify %s
+
+struct A {
+  enum E : unsigned {};
+  [[clang::preferred_type(E)]] unsigned b : 2;
+  [[clang::preferred_type(E)]] int b2 : 2;
+  // expected-warning at -1 {{underlying type 'unsigned int' of enumeration 'E' doesn't match bit-field type 'int'}}
+  [[clang::preferred_type(E)]] const unsigned b3 : 2;
+  [[clang::preferred_type(bool)]] unsigned b4 : 1;
+  [[clang::preferred_type(bool)]] unsigned b5 : 2;
+  // expected-warning at -1 {{bit-field that holds a boolean value should have width of 1 instead of 2}}
+  [[clang::preferred_type()]] unsigned b6 : 2;
+  // expected-error at -1 {{'preferred_type' attribute takes one argument}}
+  [[clang::preferred_type]] unsigned b7 : 2;
+  // expected-error at -1 {{'preferred_type' attribute takes one argument}}
+  [[clang::preferred_type(E, int)]] unsigned b8 : 2;
----------------
AaronBallman wrote:

Huh, something got confused there.... it's parsing `int` as though it were another attribute and not an argument to the `preferred_type` attribute. That surprises me!

https://github.com/llvm/llvm-project/pull/69104


More information about the cfe-commits mailing list