[clang] [clang][bytecode] Handle memmove like memcpy (PR #118431)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 2 22:01:45 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

This is the same thing for us, except for diagnostic differences.

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


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+6-2) 
- (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+7) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index aab380c0925d29..f3024dc3e26fe1 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1802,9 +1802,11 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
       peekToAPSInt(S.Stk, *S.getContext().classify(Call->getArg(2)));
   assert(!Size.isSigned() && "memcpy and friends take an unsigned size");
 
-  if (ID == Builtin::BImemcpy)
+  if (ID == Builtin::BImemcpy || ID == Builtin::BImemmove)
     diagnoseNonConstexprBuiltin(S, OpPC, ID);
 
+  bool Move = (ID == Builtin::BI__builtin_memmove || ID == Builtin::BImemmove);
+
   if (DestPtr.isDummy() || SrcPtr.isDummy())
     return false;
 
@@ -1817,7 +1819,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
   if (SrcPtr.isZero() || DestPtr.isZero()) {
     Pointer DiagPtr = (SrcPtr.isZero() ? SrcPtr : DestPtr);
     S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null)
-        << /*IsMove=*/false << /*IsWchar=*/false << !SrcPtr.isZero()
+        << /*IsMove=*/Move << /*IsWchar=*/false << !SrcPtr.isZero()
         << DiagPtr.toDiagnosticString(S.getASTContext());
     return false;
   }
@@ -2291,6 +2293,8 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
 
   case Builtin::BI__builtin_memcpy:
   case Builtin::BImemcpy:
+  case Builtin::BI__builtin_memmove:
+  case Builtin::BImemmove:
     if (!interp__builtin_memcpy(S, OpPC, Frame, F, Call))
       return false;
     break;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index dfee35d6399a6c..b94adfa3ab36bd 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1146,4 +1146,11 @@ namespace BuiltinMemcpy {
                                                                                       // both-note {{source of 'memcpy' is nullptr}}
 
 
+  constexpr int simpleMove() {
+    int a = 12;
+    int b = 0;
+    __builtin_memmove(&b, &a, sizeof(a));
+    return b;
+  }
+  static_assert(simpleMove() == 12);
 }

``````````

</details>


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


More information about the cfe-commits mailing list