[clang] [Clang] Detect bit copies that should be relocation. (PR #139104)

via cfe-commits cfe-commits at lists.llvm.org
Fri May 9 07:22:34 PDT 2025


================
@@ -9659,6 +9659,42 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call,
   if (BId == Builtin::BIbzero && !FirstArgTy->getAs<PointerType>())
     return;
 
+  // Try to detect a relocation operation
+  if (getLangOpts().CPlusPlus &&
+      (BId == Builtin::BImemmove || BId == Builtin::BImemcpy)) {
+    const Expr *Dest = Call->getArg(0)->IgnoreParenImpCasts();
+    const Expr *Src = Call->getArg(1)->IgnoreParenImpCasts();
+    QualType DestTy = Dest->getType();
+    QualType SrcTy = Src->getType();
+
+    QualType DestPointeeTy = DestTy->getPointeeType();
+    QualType SrcPointeeTy = SrcTy->getPointeeType();
+    bool HasSameTargetAndSource =
+        !DestPointeeTy.isNull() && !SrcPointeeTy.isNull() &&
+        Context.hasSameUnqualifiedType(DestPointeeTy, SrcPointeeTy);
+
+    if (HasSameTargetAndSource &&
+        !DestPointeeTy.getUnqualifiedType()->isIncompleteType() &&
+        !DestPointeeTy.isConstQualified() && !SrcPointeeTy.isConstQualified() &&
+        !DestPointeeTy.isTriviallyCopyableType(getASTContext()) &&
+        SemaRef.IsCXXTriviallyRelocatableType(DestPointeeTy)) {
+
+      bool SuggestStd = getLangOpts().CPlusPlus26 && getStdNamespace();
+      if (const Decl *D = Call->getReferencedDeclOfCallee();
+          D && !D->isInStdNamespace())
+        SuggestStd = false;
----------------
Sirraide wrote:

So if I’m reading this correctly this will cause us to suggest `std::trivially_relocate` if the user called `std::memcpy`, and `__builtin_trivially_relocate` if they called some other `memcpy` that isn’t defined in the `std` namespace. Is there a reason why we wouldn’t just always suggest `std::trivially_relocate` if C++26 is enabled (or is this because the header providing that function might not be included which could potentially be confusing if someone then uses `std::trivially_relocate` but then we start complaining about it not being found?).

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


More information about the cfe-commits mailing list