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

via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 23 02:47:30 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

<details>
<summary>Changes</summary>

isLive() is false for null pointers, so we need to special-case this here.

---
Full diff: https://github.com/llvm/llvm-project/pull/67229.diff


2 Files Affected:

- (modified) clang/lib/AST/Interp/Interp.h (+1-1) 
- (modified) clang/test/AST/Interp/functions.cpp (+7) 


``````````diff
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 8453856e526a6b2..71d49a0894f4e35 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -209,7 +209,7 @@ 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())
+    if (!Ret.isZero() && !Ret.isLive())
       return false;
   }
 
diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 331df74d50b3d62..7f03271d152db5c 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -332,3 +332,10 @@ namespace InvalidReclRefs {
   }
 #endif
 }
+
+namespace PtrReturn {
+  constexpr void *a() {
+    return nullptr;
+  }
+  static_assert(a() == nullptr, "");
+}

``````````

</details>


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


More information about the cfe-commits mailing list