[clang] nonblocking/nonallocating attributes (was: nolock/noalloc) (PR #84983)

Doug Wyatt via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 10 10:25:48 PDT 2024


================
@@ -7959,6 +7979,122 @@ static Attr *getCCTypeAttr(ASTContext &Ctx, ParsedAttr &Attr) {
   llvm_unreachable("unexpected attribute kind!");
 }
 
+static bool
+handleNonBlockingNonAllocatingTypeAttr(TypeProcessingState &state,
+                                       ParsedAttr &PAttr, QualType &type,
+                                       FunctionTypeUnwrapper &unwrapped) {
+  // Delay if this is not a function type.
+  if (!unwrapped.isFunctionType())
+    return false;
+
+  const bool isNonBlocking = PAttr.getKind() == ParsedAttr::AT_NonBlocking ||
+                             PAttr.getKind() == ParsedAttr::AT_Blocking;
+  Sema &S = state.getSema();
+
+  // Require FunctionProtoType
+  auto *FPT = unwrapped.get()->getAs<FunctionProtoType>();
+  if (FPT == nullptr) {
+    // TODO: special diagnostic?
+    return false;
+  }
+
+  bool Cond = true; // default
+
+  if (PAttr.getKind() == ParsedAttr::AT_NonBlocking ||
+      PAttr.getKind() == ParsedAttr::AT_NonAllocating) {
+    // Parse the conditional expression, if any
+    // TODO: There's a better way to do this. See PR feedback.
+    // TODO: Handle a type-dependent expression.
+    if (PAttr.getNumArgs() > 0) {
+      if (!S.checkBoolExprArgumentAttr(PAttr, 0, Cond)) {
+        PAttr.setInvalid();
+        return false;
+      }
+    }
+  } else {
+    Cond = false;
+  }
+
+  FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
+
+  auto incompatible = [&](StringRef attrTrue, StringRef attrFalse) {
+    Sema &S = state.getSema();
+    S.Diag(PAttr.getLoc(), diag::err_attributes_are_not_compatible)
+        << attrTrue << attrFalse << false;
+    // we don't necessarily have the location of the previous attribute,
+    // so no note.
+    PAttr.setInvalid();
+    return true;
+  };
+
+  // check nonblocking(true) against blocking, and same for
+  // nonallocating
+  const BoolAttrState newState =
+      Cond ? BoolAttrState::True : BoolAttrState::False;
+  const BoolAttrState oppositeNewState =
+      Cond ? BoolAttrState::False : BoolAttrState::True;
----------------
dougsonos wrote:

Yeah I've already munged these lines...

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


More information about the cfe-commits mailing list