[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)

Yeoul Na via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 19 17:03:50 PST 2024


================
@@ -8463,133 +8463,115 @@ static void handleZeroCallUsedRegsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
   D->addAttr(ZeroCallUsedRegsAttr::Create(S.Context, Kind, AL));
 }
 
-static void handleCountedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
-  if (!AL.isArgIdent(0)) {
-    S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
-        << AL << AANT_ArgumentIdentifier;
-    return;
+static const RecordDecl *GetEnclosingNamedOrTopAnonRecord(const FieldDecl *FD) {
+  const auto *RD = FD->getParent();
+  // An unnamed struct is anonymous struct only if it's not instantiated.
+  // However, the struct may not be fully processed yet to determine
+  // whether it's anonymous or not. In that case, this function treats it as
+  // an anonymous struct and tries to find a named parent.
+  while (RD && (RD->isAnonymousStructOrUnion() ||
----------------
rapidsna wrote:

@bwendling I updated the logic so that it distinguishes between anonymous struct and unnamed struct that's still processing so whether it's anonymous struct is not determined yet. We treat it as anonymous struct here and what that means is that we couldn't handle the error case like below here: 

```
struct Parent {
  struct {
    int count;
  };
  struct {
    int dummy;
    int arr[__counted_by(count)]; // error: 'counted_by' field 'count' isn't within the same struct as the flexible array
  } unnamed_s; // anonymous struct
};
```

but such a remaining error case will be handled later in https://github.com/llvm/llvm-project/pull/78000/files#diff-bf37b284dfa0a76af702a5493cde01dec7f88619533c1be15e74654862299770R4841 where the unnamed struct is determined whether it's actually anonymous or not.

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


More information about the cfe-commits mailing list