================
@@ -1839,6 +1839,65 @@ static void handleRestrictAttr(Sema &S, Decl *D, const 
ParsedAttr &AL) {
                  RestrictAttr(S.Context, AL, DeallocE, DeallocPtrIdx));
 }
 
+bool Sema::CheckSpanLikeType(const AttributeCommonInfo &CI,
+                             const QualType &Ty) {
+  // Note that there may also be numerous cases of pointer + integer /
+  // pointer + pointer / integer + pointer structures not actually exhibiting
+  // a span-like semantics, so sometimes these heuristics expectedly
+  // lead to false positive results.
+  auto emitWarning = [this, &CI](unsigned NoteDiagID) {
+    Diag(CI.getLoc(), diag::warn_attribute_return_span_only) << CI;
+    return Diag(CI.getLoc(), NoteDiagID);
+  };
+  if (Ty->isDependentType())
+    return false;
+  // isCompleteType is used to force template class instantiation.
+  if (!isCompleteType(CI.getLoc(), Ty))
+    return emitWarning(diag::note_returned_incomplete_type);
+  const RecordDecl *RD = Ty->getAsRecordDecl();
+  if (!RD || RD->isUnion())
+    return emitWarning(diag::note_returned_not_struct);
+  auto FieldsBegin = RD->field_begin();
+  const auto FieldsCount = std::distance(FieldsBegin, RD->field_end());
----------------
AaronBallman wrote:

Just making sure we're okay with this, but I think this means we're accepting:
```
struct S {
  void *ptr;
  int len;
  static int not_a_field;
};
```
(We should probably have a test case showing we thought about this situation. I 
don't have strong opinions on accept vs reject for it, but lean towards reject.)

Also, what do we think about base classes? e.g.,
```
struct S {
  void *ptr;
};

struct D : S {
  void *ptr;
  int len;
};
```
I don't think that's span-like, so I think the presence of a non-empty base 
class should probably be an error?

Also, per our coding standards, you should drop the top-level `const` uses from 
this function (it's fine to have `const T *` but `const T` is something we 
generally don't do for local variables).

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

Reply via email to