void updated this revision to Diff 514451.
void added a comment.
Herald added a subscriber: jdoerfert.

Fix failing testcase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148381/new/

https://reviews.llvm.org/D148381

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/AST/ASTImporter.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test

Index: clang/test/Misc/pragma-attribute-supported-attributes-list.test
===================================================================
--- clang/test/Misc/pragma-attribute-supported-attributes-list.test
+++ clang/test/Misc/pragma-attribute-supported-attributes-list.test
@@ -61,6 +61,7 @@
 // CHECK-NEXT: DiagnoseAsBuiltin (SubjectMatchRule_function)
 // CHECK-NEXT: DisableSanitizerInstrumentation (SubjectMatchRule_function, SubjectMatchRule_objc_method, SubjectMatchRule_variable_is_global)
 // CHECK-NEXT: DisableTailCalls (SubjectMatchRule_function, SubjectMatchRule_objc_method)
+// CHECK-NEXT: ElementCount (SubjectMatchRule_field)
 // CHECK-NEXT: EnableIf (SubjectMatchRule_function)
 // CHECK-NEXT: EnforceTCB (SubjectMatchRule_function, SubjectMatchRule_objc_method)
 // CHECK-NEXT: EnforceTCBLeaf (SubjectMatchRule_function, SubjectMatchRule_objc_method)
Index: clang/lib/Sema/SemaDeclAttr.cpp
===================================================================
--- clang/lib/Sema/SemaDeclAttr.cpp
+++ clang/lib/Sema/SemaDeclAttr.cpp
@@ -8238,6 +8238,12 @@
   D->addAttr(ZeroCallUsedRegsAttr::Create(S.Context, Kind, AL));
 }
 
+static void handleElementCountAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
+  // TODO: Probably needs more processing here. See Sema::AddAlignValueAttr.
+  IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
+  D->addAttr(::new (S.Context) ElementCountAttr(S.Context, AL, Name));
+}
+
 static void handleFunctionReturnThunksAttr(Sema &S, Decl *D,
                                            const ParsedAttr &AL) {
   StringRef KindStr;
@@ -9142,6 +9148,9 @@
   case ParsedAttr::AT_FunctionReturnThunks:
     handleFunctionReturnThunksAttr(S, D, AL);
     break;
+  case ParsedAttr::AT_ElementCount:
+    handleElementCountAttr(S, D, AL);
+    break;
 
   // Microsoft attributes:
   case ParsedAttr::AT_LayoutVersion:
Index: clang/lib/CodeGen/CGExpr.cpp
===================================================================
--- clang/lib/CodeGen/CGExpr.cpp
+++ clang/lib/CodeGen/CGExpr.cpp
@@ -946,6 +946,30 @@
         return CGF.getVLASize(VAT).NumElts;
       // Ignore pass_object_size here. It's not applicable on decayed pointers.
     }
+
+    if (auto *ME = dyn_cast<MemberExpr>(CE->getSubExpr())) {
+      if (ME->isFlexibleArrayMemberLike(CGF.getContext(),
+                                        StrictFlexArraysLevel, true)) {
+        if (auto *MD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
+          if (auto ECA = MD->getAttr<ElementCountAttr>()) {
+            RecordDecl *RD = MD->getParent();
+            IdentifierInfo *CountField = ECA->getElementCountField();
+
+            for (FieldDecl *FD : RD->fields()) {
+              if (FD->getName() != CountField->getName())
+                continue;
+
+              auto *Mem = MemberExpr::CreateImplicit(
+                  CGF.getContext(), const_cast<Expr*>(ME->getBase()), true, FD,
+                  FD->getType(), VK_LValue, OK_Ordinary);
+
+              IndexedType = Base->getType();
+              return CGF.EmitAnyExprToTemp(Mem).getScalarVal();
+            }
+          }
+        }
+      }
+    }
   }
 
   QualType EltTy{Base->getType()->getPointeeOrArrayElementType(), 0};
Index: clang/lib/AST/ASTImporter.cpp
===================================================================
--- clang/lib/AST/ASTImporter.cpp
+++ clang/lib/AST/ASTImporter.cpp
@@ -8979,6 +8979,11 @@
                   From->args_size());
     break;
   }
+  case attr::ElementCount: {
+    const auto *From = cast<ElementCountAttr>(FromAttr);
+    AI.importAttr(From, From->getElementCountField());
+    break;
+  }
 
   default: {
     // The default branch works for attributes that have no arguments to import.
Index: clang/include/clang/Basic/AttrDocs.td
===================================================================
--- clang/include/clang/Basic/AttrDocs.td
+++ clang/include/clang/Basic/AttrDocs.td
@@ -6950,3 +6950,10 @@
 its underlying representation to be a WebAssembly ``funcref``.
   }];
 }
+
+def ElementCountDocs : Documentation {
+  let Category = DocCatField;
+  let Content = [{
+Clang supports the ``element_count`` attribute for flexible array members.
+  }];
+}
Index: clang/include/clang/Basic/Attr.td
===================================================================
--- clang/include/clang/Basic/Attr.td
+++ clang/include/clang/Basic/Attr.td
@@ -4158,3 +4158,11 @@
   let Subjects = SubjectList<[Record]>;
   let Documentation = [ReadOnlyPlacementDocs];
 }
+
+def ElementCount : InheritableAttr {
+  let Spellings = [Clang<"element_count">];
+  let Subjects = SubjectList<[Field]>;
+  let Args = [IdentifierArgument<"ElementCountField">];
+  let Documentation = [ElementCountDocs];
+  let LangOpts = [COnly];
+}
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to