[clang] [clang][Interp] Fix returning nullptr from functions (PR #67229)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 29 23:31:26 PDT 2023


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/67229

>From c3fcded8f926789ee6b00dcb676231b3c8285ac6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 23 Sep 2023 11:41:52 +0200
Subject: [PATCH] [clang][Interp] Fix returning nullptr from functions

isLive() is false for null pointers, so we need to special-case
this here.
---
 clang/lib/AST/Interp/Interp.h       | 3 ++-
 clang/test/AST/Interp/functions.cpp | 7 +++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index dd37150b63f6db0..84ce164480bc36e 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -214,7 +214,8 @@ bool Ret(InterpState &S, CodePtr &PC, APValue &Result) {
     // FIXME: We could be calling isLive() here, but the emitted diagnostics
     // seem a little weird, at least if the returned expression is of
     // pointer type.
-    if (!Ret.isLive())
+    // Null pointers are considered live here.
+    if (!Ret.isZero() && !Ret.isLive())
       return false;
   }
 
diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 5cdecbff1e9d3d4..68082576f2735e9 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -343,3 +343,10 @@ namespace TemplateUndefined {
   constexpr int l = consume(0);
   static_assert(l == 0, "");
 }
+
+namespace PtrReturn {
+  constexpr void *a() {
+    return nullptr;
+  }
+  static_assert(a() == nullptr, "");
+}



More information about the cfe-commits mailing list