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

Giovanni Martins via libcxx-commits libcxx-commits at lists.llvm.org
Wed Dec 6 14:33:37 PST 2023


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 <giovannimartins2000 at gmail.com>
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<ReplaceDisallowCopyAndAssignMacroCheck>(
         "modernize-replace-disallow-copy-and-assign-macro");
+    CheckFactories.registerCheck<ReplaceMemcpyWithStdCopy>(
+        "modernize-replace-memcpy-by-stdcopy");
     CheckFactories.registerCheck<ReplaceRandomShuffleCheck>(
         "modernize-replace-random-shuffle");
     CheckFactories.registerCheck<ReturnBracedInitListCheck>(
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 00000000000000..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 <array>
+
+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 &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
+  if (!getLangOpts().CPlusPlus)
+    return;
+
+  Inserter = llvm::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
+                                                       IncludeStyle);
+  PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+}
+
+void ReplaceMemcpyByStdCopy::check(const MatchFinder::MatchResult &Result) {
+  const auto *MemcpyNode = Result.Nodes.getNodeAs<CallExpr>("memcpy_function");
+  assert(MemcpyNode != nullptr);
+
+  DiagnosticBuilder Diag =
+      diag(MemcpyNode->getExprLoc(), "use std::copy instead of memcpy");
+
+  renameFunction(Diag, MemcpyNode);
+  reorderArgs(Diag, MemcpyNode);
+  insertHeader(Diag, MemcpyNode, Result.SourceManager);
+}
+
+void ReplaceMemcpyByStdCopy::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IncludeStyle",
+                utils::IncludeSorter::toString(IncludeStyle));
+}
+
+void ReplaceMemcpyByStdCopy::renameFunction(DiagnosticBuilder &Diag,
+                                            const CallExpr *MemcpyNode) {
+  const CharSourceRange FunctionNameSourceRange = CharSourceRange::getCharRange(
+      MemcpyNode->getBeginLoc(), MemcpyNode->getArg(0)->getBeginLoc());
+
+  Diag << FixItHint::CreateReplacement(FunctionNameSourceRange, "std::copy(");
+}
+
+void ReplaceMemcpyByStdCopy::reorderArgs(DiagnosticBuilder &Diag,
+                                         const CallExpr *MemcpyNode) {
+  std::array<std::string, 3> arg;
+
+  LangOptions LangOpts;
+  LangOpts.CPlusPlus = true;
+  PrintingPolicy Policy(LangOpts);
+
+  // Retrieve all the arguments
+  for (uint8_t i = 0; i < arg.size(); i++) {
+    llvm::raw_string_ostream s(arg[i]);
+    MemcpyNode->getArg(i)->printPretty(s, nullptr, Policy);
+  }
+
+  // Create lambda that return SourceRange of an argument
+  auto getSourceRange = [MemcpyNode](uint8_t ArgCount) -> SourceRange {
+    return SourceRange(MemcpyNode->getArg(ArgCount)->getBeginLoc(),
+                       MemcpyNode->getArg(ArgCount)->getEndLoc());
+  };
+
+  // Reorder the arguments
+  Diag << FixItHint::CreateReplacement(getSourceRange(0), arg[1]);
+
+  arg[2] = arg[1] + " + ((" + arg[2] + ") / sizeof(*(" + arg[1] + ")))";
+  Diag << FixItHint::CreateReplacement(getSourceRange(1), arg[2]);
+
+  Diag << FixItHint::CreateReplacement(getSourceRange(2), arg[0]);
+}
+
+void ReplaceMemcpyByStdCopy::insertHeader(DiagnosticBuilder &Diag,
+                                          const CallExpr *MemcpyNode,
+                                          SourceManager *const SM) {
+  Optional<FixItHint> FixInclude = Inserter->CreateIncludeInsertion(
+      /*FileID=*/SM->getMainFileID(), /*Header=*/"algorithm",
+      /*IsAngled=*/true);
+  if (FixInclude)
+    Diag << *FixInclude;
+}
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
new file mode 100644
index 00000000000000..a799428dec8940
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
@@ -0,0 +1,48 @@
+//===--- ReplaceMemcpyByStdCopy.h - 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_MEMCPY_BY_STDCOPY_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_MEMCPY_BY_STDCOPY_H
+
+#include "../ClangTidyCheck.h"
+#include "../utils/IncludeInserter.h"
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+/// Replace the C memcpy function by std::copy
+class ReplaceMemcpyByStdCopy : public ClangTidyCheck {
+public:
+  ReplaceMemcpyByStdCopy(StringRef Name, ClangTidyContext *Context);
+  ~ReplaceMemcpyByStdCopy() override = default;
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
+                           Preprocessor *ModuleExpanderPP) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  void storeOptions(ClangTidyOptions::OptionMap &Options) override;
+
+private:
+  void renameFunction(DiagnosticBuilder &Diag, const CallExpr *MemcpyNode);
+  void reorderArgs(DiagnosticBuilder &Diag, const CallExpr *MemcpyNode);
+  void insertHeader(DiagnosticBuilder &Diag, const CallExpr *MemcpyNode,
+                    SourceManager *const SM);
+
+private:
+  std::unique_ptr<utils::IncludeInserter> Inserter;
+  const utils::IncludeSorter::IncludeStyle IncludeStyle;
+};
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_MEMCPY_BY_STDCOPY_H
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 4ff4494cef5624..2191e418cead24 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -377,6 +377,11 @@ Changes in existing checks
   <clang-tidy/checks/modernize/use-nullptr>` check by adding option
   `IgnoredTypes` that can be used to exclude some pointer types.
 
+- New :doc:`modernize-replace-memcpy-with-stdcopy
+  <clang-tidy/checks/modernize-replace-memcpy-by-stdcopy>` check.
+
+  Replaces all occurrences of the C ``memcpy`` function by ``std::copy``.
+
 - Improved :doc:`modernize-use-std-print
   <clang-tidy/checks/modernize/use-std-print>` check to accurately generate
   fixes for reordering arguments.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst
index df2d5d15238d63..57d13aacb81890 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -275,6 +275,7 @@ Clang-Tidy Checks
    :doc:`modernize-raw-string-literal <modernize/raw-string-literal>`, "Yes"
    :doc:`modernize-redundant-void-arg <modernize/redundant-void-arg>`, "Yes"
    :doc:`modernize-replace-auto-ptr <modernize/replace-auto-ptr>`, "Yes"
+   :doc:`modernize-replace-memcpy-with-std-copy <modernize/replace-auto-ptr>`, "Yes"
    :doc:`modernize-replace-disallow-copy-and-assign-macro <modernize/replace-disallow-copy-and-assign-macro>`, "Yes"
    :doc:`modernize-replace-random-shuffle <modernize/replace-random-shuffle>`, "Yes"
    :doc:`modernize-return-braced-init-list <modernize/return-braced-init-list>`, "Yes"
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/modernize-replace-memcpy-with-stdcopy.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/modernize-replace-memcpy-with-stdcopy.rst
new file mode 100644
index 00000000000000..e7e077b970add7
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/modernize-replace-memcpy-with-stdcopy.rst
@@ -0,0 +1,47 @@
+.. title:: clang-tidy - modernize-replace-memcpy-by-stdcopy
+
+modernize-replace-memcpy-by-stdcopy
+===================================
+
+Replaces all occurrences of the C ``memcpy`` function by ``std::copy``
+
+Example:
+
+.. code-block:: c++
+
+  /*!
+   * \param destination Pointer to the destination array where the content is to be copied
+   * \param source Pointer to the source of data to be copied
+   * \param num Number of bytes to copy
+   */
+  memcpy(destination, source, num);
+
+becomes
+
+.. code-block:: c++
+
+  /*!
+   * \param destination Pointer to the destination array where the content is to be copied
+   * \param source Pointer to the source of data to be copied
+   * \param num Number of bytes to copy
+   */
+  std::copy(source, source + (num / sizeof *source), destination);
+
+Bytes to iterator conversion
+----------------------------
+
+Unlike ``std::copy`` that take an iterator on the last element of the source array, ``memcpy`` request the number of bytes to copy.
+In order to make the check working, it will convert the size parameter to an iterator by replacing it by ``source + (num / sizeof *source)``
+
+Header inclusion
+----------------
+
+``std::copy`` being provided by the ``algorithm`` header file, this check will include it if needed.
+
+Options
+-------
+
+.. option:: IncludeStyle
+
+   A string specifying which include-style is used, `llvm` or `google`. Default
+   is `llvm`.

>From aba7870c79cf379b9d4ff5b9bc4c069800153dd8 Mon Sep 17 00:00:00 2001
From: Giovanni Martins <giovannimartins2000 at gmail.com>
Date: Wed, 6 Dec 2023 11:32:40 -0300
Subject: [PATCH 2/5] removed typo on files

---
 .../modernize/ReplaceMemcpyWithStdCopy.cpp    | 20 +++++++++----------
 .../modernize/ReplaceMemcpyWithStdCopy.h      |  8 ++++----
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
index 5a635d22d9a33e..e0738ecdffad3a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
@@ -1,4 +1,4 @@
-//===--- ReplaceMemcpyByStdCopy.cpp - clang-tidy------------------*- C++-*-===//
+//===--- ReplaceMemcpyWithStdCopy.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.
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "ReplaceMemcpyByStdCopy.h"
+#include "ReplaceMemcpyWithStdCopy.h"
 #include "../utils/OptionsUtils.h"
 #include <array>
 
@@ -17,13 +17,13 @@ namespace clang {
 namespace tidy {
 namespace modernize {
 
-ReplaceMemcpyByStdCopy::ReplaceMemcpyByStdCopy(StringRef Name,
+ReplaceMemcpyWithStdCopy::ReplaceMemcpyWithStdCopy(StringRef Name,
                                                ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
       IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
           Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
 
-void ReplaceMemcpyByStdCopy::registerMatchers(MatchFinder *Finder) {
+void ReplaceMemcpyWithStdCopy::registerMatchers(MatchFinder *Finder) {
   assert(Finder != nullptr);
 
   if (!getLangOpts().CPlusPlus)
@@ -38,7 +38,7 @@ void ReplaceMemcpyByStdCopy::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(MemcpyMatcher, this);
 }
 
-void ReplaceMemcpyByStdCopy::registerPPCallbacks(
+void ReplaceMemcpyWithStdCopy::registerPPCallbacks(
     const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
   if (!getLangOpts().CPlusPlus)
     return;
@@ -48,7 +48,7 @@ void ReplaceMemcpyByStdCopy::registerPPCallbacks(
   PP->addPPCallbacks(Inserter->CreatePPCallbacks());
 }
 
-void ReplaceMemcpyByStdCopy::check(const MatchFinder::MatchResult &Result) {
+void ReplaceMemcpyWithStdCopy::check(const MatchFinder::MatchResult &Result) {
   const auto *MemcpyNode = Result.Nodes.getNodeAs<CallExpr>("memcpy_function");
   assert(MemcpyNode != nullptr);
 
@@ -60,12 +60,12 @@ void ReplaceMemcpyByStdCopy::check(const MatchFinder::MatchResult &Result) {
   insertHeader(Diag, MemcpyNode, Result.SourceManager);
 }
 
-void ReplaceMemcpyByStdCopy::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+void ReplaceMemcpyWithStdCopy::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "IncludeStyle",
                 utils::IncludeSorter::toString(IncludeStyle));
 }
 
-void ReplaceMemcpyByStdCopy::renameFunction(DiagnosticBuilder &Diag,
+void ReplaceMemcpyWithStdCopy::renameFunction(DiagnosticBuilder &Diag,
                                             const CallExpr *MemcpyNode) {
   const CharSourceRange FunctionNameSourceRange = CharSourceRange::getCharRange(
       MemcpyNode->getBeginLoc(), MemcpyNode->getArg(0)->getBeginLoc());
@@ -73,7 +73,7 @@ void ReplaceMemcpyByStdCopy::renameFunction(DiagnosticBuilder &Diag,
   Diag << FixItHint::CreateReplacement(FunctionNameSourceRange, "std::copy(");
 }
 
-void ReplaceMemcpyByStdCopy::reorderArgs(DiagnosticBuilder &Diag,
+void ReplaceMemcpyWithStdCopy::reorderArgs(DiagnosticBuilder &Diag,
                                          const CallExpr *MemcpyNode) {
   std::array<std::string, 3> arg;
 
@@ -102,7 +102,7 @@ void ReplaceMemcpyByStdCopy::reorderArgs(DiagnosticBuilder &Diag,
   Diag << FixItHint::CreateReplacement(getSourceRange(2), arg[0]);
 }
 
-void ReplaceMemcpyByStdCopy::insertHeader(DiagnosticBuilder &Diag,
+void ReplaceMemcpyWithStdCopy::insertHeader(DiagnosticBuilder &Diag,
                                           const CallExpr *MemcpyNode,
                                           SourceManager *const SM) {
   Optional<FixItHint> FixInclude = Inserter->CreateIncludeInsertion(
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
index a799428dec8940..2e3d0fad711793 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
@@ -1,4 +1,4 @@
-//===--- ReplaceMemcpyByStdCopy.h - clang-tidy--------------------*- C++-*-===//
+//===--- ReplaceMemcpyWithStdCopy.h - 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.
@@ -20,10 +20,10 @@ namespace tidy {
 namespace modernize {
 
 /// Replace the C memcpy function by std::copy
-class ReplaceMemcpyByStdCopy : public ClangTidyCheck {
+class ReplaceMemcpyWithStdCopy : public ClangTidyCheck {
 public:
-  ReplaceMemcpyByStdCopy(StringRef Name, ClangTidyContext *Context);
-  ~ReplaceMemcpyByStdCopy() override = default;
+  ReplaceMemcpyWithStdCopy(StringRef Name, ClangTidyContext *Context);
+  ~ReplaceMemcpyWithStdCopy() override = default;
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
                            Preprocessor *ModuleExpanderPP) override;

>From 6885a9f832e8cab856e87ec4bdc02a1c6882a2c7 Mon Sep 17 00:00:00 2001
From: Giovanni Martins <giovannimartins2000 at gmail.com>
Date: Wed, 6 Dec 2023 11:33:17 -0300
Subject: [PATCH 3/5] sort imports

---
 clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index 590005c0ff3714..6bb9efa694eb2f 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -22,8 +22,8 @@
 #include "RawStringLiteralCheck.h"
 #include "RedundantVoidArgCheck.h"
 #include "ReplaceAutoPtrCheck.h"
-#include "ReplaceMemcpyWithStdCopy.h"
 #include "ReplaceDisallowCopyAndAssignMacroCheck.h"
+#include "ReplaceMemcpyWithStdCopy.h"
 #include "ReplaceRandomShuffleCheck.h"
 #include "ReturnBracedInitListCheck.h"
 #include "ShrinkToFitCheck.h"

>From 39092c7d3d5acb1cca5927b85e69a98656a22683 Mon Sep 17 00:00:00 2001
From: Giovanni Martins <giovannimartins2000 at gmail.com>
Date: Wed, 6 Dec 2023 18:37:28 -0300
Subject: [PATCH 4/5] removed some typo

---
 .../clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp | 11 ++++++-----
 .../clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h   | 11 ++++++-----
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
index e0738ecdffad3a..c4ab6e14f204c2 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
@@ -1,4 +1,4 @@
-//===--- ReplaceMemcpyWithStdCopy.cpp - clang-tidy------------------*- C++-*-===//
+//===--- ReplaceMemcpyWithStdCopy.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.
@@ -18,10 +18,11 @@ namespace tidy {
 namespace modernize {
 
 ReplaceMemcpyWithStdCopy::ReplaceMemcpyWithStdCopy(StringRef Name,
-                                               ClangTidyContext *Context)
+                                                   ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
-      IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
-          Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
+      IncludeInserter(
+        Options.getLocalOrGlobal(
+          "IncludeStyle", utils::IncludeSorter::IS_LLVM)) {}
 
 void ReplaceMemcpyWithStdCopy::registerMatchers(MatchFinder *Finder) {
   assert(Finder != nullptr);
@@ -43,7 +44,7 @@ void ReplaceMemcpyWithStdCopy::registerPPCallbacks(
   if (!getLangOpts().CPlusPlus)
     return;
 
-  Inserter = llvm::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
+  Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
                                                        IncludeStyle);
   PP->addPPCallbacks(Inserter->CreatePPCallbacks());
 }
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
index 2e3d0fad711793..0f262bf839af24 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.h
@@ -1,4 +1,4 @@
-//===--- ReplaceMemcpyWithStdCopy.h - clang-tidy--------------------*- C++-*-===//
+//===--- ReplaceMemcpyWithStdCopy.h - 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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_MEMCPY_BY_STDCOPY_H
-#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_MEMCPY_BY_STDCOPY_H
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_MEMCPY_WITH_STDCOPY_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_MEMCPY_WITH_STDCOPY_H
 
 #include "../ClangTidyCheck.h"
 #include "../utils/IncludeInserter.h"
@@ -19,7 +19,7 @@ namespace clang {
 namespace tidy {
 namespace modernize {
 
-/// Replace the C memcpy function by std::copy
+// Replace the C memcpy function with std::copy
 class ReplaceMemcpyWithStdCopy : public ClangTidyCheck {
 public:
   ReplaceMemcpyWithStdCopy(StringRef Name, ClangTidyContext *Context);
@@ -38,6 +38,7 @@ class ReplaceMemcpyWithStdCopy : public ClangTidyCheck {
 
 private:
   std::unique_ptr<utils::IncludeInserter> Inserter;
+  utils::IncludeInserter IncludeInserter;
   const utils::IncludeSorter::IncludeStyle IncludeStyle;
 };
 
@@ -45,4 +46,4 @@ class ReplaceMemcpyWithStdCopy : public ClangTidyCheck {
 } // namespace tidy
 } // namespace clang
 
-#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_MEMCPY_BY_STDCOPY_H
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_MEMCPY_WITH_STDCOPY_H

>From c4b707f434847aadbf6ee330b72fcfcb8ef3ded1 Mon Sep 17 00:00:00 2001
From: Giovanni Martins <giovannimartins2000 at gmail.com>
Date: Wed, 6 Dec 2023 19:33:24 -0300
Subject: [PATCH 5/5] solve linter reports

---
 .../modernize/ReplaceMemcpyWithStdCopy.cpp      | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
index c4ab6e14f204c2..af6b365c162517 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceMemcpyWithStdCopy.cpp
@@ -20,9 +20,9 @@ namespace modernize {
 ReplaceMemcpyWithStdCopy::ReplaceMemcpyWithStdCopy(StringRef Name,
                                                    ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
-      IncludeInserter(
-        Options.getLocalOrGlobal(
-          "IncludeStyle", utils::IncludeSorter::IS_LLVM)) {}
+      IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
+                                               utils::IncludeSorter::IS_LLVM)) {
+}
 
 void ReplaceMemcpyWithStdCopy::registerMatchers(MatchFinder *Finder) {
   assert(Finder != nullptr);
@@ -44,7 +44,8 @@ void ReplaceMemcpyWithStdCopy::registerPPCallbacks(
   if (!getLangOpts().CPlusPlus)
     return;
 
-  Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
+  Inserter =
+      std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
                                                        IncludeStyle);
   PP->addPPCallbacks(Inserter->CreatePPCallbacks());
 }
@@ -67,7 +68,7 @@ void ReplaceMemcpyWithStdCopy::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 }
 
 void ReplaceMemcpyWithStdCopy::renameFunction(DiagnosticBuilder &Diag,
-                                            const CallExpr *MemcpyNode) {
+                                              const CallExpr *MemcpyNode) {
   const CharSourceRange FunctionNameSourceRange = CharSourceRange::getCharRange(
       MemcpyNode->getBeginLoc(), MemcpyNode->getArg(0)->getBeginLoc());
 
@@ -75,7 +76,7 @@ void ReplaceMemcpyWithStdCopy::renameFunction(DiagnosticBuilder &Diag,
 }
 
 void ReplaceMemcpyWithStdCopy::reorderArgs(DiagnosticBuilder &Diag,
-                                         const CallExpr *MemcpyNode) {
+                                           const CallExpr *MemcpyNode) {
   std::array<std::string, 3> arg;
 
   LangOptions LangOpts;
@@ -104,8 +105,8 @@ void ReplaceMemcpyWithStdCopy::reorderArgs(DiagnosticBuilder &Diag,
 }
 
 void ReplaceMemcpyWithStdCopy::insertHeader(DiagnosticBuilder &Diag,
-                                          const CallExpr *MemcpyNode,
-                                          SourceManager *const SM) {
+                                            const CallExpr *MemcpyNode,
+                                            SourceManager *const SM) {
   Optional<FixItHint> FixInclude = Inserter->CreateIncludeInsertion(
       /*FileID=*/SM->getMainFileID(), /*Header=*/"algorithm",
       /*IsAngled=*/true);



More information about the libcxx-commits mailing list