[mlir] [llvm] [openmp] [clang] [flang] [clang-tools-extra] [libcxx] [clang-tidy] Replace memcpy with std::copy (PR #74663)

2023-12-08 Thread Giovanni Martins via cfe-commits

https://github.com/giovannism20 ready_for_review 
https://github.com/llvm/llvm-project/pull/74663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [clang-tools-extra] [openmp] [libcxx] [mlir] [flang] [clang] [clang-tidy] Replace memcpy with std::copy (PR #74663)

2023-12-07 Thread Giovanni Martins via cfe-commits

https://github.com/giovannism20 updated 
https://github.com/llvm/llvm-project/pull/74663

>From 992fd1fbc3568116da55c6b7cb40831757e4da6b Mon Sep 17 00:00:00 2001
From: Giovanni Martins 
Date: Wed, 6 Dec 2023 11:26:53 -0300
Subject: [PATCH 1/6] replace memcpy with std::copy on clang-tidy

---
 .../clang-tidy/modernize/CMakeLists.txt   |   1 +
 .../modernize/ModernizeTidyModule.cpp |   3 +
 .../modernize/ReplaceMemcpyWithStdCopy.cpp| 117 ++
 .../modernize/ReplaceMemcpyWithStdCopy.h  |  48 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../modernize-replace-memcpy-with-stdcopy.rst |  47 +++
 7 files changed, 222 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/modernize-replace-memcpy-with-stdcopy.rst

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index c40065358d2dc..d0a996d3be729 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -22,6 +22,7 @@ add_clang_library(clangTidyModernizeModule
   RedundantVoidArgCheck.cpp
   ReplaceAutoPtrCheck.cpp
   ReplaceDisallowCopyAndAssignMacroCheck.cpp
+  ReplaceMemcpyWithStdCopy.cpp
   ReplaceRandomShuffleCheck.cpp
   ReturnBracedInitListCheck.cpp
   ShrinkToFitCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index e994ffd2a75c8..590005c0ff371 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -22,6 +22,7 @@
 #include "RawStringLiteralCheck.h"
 #include "RedundantVoidArgCheck.h"
 #include "ReplaceAutoPtrCheck.h"
+#include "ReplaceMemcpyWithStdCopy.h"
 #include "ReplaceDisallowCopyAndAssignMacroCheck.h"
 #include "ReplaceRandomShuffleCheck.h"
 #include "ReturnBracedInitListCheck.h"
@@ -78,6 +79,8 @@ class ModernizeModule : public ClangTidyModule {
 "modernize-replace-auto-ptr");
 CheckFactories.registerCheck(
 "modernize-replace-disallow-copy-and-assign-macro");
+CheckFactories.registerCheck(
+"modernize-replace-memcpy-by-stdcopy");
 CheckFactories.registerCheck(
 "modernize-replace-random-shuffle");
 CheckFactories.registerCheck(
diff --git 
a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp 
b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
new file mode 100644
index 0..5a635d22d9a33
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
@@ -0,0 +1,117 @@
+//===--- ReplaceMemcpyByStdCopy.cpp - clang-tidy--*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ReplaceMemcpyByStdCopy.h"
+#include "../utils/OptionsUtils.h"
+#include 
+
+using namespace clang;
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+ReplaceMemcpyByStdCopy::ReplaceMemcpyByStdCopy(StringRef Name,
+   ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
+  Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
+
+void ReplaceMemcpyByStdCopy::registerMatchers(MatchFinder *Finder) {
+  assert(Finder != nullptr);
+
+  if (!getLangOpts().CPlusPlus)
+return;
+
+  auto MemcpyMatcher =
+  callExpr(hasDeclaration(functionDecl(hasName("memcpy"),
+   isExpansionInSystemHeader())),
+   isExpansionInMainFile())
+  .bind("memcpy_function");
+
+  Finder->addMatcher(MemcpyMatcher, this);
+}
+
+void ReplaceMemcpyByStdCopy::registerPPCallbacks(
+const SourceManager , Preprocessor *PP, Preprocessor *ModuleExpanderPP) 
{
+  if (!getLangOpts().CPlusPlus)
+return;
+
+  Inserter = llvm::make_unique(SM, getLangOpts(),
+   IncludeStyle);
+  PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+}
+
+void ReplaceMemcpyByStdCopy::check(const MatchFinder::MatchResult ) {
+  const auto *MemcpyNode = Result.Nodes.getNodeAs("memcpy_function");
+  assert(MemcpyNode != nullptr);
+
+  DiagnosticBuilder Diag =
+  diag(MemcpyNode->getExprLoc(), "use std::copy instead of memcpy");
+
+  renameFunction(Diag, MemcpyNode);
+  reorderArgs(Diag, MemcpyNode);
+  

[flang] [llvm] [clang] [openmp] [mlir] [libcxx] [clang-tools-extra] [clang-tidy] Replace memcpy with std::copy (PR #74663)

2023-12-07 Thread Giovanni Martins via cfe-commits

https://github.com/giovannism20 edited 
https://github.com/llvm/llvm-project/pull/74663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[flang] [clang] [llvm] [libcxx] [clang-tools-extra] [mlir] [openmp] Replace memcpy with std::copy (PR #74663)

2023-12-06 Thread Giovanni Martins via cfe-commits

https://github.com/giovannism20 updated 
https://github.com/llvm/llvm-project/pull/74663

>From 992fd1fbc3568116da55c6b7cb40831757e4da6b Mon Sep 17 00:00:00 2001
From: Giovanni Martins 
Date: Wed, 6 Dec 2023 11:26:53 -0300
Subject: [PATCH 1/5] replace memcpy with std::copy on clang-tidy

---
 .../clang-tidy/modernize/CMakeLists.txt   |   1 +
 .../modernize/ModernizeTidyModule.cpp |   3 +
 .../modernize/ReplaceMemcpyWithStdCopy.cpp| 117 ++
 .../modernize/ReplaceMemcpyWithStdCopy.h  |  48 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../modernize-replace-memcpy-with-stdcopy.rst |  47 +++
 7 files changed, 222 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/modernize-replace-memcpy-with-stdcopy.rst

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index c40065358d2dc3..d0a996d3be7292 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -22,6 +22,7 @@ add_clang_library(clangTidyModernizeModule
   RedundantVoidArgCheck.cpp
   ReplaceAutoPtrCheck.cpp
   ReplaceDisallowCopyAndAssignMacroCheck.cpp
+  ReplaceMemcpyWithStdCopy.cpp
   ReplaceRandomShuffleCheck.cpp
   ReturnBracedInitListCheck.cpp
   ShrinkToFitCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index e994ffd2a75c85..590005c0ff3714 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -22,6 +22,7 @@
 #include "RawStringLiteralCheck.h"
 #include "RedundantVoidArgCheck.h"
 #include "ReplaceAutoPtrCheck.h"
+#include "ReplaceMemcpyWithStdCopy.h"
 #include "ReplaceDisallowCopyAndAssignMacroCheck.h"
 #include "ReplaceRandomShuffleCheck.h"
 #include "ReturnBracedInitListCheck.h"
@@ -78,6 +79,8 @@ class ModernizeModule : public ClangTidyModule {
 "modernize-replace-auto-ptr");
 CheckFactories.registerCheck(
 "modernize-replace-disallow-copy-and-assign-macro");
+CheckFactories.registerCheck(
+"modernize-replace-memcpy-by-stdcopy");
 CheckFactories.registerCheck(
 "modernize-replace-random-shuffle");
 CheckFactories.registerCheck(
diff --git 
a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp 
b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
new file mode 100644
index 00..5a635d22d9a33e
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
@@ -0,0 +1,117 @@
+//===--- ReplaceMemcpyByStdCopy.cpp - clang-tidy--*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ReplaceMemcpyByStdCopy.h"
+#include "../utils/OptionsUtils.h"
+#include 
+
+using namespace clang;
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+ReplaceMemcpyByStdCopy::ReplaceMemcpyByStdCopy(StringRef Name,
+   ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
+  Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
+
+void ReplaceMemcpyByStdCopy::registerMatchers(MatchFinder *Finder) {
+  assert(Finder != nullptr);
+
+  if (!getLangOpts().CPlusPlus)
+return;
+
+  auto MemcpyMatcher =
+  callExpr(hasDeclaration(functionDecl(hasName("memcpy"),
+   isExpansionInSystemHeader())),
+   isExpansionInMainFile())
+  .bind("memcpy_function");
+
+  Finder->addMatcher(MemcpyMatcher, this);
+}
+
+void ReplaceMemcpyByStdCopy::registerPPCallbacks(
+const SourceManager , Preprocessor *PP, Preprocessor *ModuleExpanderPP) 
{
+  if (!getLangOpts().CPlusPlus)
+return;
+
+  Inserter = llvm::make_unique(SM, getLangOpts(),
+   IncludeStyle);
+  PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+}
+
+void ReplaceMemcpyByStdCopy::check(const MatchFinder::MatchResult ) {
+  const auto *MemcpyNode = Result.Nodes.getNodeAs("memcpy_function");
+  assert(MemcpyNode != nullptr);
+
+  DiagnosticBuilder Diag =
+  diag(MemcpyNode->getExprLoc(), "use std::copy instead of memcpy");
+
+  renameFunction(Diag, MemcpyNode);
+  reorderArgs(Diag, 

[mlir] [clang] [llvm] [flang] [clang-tools-extra] [openmp] [libcxx] Replace memcpy with std::copy (PR #74663)

2023-12-06 Thread Giovanni Martins via cfe-commits

https://github.com/giovannism20 edited 
https://github.com/llvm/llvm-project/pull/74663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[mlir] [clang] [llvm] [flang] [clang-tools-extra] [openmp] [libcxx] Replace memcpy with std::copy (PR #74663)

2023-12-06 Thread Giovanni Martins via cfe-commits

https://github.com/giovannism20 created 
https://github.com/llvm/llvm-project/pull/74663

22583

>From 992fd1fbc3568116da55c6b7cb40831757e4da6b Mon Sep 17 00:00:00 2001
From: Giovanni Martins 
Date: Wed, 6 Dec 2023 11:26:53 -0300
Subject: [PATCH 1/4] replace memcpy with std::copy on clang-tidy

---
 .../clang-tidy/modernize/CMakeLists.txt   |   1 +
 .../modernize/ModernizeTidyModule.cpp |   3 +
 .../modernize/ReplaceMemcpyWithStdCopy.cpp| 117 ++
 .../modernize/ReplaceMemcpyWithStdCopy.h  |  48 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../modernize-replace-memcpy-with-stdcopy.rst |  47 +++
 7 files changed, 222 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/modernize-replace-memcpy-with-stdcopy.rst

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index c40065358d2dc..d0a996d3be729 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -22,6 +22,7 @@ add_clang_library(clangTidyModernizeModule
   RedundantVoidArgCheck.cpp
   ReplaceAutoPtrCheck.cpp
   ReplaceDisallowCopyAndAssignMacroCheck.cpp
+  ReplaceMemcpyWithStdCopy.cpp
   ReplaceRandomShuffleCheck.cpp
   ReturnBracedInitListCheck.cpp
   ShrinkToFitCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index e994ffd2a75c8..590005c0ff371 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -22,6 +22,7 @@
 #include "RawStringLiteralCheck.h"
 #include "RedundantVoidArgCheck.h"
 #include "ReplaceAutoPtrCheck.h"
+#include "ReplaceMemcpyWithStdCopy.h"
 #include "ReplaceDisallowCopyAndAssignMacroCheck.h"
 #include "ReplaceRandomShuffleCheck.h"
 #include "ReturnBracedInitListCheck.h"
@@ -78,6 +79,8 @@ class ModernizeModule : public ClangTidyModule {
 "modernize-replace-auto-ptr");
 CheckFactories.registerCheck(
 "modernize-replace-disallow-copy-and-assign-macro");
+CheckFactories.registerCheck(
+"modernize-replace-memcpy-by-stdcopy");
 CheckFactories.registerCheck(
 "modernize-replace-random-shuffle");
 CheckFactories.registerCheck(
diff --git 
a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp 
b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
new file mode 100644
index 0..5a635d22d9a33
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
@@ -0,0 +1,117 @@
+//===--- ReplaceMemcpyByStdCopy.cpp - clang-tidy--*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ReplaceMemcpyByStdCopy.h"
+#include "../utils/OptionsUtils.h"
+#include 
+
+using namespace clang;
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+ReplaceMemcpyByStdCopy::ReplaceMemcpyByStdCopy(StringRef Name,
+   ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
+  Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
+
+void ReplaceMemcpyByStdCopy::registerMatchers(MatchFinder *Finder) {
+  assert(Finder != nullptr);
+
+  if (!getLangOpts().CPlusPlus)
+return;
+
+  auto MemcpyMatcher =
+  callExpr(hasDeclaration(functionDecl(hasName("memcpy"),
+   isExpansionInSystemHeader())),
+   isExpansionInMainFile())
+  .bind("memcpy_function");
+
+  Finder->addMatcher(MemcpyMatcher, this);
+}
+
+void ReplaceMemcpyByStdCopy::registerPPCallbacks(
+const SourceManager , Preprocessor *PP, Preprocessor *ModuleExpanderPP) 
{
+  if (!getLangOpts().CPlusPlus)
+return;
+
+  Inserter = llvm::make_unique(SM, getLangOpts(),
+   IncludeStyle);
+  PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+}
+
+void ReplaceMemcpyByStdCopy::check(const MatchFinder::MatchResult ) {
+  const auto *MemcpyNode = Result.Nodes.getNodeAs("memcpy_function");
+  assert(MemcpyNode != nullptr);
+
+  DiagnosticBuilder Diag =
+  diag(MemcpyNode->getExprLoc(), "use std::copy instead of memcpy");
+
+  renameFunction(Diag, MemcpyNode);
+  reorderArgs(Diag,