[clang] 9ab3b6a - [clang][bytecode] Diagnose integral source/dest in memcpy (#132715)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 24 04:44:39 PDT 2025


Author: Timm Baeder
Date: 2025-03-24T12:44:35+01:00
New Revision: 9ab3b6a006d8b5c831146eb8a7f0a8df616bd5ad

URL: https://github.com/llvm/llvm-project/commit/9ab3b6a006d8b5c831146eb8a7f0a8df616bd5ad
DIFF: https://github.com/llvm/llvm-project/commit/9ab3b6a006d8b5c831146eb8a7f0a8df616bd5ad.diff

LOG: [clang][bytecode] Diagnose integral source/dest in memcpy (#132715)

Like the current interpreter does.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/InterpBuiltin.cpp
    clang/test/AST/ByteCode/builtin-functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 71fd25c183f48..285ea7151a9cf 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1792,6 +1792,17 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
     return false;
   }
 
+  // Diagnose integral src/dest pointers specially.
+  if (SrcPtr.isIntegralPointer() || DestPtr.isIntegralPointer()) {
+    std::string DiagVal = "(void *)";
+    DiagVal += SrcPtr.isIntegralPointer()
+                   ? std::to_string(SrcPtr.getIntegerRepresentation())
+                   : std::to_string(DestPtr.getIntegerRepresentation());
+    S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null)
+        << Move << false << DestPtr.isIntegralPointer() << DiagVal;
+    return false;
+  }
+
   // Can't read from dummy pointers.
   if (DestPtr.isDummy() || SrcPtr.isDummy())
     return false;

diff  --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index c3ea158eae859..8cba1ec2e5b3b 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1290,6 +1290,12 @@ namespace BuiltinMemcpy {
     return Result1 && Result2;
   }
   static_assert(memmoveOverlapping());
+
+#define fold(x) (__builtin_constant_p(0) ? (x) : (x))
+  static_assert(__builtin_memcpy(&global, fold((wchar_t*)123), sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
+                                                                                  // both-note {{source of 'memcpy' is (void *)123}}
+  static_assert(__builtin_memcpy(fold(reinterpret_cast<wchar_t*>(123)), &global, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
+                                                                                                    // both-note {{destination of 'memcpy' is (void *)123}}
 }
 
 namespace Memcmp {


        


More information about the cfe-commits mailing list