[clang] [Clang] Add __builtin_is_within_lifetime to implement P2641R4's std::is_within_lifetime (PR #91895)

Mital Ashok via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 5 11:16:09 PDT 2024


================
@@ -1832,6 +1832,33 @@ static ExprResult BuiltinLaunder(Sema &S, CallExpr *TheCall) {
   return TheCall;
 }
 
+static ExprResult BuiltinIsWithinLifetime(Sema &S, CallExpr *TheCall) {
+  if (checkArgCount(S, TheCall, 1))
+    return ExprError();
+
+  ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0));
+  if (Arg.isInvalid())
+    return ExprError();
+  QualType ParamTy = Arg.get()->getType();
+  TheCall->setArg(0, Arg.get());
+  TheCall->setType(S.Context.BoolTy);
+
+  // A call to this function is always ill-formed if the type is not a pointer
+  // to an object type. There is no Mandates: to that effect, so we can only
+  // issue an error if it is actually evaluated as part of a constant evaluation
+  // (e.g., `false ? true :
----------------
MitalAshok wrote:

You can write `std::is_within_lifetime<int()>(&function)` (https://compiler-explorer.com/z/GYn1sEjd8). It's not too much of a hassle to accept function pointers and fail them later since they have the same representation as object pointers during constant evaluation

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


More information about the cfe-commits mailing list