[libc-commits] [libc] [libc][msan] Fix "non-constexpr function '__msan_unpoison' cannot be used in a constant expression" (PR #88719)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Mon Apr 15 09:24:40 PDT 2024


================
@@ -47,14 +47,21 @@
 // Functions to unpoison memory
 //-----------------------------------------------------------------------------
 
-#if defined(LIBC_HAVE_MEMORY_SANITIZER) && __has_builtin(__builtin_constant_p)
-// Only perform MSAN unpoison in non-constexpr context.
+#if defined(LIBC_HAVE_MEMORY_SANITIZER)
+// Only perform MSAN unpoison in non-constexpr context and silence
+// '-Wconstant-evaluated' when MSAN_UNPOISON is called from manifestly constant
+// contexts.
 #include <sanitizer/msan_interface.h>
 #define MSAN_UNPOISON(addr, size)                                              \
   do {                                                                         \
-    if (!__builtin_constant_p(*addr)) {                                        \
+    _Pragma("GCC diagnostic push \"-Wconstant-evaluated\"");                   \
+    _Pragma("GCC diagnostic ignored \"-Wconstant-evaluated\"");                \
+    _Pragma("clang diagnostic push \"-Wconstant-evaluated\"");                 \
+    _Pragma("clang diagnostic ignored \"-Wconstant-evaluated\"");              \
----------------
nickdesaulniers wrote:

```
<source>:4:20: warning: '__builtin_is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression [-Wconstant-evaluated]
    4 |     if constexpr (!__builtin_is_constant_evaluated())
      |                    ^
```
https://godbolt.org/z/c513d1oPq

I'm not sure this is the correct pattern here, but don't yet have enough experience with constexpr to know how to handle what you're trying to do. Let me think about this, or ask more people for advice.

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


More information about the libc-commits mailing list