[llvm] [FileCheck] Use move semantics instead of std::swap. NFC. (PR #123304)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 23 03:44:03 PST 2025


https://github.com/jayfoad updated https://github.com/llvm/llvm-project/pull/123304

>From 1cb24b71458362663746af68a60a4a0053f251f3 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Thu, 16 Jan 2025 17:33:14 +0000
Subject: [PATCH 1/2] [FileCheck] Use move semantics instead of std::swap. NFC.

This code was using a pre-move-semantics trick of using std::swap to
avoid expensive vector copies.
---
 llvm/lib/FileCheck/FileCheck.cpp   | 8 ++++----
 llvm/lib/FileCheck/FileCheckImpl.h | 5 +++--
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index a6df9672f81008..f7faa3ada3a200 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -1933,8 +1933,8 @@ bool FileCheck::readCheckFile(
     }
 
     // Okay, add the string we captured to the output vector and move on.
-    CheckStrings.emplace_back(P, UsedPrefix, PatternLoc);
-    std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
+    CheckStrings.emplace_back(P, UsedPrefix, PatternLoc,
+                              std::move(DagNotMatches));
     DagNotMatches = ImplicitNegativeChecks;
   }
 
@@ -1963,8 +1963,8 @@ bool FileCheck::readCheckFile(
   if (!DagNotMatches.empty()) {
     CheckStrings.emplace_back(
         Pattern(Check::CheckEOF, PatternContext.get(), LineNumber + 1),
-        *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()));
-    std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
+        *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()),
+        std::move(DagNotMatches));
   }
 
   return false;
diff --git a/llvm/lib/FileCheck/FileCheckImpl.h b/llvm/lib/FileCheck/FileCheckImpl.h
index c772eddd8ecd5e..b94ab544476917 100644
--- a/llvm/lib/FileCheck/FileCheckImpl.h
+++ b/llvm/lib/FileCheck/FileCheckImpl.h
@@ -837,8 +837,9 @@ struct FileCheckString {
   /// Hold the DAG/NOT strings occurring in the input file.
   std::vector<DagNotPrefixInfo> DagNotStrings;
 
-  FileCheckString(const Pattern &P, StringRef S, SMLoc L)
-      : Pat(P), Prefix(S), Loc(L) {}
+  FileCheckString(const Pattern &P, StringRef S, SMLoc L,
+                  std::vector<DagNotPrefixInfo> &&D)
+      : Pat(P), Prefix(S), Loc(L), DagNotStrings(std::move(D)) {}
 
   /// Matches check string and its "not strings" and/or "dag strings".
   size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,

>From 56247552ee36416c7a4b23f1bd2ee2849e8da6d9 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Thu, 23 Jan 2025 11:41:39 +0000
Subject: [PATCH 2/2] Also do Pattern

---
 llvm/lib/FileCheck/FileCheck.cpp   | 2 +-
 llvm/lib/FileCheck/FileCheckImpl.h | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index f7faa3ada3a200..5706afc357fbd3 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -1933,7 +1933,7 @@ bool FileCheck::readCheckFile(
     }
 
     // Okay, add the string we captured to the output vector and move on.
-    CheckStrings.emplace_back(P, UsedPrefix, PatternLoc,
+    CheckStrings.emplace_back(std::move(P), UsedPrefix, PatternLoc,
                               std::move(DagNotMatches));
     DagNotMatches = ImplicitNegativeChecks;
   }
diff --git a/llvm/lib/FileCheck/FileCheckImpl.h b/llvm/lib/FileCheck/FileCheckImpl.h
index b94ab544476917..4715fa9c64b619 100644
--- a/llvm/lib/FileCheck/FileCheckImpl.h
+++ b/llvm/lib/FileCheck/FileCheckImpl.h
@@ -837,9 +837,9 @@ struct FileCheckString {
   /// Hold the DAG/NOT strings occurring in the input file.
   std::vector<DagNotPrefixInfo> DagNotStrings;
 
-  FileCheckString(const Pattern &P, StringRef S, SMLoc L,
+  FileCheckString(Pattern &&P, StringRef S, SMLoc L,
                   std::vector<DagNotPrefixInfo> &&D)
-      : Pat(P), Prefix(S), Loc(L), DagNotStrings(std::move(D)) {}
+      : Pat(std::move(P)), Prefix(S), Loc(L), DagNotStrings(std::move(D)) {}
 
   /// Matches check string and its "not strings" and/or "dag strings".
   size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,



More information about the llvm-commits mailing list