From 067e30dc8d018a3632ab2a8a9c422d02c7d768e1 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Fri, 17 Oct 2025 09:55:33 -0700 Subject: [PATCH 1/2] [NFC][LLVM][ADT] Change a few `const StringRef &` arguments to value Per LLVM Programmer's Manual, StringRef should always be passed by value. Enforcing this for a few ADT functions. --- llvm/include/llvm/ADT/STLExtras.h | 6 +++--- llvm/include/llvm/ADT/Twine.h | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 658f26249122a..3de09131f8c03 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -21,6 +21,7 @@ #include "llvm/ADT/Hashing.h" #include "llvm/ADT/STLForwardCompat.h" #include "llvm/ADT/STLFunctionalExtras.h" +#include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator.h" #include "llvm/ADT/iterator_range.h" #include "llvm/Config/abi-breaking.h" @@ -2215,13 +2216,12 @@ inline void interleave(const Container &c, UnaryFunctor each_fn, template > inline void interleave(const Container &c, StreamT &os, UnaryFunctor each_fn, - const StringRef &separator) { + StringRef separator) { interleave(adl_begin(c), adl_end(c), each_fn, [&] { os << separator; }); } template > -inline void interleave(const Container &c, StreamT &os, - const StringRef &separator) { +inline void interleave(const Container &c, StreamT &os, StringRef separator) { interleave( c, os, [&](const T &a) { os << a; }, separator); } diff --git a/llvm/include/llvm/ADT/Twine.h b/llvm/include/llvm/ADT/Twine.h index d9f9c0f0d5d9c..e3b4d5e26fa17 100644 --- a/llvm/include/llvm/ADT/Twine.h +++ b/llvm/include/llvm/ADT/Twine.h @@ -285,7 +285,7 @@ class Twine { } /// Construct from a StringRef. - /*implicit*/ Twine(const StringRef &Str) : LHSKind(PtrAndLengthKind) { + /*implicit*/ Twine(StringRef Str) : LHSKind(PtrAndLengthKind) { LHS.ptrAndLength.ptr = Str.data(); LHS.ptrAndLength.length = Str.size(); assert(isValid() && "Invalid twine!"); @@ -352,7 +352,7 @@ class Twine { // right thing. Yet. /// Construct as the concatenation of a C string and a StringRef. - /*implicit*/ Twine(const char *LHS, const StringRef &RHS) + /*implicit*/ Twine(const char *LHS, StringRef RHS) : LHSKind(CStringKind), RHSKind(PtrAndLengthKind) { this->LHS.cString = LHS; this->RHS.ptrAndLength.ptr = RHS.data(); @@ -361,7 +361,7 @@ class Twine { } /// Construct as the concatenation of a StringRef and a C string. - /*implicit*/ Twine(const StringRef &LHS, const char *RHS) + /*implicit*/ Twine(StringRef LHS, const char *RHS) : LHSKind(PtrAndLengthKind), RHSKind(CStringKind) { this->LHS.ptrAndLength.ptr = LHS.data(); this->LHS.ptrAndLength.length = LHS.size(); @@ -530,14 +530,14 @@ inline Twine operator+(const Twine &LHS, const Twine &RHS) { /// Additional overload to guarantee simplified codegen; this is equivalent to /// concat(). -inline Twine operator+(const char *LHS, const StringRef &RHS) { +inline Twine operator+(const char *LHS, StringRef RHS) { return Twine(LHS, RHS); } /// Additional overload to guarantee simplified codegen; this is equivalent to /// concat(). -inline Twine operator+(const StringRef &LHS, const char *RHS) { +inline Twine operator+(StringRef LHS, const char *RHS) { return Twine(LHS, RHS); } From f6c7f8e1e2a3fe4e532d8a88fa67c197baaf9efc Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Tue, 21 Oct 2025 11:16:00 -0700 Subject: [PATCH 2/2] Undo STLExtras --- llvm/include/llvm/ADT/STLExtras.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 3de09131f8c03..658f26249122a 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -21,7 +21,6 @@ #include "llvm/ADT/Hashing.h" #include "llvm/ADT/STLForwardCompat.h" #include "llvm/ADT/STLFunctionalExtras.h" -#include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator.h" #include "llvm/ADT/iterator_range.h" #include "llvm/Config/abi-breaking.h" @@ -2216,12 +2215,13 @@ inline void interleave(const Container &c, UnaryFunctor each_fn, template > inline void interleave(const Container &c, StreamT &os, UnaryFunctor each_fn, - StringRef separator) { + const StringRef &separator) { interleave(adl_begin(c), adl_end(c), each_fn, [&] { os << separator; }); } template > -inline void interleave(const Container &c, StreamT &os, StringRef separator) { +inline void interleave(const Container &c, StreamT &os, + const StringRef &separator) { interleave( c, os, [&](const T &a) { os << a; }, separator); }