================
@@ -92,6 +94,97 @@ class RetainPtrSafetyModel : public PtrRefSafetyModel {
   RetainTypeChecker *retainTypeChecker() const override { return &RTC; }
 };
 
+QualType objectType(QualType T) {
+  while (!T.isNull()) {
+    QualType Pointee = T->getPointeeType();
+    if (Pointee.isNull())
+      break;
+    T = Pointee;
+  }
+  return T;
+}
+
+QualType borrowedType(QualType T) {
+  const auto *Specialization =
+      dyn_cast_or_null<ClassTemplateSpecializationDecl>(
+          T->getAsCXXRecordDecl());
+  if (!Specialization)
+    return QualType();
+  const auto &Args = Specialization->getTemplateArgs();
+  if (!Args.size() || Args[0].getKind() != TemplateArgument::Type)
+    return QualType();
+  return Args[0].getAsType();
+}
+
+class BorrowSafetyModel : public PtrRefSafetyModel {
+public:
+  std::optional<bool> isUnsafeType(QualType QT) const override {
+    return isView(QT);
+  }
+  std::optional<bool> isUnsafePtr(QualType QT, bool) const override {
+    return isView(QT);
+  }
+  bool isSafePtr(const CXXRecordDecl *Record) const override {
+    return isBorrow(Record);
+  }
+  bool isSafePtrType(QualType T) const override { return isBorrowType(T); }
+  bool isPtrType(const std::string &Name) const override {
+    return isBorrow(Name);
+  }
+
+  bool isSafeExpr(const Expr *Origin,
+                  bool CrossedLifetimeBound) const override {
+    if (!CrossedLifetimeBound)
+      return true;
+
+    QualType OriginType = objectType(Origin->getType());
+
+    if (OriginType.isNull())
+      return true;
+
+    if (isBorrowType(OriginType))
+      return true;
+
+    auto *Record = OriginType->getAsCXXRecordDecl();
+    if (!Record)
+      return true;
+
+    auto Borrowable = isBorrowable(Record);
+    return !Borrowable || !*Borrowable;
+  }
+
+  bool checksForInteriorDestruction() const override { return true; }
+  bool recognizesIndirectStores() const override { return true; }
+  const char *typeName() const override { return "CanBorrow type"; }
+
+  bool describeHazard(llvm::raw_ostream &Os, const Expr *Origin,
----------------
rniwa wrote:

Ideally, we'd define this for all other models.

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

Reply via email to