[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 06:14:17 PDT 2023


================
@@ -5910,6 +5910,28 @@ static void handleBuiltinAliasAttr(Sema &S, Decl *D,
   D->addAttr(::new (S.Context) BuiltinAliasAttr(S.Context, AL, Ident));
 }
 
+static void handleDebugInfoTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
+  if (!AL.hasParsedType()) {
+    S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
+    return;
+  }
+
+  TypeSourceInfo *ParmTSI = nullptr;
+  QualType type = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
+  assert(ParmTSI && "no type source info for attribute argument");
+
+  if (type->isEnumeralType()) {
+    QualType BitfieldType = llvm::cast<FieldDecl>(D)->getType();
+    QualType EnumUnderlyingType = type->getAs<EnumType>()->getDecl()->getIntegerType();
+    if (EnumUnderlyingType != BitfieldType) {
+      S.Diag(AL.getLoc(), diag::warn_attribute_underlying_type_mismatch) << EnumUnderlyingType << type << BitfieldType;
----------------
AaronBallman wrote:

Sorry, I completely missed this comment because it doesn't show up in the Files tab (how helpful).

`getArg()` is used to get arguments that are either an `Expr *` or an `IdentifierInfo *`, but this attribute accepts a type argument which is neither of those things. We have `getTypeArg()` (which is a pretty bad code smell because this only allows *one* type argument for an attribute -- it would probably be a reasonable idea to refactor this so it works through `getArg()` at some point. CC @erichkeane). This gets you a `ParsedType` which you'll need to call `Sema::GetTypeFromParser()` on to get the actual type information for, and that includes a `TypeSourceInfo *` which will have location information for the type. In the current code, you can use `ParmTSI` to get the `TypeLoc` for this.

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


More information about the cfe-commits mailing list