[clang] [Clang] Diagnose forming references to nullptr (PR #143667)

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 12 20:36:37 PDT 2025


================
@@ -5223,6 +5230,24 @@ enum EvalStmtResult {
 };
 }
 
+static bool EvaluateInitForDeclOfReferenceType(EvalInfo &Info,
+                                               const ValueDecl *D,
+                                               const Expr *Init, LValue &Result,
+                                               APValue &Val) {
+  assert(Init->isGLValue() && D->getType()->isReferenceType());
+  if (!EvaluateLValue(Init, Result, Info))
+    return false;
+  CompleteObject Obj = findCompleteObject(
+      Info, Init, AK_ReferenceInitialization, Result, Init->getType());
+  if (!Result.Designator.Invalid && Result.Designator.isOnePastTheEnd()) {
+    Info.FFDiag(Init, diag::note_constexpr_access_past_end)
+        << AK_ReferenceInitialization;
+    return false;
+  }
+  Result.moveInto(Val);
+  return !!Obj;
----------------
shafik wrote:

I feel like this line could use a comment explaining the semantics. I really like that you split this out into a function, it makes it much more readable. OTOH you could just comment the whole function on top explain the semantics of the return value.

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


More information about the cfe-commits mailing list