================
@@ -9079,6 +9051,125 @@ static void 
HandleHLSLParamModifierAttr(TypeProcessingState &State,
   }
 }
 
+/// Return true if the type built at \p chunkIndex is part of a function's
+/// return type, i.e. a function chunk wraps it at any level. In both
+/// `int *__counted_by(n) (*fp)(void)` and `int *__counted_by(n) *(*fp)(void)`,
+/// the attributed pointer is in the return type of the function `fp` points 
to.
+///
+/// Chunk 0 is the one closest to the identifier, which is the outermost part 
of
+/// the type, so the type built by chunk i is wrapped by chunk i - 1. A
+/// parameter has its own declarator, so it is never part of \p D's chunks.
+static bool isOnFunctionReturnType(const Declarator &D, unsigned chunkIndex) {
+  assert(chunkIndex <= D.getNumTypeObjects());
+  for (unsigned i = chunkIndex; i != 0; --i)
+    if (D.getTypeObject(i - 1).Kind == DeclaratorChunk::Function)
+      return true;
+  return false;
+}
+
+/// Return the number of pointer and array chunks that wrap the type built at
+/// \p chunkIndex, up to the nearest function chunk. The level is relative to
+/// the type the attribute belongs to: the declared type, or the return type of
+/// the nearest enclosing function.
+///
+/// In `int *__counted_by(n) *pp`, the attributed pointer is chunk 1, wrapped 
by
+/// the pointer at chunk 0, so its level is 1. In
+/// `int *__counted_by(n) *(*fp)(void)`, the attributed pointer is chunk 4,
+/// wrapped by the pointer at chunk 3 and then the function at chunk 2, so its
+/// level is 1, relative to the return type.
+static unsigned getPointerNestLevel(const Declarator &D, unsigned chunkIndex) {
+  assert(chunkIndex <= D.getNumTypeObjects());
+  unsigned Level = 0;
+  for (unsigned i = chunkIndex; i != 0; --i) {
+    switch (D.getTypeObject(i - 1).Kind) {
+    case DeclaratorChunk::Function:
+      return Level;
+    case DeclaratorChunk::Pointer:
+    case DeclaratorChunk::Array:
+      ++Level;
+      break;
+    default:
+      break;
+    }
+  }
+  return Level;
+}
+
+/// A counted_by-family attribute written in the decl-spec applies to the
+/// top-level type of each declarator. Move it to the top-level declarator
+/// chunk, skipping parentheses, so that `__counted_by(n) int *p` is handled
+/// like `int *__counted_by(n) p`. Return false if there is no such chunk, in
+/// which case the decl-spec type is the top-level type.
+///
+/// FIXME: One written after the type specifier, as in
+/// `int __counted_by(n) *p`, should apply to the adjacent type and be
+/// rejected. Moving it to the top-level type is an existing bug, kept for now.
+/// See https://github.com/llvm/llvm-project/issues/241454.
+static bool distributeBoundsAttrFromDeclSpec(TypeProcessingState &State,
+                                             ParsedAttr &Attr) {
+  Declarator &D = State.getDeclarator();
+  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
+    DeclaratorChunk &Chunk = D.getTypeObject(i);
+    if (Chunk.Kind == DeclaratorChunk::Paren)
+      continue;
+    // The DeclSpec attributes are shared by every declarator, so restore
+    // them once this one is processed.
+    State.saveDeclSpecAttrs();
+    moveAttrFromListToList(Attr, State.getCurrentAttributes(),
+                           Chunk.getAttrs());
+    return true;
+  }
+  return false;
+}
+
+static void HandleCountedByAttrOnType(TypeProcessingState &State,
+                                      QualType &CurType, ParsedAttr &Attr) {
+  Sema &S = State.getSema();
+
+  // This attribute is only supported in C.
+  if (!Attr.diagnoseLangOpts(S)) {
+    Attr.setInvalid();
+    return;
+  }
+
+  auto *CountExpr = Attr.getArgAsExpr(0);
+  if (!CountExpr)
+    return;
+
+  const Declarator &D = State.getDeclarator();
+  unsigned chunkIndex = State.getCurrentChunkIndex();
+
+  Sema::BoundsAttrFlags Flags;
+  if (!S.ValidateBoundsAttrTypeForTypePosition(
+          CurType, Attr.getKind(), Attr.getLoc(), Attr.getRange(),
+          getPointerNestLevel(D, chunkIndex),
+          isOnFunctionReturnType(D, chunkIndex), Flags)) {
+    Attr.setInvalid();
+    return;
+  }
+
+  // For a field, check the record and the count before building the type, so
+  // a rejected attribute never produces a CountAttributedType. The record, or
+  // the Objective-C container of an instance variable, being defined is the
+  // current context.
+  if (D.getContext() == DeclaratorContext::Member) {
+    if (S.CheckCountedByAttrInRecord(S.CurContext, CurType, CountExpr,
+                                     Attr.getLoc(), Attr.getRange(),
+                                     Flags.CountInBytes, Flags.OrNull)) {
+      Attr.setInvalid();
+      return;
+    }
+  }
+
+  QualType NewType = S.BuildCountAttributedArrayOrPointerType(
+      CurType, CountExpr, Flags.CountInBytes, Flags.OrNull);
+  if (NewType.isNull()) {
+    Attr.setInvalid();
+    return;
+  }
+  CurType = NewType;
+}
+
 bool Sema::ProcessLateParsedTypeAttr(LateParsedTypeAttrInfo &LA, QualType &QT) 
{
   // A DeclSpec attribute runs this once per declarator, as in
   // `IP __counted_by(n) a, b;`. Reuse one node, as the eager path does, so no
----------------
delcypher wrote:

I think it might mean "integer pointer" and I think this needs to be ripped out 
everywhere it's been added as it's really not obvious or much shorter than 
writing `int*`.

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

Reply via email to