[clang] [clang][Interp] Do r-to-l conversion immediately when returning (PR #80662)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 5 08:00:00 PST 2024


================
@@ -119,12 +121,26 @@ template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
 template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
   if (!isActive())
     return true;
-  EvalResult.setPointer(S.Stk.pop<Pointer>());
+
+  const Pointer &Ptr = S.Stk.pop<Pointer>();
+  // Implicitly convert lvalue to rvalue, if requested.
+  if (ConvertResultToRValue) {
+    if (std::optional<APValue> V = Ptr.toRValue(Ctx)) {
+      EvalResult.setValue(*V);
+    } else {
+      return false;
+    }
+  } else {
+    EvalResult.setPointer(Ptr);
+  }
+
   return true;
 }
 template <> bool EvalEmitter::emitRet<PT_FnPtr>(const SourceInfo &Info) {
   if (!isActive())
     return true;
+  // Function pointers are always lvalues to us and cannot be converted
+  // to rvalues, so don't do any conversion here.
----------------
tbaederr wrote:

Nothing in `test/Sema/` seems to trip that assertion up (but that's just C). Note that there's also this comment in `ExprConstant.cpp`:

```c
  // In C, function designators are not lvalues, but we evaluate them as if they
  // are.
```

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


More information about the cfe-commits mailing list